wpi-32u4-library
IRdecoder.h
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4
21{
22private:
23 uint8_t pin = -1;
24
26 {
27 IR_READY, //idle, returns to this state after you request a code
28 IR_PREAMBLE, //received the start burst, waiting for first bit
29 IR_REPEAT, //received repeat code (part of NEC protocol); last code will be returned
30 IR_ACTIVE, //have some bits, but not yet complete
31 IR_COMPLETE, //a valid code has been received
32 IR_ERROR //an error occurred; won't return a valid code
33 };
34
35 IR_STATE state = IR_READY; //a simple state machine for managing reception
36
37 volatile uint32_t lastReceiveTime = 0; //not really used -- could be used to sunset codes
38
39 volatile uint32_t currCode = 0; //the most recently received valid code
40 volatile uint8_t index = 0; //for tracking which bit we're on
41
42 volatile uint32_t fallingEdge = 0;
43 volatile uint32_t risingEdge = 0;
44
45 volatile uint32_t lastRisingEdge = 0; //used for tracking spacing between rising edges, i.e., bit value
46public:
47 //volatile uint16_t bits[32]; //I used this for debugging; obsolete
48
49public:
50 IRDecoder(uint8_t p) : pin(p) {}
51 void init(void); //call this in the setup()
52 void handleIRsensor(void); //ISR
53
54 uint32_t getCode(void) //returns the most recent valid code; returns zero if there was an error
55 {
56 if (state == IR_COMPLETE || state == IR_REPEAT)
57 {
59 return currCode;
60 }
61 else
62 return 0;
63 }
64
65 int16_t getKeyCode(bool acceptRepeat = false) //returns the most recent key code; returns -1 on error (not sure if 0 can be a code or not!!!)
66 {
67 if (state == IR_COMPLETE || (acceptRepeat == true && state == IR_REPEAT))
68 {
70 return (currCode >> 16) & 0x0ff;
71 }
72 else
73 return -1;
74 }
75};
76
77extern IRDecoder decoder;
IRDecoder decoder
volatile uint32_t risingEdge
Definition: IRdecoder.h:43
int16_t getKeyCode(bool acceptRepeat=false)
Definition: IRdecoder.h:65
volatile uint32_t lastReceiveTime
Definition: IRdecoder.h:37
void handleIRsensor(void)
Definition: IRdecoder.cpp:32
volatile uint32_t currCode
Definition: IRdecoder.h:39
volatile uint8_t index
Definition: IRdecoder.h:40
void init(void)
Definition: IRdecoder.cpp:12
volatile uint32_t lastRisingEdge
Definition: IRdecoder.h:45
uint8_t pin
Definition: IRdecoder.h:23
IRDecoder(uint8_t p)
Definition: IRdecoder.h:50
@ IR_ACTIVE
Definition: IRdecoder.h:30
@ IR_PREAMBLE
Definition: IRdecoder.h:28
@ IR_COMPLETE
Definition: IRdecoder.h:31
@ IR_READY
Definition: IRdecoder.h:27
@ IR_REPEAT
Definition: IRdecoder.h:29
@ IR_ERROR
Definition: IRdecoder.h:32
volatile uint32_t fallingEdge
Definition: IRdecoder.h:42
uint32_t getCode(void)
Definition: IRdecoder.h:54
IR_STATE state
Definition: IRdecoder.h:35