wpi-32u4-library
src
IRdecoder.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <Arduino.h>
4
20
class
IRDecoder
21
{
22
private
:
23
uint8_t
pin
= -1;
24
25
enum
IR_STATE
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
46
public
:
47
//volatile uint16_t bits[32]; //I used this for debugging; obsolete
48
49
public
:
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
{
58
state
=
IR_READY
;
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
{
69
state
=
IR_READY
;
70
return
(
currCode
>> 16) & 0x0ff;
71
}
72
else
73
return
-1;
74
}
75
};
76
77
extern
IRDecoder
decoder
;
decoder
IRDecoder decoder
IRDecoder
Definition:
IRdecoder.h:21
IRDecoder::risingEdge
volatile uint32_t risingEdge
Definition:
IRdecoder.h:43
IRDecoder::getKeyCode
int16_t getKeyCode(bool acceptRepeat=false)
Definition:
IRdecoder.h:65
IRDecoder::lastReceiveTime
volatile uint32_t lastReceiveTime
Definition:
IRdecoder.h:37
IRDecoder::handleIRsensor
void handleIRsensor(void)
Definition:
IRdecoder.cpp:32
IRDecoder::currCode
volatile uint32_t currCode
Definition:
IRdecoder.h:39
IRDecoder::index
volatile uint8_t index
Definition:
IRdecoder.h:40
IRDecoder::init
void init(void)
Definition:
IRdecoder.cpp:12
IRDecoder::lastRisingEdge
volatile uint32_t lastRisingEdge
Definition:
IRdecoder.h:45
IRDecoder::pin
uint8_t pin
Definition:
IRdecoder.h:23
IRDecoder::IRDecoder
IRDecoder(uint8_t p)
Definition:
IRdecoder.h:50
IRDecoder::IR_STATE
IR_STATE
Definition:
IRdecoder.h:26
IRDecoder::IR_ACTIVE
@ IR_ACTIVE
Definition:
IRdecoder.h:30
IRDecoder::IR_PREAMBLE
@ IR_PREAMBLE
Definition:
IRdecoder.h:28
IRDecoder::IR_COMPLETE
@ IR_COMPLETE
Definition:
IRdecoder.h:31
IRDecoder::IR_READY
@ IR_READY
Definition:
IRdecoder.h:27
IRDecoder::IR_REPEAT
@ IR_REPEAT
Definition:
IRdecoder.h:29
IRDecoder::IR_ERROR
@ IR_ERROR
Definition:
IRdecoder.h:32
IRDecoder::fallingEdge
volatile uint32_t fallingEdge
Definition:
IRdecoder.h:42
IRDecoder::getCode
uint32_t getCode(void)
Definition:
IRdecoder.h:54
IRDecoder::state
IR_STATE state
Definition:
IRdecoder.h:35
Generated by
1.9.3