Infrared4Arduino
IrReceiverSampler.cpp
Go to the documentation of this file.
1 #include "IrReceiverSampler.h"
2 #include <IrTimerDefs.h>
3 
4 uint32_t IrReceiverSampler::millisecs2ticks(milliseconds_t ms) {
5  return (1000UL * (uint32_t) ms) / microsPerTick;
6 }
7 
8 milliseconds_t IrReceiverSampler::ticks2millisecs(uint32_t tix) {
9  return (milliseconds_t) ((tix * microsPerTick)/1000UL);
10 }
11 
12 IrReceiverSampler *IrReceiverSampler::instance = NULL;
13 
14 IrReceiverSampler::IrReceiverSampler(size_t captureLength,
15  pin_t pin_,
16  boolean pullup,
17  microseconds_t markExcess,
18  milliseconds_t beginningTimeout,
19  milliseconds_t endingTimeout) : IrReceiver(captureLength, pin_, pullup, markExcess) {
20  setBeginningTimeout(beginningTimeout);
21  setEndingTimeout(endingTimeout);
22  durationData = new microseconds_t[bufferSize];
23  dataLength = 0;
24  timer = 0;
25  receiverState = STATE_IDLE;
26 }
27 
29  pin_t pin,
30  boolean pullup,
31  microseconds_t markExcess,
32  milliseconds_t beginningTimeout,
33  milliseconds_t endingTimeout) {
34  if (instance != NULL || pin == invalidPin)
35  return NULL;
36  instance = new IrReceiverSampler(captureLength, pin, pullup, markExcess, beginningTimeout, endingTimeout);
37  return instance;
38 }
39 
41  delete instance;
42  instance = NULL;
43 }
44 
46  delete [] durationData;
47 }
48 
49 /*
50  * The original IRrecv which uses 50us timer driven interrupts to sample input pin.
51  */
54  dataLength = 0;
55 }
56 
58  reset();
59  noInterrupts();
63  interrupts();
64 }
65 
68 }
69 
71  endingTimeoutInTicks = millisecs2ticks(timeOut);
72 }
73 
75  beginningTimeoutInTicks = millisecs2ticks(timeOut);
76 }
77 
79  return ticks2millisecs(endingTimeoutInTicks);
80 }
81 
83  return ticks2millisecs(beginningTimeoutInTicks);
84 }
85 
89  IrReceiver::irdata_t irdata = recv->readIr();
90  recv->timer++; // One more 50us tick
91  if (recv->dataLength >= recv->getBufferSize()) {
92  // Buffer full
94  }
95  switch (recv->receiverState) {
96  case IrReceiverSampler::STATE_IDLE: // Looking for first mark
97  if (irdata == IrReceiver::IR_MARK) {
98  // Got the first mark, record duration and start recording transmission
99  recv->dataLength = 0;
100  recv->timer = 0;
102  } else {
103  if (recv->timer >= recv->beginningTimeoutInTicks) {
104  recv->durationData[recv->dataLength] = recv->timer;
105  recv->timer = 0;
107  }
108  }
109  break;
111  if (irdata == IrReceiver::IR_SPACE) {
112  // MARK ended, record time
113  recv->durationData[recv->dataLength++] = recv->timer;
114  recv->timer = 0;
116  }
117  break;
119  if (irdata == IrReceiver::IR_MARK) {
120  // SPACE just ended, record it
121  recv->durationData[recv->dataLength++] = recv->timer;
122  recv->timer = 0;
124  } else {
125  // still silence, is it over?
126  if (recv->timer > recv->endingTimeoutInTicks) {
127  // big SPACE, indicates gap between codes
128  recv->durationData[recv->dataLength++] = recv->timer;
129  //recv->timer = 0;
131  }
132  }
133  break;
135  break;
136  default:
137  // should not happen
138  break;
139  }
140 }
void setBeginningTimeout(milliseconds_t timeOut)
off-period, also called gap
Definition: IrReceiver.h:49
Between signals; waiting for first mark.
static const unsigned long microsPerTick
microseconds per clock interrupt
void setBeginningTimeout(milliseconds_t timeOut)
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:16
volatile ReceiverState_t receiverState
State of the state machine.
#define TIMER_CONFIG_NORMAL()
Definition: IRremoteInt.h:253
uint8_t pin_t
Type for GPIO pin, compatible with Arduino libs.
Definition: InfraredTypes.h:40
volatile size_t dataLength
Number of entries in durationData.
#define TIMER_RESET
Definition: IRremoteInt.h:227
irdata_t
Enum for the duration types.
Definition: IrReceiver.h:47
#define TIMER_INTR_NAME
Definition: IRremoteInt.h:232
void setEndingTimeout(milliseconds_t timeOut)
This receiving class samples the input pin every 50 microseconds using a timer interrupt.
size_t bufferSize
Definition: IrReader.h:41
uint16_t milliseconds_t
Type for durations in milli seconds.
Definition: InfraredTypes.h:26
Abstract base class for demodulating IR receivers.
Definition: IrReceiver.h:11
void enable()
Start reception of IR data.
#define TIMER_DISABLE_INTR
Definition: IRremoteInt.h:231
unsigned int getBufferSize() const
Definition: IrReader.h:139
#define STATE_IDLE
Definition: IRremoteInt.h:60
static IrReceiverSampler * newIrReceiverSampler(size_t captureLength=defaultCaptureLength, pin_t pin=defaultPin, boolean pullup=false, microseconds_t markExcess=defaultMarkExcess, milliseconds_t beginningTimeout=defaultBeginningTimeout, milliseconds_t endingTimeout=defaultEndingTimeout)
This factory method replaces public constructors.
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...
#define TIMER_ENABLE_INTR
Definition: IRremoteInt.h:230
volatile microseconds_t * durationData
Data buffer.
on-period, also called flash
Definition: IrReceiver.h:48
void disable()
Stop reception of IR data.
Complete signal has been read.
const pin_t invalidPin
Symbolic name for an invalid pin number.
Definition: InfraredTypes.h:42
milliseconds_t getBeginningTimeout() const
uint32_t beginningTimeoutInTicks
void setEndingTimeout(milliseconds_t timeOut)
Definition of timers etc is encapsulated in this file.
milliseconds_t getEndingTimeout() const
uint32_t endingTimeoutInTicks
static IrReceiverSampler * getInstance()
Returns a pointer to the instance, or NULL.
ISR(TIMER_INTR_NAME)
Interrupt routine.