Engduino v2.1
|
00001 /** 00002 * \defgroup EngduinoIR Driver for Engduino infrared comms 00003 * 00004 * @{ 00005 */ 00006 00007 /** 00008 * \file 00009 * Engduino IR driver 00010 * \author 00011 * Engduino team: support@engduino.org 00012 */ 00013 00014 00015 #ifndef EngduinoIR_h 00016 #define EngduinoIR_h 00017 00018 #include <Arduino.h> 00019 #include <Engduino.h> 00020 00021 // Size of the send/receive buffers. 00022 // IRBUFSZ is the max size that a caller of send/recv should 00023 // use for a send/receive buffer. 00024 // 00025 // Note that the raw buffer is used to contain timings of 00026 // the data bits and the start/stop bits, so is in effect just 00027 // short of the max number of bits. The size is therefore longer. 00028 // 00029 // 00030 #define IRBUFSZ 12 // Length of the max packet size in bytes 00031 #define RAWBUFSZ 100 // Length of raw duration buffer 00032 00033 // Needed to ensure correct linkage between C++ and C linkage of ISRs 00034 extern "C" void TIMER1_COMPB_vect(void) __attribute__ ((signal)); 00035 extern "C" void TIMER3_COMPB_vect(void) __attribute__ ((signal)); 00036 extern "C" void INT2_vect(void) __attribute__ ((signal)); 00037 00038 class EngduinoIRClass 00039 { 00040 private: 00041 volatile uint8_t rcvstate; 00042 volatile uint8_t rawlen; 00043 volatile uint16_t rawbuf[RAWBUFSZ]; 00044 volatile bool sending; 00045 00046 void mark(uint16_t time); 00047 void space(uint16_t time); 00048 00049 public: 00050 EngduinoIRClass(); 00051 void begin(); 00052 void end(); 00053 00054 void sendBit(bool b); 00055 void send(uint8_t b, bool startstop=true); 00056 void send(uint8_t *buf, unsigned int len, bool startstop=true); 00057 void send(char *buf, unsigned int len, bool startstop=true); 00058 void sendRaw(unsigned int *buf, int len); 00059 00060 int recv(uint8_t *buf, uint16_t timeout=0, bool startstop=true); 00061 int recvRaw(uint16_t *buf, uint16_t timeout=0); 00062 00063 /* 00064 * The ISR needs access to the private variables, so we declare it 00065 * a friend of the class 00066 */ 00067 friend void TIMER3_COMPB_vect(); 00068 friend void TIMER1_COMPB_vect(); 00069 friend void INT2_vect(); 00070 }; 00071 00072 00073 00074 // Receiver states 00075 // 00076 #define STATE_BLOCKED 1 00077 #define STATE_IDLE 2 00078 #define STATE_READING 3 00079 #define STATE_STOP 4 00080 #define STATE_TIMEOUT 5 00081 00082 00083 // Between code gap 00084 // 00085 #define GAP 5000 // 5ms without a mark = counts between code gap 00086 00087 // Timer munging - delayMicroseconds waits for longer than it should by about 00088 // the amount given in DELAYOFFSET. 00089 // 00090 #define DELAYOFFSET 9 // Time that delayMicroseconds is actually late by, in us. 00091 #define SPACETIME (500-DELAYOFFSET) // This should give us 500 us per bit 00092 #define MARKTIME (50-DELAYOFFSET) // This should give us 50 us per bit if it's a mark - but we send it twice 00093 #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. 00094 00095 // Error codes 00096 // 00097 #define E_TIMEOUT -1 00098 #define E_SYSERR -2 00099 00100 #define MARK true 00101 #define SPACE false 00102 00103 extern EngduinoIRClass EngduinoIR; 00104 00105 #endif 00106 00107 /** @} */