Engduino v1.0
EngduinoIR.h
Go to the documentation of this file.
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 TIMER3_COMPB_vect(void) __attribute__ ((signal));
00035 extern "C" void INT2_vect(void) __attribute__ ((signal));
00036 
00037 class EngduinoIRClass
00038 {
00039         private:
00040                 volatile uint8_t  rcvstate;
00041                 volatile uint8_t  rawlen;
00042                 volatile uint16_t rawbuf[RAWBUFSZ];
00043                 volatile bool     sending;
00044 
00045                 void mark(uint16_t time);
00046                 void space(uint16_t time);
00047                                 
00048         public:
00049                 EngduinoIRClass();
00050                 void begin();
00051                 void end();
00052                 
00053                 void sendBit(bool b);
00054                 void send(uint8_t b, bool startstop=true);
00055                 void send(uint8_t *buf, unsigned int len, bool startstop=true);
00056                 void send(char    *buf, unsigned int len, bool startstop=true);
00057                 void sendRaw(unsigned int *buf, int len);
00058                 
00059                 int  recv(uint8_t *buf, uint16_t timeout=0, bool startstop=true);
00060                 int  recvRaw(uint16_t *buf, uint16_t timeout=0);
00061 
00062                 /*
00063                  * The ISR needs access to the private variables, so we declare it
00064                  * a friend of the class
00065                  */
00066                 friend void TIMER3_COMPB_vect();
00067                 friend void INT2_vect();
00068 };
00069 
00070 
00071 
00072 // Receiver states
00073 //
00074 #define STATE_BLOCKED 1
00075 #define STATE_IDLE    2
00076 #define STATE_READING 3
00077 #define STATE_STOP    4
00078 #define STATE_TIMEOUT 5
00079 
00080 
00081 // Between code gap
00082 //
00083 #define GAP               5000                          // 5ms without a mark = counts between code gap
00084 
00085 // Timer munging - delayMicroseconds waits for longer than it should by about
00086 // the amount given in DELAYOFFSET.
00087 //
00088 #define DELAYOFFSET        9                                    // Time that delayMicroseconds is actually late by, in us.
00089 #define BITTIME            (600-DELAYOFFSET)    // This should give us 500 us per bit
00090 #define MARKSPACESPLIT 1500
00091 
00092 // Error codes
00093 //
00094 #define E_TIMEOUT         -1
00095 #define E_SYSERR          -2
00096 
00097 #define MARK  true
00098 #define SPACE false
00099 
00100 extern EngduinoIRClass EngduinoIR;
00101 
00102 #endif
00103 
00104 /** @} */