COSA
An Object-Oriented Platform for Arduino Programming
IR.cpp
Go to the documentation of this file.
1 
21 #include "IR.hh"
22 #include "Cosa/RTT.hh"
23 #include "Cosa/Watchdog.hh"
24 
25 void
27 {
28  UNUSED(arg);
29 
30  // Check if the buffer is full
31  if (UNLIKELY(m_ix == m_max)) return;
32 
33  // Check if the time should be set; i.e. queue for timeout events
34  if (m_ix == 0) {
35  expire_after(TIMEOUT);
36  start();
37  }
38 
39  // Measure the sample period
40  uint32_t now = RTT::micros();
41  uint32_t us = (now - m_start);
42  m_start = now;
43 
44  // Check if samples should be collected
45  if (m_sample != NULL) m_sample[m_ix] = us;
46 
47  // Fix: Check for repeat code
48 
49  // And generate binary code. Skip two first and two last samples
50  if (m_ix > 1 && m_ix < m_max - 2)
51  m_code = (m_code << 1) + (us > m_threshold);
52 
53  // Check if all samples have been received
54  if (++m_ix != m_max) return;
55 
56  // Disable further interrupts and remove from timer queue
57  disable();
58  stop();
59 
60  // Push an event with the received code
62 }
63 
64 void
66 {
67  // Remove from any queue
68  stop();
69 
70  // Initial state
71  m_ix = 0;
72  m_code = 0;
73 
74  // Reset start time and enable the interrupt handler
75  m_start = RTT::micros();
76  enable();
77 }
78 
79 int
80 IR::Receiver::lookup(uint16_t code)
81 {
82  if (UNLIKELY(m_keymap == NULL)) return (-1);
83  for (uint8_t i = 0; i < m_keys; i++)
84  if (code == pgm_read_word(&m_keymap[i].code))
85  return (pgm_read_word(&m_keymap[i].key));
86  return (-1);
87 }
88 
90 {
91  if (UNLIKELY(receiver.m_sample == NULL)) return (outs);
92  for (uint8_t ix = 0; ix < receiver.m_ix; ix++)
93  outs << ix << ':' << receiver.m_sample[ix] << endl;
94  return (outs);
95 }
virtual void on_interrupt(uint16_t arg=0)
Definition: IR.cpp:26
int32_t expire_after() const
Definition: Job.hh:129
#define NULL
Definition: Types.h:101
virtual void disable()
int lookup(uint16_t code)
Definition: IR.cpp:80
static uint32_t micros()
Definition: RTT.cpp:103
friend IOStream & operator<<(IOStream &outs, Receiver &receiver)
Definition: IR.cpp:89
#define UNUSED(x)
Definition: ATmega328P.hh:31
IOStream & endl(IOStream &outs)
Definition: IOStream.hh:817
bool stop()
Definition: Job.hh:169
void reset()
Definition: IR.cpp:65
bool start()
Definition: Job.hh:159
#define UNLIKELY(x)
Definition: Types.h:153
static bool push(uint8_t type, Handler *target, uint16_t value=0)
Definition: Event.hh:180