Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
IrReceiverSampler.cpp
Go to the documentation of this file.
1#include "IrReceiverSampler.h"
2#include "Board.h"
3
4#if HAS_SAMPLING
5
6uint32_t IrReceiverSampler::millisecs2ticks(milliseconds_t ms) {
7 return (1000UL * (uint32_t) ms) / Board::microsPerTick;
8}
9
10milliseconds_t IrReceiverSampler::ticks2millisecs(uint32_t tix) {
11 return (milliseconds_t) ((tix * Board::microsPerTick)/1000UL);
12}
13
14IrReceiverSampler *IrReceiverSampler::instance = nullptr;
15
16IrReceiverSampler::IrReceiverSampler(size_t captureLength,
17 pin_t pin_,
18 bool pullup,
19 microseconds_t markExcess,
20 milliseconds_t beginningTimeout,
21 milliseconds_t endingTimeout) : IrReceiver(captureLength, pin_, pullup, markExcess) {
22 setBeginningTimeout(beginningTimeout);
23 setEndingTimeout(endingTimeout);
24 durationData = new microseconds_t[bufferSize];
25 dataLength = 0;
26 timer = 0;
27 receiverState = STATE_IDLE;
28}
29
31 pin_t pin,
32 bool pullup,
33 microseconds_t markExcess,
34 milliseconds_t beginningTimeout,
35 milliseconds_t endingTimeout) {
36 if (instance != nullptr || pin == invalidPin)
37 return nullptr;
38 instance = new IrReceiverSampler(captureLength, pin, pullup, markExcess, beginningTimeout, endingTimeout);
39 return instance;
40}
41
43 delete instance;
44 instance = nullptr;
45}
46
48 delete [] durationData;
49}
50
51/*
52 * The original IRrecv which uses 50us timer driven interrupts to sample input pin.
53 */
56 dataLength = 0;
57 timer = 0U;
58}
59
61 // Initialize state machine variables
62 reset();
63 noInterrupts();
65 interrupts();
66}
67
70}
71
73 endingTimeoutInTicks = millisecs2ticks(timeOut);
74}
75
77 beginningTimeoutInTicks = millisecs2ticks(timeOut);
78}
79
81 return ticks2millisecs(endingTimeoutInTicks);
82}
83
85 return ticks2millisecs(beginningTimeoutInTicks);
86}
87
88#ifdef ISR
94 IrReceiver::irdata_t irdata = recv->readIr();
95 recv->timer++; // One more 50us tick
96 if (recv->dataLength >= recv->getBufferSize()) {
97 // Buffer full
99 }
100 switch (recv->receiverState) {
101 case IrReceiverSampler::STATE_IDLE: // Looking for first mark
102 if (irdata == IrReceiver::IR_MARK) {
103 // Got the first mark, record duration and start recording transmission
104 recv->dataLength = 0;
105 recv->timer = 0;
107 } else {
108 if (recv->timer >= recv->beginningTimeoutInTicks) {
109 recv->durationData[recv->dataLength] = recv->timer;
110 recv->timer = 0;
112 }
113 }
114 break;
116 if (irdata == IrReceiver::IR_SPACE) {
117 // MARK ended, record time
118 recv->durationData[recv->dataLength++] = recv->timer;
119 recv->timer = 0;
121 }
122 break;
124 if (irdata == IrReceiver::IR_MARK) {
125 // SPACE just ended, record it
126 recv->durationData[recv->dataLength++] = recv->timer;
127 recv->timer = 0;
129 } else {
130 // still silence, is it over?
131 if (recv->timer > recv->endingTimeoutInTicks) {
132 // big SPACE, indicates gap between codes
133 recv->durationData[recv->dataLength++] = recv->timer;
134// recv->timer = 0;
136 }
137 }
138 break;
140 break;
141 default:
142 // should not happen
143 break;
144 }
146}
147#endif // ISR
148
149#endif
#define TIMER_INTR_NAME
Definition: ATmega328P.h:90
#define ISR(f)
Definition: Esp32.h:48
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
uint16_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:24
uint8_t pin_t
Type for GPIO pin, compatible with Arduino libs.
Definition: InfraredTypes.h:41
static constexpr pin_t invalidPin
Symbolic name for an invalid pin number.
Definition: InfraredTypes.h:43
virtual void enableSampler(pin_t pin __attribute__((unused)))
Start the periodic ISR sampler routine.
Definition: Board.h:74
virtual void disableSampler()
Turn off sampler routine.
Definition: Board.h:83
static void debugPinHigh()
Definition: Board.h:274
static Board * getInstance()
Definition: Board.h:53
static void debugPinLow()
Definition: Board.h:280
static const unsigned long microsPerTick
Definition: Board.h:57
virtual void timerReset()
TODO.
Definition: Board.h:115
int16_t markExcess
Microseconds subtracted from pulses and added to gaps.
Definition: IrReader.h:44
milliseconds_t beginningTimeout
Definition: IrReader.h:38
milliseconds_t endingTimeout
Definition: IrReader.h:39
unsigned int getBufferSize() const
Definition: IrReader.h:161
This receiving class samples the input pin every 50 microseconds using a timer interrupt.
void enable()
Start reception of IR data.
milliseconds_t getBeginningTimeout() const
@ STATE_IDLE
Between signals; waiting for first mark.
@ STATE_STOP
Complete signal has been read.
@ STATE_MARK
Last read a mark.
@ STATE_SPACE
Last read a space.
uint32_t beginningTimeoutInTicks
milliseconds_t getEndingTimeout() const
volatile microseconds_t * durationData
Data buffer.
static IrReceiverSampler * newIrReceiverSampler(size_t captureLength=defaultCaptureLength, pin_t pin=defaultPin, bool pullup=false, microseconds_t markExcess=defaultMarkExcess, milliseconds_t beginningTimeout=defaultBeginningTimeout, milliseconds_t endingTimeout=defaultEndingTimeout)
This factory method replaces public constructors.
virtual ~IrReceiverSampler()
volatile ReceiverState_t receiverState
State of the state machine.
static IrReceiverSampler * getInstance()
Returns a pointer to the instance, or nullptr.
void setBeginningTimeout(milliseconds_t timeOut)
volatile size_t dataLength
Number of entries in durationData.
volatile uint32_t timer
state timer, counts 50uS ticks.
static void deleteInstance()
Deletes the instance, thereby freeing up the resources it occupied, and allowing for another instance...
void disable()
Stop reception of IR data.
uint32_t endingTimeoutInTicks
void setEndingTimeout(milliseconds_t timeOut)
Abstract base class for demodulating IR receivers.
Definition: IrReceiver.h:12
pin_t getPin() const
Definition: IrReceiver.h:45
irdata_t
Enum for the duration types.
Definition: IrReceiver.h:52
@ IR_SPACE
off-period, also called gap
Definition: IrReceiver.h:54
@ IR_MARK
on-period, also called flash
Definition: IrReceiver.h:53