Engduino  3.1.0
A fun device for learning coding
EngduinoIR.h
Go to the documentation of this file.
1 
14 //v3
15 #ifndef EngduinoIR_h
16 #define EngduinoIR_h
17 
18 #include <Arduino.h>
19 #include <Engduino.h>
20 
21 // Size of the send/receive buffers.
22 // IRBUFSZ is the max size that a caller of send/recv should
23 // use for a send/receive buffer.
24 //
25 // Note that the raw buffer is used to contain timings of
26 // the data bits and the start/stop bits, so is in effect just
27 // short of the max number of bits. The size is therefore longer.
28 //
29 //
30 #define IRBUFSZ 12 // Length of the max packet size in bytes
31 #define RAWBUFSZ 100 // Length of raw duration buffer
32 
33 // Needed to ensure correct linkage between C++ and C linkage of ISRs
34 extern "C" void TIMER3_COMPB_vect(void) __attribute__ ((signal));
35 extern "C" void INT2_vect(void) __attribute__ ((signal));
36 
37 #if defined(__BOARD_ENGDUINOV2)
38  extern "C" void TIMER1_COMPB_vect(void) __attribute__ ((signal));
39 #elif defined(__BOARD_ENGDUINOV3)
40  extern "C" void PCINT0_vect(void) __attribute__ ((signal));
41 #endif
42 
44 {
45  private:
46  volatile uint8_t rcvstate;
47  volatile uint8_t rawlen;
48  volatile uint16_t rawbuf[RAWBUFSZ];
49  volatile bool sending;
50 
51  void mark(uint16_t time);
52  void space(uint16_t time);
53 
54  public:
56  void begin();
57  void end();
58 
59  void sendBit(bool b);
60  void send(uint8_t b, bool startstop=true);
61  void send(uint8_t *buf, unsigned int len, bool startstop=true);
62  void send(char *buf, unsigned int len, bool startstop=true);
63  void sendRaw(unsigned int *buf, int len);
64 
65  int recv(uint8_t *buf, uint16_t timeout=0, bool startstop=true);
66  int recvRaw(uint16_t *buf, uint16_t timeout=0);
67 
68  /*
69  * The ISR needs access to the private variables, so we declare it
70  * a friend of the class
71  */
72  friend void TIMER3_COMPB_vect();
73 #if defined(__BOARD_ENGDUINOV1)
74  friend void INT2_vect();
75 #elif defined(__BOARD_ENGDUINOV2)
76  friend void TIMER1_COMPB_vect();
77  friend void INT2_vect();
78 #elif defined(__BOARD_ENGDUINOV3)
79  friend void PCINT0_vect();
80 #endif
81 };
82 
83 
84 
85 // Receiver states
86 //
87 #define STATE_BLOCKED 1
88 #define STATE_IDLE 2
89 #define STATE_READING 3
90 #define STATE_STOP 4
91 #define STATE_TIMEOUT 5
92 
93 
94 // Between code gap
95 //
96 #define GAP 5000 // 5ms without a mark = counts between code gap
97 
98 // Timer munging - delayMicroseconds waits for longer than it should by about
99 // the amount given in DELAYOFFSET.
100 //
101 
102 #define DELAYOFFSET 9 // Time that delayMicroseconds is actually late by, in us.
103 #if defined(__BOARD_ENGDUINOV1) || defined(__BOARD_ENGDUINOV3)
104  #define BITTIME (600-DELAYOFFSET) // This should give us 500 us per bit
105  #define MARKSPACESPLIT 1500
106 #elif defined(__BOARD_ENGDUINOV2)
107  #define SPACETIME (500-DELAYOFFSET) // This should give us 500 us per bit
108  #define MARKTIME (50-DELAYOFFSET) // This should give us 50 us per bit if it's a mark - but we send it twice
109  #define MARKSPACESPLIT 0.9*(2*MARKTIME + SPACETIME) // This is 90% of the least amount of time a space can occupy. Marks should be way below this.
110 #endif
111 // Error codes
112 //
113 #define E_TIMEOUT -1
114 #define E_SYSERR -2
115 
116 #define MARK true
117 #define SPACE false
118 
119 extern EngduinoIRClass EngduinoIR;
120 
121 #endif
122 
void send(uint8_t b, bool startstop=true)
Send a byte.
Definition: EngduinoIR.cpp:232
void sendRaw(unsigned int *buf, int len)
Raw send function. Provide timings for mark and space pairs.
Definition: EngduinoIR.cpp:322
Definition: EngduinoIR.h:43
int recvRaw(uint16_t *buf, uint16_t timeout=0)
Raw receive function - returns timings for inter-mark gaps.
Definition: EngduinoIR.cpp:712
void sendBit(bool b)
Send a single bit.
Definition: EngduinoIR.cpp:192
void end()
end function - switch off the IR
Definition: EngduinoIR.cpp:168
EngduinoIRClass()
Constructor.
Definition: EngduinoIR.cpp:51
void begin()
begin function - must be called before using other functions
Definition: EngduinoIR.cpp:68
int recv(uint8_t *buf, uint16_t timeout=0, bool startstop=true)
Blocking receive of an IR transmission, with optional timeout.
Definition: EngduinoIR.cpp:655