Subversion Repositories HomeAutomation

Rev

Rev 1929 | Rev 2199 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * @defgroup infrared IR Protocols Driver Library
  3.  * @code #include <drivers/ir/protocols.h> @endcode
  4.  *
  5.  * @brief  
  6.  *
  7.  *
  8.  * @author  Andreas Fritiofson, Anders Runeson
  9.  * @date    2007
  10.  */
  11.  
  12. /**@{*/
  13.  
  14.  
  15. #ifndef IR_PROTOCOLS_H_
  16. #define IR_PROTOCOLS_H_
  17. #include <stdio.h>
  18. #include <config.h>
  19.  
  20. /* Protocol data structure */
  21. typedef struct {
  22.     uint8_t protocol;
  23.     uint64_t data;
  24.     uint16_t timeout;       //Set to 0 to indicate burst protocol (only one pulse train is sent)
  25.     uint8_t repeats;
  26.     uint16_t modfreq;
  27.     uint8_t framecnt;
  28. } Ir_Protocol_Data_t;
  29.  
  30. /* Which protocols to use. These should perhaps be configuration options
  31.  * (config.inc). Only applies to receiver right now */
  32. #define IR_PROTOCOLS_USE_SIRC       1
  33. #define IR_PROTOCOLS_USE_RC5        1
  34. #define IR_PROTOCOLS_USE_SHARP      1
  35. #define IR_PROTOCOLS_USE_NEC        1
  36. #define IR_PROTOCOLS_USE_SAMSUNG    1
  37. #define IR_PROTOCOLS_USE_MARANTZ    1
  38. #define IR_PROTOCOLS_USE_PANASONIC  1
  39. #define IR_PROTOCOLS_USE_SKY        1
  40. #define IR_PROTOCOLS_USE_NEXA2      1
  41. #define IR_PROTOCOLS_USE_NEXA1      1
  42.  
  43. /* All these functions take a buffer with pulse times and tries to parse it
  44.  * to a Ir_Protocol_Data_t structure. They return IR_OK on success
  45.  * and IR_NOT_CORRECT_DATA if there was no match. */
  46. #if (IR_PROTOCOLS_USE_SIRC)
  47. int8_t parseSIRC(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  48. #endif
  49. #if (IR_PROTOCOLS_USE_RC5)
  50. int8_t parseRC5(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  51. #endif
  52. #if (IR_PROTOCOLS_USE_SHARP)
  53. int8_t parseSharp(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  54. #endif
  55. #if (IR_PROTOCOLS_USE_NEC)
  56. int8_t parseNEC(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  57. #endif
  58. #if (IR_PROTOCOLS_USE_SAMSUNG)
  59. int8_t parseSamsung(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  60. #endif
  61. #if (IR_PROTOCOLS_USE_MARANTZ)
  62. int8_t parseMarantz(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  63. #endif
  64. #if (IR_PROTOCOLS_USE_PANASONIC)
  65. int8_t parsePanasonic(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  66. #endif
  67. #if (IR_PROTOCOLS_USE_SKY)
  68. int8_t parseSky(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  69. #endif
  70. #if (IR_PROTOCOLS_USE_NEXA2)
  71. int8_t parseNexa2(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  72. #endif
  73. #if (IR_PROTOCOLS_USE_NEXA1)
  74. int8_t parseNexa1(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  75. #endif
  76.  
  77. /* Try to parse all above protocols until a match is found. */
  78. int8_t parseProtocol(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  79. /* The Hash protocol always succeeds and creates a one-way signature of the
  80.  * pulse times buffer that is supposed to be unique and constant for a specific
  81.  * IR event. It may be used to send a valid event even if the protocol hasn't
  82.  * been implemented or decoded yet. */
  83. int8_t parseHash(const uint16_t *buf, uint8_t len, Ir_Protocol_Data_t *proto);
  84.  
  85. /* These functions expand a Ir_Protocol_Data_t structure into a raw buffer with
  86.  * pulse lengths. They return IR_OK on success and IR_NOT_CORRECT_DATA if the
  87.  * Ir_Protocol_Data_t structure is for another protocol or contains invalid
  88.  * data. */
  89. int8_t expandSIRC(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  90. int8_t expandRC5(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  91. int8_t expandSharp(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  92. int8_t expandNEC(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  93. int8_t expandSamsung(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  94. int8_t expandMarantz(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  95. int8_t expandPanasonic(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  96. int8_t expandSky(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  97. int8_t expandNexa2(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  98. int8_t expandNexa1(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  99. /* Pass the Ir_Protocol_Data_t automatically to the correct function. */
  100. int8_t expandProtocol(uint16_t *buf, uint8_t *len, Ir_Protocol_Data_t *proto);
  101.  
  102.  
  103. #define IR_OK               1
  104. #define IR_NO_PROTOCOL      2
  105. #define IR_TIME_OVFL        3
  106. #define IR_NO_DATA          4
  107. #define IR_NOT_CORRECT_DATA 5
  108. #define IR_TO_MUCH_DATA     6
  109. #define IR_NOT_FINISHED     7
  110.  
  111. #define IR_BUTTON_DOWN      0x0
  112. #define IR_BUTTON_UP        0xf
  113.  
  114. // defined for the module, uses same definitions for pressed and released as other buttons
  115. #define IR_BUTTON_RELEASED  0x0
  116. #define IR_BUTTON_PRESSED   0x1
  117.  
  118.  
  119. #define CYCLES_PER_US       (F_CPU/1000000UL)
  120. #define TIMER_PRESC         (8)
  121.  
  122. #ifndef IR_MIN_PULSE_WIDTH
  123. #define IR_MIN_PULSE_WIDTH  (0)                                 //us, used in continuous mode
  124. #endif
  125. #ifndef IR_MIN_STARTPULSE_WIDTH
  126. #define IR_MIN_STARTPULSE_WIDTH (0UL)                           //us, not used when set to 0
  127. #endif
  128. #ifndef IR_MAX_PULSE_WIDTH
  129. #define IR_MAX_PULSE_WIDTH  (12000UL)                           //us
  130. #endif
  131.  
  132. /* RC5 Implementation
  133.  * Receiver: DONE
  134.  * Transmitter: DONE
  135.  */
  136. #define IR_PROTO_RC5        (0)
  137. #define IR_RC5_HALF_BIT     (889*CYCLES_PER_US/TIMER_PRESC)     //us
  138. #define IR_RC5_BIT          (889*2*CYCLES_PER_US/TIMER_PRESC)   //us
  139. #define IR_RC5_TIMEOUT      (117-IR_MAX_PULSE_WIDTH/1000)       //ms    (time between ir frames)
  140. #define IR_RC5_REPS         (1)                                 //      (minimum number of times to repeat code)
  141. #define IR_RC5_F_MOD        (36)                                //kHz   (modulation frequency)
  142. #define IR_RC5_TOL_DIV      (4)
  143.  
  144. /* RC6 Implementation
  145.  * Receiver:
  146.  * Transmitter:
  147.  */
  148. #define IR_PROTO_RC6        (1)
  149. #define IR_RC6_ST_BIT       (1*CYCLES_PER_US/TIMER_PRESC)       //us
  150. #define IR_RC6_TIMEOUT      (50-IR_MAX_PULSE_WIDTH/1000)        //ms    (time between ir frames)
  151. #define IR_RC6_REPS         (1)                                 //      (minimum number of times to repeat code)
  152. #define IR_RC6_F_MOD        (36)                                //kHz   (modulation frequency)
  153. #define IR_RC6_TOL_DIV      (4)
  154.  
  155. /* RCMM Implementation
  156.  * Receiver:
  157.  * Transmitter:
  158.  */
  159. #define IR_PROTO_RCMM       (2)
  160. #define IR_RCMM_ST_BIT      (256*CYCLES_PER_US/TIMER_PRESC)     //us
  161. #define IR_RCMM_TIMEOUT     (170-IR_MAX_PULSE_WIDTH/1000)       //ms    (time between ir frames)
  162. #define IR_RCMM_REPS        (1)                                 //      (minimum number of times to repeat code)
  163. #define IR_RCMM_F_MOD       (36)                                //kHz   (modulation frequency)
  164. #define IR_RCMM_TOL_DIV     (4)
  165.  
  166. /* SIRC Implementation
  167.  * Receiver: DONE
  168.  * Transmitter: DONE
  169.  */
  170. #define IR_PROTO_SIRC       (3)
  171. #define IR_SIRC_ST_BIT      (2400*CYCLES_PER_US/TIMER_PRESC)    //us
  172. #define IR_SIRC_LOW         (600*CYCLES_PER_US/TIMER_PRESC)     //us
  173. #define IR_SIRC_HIGH_ONE    (1200*CYCLES_PER_US/TIMER_PRESC)    //us
  174. #define IR_SIRC_HIGH_ZERO   (600*CYCLES_PER_US/TIMER_PRESC)     //us
  175. #define IR_SIRC_TIMEOUT     (52-IR_MAX_PULSE_WIDTH/1000)                                    //ms    (time between ir frames)
  176. #define IR_SIRC_REPS        (3)                                 //      (minimum number of times to repeat code)
  177. #define IR_SIRC_F_MOD       (40)                                //kHz   (modulation frequency)
  178. #define IR_SIRC_TOL_DIV     (4)
  179.  
  180. /* Sharp Implementation
  181.  * Receiver: DONE
  182.  * Transmitter:
  183.  */
  184. #define IR_PROTO_SHARP      (4)
  185. #define IR_SHARP_HIGH       (280*CYCLES_PER_US/TIMER_PRESC)     //us
  186. #define IR_SHARP_LOW_ONE    (1850*CYCLES_PER_US/TIMER_PRESC)    //us
  187. #define IR_SHARP_LOW_ZERO   (780*CYCLES_PER_US/TIMER_PRESC)     //us
  188. #define IR_SHARP_TIMEOUT    (67-IR_MAX_PULSE_WIDTH/1000)        //ms    (time between ir frames)
  189. #define IR_SHARP_REPS       (2)                                 //      (minimum number of times to repeat code)
  190. #define IR_SHARP_F_MOD      (38)                                //kHz   (modulation frequency)
  191. #define IR_SHARP_TOL_DIV    (4)
  192.  
  193. /* NEC Implementation
  194.  * Receiver: DONE
  195.  * Transmitter: DONE
  196.  */
  197. #define IR_PROTO_NEC        (5)
  198. #define IR_NEC_ST_BIT       (9000*CYCLES_PER_US/TIMER_PRESC)    //us
  199. #define IR_NEC_ST_PAUSE     (4500*CYCLES_PER_US/TIMER_PRESC)    //us
  200. #define IR_NEC_LOW_ONE      (1690*CYCLES_PER_US/TIMER_PRESC)    //us
  201. #define IR_NEC_LOW_ZERO     (560*CYCLES_PER_US/TIMER_PRESC)     //us
  202. #define IR_NEC_HIGH         (560*CYCLES_PER_US/TIMER_PRESC)     //us
  203. #define IR_NEC_TIMEOUT      (122-IR_MAX_PULSE_WIDTH/1000)       //ms    (time between ir frames)
  204. #define IR_NEC_ST_TIMEOUT   (65)                                //ms    (time between first ir frames and second)
  205. #define IR_NEC_REPS         (1)                                 //      (minimum number of times to repeat code)
  206. #define IR_NEC_F_MOD        (38)                                //kHz   (modulation frequency)
  207. #define IR_NEC_TOL_DIV      (4)
  208.  
  209. /* Samsung Implementation
  210.  * Receiver: DONE
  211.  * Transmitter: DONE
  212.  */
  213. #define IR_PROTO_SAMS       (6)
  214. #define IR_SAMS_ST_BIT      (4500*CYCLES_PER_US/TIMER_PRESC)    //us
  215. #define IR_SAMS_ST_PAUSE    (4500*CYCLES_PER_US/TIMER_PRESC)    //us
  216. #define IR_SAMS_LOW_ONE     (1720*CYCLES_PER_US/TIMER_PRESC)    //us
  217. #define IR_SAMS_LOW_ZERO    (650*CYCLES_PER_US/TIMER_PRESC)     //us
  218. #define IR_SAMS_HIGH        (500*CYCLES_PER_US/TIMER_PRESC)     //us
  219. #define IR_SAMS_TIMEOUT     (62-IR_MAX_PULSE_WIDTH/1000)        //ms    (time between ir frames)
  220. #define IR_SAMS_REPS        (1)                                 //      (minimum number of times to repeat code)
  221. #define IR_SAMS_F_MOD       (38)                                //kHz   (modulation frequency)
  222. #define IR_SAMS_TOL_DIV     (4)
  223.  
  224. /* Marantz Implementation
  225.  * Receiver: DONE (has not been tested with odd adressbits)
  226.  * Transmitter: DONE
  227.  */
  228. #define IR_PROTO_MARANTZ        (7)
  229. #define IR_MARANTZ_HALF_BIT     (889*CYCLES_PER_US/TIMER_PRESC)     //us
  230. #define IR_MARANTZ_BIT          (889*2*CYCLES_PER_US/TIMER_PRESC)   //us
  231. #define IR_MARANTZ_TIMEOUT      (117-IR_MAX_PULSE_WIDTH/1000)       //ms    (time between ir frames)
  232. #define IR_MARANTZ_REPS         (1)                                 //      (minimum number of times to repeat code)
  233. #define IR_MARANTZ_F_MOD        (36)                                //kHz   (modulation frequency)
  234. #define IR_MARANTZ_TOL_DIV      (4)
  235.  
  236. /* Panasonic Implementation
  237.  * Receiver: DONE
  238.  * Transmitter: DONE
  239.  */
  240. #define IR_PROTO_PANASONIC  (8)
  241. #define IR_PANA_ST_BIT      (3570*CYCLES_PER_US/TIMER_PRESC)    //us
  242. #define IR_PANA_ST_PAUSE    (1630*CYCLES_PER_US/TIMER_PRESC)    //us
  243. #define IR_PANA_LOW_ONE     (1240*CYCLES_PER_US/TIMER_PRESC)    //us
  244. #define IR_PANA_LOW_ZERO    (400*CYCLES_PER_US/TIMER_PRESC)     //us
  245. #define IR_PANA_HIGH        (495*CYCLES_PER_US/TIMER_PRESC)     //us
  246. #define IR_PANA_TIMEOUT     (92-IR_MAX_PULSE_WIDTH/1000)        //ms    (time between ir frames)
  247. #define IR_PANA_REPS        (1)                                 //      (minimum number of times to repeat code)
  248. #define IR_PANA_F_MOD       (38)                                //kHz   (modulation frequency)
  249. #define IR_PANA_TOL_DIV     (4)
  250.  
  251. /* Sky Implementation
  252.  * Receiver: DONE
  253.  * Transmitter:
  254.  */
  255. #define IR_PROTO_SKY        (9)
  256. #define IR_SKY_ST_BIT       (2800*CYCLES_PER_US/TIMER_PRESC)    //us
  257. #define IR_SKY_SHORT        (460*CYCLES_PER_US/TIMER_PRESC)     //us
  258. #define IR_SKY_LONG         (920*CYCLES_PER_US/TIMER_PRESC)     //us
  259. #define IR_SKY_TIMEOUT      (162-IR_MAX_PULSE_WIDTH/1000)       //ms    (time between ir frames)
  260. #define IR_SKY_REPS         (1)                                 //      (minimum number of times to repeat code)
  261. #define IR_SKY_F_MOD        (38)                                //kHz   (modulation frequency)
  262. #define IR_SKY_TOL_DIV      (4)
  263.  
  264.  
  265. /* Nexa2 Implementation
  266.  * Receiver: DONE
  267.  * Transmitter: DONE
  268.  */
  269. #define IR_PROTO_NEXA2      (10)
  270. #define IR_NEXA2_START1     (10000*CYCLES_PER_US/TIMER_PRESC)   //us
  271. #define IR_NEXA2_START2     (2500*CYCLES_PER_US/TIMER_PRESC)    //us
  272. #define IR_NEXA2_HIGH       (320*CYCLES_PER_US/TIMER_PRESC)     //us
  273. #define IR_NEXA2_LOW_ONE    (210*CYCLES_PER_US/TIMER_PRESC)     //us
  274. #define IR_NEXA2_LOW_ZERO   (1200*CYCLES_PER_US/TIMER_PRESC)    //us
  275. #define IR_NEXA2_TIMEOUT    (200)                               //ms    (time between ir frames)
  276. #define IR_NEXA2_REPS       (5)                                 //      (minimum number of times to repeat code)
  277. #define IR_NEXA2_F_MOD      (38)                                //kHz   (modulation frequency)
  278. #define IR_NEXA2_TOL_DIV    (2)
  279.  
  280.  
  281. /* Nexa1 Implementation
  282.  * Receiver: DONE
  283.  * Transmitter:
  284.  */
  285. #define IR_PROTO_NEXA1      (11)
  286. #define IR_NEXA1_START      (10000*CYCLES_PER_US/TIMER_PRESC)   //us
  287. #define IR_NEXA1_SHORT      (370*CYCLES_PER_US/TIMER_PRESC)     //us
  288. #define IR_NEXA1_LONG       (1050*CYCLES_PER_US/TIMER_PRESC)    //us
  289. #define IR_NEXA1_TIMEOUT    (200)                               //ms    (time between ir frames)
  290. #define IR_NEXA1_REPS       (4)                                 //      (minimum number of times to repeat code)
  291. #define IR_NEXA1_F_MOD      (38)                                //kHz   (modulation frequency)
  292. #define IR_NEXA1_TOL_DIV    (2)
  293.  
  294.  
  295. /* Viking Temperature sensor Implementation
  296.  * Receiver:
  297.  */
  298. #define IR_PROTO_VIKING     (12)
  299. #define IR_VIKING_LOW       (940*CYCLES_PER_US/TIMER_PRESC)     //us
  300. #define IR_VIKING_HIGH_ONE  (1483*CYCLES_PER_US/TIMER_PRESC)        //us
  301. #define IR_VIKING_HIGH_ZERO (550*CYCLES_PER_US/TIMER_PRESC) //us
  302. #define IR_VIKING_TIMEOUT   (0)                                 //ms BURST! (time between ir frames)
  303. #define IR_VIKING_REPS      (1)                                 //      (minimum number of times to repeat code)
  304. #define IR_VIKING_F_MOD     (38)                                //kHz   (modulation frequency)
  305. #define IR_VIKING_TOL_DIV   (4)
  306.  
  307.  
  308. #define IR_PROTO_HASH       0xfe
  309. #define IR_PROTO_UNKNOWN    0xff                                //
  310.  
  311.  
  312.  
  313. /**@}*/
  314. #endif /*IR_PROTOCOLS_H_*/
  315.