Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
IrReceiverPoll.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include "IrReceiverPoll.h"
3
4IrReceiverPoll::IrReceiverPoll(size_t captureLength,
5 pin_t pin_,
6 bool pullup,
7 microseconds_t markExcess,
8 milliseconds_t beginningTimeout,
9 milliseconds_t endingTimeout) : IrReceiver(captureLength, pin_, pullup, markExcess) {
12 durationData = new microseconds_t[bufferSize];
13 dataLength = 0;
14}
15
17 delete [] durationData;
18 // let the pin stay as input
19}
20
23 dataLength = 0;
24}
25
27 reset();
28 timeouted = !searchForStart();
29 if (timeouted)
30 return;
31 collectData();
32}
33
34unsigned long timeSince(unsigned long then) {
35 return micros() - then;
36}
37
38bool IrReceiverPoll::searchForStart() {
39 unsigned long start = micros();
40 unsigned long beginningTimeoutInMicros = 1000UL * beginningTimeout;
41 while (readIr() == IrReceiver::IR_SPACE) {
42 if (timeSince(start) > beginningTimeoutInMicros)
43 return false;
44 yield();
45 }
46 return true;
47}
48
49void IrReceiverPoll::collectData() {
51 unsigned long endingTimeoutInMicros = 1000UL * endingTimeout;
52 unsigned long lastTime = micros();
53 while (dataLength < bufferSize) {
54 IrReceiver::irdata_t data = readIr();
55 if (data != lastDataRead) {
56 unsigned long now = micros();
57 recordDuration(now - lastTime);
58 lastDataRead = data;
59 lastTime = now;
60 } else if (data == IrReceiver::IR_SPACE) {
61 unsigned long now = micros();
62 if (now - lastTime > endingTimeoutInMicros) {
63 recordDuration(now - lastTime);
64 return; // normal exit
65 }
66 }
67 yield();
68 }
69}
70
71void IrReceiverPoll::recordDuration(unsigned long t) {
72 durationData[dataLength++] = t <= MICROSECONDS_T_MAX ? (microseconds_t) t : MICROSECONDS_T_MAX;
73}
static constexpr microseconds_t MICROSECONDS_T_MAX
Largest microseconds_t number possible.
Definition: InfraredTypes.h:18
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
unsigned long timeSince(unsigned long then)
milliseconds_t beginningTimeout
Definition: IrReader.h:38
virtual void reset()
Definition: IrReader.h:74
size_t bufferSize
Definition: IrReader.h:41
milliseconds_t endingTimeout
Definition: IrReader.h:39
bool timeouted
True if last receive ended with a timeout.
Definition: IrReader.h:47
void setBeginningTimeout(milliseconds_t timeOut)
IrReceiverPoll(size_t captureLength=defaultCaptureLength, pin_t pin=defaultPin, bool pullup=false, microseconds_t markExcess=defaultMarkExcess, milliseconds_t beginningTimeout=defaultBeginningTimeout, milliseconds_t endingTimeout=defaultEndingTimeout)
void enable()
In this class, enable does the actual collection of the data.
void setEndingTimeout(milliseconds_t timeOut)
Abstract base class for demodulating IR receivers.
Definition: IrReceiver.h:12
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