BrickUp API Service for Docker version.

IRSendRevInt.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * IRremote
  3. * Version 0.1 July, 2009
  4. * Copyright 2009 Ken Shirriff
  5. * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
  6. *
  7. * Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
  8. *
  9. * Interrupt code based on NECIRrcv by Joe Knapp
  10. * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
  11. * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
  12. *
  13. * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
  14. */
  15. #ifndef _IRSENDREVINT_H_
  16. #define _IRSENDREVINT_H_
  17. #if defined(ARDUINO) && ARDUINO >= 100
  18. #include <Arduino.h>
  19. #else
  20. #include <WProgram.h>
  21. #endif
  22. // define which timer to use
  23. //
  24. // Uncomment the timer you wish to use on your board. If you
  25. // are using another library which uses timer2, you have options
  26. // to switch IRremote to use a different timer.
  27. // Arduino Mega
  28. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  29. //#define IR_USE_TIMER1 // tx = pin 11
  30. #define IR_USE_TIMER2 // tx = pin 9
  31. //#define IR_USE_TIMER3 // tx = pin 5
  32. //#define IR_USE_TIMER4 // tx = pin 6
  33. //#define IR_USE_TIMER5 // tx = pin 46
  34. // Teensy 1.0
  35. #elif defined(__AVR_AT90USB162__)
  36. #define IR_USE_TIMER1 // tx = pin 17
  37. // Teensy 2.0
  38. #elif defined(__AVR_ATmega32U4__)
  39. //#define IR_USE_TIMER1 // tx = pin 14
  40. //#define IR_USE_TIMER3 // tx = pin 9
  41. #define IR_USE_TIMER4_HS // tx = pin 10
  42. // Teensy++ 1.0 & 2.0
  43. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  44. //#define IR_USE_TIMER1 // tx = pin 25
  45. #define IR_USE_TIMER2 // tx = pin 1
  46. //#define IR_USE_TIMER3 // tx = pin 16
  47. // Sanguino
  48. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  49. //#define IR_USE_TIMER1 // tx = pin 13
  50. #define IR_USE_TIMER2 // tx = pin 14
  51. // Atmega8
  52. #elif defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  53. #define IR_USE_TIMER1 // tx = pin 9
  54. // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
  55. #else
  56. //#define IR_USE_TIMER1 // tx = pin 9
  57. #define IR_USE_TIMER2 // tx = pin 3
  58. #endif
  59. #ifdef F_CPU
  60. #define SYSCLOCK F_CPU // main Arduino clock
  61. #else
  62. #define SYSCLOCK 16000000 // main Arduino clock
  63. #endif
  64. #define ERR 0
  65. #define DECODED 1
  66. // defines for setting and clearing register bits
  67. #ifndef cbi
  68. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  69. #endif
  70. #ifndef sbi
  71. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  72. #endif
  73. #define TOLERANCE 25 // percent tolerance in measurements
  74. #define LTOL (1.0 - TOLERANCE/100.)
  75. #define UTOL (1.0 + TOLERANCE/100.)
  76. #define _GAP 5000 // Minimum map between transmissions
  77. #define GAP_TICKS (_GAP/USECPERTICK)
  78. #define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK))
  79. #define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1))
  80. // receiver states
  81. #define STATE_IDLE 2
  82. #define STATE_MARK 3
  83. #define STATE_SPACE 4
  84. #define STATE_STOP 5
  85. // information for the interrupt handler
  86. typedef struct {
  87. uint8_t recvpin; // pin for IR data from detector
  88. uint8_t rcvstate; // state machine
  89. unsigned int timer; // state timer, counts 50uS ticks.
  90. unsigned int rawbuf[RAWBUF]; // raw data
  91. uint8_t rawlen; // counter of entries in rawbuf
  92. } irparams_t;
  93. // Defined in IRremote.cpp
  94. extern volatile irparams_t irparams;
  95. // IR detector output is active low
  96. #define MARK 0
  97. #define SPACE 1
  98. #define TOPBIT 0x80000000
  99. // defines for timer2 (8 bits)
  100. #if defined(IR_USE_TIMER2)
  101. #define TIMER_RESET
  102. #define TIMER_ENABLE_PWM (TCCR2A |= _BV(COM2B1))
  103. #define TIMER_DISABLE_PWM (TCCR2A &= ~(_BV(COM2B1)))
  104. #define TIMER_ENABLE_INTR (TIMSK2 = _BV(OCIE2A))
  105. #define TIMER_DISABLE_INTR (TIMSK2 = 0)
  106. #define TIMER_INTR_NAME TIMER2_COMPA_vect
  107. #define TIMER_CONFIG_KHZ(val) ({ \
  108. const uint8_t pwmval = SYSCLOCK / 2000 / (val); \
  109. TCCR2A = _BV(WGM20); \
  110. TCCR2B = _BV(WGM22) | _BV(CS20); \
  111. OCR2A = pwmval; \
  112. OCR2B = pwmval / (100/10); \
  113. })
  114. #define TIMER_COUNT_TOP (SYSCLOCK * USECPERTICK / 1000000)
  115. #if (TIMER_COUNT_TOP < 256)
  116. #define TIMER_CONFIG_NORMAL() ({ \
  117. TCCR2A = _BV(WGM21); \
  118. TCCR2B = _BV(CS20); \
  119. OCR2A = TIMER_COUNT_TOP; \
  120. TCNT2 = 0; \
  121. })
  122. #else
  123. #define TIMER_CONFIG_NORMAL() ({ \
  124. TCCR2A = _BV(WGM21); \
  125. TCCR2B = _BV(CS21); \
  126. OCR2A = TIMER_COUNT_TOP / 8; \
  127. TCNT2 = 0; \
  128. })
  129. #endif
  130. #if defined(CORE_OC2B_PIN)
  131. #define TIMER_PWM_PIN CORE_OC2B_PIN /* Teensy */
  132. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  133. #define TIMER_PWM_PIN 9 /* Arduino Mega */
  134. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  135. #define TIMER_PWM_PIN 14 /* Sanguino */
  136. #else
  137. #define TIMER_PWM_PIN 3 /* Arduino Duemilanove, Diecimi */
  138. /* la, LilyPad, etc */
  139. #endif
  140. // defines for timer1 (16 bits)
  141. #elif defined(IR_USE_TIMER1)
  142. #define TIMER_RESET
  143. #define TIMER_ENABLE_PWM (TCCR1A |= _BV(COM1A1))
  144. #define TIMER_DISABLE_PWM (TCCR1A &= ~(_BV(COM1A1)))
  145. #if defined(__AVR_ATmega8P__) || defined(__AVR_ATmega8__)
  146. #define TIMER_ENABLE_INTR (TIMSK = _BV(OCIE1A))
  147. #define TIMER_DISABLE_INTR (TIMSK = 0)
  148. #else
  149. #define TIMER_ENABLE_INTR (TIMSK1 = _BV(OCIE1A))
  150. #define TIMER_DISABLE_INTR (TIMSK1 = 0)
  151. #endif
  152. #define TIMER_INTR_NAME TIMER1_COMPA_vect
  153. #define TIMER_CONFIG_KHZ(val) ({ \
  154. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  155. TCCR1A = _BV(WGM11); \
  156. TCCR1B = _BV(WGM13) | _BV(CS10); \
  157. ICR1 = pwmval; \
  158. OCR1A = pwmval / 3; \
  159. })
  160. #define TIMER_CONFIG_NORMAL() ({ \
  161. TCCR1A = 0; \
  162. TCCR1B = _BV(WGM12) | _BV(CS10); \
  163. OCR1A = SYSCLOCK * USECPERTICK / 1000000; \
  164. TCNT1 = 0; \
  165. })
  166. #if defined(CORE_OC1A_PIN)
  167. #define TIMER_PWM_PIN CORE_OC1A_PIN /* Teensy */
  168. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  169. #define TIMER_PWM_PIN 11 /* Arduino Mega */
  170. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)
  171. #define TIMER_PWM_PIN 13 /* Sanguino */
  172. #else
  173. #define TIMER_PWM_PIN 9 /* Arduino Duemilanove, Diecimi */
  174. /* la, LilyPad, etc */
  175. #endif
  176. // defines for timer3 (16 bits)
  177. #elif defined(IR_USE_TIMER3)
  178. #define TIMER_RESET
  179. #define TIMER_ENABLE_PWM (TCCR3A |= _BV(COM3A1))
  180. #define TIMER_DISABLE_PWM (TCCR3A &= ~(_BV(COM3A1)))
  181. #define TIMER_ENABLE_INTR (TIMSK3 = _BV(OCIE3A))
  182. #define TIMER_DISABLE_INTR (TIMSK3 = 0)
  183. #define TIMER_INTR_NAME TIMER3_COMPA_vect
  184. #define TIMER_CONFIG_KHZ(val) ({ \
  185. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  186. TCCR3A = _BV(WGM31); \
  187. TCCR3B = _BV(WGM33) | _BV(CS30); \
  188. ICR3 = pwmval; \
  189. OCR3A = pwmval / 3; \
  190. })
  191. #define TIMER_CONFIG_NORMAL() ({ \
  192. TCCR3A = 0; \
  193. TCCR3B = _BV(WGM32) | _BV(CS30); \
  194. OCR3A = SYSCLOCK * USECPERTICK / 1000000; \
  195. TCNT3 = 0; \
  196. })
  197. #if defined(CORE_OC3A_PIN)
  198. #define TIMER_PWM_PIN CORE_OC3A_PIN /* Teensy */
  199. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  200. #define TIMER_PWM_PIN 5 /* Arduino Mega */
  201. #else
  202. #error "Please add OC3A pin number here\n"
  203. #endif
  204. // defines for timer4 (10 bits, high speed option)
  205. #elif defined(IR_USE_TIMER4_HS)
  206. #define TIMER_RESET
  207. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  208. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  209. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(TOIE4))
  210. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  211. #define TIMER_INTR_NAME TIMER4_OVF_vect
  212. #define TIMER_CONFIG_KHZ(val) ({ \
  213. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  214. TCCR4A = (1<<PWM4A); \
  215. TCCR4B = _BV(CS40); \
  216. TCCR4C = 0; \
  217. TCCR4D = (1<<WGM40); \
  218. TCCR4E = 0; \
  219. TC4H = pwmval >> 8; \
  220. OCR4C = pwmval; \
  221. TC4H = (pwmval / 3) >> 8; \
  222. OCR4A = (pwmval / 3) & 255; \
  223. })
  224. #define TIMER_CONFIG_NORMAL() ({ \
  225. TCCR4A = 0; \
  226. TCCR4B = _BV(CS40); \
  227. TCCR4C = 0; \
  228. TCCR4D = 0; \
  229. TCCR4E = 0; \
  230. TC4H = (SYSCLOCK * USECPERTICK / 1000000) >> 8; \
  231. OCR4C = (SYSCLOCK * USECPERTICK / 1000000) & 255; \
  232. TC4H = 0; \
  233. TCNT4 = 0; \
  234. })
  235. #if defined(CORE_OC4A_PIN)
  236. #define TIMER_PWM_PIN CORE_OC4A_PIN /* Teensy */
  237. #else
  238. #error "Please add OC4A pin number here\n"
  239. #endif
  240. // defines for timer4 (16 bits)
  241. #elif defined(IR_USE_TIMER4)
  242. #define TIMER_RESET
  243. #define TIMER_ENABLE_PWM (TCCR4A |= _BV(COM4A1))
  244. #define TIMER_DISABLE_PWM (TCCR4A &= ~(_BV(COM4A1)))
  245. #define TIMER_ENABLE_INTR (TIMSK4 = _BV(OCIE4A))
  246. #define TIMER_DISABLE_INTR (TIMSK4 = 0)
  247. #define TIMER_INTR_NAME TIMER4_COMPA_vect
  248. #define TIMER_CONFIG_KHZ(val) ({ \
  249. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  250. TCCR4A = _BV(WGM41); \
  251. TCCR4B = _BV(WGM43) | _BV(CS40); \
  252. ICR4 = pwmval; \
  253. OCR4A = pwmval / 3; \
  254. })
  255. #define TIMER_CONFIG_NORMAL() ({ \
  256. TCCR4A = 0; \
  257. TCCR4B = _BV(WGM42) | _BV(CS40); \
  258. OCR4A = SYSCLOCK * USECPERTICK / 1000000; \
  259. TCNT4 = 0; \
  260. })
  261. #if defined(CORE_OC4A_PIN)
  262. #define TIMER_PWM_PIN CORE_OC4A_PIN
  263. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  264. #define TIMER_PWM_PIN 6 /* Arduino Mega */
  265. #else
  266. #error "Please add OC4A pin number here\n"
  267. #endif
  268. // defines for timer5 (16 bits)
  269. #elif defined(IR_USE_TIMER5)
  270. #define TIMER_RESET
  271. #define TIMER_ENABLE_PWM (TCCR5A |= _BV(COM5A1))
  272. #define TIMER_DISABLE_PWM (TCCR5A &= ~(_BV(COM5A1)))
  273. #define TIMER_ENABLE_INTR (TIMSK5 = _BV(OCIE5A))
  274. #define TIMER_DISABLE_INTR (TIMSK5 = 0)
  275. #define TIMER_INTR_NAME TIMER5_COMPA_vect
  276. #define TIMER_CONFIG_KHZ(val) ({ \
  277. const uint16_t pwmval = SYSCLOCK / 2000 / (val); \
  278. TCCR5A = _BV(WGM51); \
  279. TCCR5B = _BV(WGM53) | _BV(CS50); \
  280. ICR5 = pwmval; \
  281. OCR5A = pwmval / 3; \
  282. })
  283. #define TIMER_CONFIG_NORMAL() ({ \
  284. TCCR5A = 0; \
  285. TCCR5B = _BV(WGM52) | _BV(CS50); \
  286. OCR5A = SYSCLOCK * USECPERTICK / 1000000; \
  287. TCNT5 = 0; \
  288. })
  289. #if defined(CORE_OC5A_PIN)
  290. #define TIMER_PWM_PIN CORE_OC5A_PIN
  291. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  292. #define TIMER_PWM_PIN 46 /* Arduino Mega */
  293. #else
  294. #error "Please add OC5A pin number here\n"
  295. #endif
  296. #else // unknown timer
  297. #error "Internal code configuration error, no known IR_USE_TIMER# defined\n"
  298. #endif
  299. #endif