IRremote.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * IRremote
  3. * Version 0.11 August, 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. * Modified by Mitra Ardron <mitra@mitra.biz>
  9. * Added Sanyo and Mitsubishi controllers
  10. * Modified Sony to spot the repeat codes that some Sony's send
  11. *
  12. * Interrupt code based on NECIRrcv by Joe Knapp
  13. * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
  14. * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
  15. *
  16. * JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
  17. */
  18. #include "IRremote.h"
  19. #include "IRremoteInt.h"
  20. // Provides ISR
  21. #include <avr/interrupt.h>
  22. volatile irparams_t irparams;
  23. // These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging.
  24. // To use them, set DEBUG in IRremoteInt.h
  25. // Normally macros are used for efficiency
  26. #ifdef DEBUG
  27. int MATCH(int measured, int desired) {
  28. Serial.print("Testing: ");
  29. Serial.print(TICKS_LOW(desired), DEC);
  30. Serial.print(" <= ");
  31. Serial.print(measured, DEC);
  32. Serial.print(" <= ");
  33. Serial.println(TICKS_HIGH(desired), DEC);
  34. return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);
  35. }
  36. int MATCH_MARK(int measured_ticks, int desired_us) {
  37. Serial.print("Testing mark ");
  38. Serial.print(measured_ticks * USECPERTICK, DEC);
  39. Serial.print(" vs ");
  40. Serial.print(desired_us, DEC);
  41. Serial.print(": ");
  42. Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
  43. Serial.print(" <= ");
  44. Serial.print(measured_ticks, DEC);
  45. Serial.print(" <= ");
  46. Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
  47. return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS);
  48. }
  49. int MATCH_SPACE(int measured_ticks, int desired_us) {
  50. Serial.print("Testing space ");
  51. Serial.print(measured_ticks * USECPERTICK, DEC);
  52. Serial.print(" vs ");
  53. Serial.print(desired_us, DEC);
  54. Serial.print(": ");
  55. Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
  56. Serial.print(" <= ");
  57. Serial.print(measured_ticks, DEC);
  58. Serial.print(" <= ");
  59. Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
  60. return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS);
  61. }
  62. #endif
  63. void IRsend::sendNEC(unsigned long data, int nbits)
  64. {
  65. enableIROut(38);
  66. mark(NEC_HDR_MARK);
  67. space(NEC_HDR_SPACE);
  68. for (int i = 0; i < nbits; i++) {
  69. if (data & TOPBIT) {
  70. mark(NEC_BIT_MARK);
  71. space(NEC_ONE_SPACE);
  72. }
  73. else {
  74. mark(NEC_BIT_MARK);
  75. space(NEC_ZERO_SPACE);
  76. }
  77. data <<= 1;
  78. }
  79. mark(NEC_BIT_MARK);
  80. space(0);
  81. }
  82. void IRsend::sendSony(unsigned long data, int nbits) {
  83. enableIROut(40);
  84. mark(SONY_HDR_MARK);
  85. space(SONY_HDR_SPACE);
  86. data = data << (32 - nbits);
  87. for (int i = 0; i < nbits; i++) {
  88. if (data & TOPBIT) {
  89. mark(SONY_ONE_MARK);
  90. space(SONY_HDR_SPACE);
  91. }
  92. else {
  93. mark(SONY_ZERO_MARK);
  94. space(SONY_HDR_SPACE);
  95. }
  96. data <<= 1;
  97. }
  98. }
  99. void IRsend::sendRaw(unsigned int buf[], int len, int hz)
  100. {
  101. enableIROut(hz);
  102. for (int i = 0; i < len; i++) {
  103. if (i & 1) {
  104. space(buf[i]);
  105. }
  106. else {
  107. mark(buf[i]);
  108. }
  109. }
  110. space(0); // Just to be sure
  111. }
  112. // Note: first bit must be a one (start bit)
  113. void IRsend::sendRC5(unsigned long data, int nbits)
  114. {
  115. enableIROut(36);
  116. data = data << (32 - nbits);
  117. mark(RC5_T1); // First start bit
  118. space(RC5_T1); // Second start bit
  119. mark(RC5_T1); // Second start bit
  120. for (int i = 0; i < nbits; i++) {
  121. if (data & TOPBIT) {
  122. space(RC5_T1); // 1 is space, then mark
  123. mark(RC5_T1);
  124. }
  125. else {
  126. mark(RC5_T1);
  127. space(RC5_T1);
  128. }
  129. data <<= 1;
  130. }
  131. space(0); // Turn off at end
  132. }
  133. // Caller needs to take care of flipping the toggle bit
  134. void IRsend::sendRC6(unsigned long data, int nbits)
  135. {
  136. enableIROut(36);
  137. data = data << (32 - nbits);
  138. mark(RC6_HDR_MARK);
  139. space(RC6_HDR_SPACE);
  140. mark(RC6_T1); // start bit
  141. space(RC6_T1);
  142. int t;
  143. for (int i = 0; i < nbits; i++) {
  144. if (i == 3) {
  145. // double-wide trailer bit
  146. t = 2 * RC6_T1;
  147. }
  148. else {
  149. t = RC6_T1;
  150. }
  151. if (data & TOPBIT) {
  152. mark(t);
  153. space(t);
  154. }
  155. else {
  156. space(t);
  157. mark(t);
  158. }
  159. data <<= 1;
  160. }
  161. space(0); // Turn off at end
  162. }
  163. void IRsend::sendPanasonic(unsigned int address, unsigned long data) {
  164. enableIROut(35);
  165. mark(PANASONIC_HDR_MARK);
  166. space(PANASONIC_HDR_SPACE);
  167. for(int i=0;i<16;i++)
  168. {
  169. mark(PANASONIC_BIT_MARK);
  170. if (address & 0x8000) {
  171. space(PANASONIC_ONE_SPACE);
  172. } else {
  173. space(PANASONIC_ZERO_SPACE);
  174. }
  175. address <<= 1;
  176. }
  177. for (int i=0; i < 32; i++) {
  178. mark(PANASONIC_BIT_MARK);
  179. if (data & TOPBIT) {
  180. space(PANASONIC_ONE_SPACE);
  181. } else {
  182. space(PANASONIC_ZERO_SPACE);
  183. }
  184. data <<= 1;
  185. }
  186. mark(PANASONIC_BIT_MARK);
  187. space(0);
  188. }
  189. void IRsend::sendJVC(unsigned long data, int nbits, int repeat)
  190. {
  191. enableIROut(38);
  192. data = data << (32 - nbits);
  193. if (!repeat){
  194. mark(JVC_HDR_MARK);
  195. space(JVC_HDR_SPACE);
  196. }
  197. for (int i = 0; i < nbits; i++) {
  198. if (data & TOPBIT) {
  199. mark(JVC_BIT_MARK);
  200. space(JVC_ONE_SPACE);
  201. }
  202. else {
  203. mark(JVC_BIT_MARK);
  204. space(JVC_ZERO_SPACE);
  205. }
  206. data <<= 1;
  207. }
  208. mark(JVC_BIT_MARK);
  209. space(0);
  210. }
  211. void IRsend::mark(int time) {
  212. // Sends an IR mark for the specified number of microseconds.
  213. // The mark output is modulated at the PWM frequency.
  214. TIMER_ENABLE_PWM; // Enable pin 3 PWM output
  215. delayMicroseconds(time);
  216. }
  217. /* Leave pin off for time (given in microseconds) */
  218. void IRsend::space(int time) {
  219. // Sends an IR space for the specified number of microseconds.
  220. // A space is no output, so the PWM output is disabled.
  221. TIMER_DISABLE_PWM; // Disable pin 3 PWM output
  222. delayMicroseconds(time);
  223. }
  224. void IRsend::enableIROut(int khz) {
  225. // Enables IR output. The khz value controls the modulation frequency in kilohertz.
  226. // The IR output will be on pin 3 (OC2B).
  227. // This routine is designed for 36-40KHz; if you use it for other values, it's up to you
  228. // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
  229. // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
  230. // controlling the duty cycle.
  231. // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
  232. // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
  233. // A few hours staring at the ATmega documentation and this will all make sense.
  234. // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
  235. // Disable the Timer2 Interrupt (which is used for receiving IR)
  236. TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt
  237. pinMode(TIMER_PWM_PIN, OUTPUT);
  238. digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low
  239. // COM2A = 00: disconnect OC2A
  240. // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
  241. // WGM2 = 101: phase-correct PWM with OCRA as top
  242. // CS2 = 000: no prescaling
  243. // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
  244. TIMER_CONFIG_KHZ(khz);
  245. }
  246. IRrecv::IRrecv(int recvpin)
  247. {
  248. irparams.recvpin = recvpin;
  249. irparams.blinkflag = 0;
  250. }
  251. // initialization
  252. void IRrecv::enableIRIn() {
  253. cli();
  254. // setup pulse clock timer interrupt
  255. //Prescale /8 (16M/8 = 0.5 microseconds per tick)
  256. // Therefore, the timer interval can range from 0.5 to 128 microseconds
  257. // depending on the reset value (255 to 0)
  258. TIMER_CONFIG_NORMAL();
  259. //Timer2 Overflow Interrupt Enable
  260. TIMER_ENABLE_INTR;
  261. TIMER_RESET;
  262. sei(); // enable interrupts
  263. // initialize state machine variables
  264. irparams.rcvstate = STATE_IDLE;
  265. irparams.rawlen = 0;
  266. // set pin modes
  267. pinMode(irparams.recvpin, INPUT);
  268. }
  269. // enable/disable blinking of pin 13 on IR processing
  270. void IRrecv::blink13(int blinkflag)
  271. {
  272. irparams.blinkflag = blinkflag;
  273. if (blinkflag)
  274. pinMode(BLINKLED, OUTPUT);
  275. }
  276. // TIMER2 interrupt code to collect raw data.
  277. // Widths of alternating SPACE, MARK are recorded in rawbuf.
  278. // Recorded in ticks of 50 microseconds.
  279. // rawlen counts the number of entries recorded so far.
  280. // First entry is the SPACE between transmissions.
  281. // As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
  282. // As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
  283. ISR(TIMER_INTR_NAME)
  284. {
  285. TIMER_RESET;
  286. uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
  287. irparams.timer++; // One more 50us tick
  288. if (irparams.rawlen >= RAWBUF) {
  289. // Buffer overflow
  290. irparams.rcvstate = STATE_STOP;
  291. }
  292. switch(irparams.rcvstate) {
  293. case STATE_IDLE: // In the middle of a gap
  294. if (irdata == MARK) {
  295. if (irparams.timer < GAP_TICKS) {
  296. // Not big enough to be a gap.
  297. irparams.timer = 0;
  298. }
  299. else {
  300. // gap just ended, record duration and start recording transmission
  301. irparams.rawlen = 0;
  302. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  303. irparams.timer = 0;
  304. irparams.rcvstate = STATE_MARK;
  305. }
  306. }
  307. break;
  308. case STATE_MARK: // timing MARK
  309. if (irdata == SPACE) { // MARK ended, record time
  310. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  311. irparams.timer = 0;
  312. irparams.rcvstate = STATE_SPACE;
  313. }
  314. break;
  315. case STATE_SPACE: // timing SPACE
  316. if (irdata == MARK) { // SPACE just ended, record it
  317. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  318. irparams.timer = 0;
  319. irparams.rcvstate = STATE_MARK;
  320. }
  321. else { // SPACE
  322. if (irparams.timer > GAP_TICKS) {
  323. // big SPACE, indicates gap between codes
  324. // Mark current code as ready for processing
  325. // Switch to STOP
  326. // Don't reset timer; keep counting space width
  327. irparams.rcvstate = STATE_STOP;
  328. }
  329. }
  330. break;
  331. case STATE_STOP: // waiting, measuring gap
  332. if (irdata == MARK) { // reset gap timer
  333. irparams.timer = 0;
  334. }
  335. break;
  336. }
  337. if (irparams.blinkflag) {
  338. if (irdata == MARK) {
  339. BLINKLED_ON(); // turn pin 13 LED on
  340. }
  341. else {
  342. BLINKLED_OFF(); // turn pin 13 LED off
  343. }
  344. }
  345. }
  346. void IRrecv::resume() {
  347. irparams.rcvstate = STATE_IDLE;
  348. irparams.rawlen = 0;
  349. }
  350. // Decodes the received IR message
  351. // Returns 0 if no data ready, 1 if data ready.
  352. // Results of decoding are stored in results
  353. int IRrecv::decode(decode_results *results) {
  354. results->rawbuf = irparams.rawbuf;
  355. results->rawlen = irparams.rawlen;
  356. if (irparams.rcvstate != STATE_STOP) {
  357. return ERR;
  358. }
  359. #ifdef DEBUG
  360. Serial.println("Attempting NEC decode");
  361. #endif
  362. if (decodeNEC(results)) {
  363. return DECODED;
  364. }
  365. #ifdef DEBUG
  366. Serial.println("Attempting Sony decode");
  367. #endif
  368. if (decodeSony(results)) {
  369. return DECODED;
  370. }
  371. #ifdef DEBUG
  372. Serial.println("Attempting Sanyo decode");
  373. #endif
  374. if (decodeSanyo(results)) {
  375. return DECODED;
  376. }
  377. #ifdef DEBUG
  378. Serial.println("Attempting Mitsubishi decode");
  379. #endif
  380. if (decodeMitsubishi(results)) {
  381. return DECODED;
  382. }
  383. #ifdef DEBUG
  384. Serial.println("Attempting RC5 decode");
  385. #endif
  386. if (decodeRC5(results)) {
  387. return DECODED;
  388. }
  389. #ifdef DEBUG
  390. Serial.println("Attempting RC6 decode");
  391. #endif
  392. if (decodeRC6(results)) {
  393. return DECODED;
  394. }
  395. #ifdef DEBUG
  396. Serial.println("Attempting Panasonic decode");
  397. #endif
  398. if (decodePanasonic(results)) {
  399. return DECODED;
  400. }
  401. #ifdef DEBUG
  402. Serial.println("Attempting JVC decode");
  403. #endif
  404. if (decodeJVC(results)) {
  405. return DECODED;
  406. }
  407. // decodeHash returns a hash on any input.
  408. // Thus, it needs to be last in the list.
  409. // If you add any decodes, add them before this.
  410. if (decodeHash(results)) {
  411. return DECODED;
  412. }
  413. // Throw away and start over
  414. resume();
  415. return ERR;
  416. }
  417. // NECs have a repeat only 4 items long
  418. long IRrecv::decodeNEC(decode_results *results) {
  419. long data = 0;
  420. int offset = 1; // Skip first space
  421. // Initial mark
  422. if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) {
  423. return ERR;
  424. }
  425. offset++;
  426. // Check for repeat
  427. if (irparams.rawlen == 4 &&
  428. MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) &&
  429. MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) {
  430. results->bits = 0;
  431. results->value = REPEAT;
  432. results->decode_type = NEC;
  433. return DECODED;
  434. }
  435. if (irparams.rawlen < 2 * NEC_BITS + 4) {
  436. return ERR;
  437. }
  438. // Initial space
  439. if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) {
  440. return ERR;
  441. }
  442. offset++;
  443. for (int i = 0; i < NEC_BITS; i++) {
  444. if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) {
  445. return ERR;
  446. }
  447. offset++;
  448. if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) {
  449. data = (data << 1) | 1;
  450. }
  451. else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) {
  452. data <<= 1;
  453. }
  454. else {
  455. return ERR;
  456. }
  457. offset++;
  458. }
  459. // Success
  460. results->bits = NEC_BITS;
  461. results->value = data;
  462. results->decode_type = NEC;
  463. return DECODED;
  464. }
  465. long IRrecv::decodeSony(decode_results *results) {
  466. long data = 0;
  467. if (irparams.rawlen < 2 * SONY_BITS + 2) {
  468. return ERR;
  469. }
  470. int offset = 0; // Dont skip first space, check its size
  471. // Some Sony's deliver repeats fast after first
  472. // unfortunately can't spot difference from of repeat from two fast clicks
  473. if (results->rawbuf[offset] < SONY_DOUBLE_SPACE_USECS) {
  474. // Serial.print("IR Gap found: ");
  475. results->bits = 0;
  476. results->value = REPEAT;
  477. results->decode_type = SANYO;
  478. return DECODED;
  479. }
  480. offset++;
  481. // Initial mark
  482. if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) {
  483. return ERR;
  484. }
  485. offset++;
  486. while (offset + 1 < irparams.rawlen) {
  487. if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) {
  488. break;
  489. }
  490. offset++;
  491. if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) {
  492. data = (data << 1) | 1;
  493. }
  494. else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) {
  495. data <<= 1;
  496. }
  497. else {
  498. return ERR;
  499. }
  500. offset++;
  501. }
  502. // Success
  503. results->bits = (offset - 1) / 2;
  504. if (results->bits < 12) {
  505. results->bits = 0;
  506. return ERR;
  507. }
  508. results->value = data;
  509. results->decode_type = SONY;
  510. return DECODED;
  511. }
  512. // I think this is a Sanyo decoder - serial = SA 8650B
  513. // Looks like Sony except for timings, 48 chars of data and time/space different
  514. long IRrecv::decodeSanyo(decode_results *results) {
  515. long data = 0;
  516. if (irparams.rawlen < 2 * SANYO_BITS + 2) {
  517. return ERR;
  518. }
  519. int offset = 0; // Skip first space
  520. // Initial space
  521. /* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay
  522. Serial.print("IR Gap: ");
  523. Serial.println( results->rawbuf[offset]);
  524. Serial.println( "test against:");
  525. Serial.println(results->rawbuf[offset]);
  526. */
  527. if (results->rawbuf[offset] < SANYO_DOUBLE_SPACE_USECS) {
  528. // Serial.print("IR Gap found: ");
  529. results->bits = 0;
  530. results->value = REPEAT;
  531. results->decode_type = SANYO;
  532. return DECODED;
  533. }
  534. offset++;
  535. // Initial mark
  536. if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
  537. return ERR;
  538. }
  539. offset++;
  540. // Skip Second Mark
  541. if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
  542. return ERR;
  543. }
  544. offset++;
  545. while (offset + 1 < irparams.rawlen) {
  546. if (!MATCH_SPACE(results->rawbuf[offset], SANYO_HDR_SPACE)) {
  547. break;
  548. }
  549. offset++;
  550. if (MATCH_MARK(results->rawbuf[offset], SANYO_ONE_MARK)) {
  551. data = (data << 1) | 1;
  552. }
  553. else if (MATCH_MARK(results->rawbuf[offset], SANYO_ZERO_MARK)) {
  554. data <<= 1;
  555. }
  556. else {
  557. return ERR;
  558. }
  559. offset++;
  560. }
  561. // Success
  562. results->bits = (offset - 1) / 2;
  563. if (results->bits < 12) {
  564. results->bits = 0;
  565. return ERR;
  566. }
  567. results->value = data;
  568. results->decode_type = SANYO;
  569. return DECODED;
  570. }
  571. // Looks like Sony except for timings, 48 chars of data and time/space different
  572. long IRrecv::decodeMitsubishi(decode_results *results) {
  573. // Serial.print("?!? decoding Mitsubishi:");Serial.print(irparams.rawlen); Serial.print(" want "); Serial.println( 2 * MITSUBISHI_BITS + 2);
  574. long data = 0;
  575. if (irparams.rawlen < 2 * MITSUBISHI_BITS + 2) {
  576. return ERR;
  577. }
  578. int offset = 0; // Skip first space
  579. // Initial space
  580. /* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay
  581. Serial.print("IR Gap: ");
  582. Serial.println( results->rawbuf[offset]);
  583. Serial.println( "test against:");
  584. Serial.println(results->rawbuf[offset]);
  585. */
  586. /* Not seeing double keys from Mitsubishi
  587. if (results->rawbuf[offset] < MITSUBISHI_DOUBLE_SPACE_USECS) {
  588. // Serial.print("IR Gap found: ");
  589. results->bits = 0;
  590. results->value = REPEAT;
  591. results->decode_type = MITSUBISHI;
  592. return DECODED;
  593. }
  594. */
  595. offset++;
  596. // Typical
  597. // 14200 7 41 7 42 7 42 7 17 7 17 7 18 7 41 7 18 7 17 7 17 7 18 7 41 8 17 7 17 7 18 7 17 7
  598. // Initial Space
  599. if (!MATCH_MARK(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
  600. return ERR;
  601. }
  602. offset++;
  603. while (offset + 1 < irparams.rawlen) {
  604. if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ONE_MARK)) {
  605. data = (data << 1) | 1;
  606. }
  607. else if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ZERO_MARK)) {
  608. data <<= 1;
  609. }
  610. else {
  611. // Serial.println("A"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
  612. return ERR;
  613. }
  614. offset++;
  615. if (!MATCH_SPACE(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
  616. // Serial.println("B"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
  617. break;
  618. }
  619. offset++;
  620. }
  621. // Success
  622. results->bits = (offset - 1) / 2;
  623. if (results->bits < MITSUBISHI_BITS) {
  624. results->bits = 0;
  625. return ERR;
  626. }
  627. results->value = data;
  628. results->decode_type = MITSUBISHI;
  629. return DECODED;
  630. }
  631. // Gets one undecoded level at a time from the raw buffer.
  632. // The RC5/6 decoding is easier if the data is broken into time intervals.
  633. // E.g. if the buffer has MARK for 2 time intervals and SPACE for 1,
  634. // successive calls to getRClevel will return MARK, MARK, SPACE.
  635. // offset and used are updated to keep track of the current position.
  636. // t1 is the time interval for a single bit in microseconds.
  637. // Returns -1 for error (measured time interval is not a multiple of t1).
  638. int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) {
  639. if (*offset >= results->rawlen) {
  640. // After end of recorded buffer, assume SPACE.
  641. return SPACE;
  642. }
  643. int width = results->rawbuf[*offset];
  644. int val = ((*offset) % 2) ? MARK : SPACE;
  645. int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS;
  646. int avail;
  647. if (MATCH(width, t1 + correction)) {
  648. avail = 1;
  649. }
  650. else if (MATCH(width, 2*t1 + correction)) {
  651. avail = 2;
  652. }
  653. else if (MATCH(width, 3*t1 + correction)) {
  654. avail = 3;
  655. }
  656. else {
  657. return -1;
  658. }
  659. (*used)++;
  660. if (*used >= avail) {
  661. *used = 0;
  662. (*offset)++;
  663. }
  664. #ifdef DEBUG
  665. if (val == MARK) {
  666. Serial.println("MARK");
  667. }
  668. else {
  669. Serial.println("SPACE");
  670. }
  671. #endif
  672. return val;
  673. }
  674. long IRrecv::decodeRC5(decode_results *results) {
  675. if (irparams.rawlen < MIN_RC5_SAMPLES + 2) {
  676. return ERR;
  677. }
  678. int offset = 1; // Skip gap space
  679. long data = 0;
  680. int used = 0;
  681. // Get start bits
  682. if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
  683. if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR;
  684. if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
  685. int nbits;
  686. for (nbits = 0; offset < irparams.rawlen; nbits++) {
  687. int levelA = getRClevel(results, &offset, &used, RC5_T1);
  688. int levelB = getRClevel(results, &offset, &used, RC5_T1);
  689. if (levelA == SPACE && levelB == MARK) {
  690. // 1 bit
  691. data = (data << 1) | 1;
  692. }
  693. else if (levelA == MARK && levelB == SPACE) {
  694. // zero bit
  695. data <<= 1;
  696. }
  697. else {
  698. return ERR;
  699. }
  700. }
  701. // Success
  702. results->bits = nbits;
  703. results->value = data;
  704. results->decode_type = RC5;
  705. return DECODED;
  706. }
  707. long IRrecv::decodeRC6(decode_results *results) {
  708. if (results->rawlen < MIN_RC6_SAMPLES) {
  709. return ERR;
  710. }
  711. int offset = 1; // Skip first space
  712. // Initial mark
  713. if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) {
  714. return ERR;
  715. }
  716. offset++;
  717. if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) {
  718. return ERR;
  719. }
  720. offset++;
  721. long data = 0;
  722. int used = 0;
  723. // Get start bit (1)
  724. if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR;
  725. if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR;
  726. int nbits;
  727. for (nbits = 0; offset < results->rawlen; nbits++) {
  728. int levelA, levelB; // Next two levels
  729. levelA = getRClevel(results, &offset, &used, RC6_T1);
  730. if (nbits == 3) {
  731. // T bit is double wide; make sure second half matches
  732. if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
  733. }
  734. levelB = getRClevel(results, &offset, &used, RC6_T1);
  735. if (nbits == 3) {
  736. // T bit is double wide; make sure second half matches
  737. if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
  738. }
  739. if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5
  740. // 1 bit
  741. data = (data << 1) | 1;
  742. }
  743. else if (levelA == SPACE && levelB == MARK) {
  744. // zero bit
  745. data <<= 1;
  746. }
  747. else {
  748. return ERR; // Error
  749. }
  750. }
  751. // Success
  752. results->bits = nbits;
  753. results->value = data;
  754. results->decode_type = RC6;
  755. return DECODED;
  756. }
  757. long IRrecv::decodePanasonic(decode_results *results) {
  758. unsigned long long data = 0;
  759. int offset = 1;
  760. if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_MARK)) {
  761. return ERR;
  762. }
  763. offset++;
  764. if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_SPACE)) {
  765. return ERR;
  766. }
  767. offset++;
  768. // decode address
  769. for (int i = 0; i < PANASONIC_BITS; i++) {
  770. if (!MATCH_MARK(results->rawbuf[offset++], PANASONIC_BIT_MARK)) {
  771. return ERR;
  772. }
  773. if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ONE_SPACE)) {
  774. data = (data << 1) | 1;
  775. } else if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ZERO_SPACE)) {
  776. data <<= 1;
  777. } else {
  778. return ERR;
  779. }
  780. offset++;
  781. }
  782. results->value = (unsigned long)data;
  783. results->panasonicAddress = (unsigned int)(data >> 32);
  784. results->decode_type = PANASONIC;
  785. results->bits = PANASONIC_BITS;
  786. return DECODED;
  787. }
  788. long IRrecv::decodeJVC(decode_results *results) {
  789. long data = 0;
  790. int offset = 1; // Skip first space
  791. // Check for repeat
  792. if (irparams.rawlen - 1 == 33 &&
  793. MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK) &&
  794. MATCH_MARK(results->rawbuf[irparams.rawlen-1], JVC_BIT_MARK)) {
  795. results->bits = 0;
  796. results->value = REPEAT;
  797. results->decode_type = JVC;
  798. return DECODED;
  799. }
  800. // Initial mark
  801. if (!MATCH_MARK(results->rawbuf[offset], JVC_HDR_MARK)) {
  802. return ERR;
  803. }
  804. offset++;
  805. if (irparams.rawlen < 2 * JVC_BITS + 1 ) {
  806. return ERR;
  807. }
  808. // Initial space
  809. if (!MATCH_SPACE(results->rawbuf[offset], JVC_HDR_SPACE)) {
  810. return ERR;
  811. }
  812. offset++;
  813. for (int i = 0; i < JVC_BITS; i++) {
  814. if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)) {
  815. return ERR;
  816. }
  817. offset++;
  818. if (MATCH_SPACE(results->rawbuf[offset], JVC_ONE_SPACE)) {
  819. data = (data << 1) | 1;
  820. }
  821. else if (MATCH_SPACE(results->rawbuf[offset], JVC_ZERO_SPACE)) {
  822. data <<= 1;
  823. }
  824. else {
  825. return ERR;
  826. }
  827. offset++;
  828. }
  829. //Stop bit
  830. if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)){
  831. return ERR;
  832. }
  833. // Success
  834. results->bits = JVC_BITS;
  835. results->value = data;
  836. results->decode_type = JVC;
  837. return DECODED;
  838. }
  839. /* -----------------------------------------------------------------------
  840. * hashdecode - decode an arbitrary IR code.
  841. * Instead of decoding using a standard encoding scheme
  842. * (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
  843. *
  844. * The algorithm: look at the sequence of MARK signals, and see if each one
  845. * is shorter (0), the same length (1), or longer (2) than the previous.
  846. * Do the same with the SPACE signals. Hszh the resulting sequence of 0's,
  847. * 1's, and 2's to a 32-bit value. This will give a unique value for each
  848. * different code (probably), for most code systems.
  849. *
  850. * http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
  851. */
  852. // Compare two tick values, returning 0 if newval is shorter,
  853. // 1 if newval is equal, and 2 if newval is longer
  854. // Use a tolerance of 20%
  855. int IRrecv::compare(unsigned int oldval, unsigned int newval) {
  856. if (newval < oldval * .8) {
  857. return 0;
  858. }
  859. else if (oldval < newval * .8) {
  860. return 2;
  861. }
  862. else {
  863. return 1;
  864. }
  865. }
  866. // Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
  867. #define FNV_PRIME_32 16777619
  868. #define FNV_BASIS_32 2166136261
  869. /* Converts the raw code values into a 32-bit hash code.
  870. * Hopefully this code is unique for each button.
  871. * This isn't a "real" decoding, just an arbitrary value.
  872. */
  873. long IRrecv::decodeHash(decode_results *results) {
  874. // Require at least 6 samples to prevent triggering on noise
  875. if (results->rawlen < 6) {
  876. return ERR;
  877. }
  878. long hash = FNV_BASIS_32;
  879. for (int i = 1; i+2 < results->rawlen; i++) {
  880. int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
  881. // Add value into the hash
  882. hash = (hash * FNV_PRIME_32) ^ value;
  883. }
  884. results->value = hash;
  885. results->bits = 32;
  886. results->decode_type = UNKNOWN;
  887. return DECODED;
  888. }
  889. /* Sharp and DISH support by Todd Treece ( http://unionbridge.org/design/ircommand )
  890. The Dish send function needs to be repeated 4 times, and the Sharp function
  891. has the necessary repeat built in because of the need to invert the signal.
  892. Sharp protocol documentation:
  893. http://www.sbprojects.com/knowledge/ir/sharp.htm
  894. Here are the LIRC files that I found that seem to match the remote codes
  895. from the oscilloscope:
  896. Sharp LCD TV:
  897. http://lirc.sourceforge.net/remotes/sharp/GA538WJSA
  898. DISH NETWORK (echostar 301):
  899. http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx
  900. For the DISH codes, only send the last for characters of the hex.
  901. i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the
  902. linked LIRC file.
  903. */
  904. void IRsend::sendSharp(unsigned long data, int nbits) {
  905. unsigned long invertdata = data ^ SHARP_TOGGLE_MASK;
  906. enableIROut(38);
  907. for (int i = 0; i < nbits; i++) {
  908. if (data & 0x4000) {
  909. mark(SHARP_BIT_MARK);
  910. space(SHARP_ONE_SPACE);
  911. }
  912. else {
  913. mark(SHARP_BIT_MARK);
  914. space(SHARP_ZERO_SPACE);
  915. }
  916. data <<= 1;
  917. }
  918. mark(SHARP_BIT_MARK);
  919. space(SHARP_ZERO_SPACE);
  920. delay(46);
  921. for (int i = 0; i < nbits; i++) {
  922. if (invertdata & 0x4000) {
  923. mark(SHARP_BIT_MARK);
  924. space(SHARP_ONE_SPACE);
  925. }
  926. else {
  927. mark(SHARP_BIT_MARK);
  928. space(SHARP_ZERO_SPACE);
  929. }
  930. invertdata <<= 1;
  931. }
  932. mark(SHARP_BIT_MARK);
  933. space(SHARP_ZERO_SPACE);
  934. delay(46);
  935. }
  936. void IRsend::sendDISH(unsigned long data, int nbits)
  937. {
  938. enableIROut(56);
  939. mark(DISH_HDR_MARK);
  940. space(DISH_HDR_SPACE);
  941. for (int i = 0; i < nbits; i++) {
  942. if (data & DISH_TOP_BIT) {
  943. mark(DISH_BIT_MARK);
  944. space(DISH_ONE_SPACE);
  945. }
  946. else {
  947. mark(DISH_BIT_MARK);
  948. space(DISH_ZERO_SPACE);
  949. }
  950. data <<= 1;
  951. }
  952. }