Advertisement
Fyrilin

IRremote_full.cpp

Oct 6th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.43 KB | None | 0 0
  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. * LG added by Darryl Smith (based on the JVC protocol)
  18. * Whynter A/C ARC-110WD added by Francesco Meschia
  19. */
  20.  
  21. #include "IRremote_full.h"
  22. #include "IRremoteInt_full.h"
  23.  
  24. // Provides ISR
  25. #include <avr/interrupt.h>
  26.  
  27. volatile irparams_t irparams;
  28.  
  29. // These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging.
  30. // To use them, set DEBUG in IRremoteInt.h
  31. // Normally macros are used for efficiency
  32. #ifdef DEBUG
  33. int MATCH(int measured, int desired) {
  34. Serial.print("Testing: ");
  35. Serial.print(TICKS_LOW(desired), DEC);
  36. Serial.print(" <= ");
  37. Serial.print(measured, DEC);
  38. Serial.print(" <= ");
  39. Serial.println(TICKS_HIGH(desired), DEC);
  40. return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);
  41. }
  42.  
  43. int MATCH_MARK(int measured_ticks, int desired_us) {
  44. Serial.print("Testing mark ");
  45. Serial.print(measured_ticks * USECPERTICK, DEC);
  46. Serial.print(" vs ");
  47. Serial.print(desired_us, DEC);
  48. Serial.print(": ");
  49. Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
  50. Serial.print(" <= ");
  51. Serial.print(measured_ticks, DEC);
  52. Serial.print(" <= ");
  53. Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
  54. return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS);
  55. }
  56.  
  57. int MATCH_SPACE(int measured_ticks, int desired_us) {
  58. Serial.print("Testing space ");
  59. Serial.print(measured_ticks * USECPERTICK, DEC);
  60. Serial.print(" vs ");
  61. Serial.print(desired_us, DEC);
  62. Serial.print(": ");
  63. Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
  64. Serial.print(" <= ");
  65. Serial.print(measured_ticks, DEC);
  66. Serial.print(" <= ");
  67. Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
  68. return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS);
  69. }
  70. #else
  71. int MATCH(int measured, int desired) {return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);}
  72. int MATCH_MARK(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us + MARK_EXCESS));}
  73. int MATCH_SPACE(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us - MARK_EXCESS));}
  74. // Debugging versions are in IRremote.cpp
  75. #endif
  76.  
  77. #ifdef NEC
  78. void IRsend::sendNEC(unsigned long data, int nbits)
  79. {
  80. enableIROut(38);
  81. mark(NEC_HDR_MARK);
  82. space(NEC_HDR_SPACE);
  83. for (int i = 0; i < nbits; i++) {
  84. if (data & TOPBIT) {
  85. mark(NEC_BIT_MARK);
  86. space(NEC_ONE_SPACE);
  87. }
  88. else {
  89. mark(NEC_BIT_MARK);
  90. space(NEC_ZERO_SPACE);
  91. }
  92. data <<= 1;
  93. }
  94. mark(NEC_BIT_MARK);
  95. space(0);
  96. }
  97. #endif
  98.  
  99. #ifdef CISCO
  100. void IRsend::sendCISCO(unsigned long data, int nbits)
  101. {
  102. enableIROut(38);
  103. mark(CISCO_HDR_MARK);
  104. space(CISCO_HDR_SPACE);
  105. for (int i = 0; i < nbits; i++) {
  106. if (data & TOPBIT) {
  107. mark(CISCO_BIT_MARK);
  108. space(CISCO_ONE_SPACE);
  109. }
  110. else {
  111. mark(CISCO_BIT_MARK);
  112. space(CISCO_ZERO_SPACE);
  113. }
  114. data <<= 1;
  115. }
  116. mark(CISCO_BIT_MARK);
  117.  
  118. // tail
  119. //space(CISCO_TLR_SEP);
  120. //mark(CISCO_HDR_MARK);
  121. //space(CISCO_TLR_SPACE);
  122. //mark(CISCO_TLR_MARK);
  123. space(0);
  124. }
  125. #endif
  126.  
  127. #ifdef WHYNTER
  128. void IRsend::sendWhynter(unsigned long data, int nbits) {
  129. enableIROut(38);
  130. mark(WHYNTER_ZERO_MARK);
  131. space(WHYNTER_ZERO_SPACE);
  132. mark(WHYNTER_HDR_MARK);
  133. space(WHYNTER_HDR_SPACE);
  134. for (int i = 0; i < nbits; i++) {
  135. if (data & TOPBIT) {
  136. mark(WHYNTER_ONE_MARK);
  137. space(WHYNTER_ONE_SPACE);
  138. }
  139. else {
  140. mark(WHYNTER_ZERO_MARK);
  141. space(WHYNTER_ZERO_SPACE);
  142. }
  143. data <<= 1;
  144. }
  145. mark(WHYNTER_ZERO_MARK);
  146. space(WHYNTER_ZERO_SPACE);
  147. }
  148. #endif
  149.  
  150. #ifdef SONY
  151. void IRsend::sendSony(unsigned long data, int nbits) {
  152. enableIROut(40);
  153. mark(SONY_HDR_MARK);
  154. space(SONY_HDR_SPACE);
  155. data = data << (32 - nbits);
  156. for (int i = 0; i < nbits; i++) {
  157. if (data & TOPBIT) {
  158. mark(SONY_ONE_MARK);
  159. space(SONY_HDR_SPACE);
  160. }
  161. else {
  162. mark(SONY_ZERO_MARK);
  163. space(SONY_HDR_SPACE);
  164. }
  165. data <<= 1;
  166. }
  167. }
  168. #endif
  169.  
  170. void IRsend::sendRaw(unsigned int buf[], int len, int hz)
  171. {
  172. enableIROut(hz);
  173. for (int i = 0; i < len; i++) {
  174. if (i & 1) {
  175. space(buf[i]);
  176. }
  177. else {
  178. mark(buf[i]);
  179. }
  180. }
  181. space(0); // Just to be sure
  182. }
  183.  
  184. // Note: first bit must be a one (start bit)
  185. void IRsend::sendRC5(unsigned long data, int nbits)
  186. {
  187. enableIROut(36);
  188. data = data << (32 - nbits);
  189. mark(RC5_T1); // First start bit
  190. space(RC5_T1); // Second start bit
  191. mark(RC5_T1); // Second start bit
  192. for (int i = 0; i < nbits; i++) {
  193. if (data & TOPBIT) {
  194. space(RC5_T1); // 1 is space, then mark
  195. mark(RC5_T1);
  196. }
  197. else {
  198. mark(RC5_T1);
  199. space(RC5_T1);
  200. }
  201. data <<= 1;
  202. }
  203. space(0); // Turn off at end
  204. }
  205.  
  206. // Caller needs to take care of flipping the toggle bit
  207. void IRsend::sendRC6(unsigned long data, int nbits)
  208. {
  209. enableIROut(36);
  210. data = data << (32 - nbits);
  211. mark(RC6_HDR_MARK);
  212. space(RC6_HDR_SPACE);
  213. mark(RC6_T1); // start bit
  214. space(RC6_T1);
  215. int t;
  216. for (int i = 0; i < nbits; i++) {
  217. if (i == 3) {
  218. // double-wide trailer bit
  219. t = 2 * RC6_T1;
  220. }
  221. else {
  222. t = RC6_T1;
  223. }
  224. if (data & TOPBIT) {
  225. mark(t);
  226. space(t);
  227. }
  228. else {
  229. space(t);
  230. mark(t);
  231. }
  232.  
  233. data <<= 1;
  234. }
  235. space(0); // Turn off at end
  236. }
  237.  
  238. #ifdef PANASONIC
  239. void IRsend::sendPanasonic(unsigned int address, unsigned long data) {
  240. enableIROut(35);
  241. mark(PANASONIC_HDR_MARK);
  242. space(PANASONIC_HDR_SPACE);
  243.  
  244. for(int i=0;i<16;i++)
  245. {
  246. mark(PANASONIC_BIT_MARK);
  247. if (address & 0x8000) {
  248. space(PANASONIC_ONE_SPACE);
  249. } else {
  250. space(PANASONIC_ZERO_SPACE);
  251. }
  252. address <<= 1;
  253. }
  254. for (int i=0; i < 32; i++) {
  255. mark(PANASONIC_BIT_MARK);
  256. if (data & TOPBIT) {
  257. space(PANASONIC_ONE_SPACE);
  258. } else {
  259. space(PANASONIC_ZERO_SPACE);
  260. }
  261. data <<= 1;
  262. }
  263. mark(PANASONIC_BIT_MARK);
  264. space(0);
  265. }
  266. #endif
  267.  
  268. #ifdef JVC
  269. void IRsend::sendJVC(unsigned long data, int nbits, int repeat)
  270. {
  271. enableIROut(38);
  272. data = data << (32 - nbits);
  273. if (!repeat){
  274. mark(JVC_HDR_MARK);
  275. space(JVC_HDR_SPACE);
  276. }
  277. for (int i = 0; i < nbits; i++) {
  278. if (data & TOPBIT) {
  279. mark(JVC_BIT_MARK);
  280. space(JVC_ONE_SPACE);
  281. }
  282. else {
  283. mark(JVC_BIT_MARK);
  284. space(JVC_ZERO_SPACE);
  285. }
  286. data <<= 1;
  287. }
  288. mark(JVC_BIT_MARK);
  289. space(0);
  290. }
  291. #endif
  292.  
  293. #ifdef SAMSUNG
  294. void IRsend::sendSAMSUNG(unsigned long data, int nbits)
  295. {
  296. enableIROut(38);
  297. mark(SAMSUNG_HDR_MARK);
  298. space(SAMSUNG_HDR_SPACE);
  299. for (int i = 0; i < nbits; i++) {
  300. if (data & TOPBIT) {
  301. mark(SAMSUNG_BIT_MARK);
  302. space(SAMSUNG_ONE_SPACE);
  303. }
  304. else {
  305. mark(SAMSUNG_BIT_MARK);
  306. space(SAMSUNG_ZERO_SPACE);
  307. }
  308. data <<= 1;
  309. }
  310. mark(SAMSUNG_BIT_MARK);
  311. space(0);
  312. }
  313. #endif
  314.  
  315. void IRsend::mark(int time) {
  316. // Sends an IR mark for the specified number of microseconds.
  317. // The mark output is modulated at the PWM frequency.
  318. TIMER_ENABLE_PWM; // Enable pin 3 PWM output
  319. if (time > 0) delayMicroseconds(time);
  320. }
  321.  
  322. /* Leave pin off for time (given in microseconds) */
  323. void IRsend::space(int time) {
  324. // Sends an IR space for the specified number of microseconds.
  325. // A space is no output, so the PWM output is disabled.
  326. TIMER_DISABLE_PWM; // Disable pin 3 PWM output
  327. if (time > 0) delayMicroseconds(time);
  328. }
  329.  
  330. void IRsend::enableIROut(int khz) {
  331. // Enables IR output. The khz value controls the modulation frequency in kilohertz.
  332. // The IR output will be on pin 3 (OC2B).
  333. // This routine is designed for 36-40KHz; if you use it for other values, it's up to you
  334. // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
  335. // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
  336. // controlling the duty cycle.
  337. // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
  338. // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
  339. // A few hours staring at the ATmega documentation and this will all make sense.
  340. // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
  341.  
  342.  
  343. // Disable the Timer2 Interrupt (which is used for receiving IR)
  344. TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt
  345.  
  346. pinMode(TIMER_PWM_PIN, OUTPUT);
  347. digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low
  348.  
  349. // COM2A = 00: disconnect OC2A
  350. // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
  351. // WGM2 = 101: phase-correct PWM with OCRA as top
  352. // CS2 = 000: no prescaling
  353. // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
  354. TIMER_CONFIG_KHZ(khz);
  355. }
  356.  
  357. IRrecv::IRrecv(int recvpin)
  358. {
  359. irparams.recvpin = recvpin;
  360. irparams.blinkflag = 0;
  361. }
  362.  
  363. // initialization
  364. void IRrecv::enableIRIn() {
  365. cli();
  366. // setup pulse clock timer interrupt
  367. //Prescale /8 (16M/8 = 0.5 microseconds per tick)
  368. // Therefore, the timer interval can range from 0.5 to 128 microseconds
  369. // depending on the reset value (255 to 0)
  370. TIMER_CONFIG_NORMAL();
  371.  
  372. //Timer2 Overflow Interrupt Enable
  373. TIMER_ENABLE_INTR;
  374.  
  375. TIMER_RESET;
  376.  
  377. sei(); // enable interrupts
  378.  
  379. // initialize state machine variables
  380. irparams.rcvstate = STATE_IDLE;
  381. irparams.rawlen = 0;
  382.  
  383. // set pin modes
  384. pinMode(irparams.recvpin, INPUT);
  385. }
  386.  
  387. // enable/disable blinking of pin 13 on IR processing
  388. void IRrecv::blink13(int blinkflag)
  389. {
  390. irparams.blinkflag = blinkflag;
  391. if (blinkflag)
  392. pinMode(BLINKLED, OUTPUT);
  393. }
  394.  
  395. // TIMER2 interrupt code to collect raw data.
  396. // Widths of alternating SPACE, MARK are recorded in rawbuf.
  397. // Recorded in ticks of 50 microseconds.
  398. // rawlen counts the number of entries recorded so far.
  399. // First entry is the SPACE between transmissions.
  400. // As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
  401. // As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
  402. ISR(TIMER_INTR_NAME)
  403. {
  404. TIMER_RESET;
  405.  
  406. uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
  407.  
  408. irparams.timer++; // One more 50us tick
  409. if (irparams.rawlen >= RAWBUF) {
  410. // Buffer overflow
  411. irparams.rcvstate = STATE_STOP;
  412. }
  413. switch(irparams.rcvstate) {
  414. case STATE_IDLE: // In the middle of a gap
  415. if (irdata == MARK) {
  416. if (irparams.timer < GAP_TICKS) {
  417. // Not big enough to be a gap.
  418. irparams.timer = 0;
  419. }
  420. else {
  421. // gap just ended, record duration and start recording transmission
  422. irparams.rawlen = 0;
  423. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  424. irparams.timer = 0;
  425. irparams.rcvstate = STATE_MARK;
  426. }
  427. }
  428. break;
  429. case STATE_MARK: // timing MARK
  430. if (irdata == SPACE) { // MARK ended, record time
  431. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  432. irparams.timer = 0;
  433. irparams.rcvstate = STATE_SPACE;
  434. }
  435. break;
  436. case STATE_SPACE: // timing SPACE
  437. if (irdata == MARK) { // SPACE just ended, record it
  438. irparams.rawbuf[irparams.rawlen++] = irparams.timer;
  439. irparams.timer = 0;
  440. irparams.rcvstate = STATE_MARK;
  441. }
  442. else { // SPACE
  443. if (irparams.timer > GAP_TICKS) {
  444. // big SPACE, indicates gap between codes
  445. // Mark current code as ready for processing
  446. // Switch to STOP
  447. // Don't reset timer; keep counting space width
  448. irparams.rcvstate = STATE_STOP;
  449. }
  450. }
  451. break;
  452. case STATE_STOP: // waiting, measuring gap
  453. if (irdata == MARK) { // reset gap timer
  454. irparams.timer = 0;
  455. }
  456. break;
  457. }
  458.  
  459. if (irparams.blinkflag) {
  460. if (irdata == MARK) {
  461. BLINKLED_ON(); // turn pin 13 LED on
  462. }
  463. else {
  464. BLINKLED_OFF(); // turn pin 13 LED off
  465. }
  466. }
  467. }
  468.  
  469. void IRrecv::resume() {
  470. irparams.rcvstate = STATE_IDLE;
  471. irparams.rawlen = 0;
  472. }
  473.  
  474.  
  475.  
  476. // Decodes the received IR message
  477. // Returns 0 if no data ready, 1 if data ready.
  478. // Results of decoding are stored in results
  479. int IRrecv::decode(decode_results *results) {
  480. results->rawbuf = irparams.rawbuf;
  481. results->rawlen = irparams.rawlen;
  482. if (irparams.rcvstate != STATE_STOP) {
  483. return ERR;
  484. }
  485. #ifdef NEC
  486. #ifdef DEBUG
  487. Serial.println("Attempting NEC decode");
  488. #endif
  489. if (decodeNEC(results)) {
  490. return DECODED;
  491. }
  492. #endif
  493.  
  494. #ifdef CISCO
  495. #ifdef DEBUG
  496. Serial.println("Attempting CISCO decode");
  497. #endif
  498. if (decodeCISCO(results)) {
  499. return DECODED;
  500. }
  501. #endif
  502.  
  503. #ifdef SONY
  504. #ifdef DEBUG
  505. Serial.println("Attempting Sony decode");
  506. #endif
  507. if (decodeSony(results)) {
  508. return DECODED;
  509. }
  510. #endif
  511.  
  512. #ifdef SANYO
  513. #ifdef DEBUG
  514. Serial.println("Attempting Sanyo decode");
  515. #endif
  516. if (decodeSanyo(results)) {
  517. return DECODED;
  518. }
  519. #endif
  520.  
  521. #ifdef MITSUBISHI
  522. #ifdef DEBUG
  523. Serial.println("Attempting Mitsubishi decode");
  524. #endif
  525. if (decodeMitsubishi(results)) {
  526. return DECODED;
  527. }
  528. #endif
  529.  
  530. #ifdef RC5
  531. #ifdef DEBUG
  532. Serial.println("Attempting RC5 decode");
  533. #endif
  534. if (decodeRC5(results)) {
  535. return DECODED;
  536. }
  537. #endif
  538.  
  539. #ifdef RC6
  540. #ifdef DEBUG
  541. Serial.println("Attempting RC6 decode");
  542. #endif
  543. if (decodeRC6(results)) {
  544. return DECODED;
  545. }
  546. #endif
  547.  
  548. #ifdef PANASONIC
  549. #ifdef DEBUG
  550. Serial.println("Attempting Panasonic decode");
  551. #endif
  552. if (decodePanasonic(results)) {
  553. return DECODED;
  554. }
  555. #endif
  556.  
  557. #ifdef JVC
  558. #ifdef DEBUG
  559. Serial.println("Attempting LG decode");
  560. #endif
  561. if (decodeLG(results)) {
  562. return DECODED;
  563. }
  564. #ifdef DEBUG
  565. Serial.println("Attempting JVC decode");
  566. #endif
  567. if (decodeJVC(results)) {
  568. return DECODED;
  569. }
  570. #endif
  571.  
  572. #ifdef SAMSUNG
  573. #ifdef DEBUG
  574. Serial.println("Attempting SAMSUNG decode");
  575. #endif
  576. if (decodeSAMSUNG(results)) {
  577. return DECODED;
  578. }
  579. #ifdef DEBUG
  580. Serial.println("Attempting Whynter decode");
  581. #endif
  582. if (decodeWhynter(results)) {
  583. return DECODED;
  584. }
  585. // Aiwa RC-T501
  586. #ifdef AIWA_RC_T501
  587. #ifdef DEBUG
  588. Serial.println("Attempting Aiwa RC-T501 decode");
  589. #endif
  590. if (decodeAiwaRCT501(results)) {
  591. return DECODED;
  592. }
  593. #endif
  594.  
  595. // decodeHash returns a hash on any input.
  596. // Thus, it needs to be last in the list.
  597. // If you add any decodes, add them before this.
  598. if (decodeHash(results)) {
  599. return DECODED;
  600. }
  601. // Throw away and start over
  602. resume();
  603. return ERR;
  604. }
  605.  
  606. #ifdef NEC
  607. // NECs have a repeat only 4 items long
  608. long IRrecv::decodeNEC(decode_results *results) {
  609. long data = 0;
  610. int offset = 1; // Skip first space
  611. // Initial mark
  612. if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) {
  613. return ERR;
  614. }
  615. offset++;
  616. // Check for repeat
  617. if (irparams.rawlen == 4 &&
  618. MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) &&
  619. MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) {
  620. results->bits = 0;
  621. results->value = REPEAT;
  622. results->decode_type = NEC;
  623. return DECODED;
  624. }
  625. if (irparams.rawlen < 2 * NEC_BITS + 4) {
  626. return ERR;
  627. }
  628. // Initial space
  629. if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) {
  630. return ERR;
  631. }
  632. offset++;
  633. for (int i = 0; i < NEC_BITS; i++) {
  634. if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) {
  635. return ERR;
  636. }
  637. offset++;
  638. if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) {
  639. data = (data << 1) | 1;
  640. }
  641. else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) {
  642. data <<= 1;
  643. }
  644. else {
  645. return ERR;
  646. }
  647. offset++;
  648. }
  649. // Success
  650. results->bits = NEC_BITS;
  651. results->value = data;
  652. results->decode_type = NEC;
  653. return DECODED;
  654. }
  655. #endif
  656.  
  657. #ifdef CISCO
  658. // CISCOs have a repeat only 4 items long
  659. long IRrecv::decodeCISCO(decode_results *results) {
  660. long data = 0;
  661. int offset = 1; // Skip first space
  662. // Initial mark
  663. if (!MATCH_MARK(results->rawbuf[offset], CISCO_HDR_MARK)) {
  664. return ERR;
  665. }
  666. offset++;
  667. // Check for repeat
  668.  
  669. if (irparams.rawlen == 4 &&
  670. MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) &&
  671. MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) { // cisco uses NEC repeats
  672. results->bits = 0;
  673. results->value = REPEAT;
  674. results->decode_type = CISCO;
  675. return DECODED;
  676. }
  677. if (irparams.rawlen < 2 * CISCO_BITS + 4) {
  678. return ERR;
  679. }
  680. // Initial space
  681. if (!MATCH_SPACE(results->rawbuf[offset], CISCO_HDR_SPACE)) {
  682. return ERR;
  683. }
  684. offset++;
  685. for (int i = 0; i < CISCO_BITS; i++) {
  686. if (!MATCH_MARK(results->rawbuf[offset], CISCO_BIT_MARK)) {
  687. return ERR;
  688. }
  689. offset++;
  690. if (MATCH_SPACE(results->rawbuf[offset], CISCO_ONE_SPACE)) {
  691. data = (data << 1) | 1;
  692. }
  693. else if (MATCH_SPACE(results->rawbuf[offset], CISCO_ZERO_SPACE)) {
  694. data <<= 1;
  695. }
  696. else {
  697. return ERR;
  698. }
  699. offset++;
  700. }
  701. // Success
  702. results->bits = CISCO_BITS;
  703. results->value = data;
  704. results->decode_type = CISCO;
  705. return DECODED;
  706. }
  707. #endif
  708.  
  709. #ifdef SONY
  710. long IRrecv::decodeSony(decode_results *results) {
  711. long data = 0;
  712. if (irparams.rawlen < 2 * SONY_BITS + 2) {
  713. return ERR;
  714. }
  715. int offset = 0; // Dont skip first space, check its size
  716.  
  717. // Some Sony's deliver repeats fast after first
  718. // unfortunately can't spot difference from of repeat from two fast clicks
  719. if (results->rawbuf[offset] < SONY_DOUBLE_SPACE_USECS) {
  720. // Serial.print("IR Gap found: ");
  721. results->bits = 0;
  722. results->value = REPEAT;
  723. #ifdef SANYO
  724. results->decode_type = SANYO;
  725. #else
  726. results->decode_type = UNKNOWN;
  727. #endif
  728. return DECODED;
  729. }
  730. offset++;
  731.  
  732. // Initial mark
  733. if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) {
  734. return ERR;
  735. }
  736. offset++;
  737.  
  738. while (offset + 1 < irparams.rawlen) {
  739. if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) {
  740. break;
  741. }
  742. offset++;
  743. if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) {
  744. data = (data << 1) | 1;
  745. }
  746. else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) {
  747. data <<= 1;
  748. }
  749. else {
  750. return ERR;
  751. }
  752. offset++;
  753. }
  754.  
  755. // Success
  756. results->bits = (offset - 1) / 2;
  757. if (results->bits < 12) {
  758. results->bits = 0;
  759. return ERR;
  760. }
  761. results->value = data;
  762. results->decode_type = SONY;
  763. return DECODED;
  764. }
  765. #endif
  766.  
  767. long IRrecv::decodeWhynter(decode_results *results) {
  768. long data = 0;
  769.  
  770. if (irparams.rawlen < 2 * WHYNTER_BITS + 6) {
  771. return ERR;
  772. }
  773.  
  774. int offset = 1; // skip initial space
  775.  
  776. // sequence begins with a bit mark and a zero space
  777. if (!MATCH_MARK(results->rawbuf[offset], WHYNTER_BIT_MARK)) {
  778. return ERR;
  779. }
  780. offset++;
  781. if (!MATCH_SPACE(results->rawbuf[offset], WHYNTER_ZERO_SPACE)) {
  782. return ERR;
  783. }
  784. offset++;
  785.  
  786. // header mark and space
  787. if (!MATCH_MARK(results->rawbuf[offset], WHYNTER_HDR_MARK)) {
  788. return ERR;
  789. }
  790. offset++;
  791. if (!MATCH_SPACE(results->rawbuf[offset], WHYNTER_HDR_SPACE)) {
  792. return ERR;
  793. }
  794. offset++;
  795.  
  796. // data bits
  797. for (int i = 0; i < WHYNTER_BITS; i++) {
  798. if (!MATCH_MARK(results->rawbuf[offset], WHYNTER_BIT_MARK)) {
  799. return ERR;
  800. }
  801. offset++;
  802. if (MATCH_SPACE(results->rawbuf[offset], WHYNTER_ONE_SPACE)) {
  803. data = (data << 1) | 1;
  804. }
  805. else if (MATCH_SPACE(results->rawbuf[offset],WHYNTER_ZERO_SPACE)) {
  806. data <<= 1;
  807. }
  808. else {
  809. return ERR;
  810. }
  811. offset++;
  812. }
  813.  
  814. // trailing mark
  815. if (!MATCH_MARK(results->rawbuf[offset], WHYNTER_BIT_MARK)) {
  816. return ERR;
  817. }
  818. // Success
  819. results->bits = WHYNTER_BITS;
  820. results->value = data;
  821. results->decode_type = WHYNTER;
  822. return DECODED;
  823. }
  824.  
  825.  
  826. #ifdef SANYO
  827. // I think this is a Sanyo decoder - serial = SA 8650B
  828. // Looks like Sony except for timings, 48 chars of data and time/space different
  829. long IRrecv::decodeSanyo(decode_results *results) {
  830. long data = 0;
  831. if (irparams.rawlen < 2 * SANYO_BITS + 2) {
  832. return ERR;
  833. }
  834. int offset = 0; // Skip first space
  835. // Initial space
  836. /* 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
  837. Serial.print("IR Gap: ");
  838. Serial.println( results->rawbuf[offset]);
  839. Serial.println( "test against:");
  840. Serial.println(results->rawbuf[offset]);
  841. */
  842. if (results->rawbuf[offset] < SANYO_DOUBLE_SPACE_USECS) {
  843. // Serial.print("IR Gap found: ");
  844. results->bits = 0;
  845. results->value = REPEAT;
  846. results->decode_type = SANYO;
  847. return DECODED;
  848. }
  849. offset++;
  850.  
  851. // Initial mark
  852. if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
  853. return ERR;
  854. }
  855. offset++;
  856.  
  857. // Skip Second Mark
  858. if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
  859. return ERR;
  860. }
  861. offset++;
  862.  
  863. while (offset + 1 < irparams.rawlen) {
  864. if (!MATCH_SPACE(results->rawbuf[offset], SANYO_HDR_SPACE)) {
  865. break;
  866. }
  867. offset++;
  868. if (MATCH_MARK(results->rawbuf[offset], SANYO_ONE_MARK)) {
  869. data = (data << 1) | 1;
  870. }
  871. else if (MATCH_MARK(results->rawbuf[offset], SANYO_ZERO_MARK)) {
  872. data <<= 1;
  873. }
  874. else {
  875. return ERR;
  876. }
  877. offset++;
  878. }
  879.  
  880. // Success
  881. results->bits = (offset - 1) / 2;
  882. if (results->bits < 12) {
  883. results->bits = 0;
  884. return ERR;
  885. }
  886. results->value = data;
  887. results->decode_type = SANYO;
  888. return DECODED;
  889. }
  890. #endif
  891.  
  892. #ifdef MITSUBISHI
  893. // Looks like Sony except for timings, 48 chars of data and time/space different
  894. long IRrecv::decodeMitsubishi(decode_results *results) {
  895. // Serial.print("?!? decoding Mitsubishi:");Serial.print(irparams.rawlen); Serial.print(" want "); Serial.println( 2 * MITSUBISHI_BITS + 2);
  896. long data = 0;
  897. if (irparams.rawlen < 2 * MITSUBISHI_BITS + 2) {
  898. return ERR;
  899. }
  900. int offset = 0; // Skip first space
  901. // Initial space
  902. /* 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
  903. Serial.print("IR Gap: ");
  904. Serial.println( results->rawbuf[offset]);
  905. Serial.println( "test against:");
  906. Serial.println(results->rawbuf[offset]);
  907. */
  908. /* Not seeing double keys from Mitsubishi
  909. if (results->rawbuf[offset] < MITSUBISHI_DOUBLE_SPACE_USECS) {
  910. // Serial.print("IR Gap found: ");
  911. results->bits = 0;
  912. results->value = REPEAT;
  913. results->decode_type = MITSUBISHI;
  914. return DECODED;
  915. }
  916. */
  917. offset++;
  918.  
  919. // Typical
  920. // 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
  921.  
  922. // Initial Space
  923. if (!MATCH_MARK(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
  924. return ERR;
  925. }
  926. offset++;
  927. while (offset + 1 < irparams.rawlen) {
  928. if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ONE_MARK)) {
  929. data = (data << 1) | 1;
  930. }
  931. else if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ZERO_MARK)) {
  932. data <<= 1;
  933. }
  934. else {
  935. // Serial.println("A"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
  936. return ERR;
  937. }
  938. offset++;
  939. if (!MATCH_SPACE(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
  940. // Serial.println("B"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
  941. break;
  942. }
  943. offset++;
  944. }
  945.  
  946. // Success
  947. results->bits = (offset - 1) / 2;
  948. if (results->bits < MITSUBISHI_BITS) {
  949. results->bits = 0;
  950. return ERR;
  951. }
  952. results->value = data;
  953. results->decode_type = MITSUBISHI;
  954. return DECODED;
  955. }
  956. #endif
  957.  
  958. // Gets one undecoded level at a time from the raw buffer.
  959. // The RC5/6 decoding is easier if the data is broken into time intervals.
  960. // E.g. if the buffer has MARK for 2 time intervals and SPACE for 1,
  961. // successive calls to getRClevel will return MARK, MARK, SPACE.
  962. // offset and used are updated to keep track of the current position.
  963. // t1 is the time interval for a single bit in microseconds.
  964. // Returns -1 for error (measured time interval is not a multiple of t1).
  965. int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) {
  966. if (*offset >= results->rawlen) {
  967. // After end of recorded buffer, assume SPACE.
  968. return SPACE;
  969. }
  970. int width = results->rawbuf[*offset];
  971. int val = ((*offset) % 2) ? MARK : SPACE;
  972. int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS;
  973.  
  974. int avail;
  975. if (MATCH(width, t1 + correction)) {
  976. avail = 1;
  977. }
  978. else if (MATCH(width, 2*t1 + correction)) {
  979. avail = 2;
  980. }
  981. else if (MATCH(width, 3*t1 + correction)) {
  982. avail = 3;
  983. }
  984. else {
  985. return -1;
  986. }
  987.  
  988. (*used)++;
  989. if (*used >= avail) {
  990. *used = 0;
  991. (*offset)++;
  992. }
  993. #ifdef DEBUG
  994. if (val == MARK) {
  995. Serial.println("MARK");
  996. }
  997. else {
  998. Serial.println("SPACE");
  999. }
  1000. #endif
  1001. return val;
  1002. }
  1003. #endif
  1004.  
  1005. long IRrecv::decodeRC5(decode_results *results) {
  1006. if (irparams.rawlen < MIN_RC5_SAMPLES + 2) {
  1007. return ERR;
  1008. }
  1009. int offset = 1; // Skip gap space
  1010. long data = 0;
  1011. int used = 0;
  1012. // Get start bits
  1013. if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
  1014. if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR;
  1015. if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
  1016. int nbits;
  1017. for (nbits = 0; offset < irparams.rawlen; nbits++) {
  1018. int levelA = getRClevel(results, &offset, &used, RC5_T1);
  1019. int levelB = getRClevel(results, &offset, &used, RC5_T1);
  1020. if (levelA == SPACE && levelB == MARK) {
  1021. // 1 bit
  1022. data = (data << 1) | 1;
  1023. }
  1024. else if (levelA == MARK && levelB == SPACE) {
  1025. // zero bit
  1026. data <<= 1;
  1027. }
  1028. else {
  1029. return ERR;
  1030. }
  1031. }
  1032.  
  1033. // Success
  1034. results->bits = nbits;
  1035. results->value = data;
  1036. results->decode_type = RC5;
  1037. return DECODED;
  1038. }
  1039.  
  1040. long IRrecv::decodeRC6(decode_results *results) {
  1041. if (results->rawlen < MIN_RC6_SAMPLES) {
  1042. return ERR;
  1043. }
  1044. int offset = 1; // Skip first space
  1045. // Initial mark
  1046. if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) {
  1047. return ERR;
  1048. }
  1049. offset++;
  1050. if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) {
  1051. return ERR;
  1052. }
  1053. offset++;
  1054. long data = 0;
  1055. int used = 0;
  1056. // Get start bit (1)
  1057. if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR;
  1058. if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR;
  1059. int nbits;
  1060. for (nbits = 0; offset < results->rawlen; nbits++) {
  1061. int levelA, levelB; // Next two levels
  1062. levelA = getRClevel(results, &offset, &used, RC6_T1);
  1063. if (nbits == 3) {
  1064. // T bit is double wide; make sure second half matches
  1065. if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
  1066. }
  1067. levelB = getRClevel(results, &offset, &used, RC6_T1);
  1068. if (nbits == 3) {
  1069. // T bit is double wide; make sure second half matches
  1070. if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
  1071. }
  1072. if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5
  1073. // 1 bit
  1074. data = (data << 1) | 1;
  1075. }
  1076. else if (levelA == SPACE && levelB == MARK) {
  1077. // zero bit
  1078. data <<= 1;
  1079. }
  1080. else {
  1081. return ERR; // Error
  1082. }
  1083. }
  1084. // Success
  1085. results->bits = nbits;
  1086. results->value = data;
  1087. results->decode_type = RC6;
  1088. return DECODED;
  1089. }
  1090.  
  1091. #ifdef PANASONIC
  1092. long IRrecv::decodePanasonic(decode_results *results) {
  1093. unsigned long long data = 0;
  1094. int offset = 1;
  1095.  
  1096. if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_MARK)) {
  1097. return ERR;
  1098. }
  1099. offset++;
  1100. if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_SPACE)) {
  1101. return ERR;
  1102. }
  1103. offset++;
  1104.  
  1105. // decode address
  1106. for (int i = 0; i < PANASONIC_BITS; i++) {
  1107. if (!MATCH_MARK(results->rawbuf[offset++], PANASONIC_BIT_MARK)) {
  1108. return ERR;
  1109. }
  1110. if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ONE_SPACE)) {
  1111. data = (data << 1) | 1;
  1112. } else if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ZERO_SPACE)) {
  1113. data <<= 1;
  1114. } else {
  1115. return ERR;
  1116. }
  1117. offset++;
  1118. }
  1119. results->value = (unsigned long)data;
  1120. results->panasonicAddress = (unsigned int)(data >> 32);
  1121. results->decode_type = PANASONIC;
  1122. results->bits = PANASONIC_BITS;
  1123. return DECODED;
  1124. }
  1125. #endif
  1126.  
  1127. #ifdef LG
  1128. long IRrecv::decodeLG(decode_results *results) {
  1129. long data = 0;
  1130. int offset = 1; // Skip first space
  1131.  
  1132. // Initial mark
  1133. if (!MATCH_MARK(results->rawbuf[offset], LG_HDR_MARK)) {
  1134. return ERR;
  1135. }
  1136. offset++;
  1137. if (irparams.rawlen < 2 * LG_BITS + 1 ) {
  1138. return ERR;
  1139. }
  1140. // Initial space
  1141. if (!MATCH_SPACE(results->rawbuf[offset], LG_HDR_SPACE)) {
  1142. return ERR;
  1143. }
  1144. offset++;
  1145. for (int i = 0; i < LG_BITS; i++) {
  1146. if (!MATCH_MARK(results->rawbuf[offset], LG_BIT_MARK)) {
  1147. return ERR;
  1148. }
  1149. offset++;
  1150. if (MATCH_SPACE(results->rawbuf[offset], LG_ONE_SPACE)) {
  1151. data = (data << 1) | 1;
  1152. }
  1153. else if (MATCH_SPACE(results->rawbuf[offset], LG_ZERO_SPACE)) {
  1154. data <<= 1;
  1155. }
  1156. else {
  1157. return ERR;
  1158. }
  1159. offset++;
  1160. }
  1161. //Stop bit
  1162. if (!MATCH_MARK(results->rawbuf[offset], LG_BIT_MARK)){
  1163. return ERR;
  1164. }
  1165. // Success
  1166. results->bits = LG_BITS;
  1167. results->value = data;
  1168. results->decode_type = LG;
  1169. return DECODED;
  1170. }
  1171.  
  1172.  
  1173. #endif
  1174.  
  1175. #ifdef JVC
  1176. long IRrecv::decodeJVC(decode_results *results) {
  1177. long data = 0;
  1178. int offset = 1; // Skip first space
  1179. // Check for repeat
  1180. if (irparams.rawlen - 1 == 33 &&
  1181. MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK) &&
  1182. MATCH_MARK(results->rawbuf[irparams.rawlen-1], JVC_BIT_MARK)) {
  1183. results->bits = 0;
  1184. results->value = REPEAT;
  1185. results->decode_type = JVC;
  1186. return DECODED;
  1187. }
  1188. // Initial mark
  1189. if (!MATCH_MARK(results->rawbuf[offset], JVC_HDR_MARK)) {
  1190. return ERR;
  1191. }
  1192. offset++;
  1193. if (irparams.rawlen < 2 * JVC_BITS + 1 ) {
  1194. return ERR;
  1195. }
  1196. // Initial space
  1197. if (!MATCH_SPACE(results->rawbuf[offset], JVC_HDR_SPACE)) {
  1198. return ERR;
  1199. }
  1200. offset++;
  1201. for (int i = 0; i < JVC_BITS; i++) {
  1202. if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)) {
  1203. return ERR;
  1204. }
  1205. offset++;
  1206. if (MATCH_SPACE(results->rawbuf[offset], JVC_ONE_SPACE)) {
  1207. data = (data << 1) | 1;
  1208. }
  1209. else if (MATCH_SPACE(results->rawbuf[offset], JVC_ZERO_SPACE)) {
  1210. data <<= 1;
  1211. }
  1212. else {
  1213. return ERR;
  1214. }
  1215. offset++;
  1216. }
  1217. //Stop bit
  1218. if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)){
  1219. return ERR;
  1220. }
  1221. // Success
  1222. results->bits = JVC_BITS;
  1223. results->value = data;
  1224. results->decode_type = JVC;
  1225. return DECODED;
  1226. }
  1227. #endif
  1228.  
  1229. #ifdef SAMSUNG
  1230. // SAMSUNGs have a repeat only 4 items long
  1231. long IRrecv::decodeSAMSUNG(decode_results *results) {
  1232. long data = 0;
  1233. int offset = 1; // Skip first space
  1234. // Initial mark
  1235. if (!MATCH_MARK(results->rawbuf[offset], SAMSUNG_HDR_MARK)) {
  1236. return ERR;
  1237. }
  1238. offset++;
  1239. // Check for repeat
  1240. if (irparams.rawlen == 4 &&
  1241. MATCH_SPACE(results->rawbuf[offset], SAMSUNG_RPT_SPACE) &&
  1242. MATCH_MARK(results->rawbuf[offset+1], SAMSUNG_BIT_MARK)) {
  1243. results->bits = 0;
  1244. results->value = REPEAT;
  1245. results->decode_type = SAMSUNG;
  1246. return DECODED;
  1247. }
  1248. if (irparams.rawlen < 2 * SAMSUNG_BITS + 4) {
  1249. return ERR;
  1250. }
  1251. // Initial space
  1252. if (!MATCH_SPACE(results->rawbuf[offset], SAMSUNG_HDR_SPACE)) {
  1253. return ERR;
  1254. }
  1255. offset++;
  1256. for (int i = 0; i < SAMSUNG_BITS; i++) {
  1257. if (!MATCH_MARK(results->rawbuf[offset], SAMSUNG_BIT_MARK)) {
  1258. return ERR;
  1259. }
  1260. offset++;
  1261. if (MATCH_SPACE(results->rawbuf[offset], SAMSUNG_ONE_SPACE)) {
  1262. data = (data << 1) | 1;
  1263. }
  1264. else if (MATCH_SPACE(results->rawbuf[offset], SAMSUNG_ZERO_SPACE)) {
  1265. data <<= 1;
  1266. }
  1267. else {
  1268. return ERR;
  1269. }
  1270. offset++;
  1271. }
  1272. // Success
  1273. results->bits = SAMSUNG_BITS;
  1274. results->value = data;
  1275. results->decode_type = SAMSUNG;
  1276. return DECODED;
  1277. }
  1278. #endif
  1279.  
  1280. /**
  1281. * Aiwa system
  1282. * Remote control RC-T501
  1283. * Lirc file http://lirc.sourceforge.net/remotes/aiwa/RC-T501
  1284. *
  1285. */
  1286. #ifdef AIWA_RC_T501
  1287. long IRrecv::decodeAiwaRCT501(decode_results *results) {
  1288. int data = 0;
  1289. int offset = 1; // skip first garbage read
  1290.  
  1291. // Check SIZE
  1292. if(irparams.rawlen < 2 * (AIWA_RC_T501_SUM_BITS) + 4) {
  1293. return ERR;
  1294. }
  1295.  
  1296. // Check HDR
  1297. if(!MATCH_MARK(results->rawbuf[offset], AIWA_RC_T501_HDR_MARK)) {
  1298. return ERR;
  1299. }
  1300. offset++;
  1301.  
  1302. // Check HDR space
  1303. if(!MATCH_SPACE(results->rawbuf[offset], AIWA_RC_T501_HDR_SPACE)) {
  1304. return ERR;
  1305. }
  1306. offset++;
  1307.  
  1308. offset += 26; // skip pre-data - optional
  1309. while(offset < irparams.rawlen - 4) {
  1310. if(MATCH_MARK(results->rawbuf[offset], AIWA_RC_T501_BIT_MARK)) {
  1311. offset++;
  1312. }
  1313. else {
  1314. return ERR;
  1315. }
  1316.  
  1317. // ONE & ZERO
  1318. if(MATCH_SPACE(results->rawbuf[offset], AIWA_RC_T501_ONE_SPACE)) {
  1319. data = (data << 1) | 1;
  1320. }
  1321. else if(MATCH_SPACE(results->rawbuf[offset], AIWA_RC_T501_ZERO_SPACE)) {
  1322. data <<= 1;
  1323. }
  1324. else {
  1325. // End of one & zero detected
  1326. break;
  1327. }
  1328. offset++;
  1329. }
  1330.  
  1331. results->bits = (offset - 1) / 2;
  1332. if(results->bits < 42) {
  1333. return ERR;
  1334. }
  1335. results->value = data;
  1336. results->decode_type = AIWA_RC_T501;
  1337. return DECODED;
  1338. }
  1339.  
  1340. #endif
  1341.  
  1342. /* -----------------------------------------------------------------------
  1343. * hashdecode - decode an arbitrary IR code.
  1344. * Instead of decoding using a standard encoding scheme
  1345. * (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
  1346. *
  1347. * The algorithm: look at the sequence of MARK signals, and see if each one
  1348. * is shorter (0), the same length (1), or longer (2) than the previous.
  1349. * Do the same with the SPACE signals. Hszh the resulting sequence of 0's,
  1350. * 1's, and 2's to a 32-bit value. This will give a unique value for each
  1351. * different code (probably), for most code systems.
  1352. *
  1353. * http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
  1354. */
  1355.  
  1356. // Compare two tick values, returning 0 if newval is shorter,
  1357. // 1 if newval is equal, and 2 if newval is longer
  1358. // Use a tolerance of 20%
  1359. int IRrecv::compare(unsigned int oldval, unsigned int newval) {
  1360. if (newval < oldval * .8) {
  1361. return 0;
  1362. }
  1363. else if (oldval < newval * .8) {
  1364. return 2;
  1365. }
  1366. else {
  1367. return 1;
  1368. }
  1369. }
  1370.  
  1371. // Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
  1372. #define FNV_PRIME_32 16777619
  1373. #define FNV_BASIS_32 2166136261
  1374.  
  1375. /* Converts the raw code values into a 32-bit hash code.
  1376. * Hopefully this code is unique for each button.
  1377. * This isn't a "real" decoding, just an arbitrary value.
  1378. */
  1379. long IRrecv::decodeHash(decode_results *results) {
  1380. // Require at least 6 samples to prevent triggering on noise
  1381. if (results->rawlen < 6) {
  1382. return ERR;
  1383. }
  1384. long hash = FNV_BASIS_32;
  1385. for (int i = 1; i+2 < results->rawlen; i++) {
  1386. int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
  1387. // Add value into the hash
  1388. hash = (hash * FNV_PRIME_32) ^ value;
  1389. }
  1390. results->value = hash;
  1391. results->bits = 32;
  1392. results->decode_type = UNKNOWN;
  1393. return DECODED;
  1394. }
  1395.  
  1396. /* Sharp and DISH support by Todd Treece ( http://unionbridge.org/design/ircommand )
  1397.  
  1398. The Dish send function needs to be repeated 4 times, and the Sharp function
  1399. has the necessary repeat built in because of the need to invert the signal.
  1400.  
  1401. Sharp protocol documentation:
  1402. http://www.sbprojects.com/knowledge/ir/sharp.htm
  1403.  
  1404. Here are the LIRC files that I found that seem to match the remote codes
  1405. from the oscilloscope:
  1406.  
  1407. Sharp LCD TV:
  1408. http://lirc.sourceforge.net/remotes/sharp/GA538WJSA
  1409.  
  1410. DISH NETWORK (echostar 301):
  1411. http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx
  1412.  
  1413. For the DISH codes, only send the last for characters of the hex.
  1414. i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the
  1415. linked LIRC file.
  1416. */
  1417.  
  1418. #ifdef IRsendSHARP
  1419. void IRsend::sendSharp(unsigned long data, int nbits) {
  1420. unsigned long invertdata = data ^ SHARP_TOGGLE_MASK;
  1421. enableIROut(38);
  1422.  
  1423. // Sending codes in bursts of 3 (normal, inverted, normal) makes transmission
  1424. // much more reliable. That's the exact behaviour of CD-S6470 remote control.
  1425. for (int n = 0; n < 3; n++) {
  1426. for (int i = 1 << (nbits-1); i > 0; i>>=1) {
  1427. if (data & i) {
  1428. mark(SHARP_BIT_MARK);
  1429. space(SHARP_ONE_SPACE);
  1430. }
  1431. else {
  1432. mark(SHARP_BIT_MARK);
  1433. space(SHARP_ZERO_SPACE);
  1434. }
  1435. }
  1436.  
  1437. mark(SHARP_BIT_MARK);
  1438. space(SHARP_ZERO_SPACE);
  1439. delay(40);
  1440.  
  1441. data = data ^ SHARP_TOGGLE_MASK;
  1442. }
  1443. }
  1444.  
  1445. // Sharp send compatible with data obtained through decodeSharp
  1446. void IRsend::sendSharp(unsigned int address, unsigned int command) {
  1447. sendSharpRaw((address << 10) | (command << 2) | 2, 15);
  1448. }
  1449.  
  1450. #endif
  1451.  
  1452. #ifdef IRsendDISH
  1453. void IRsend::sendDISH(unsigned long data, int nbits)
  1454. {
  1455. enableIROut(56);
  1456. mark(DISH_HDR_MARK);
  1457. space(DISH_HDR_SPACE);
  1458. for (int i = 0; i < nbits; i++) {
  1459. if (data & DISH_TOP_BIT) {
  1460. mark(DISH_BIT_MARK);
  1461. space(DISH_ONE_SPACE);
  1462. }
  1463. else {
  1464. mark(DISH_BIT_MARK);
  1465. space(DISH_ZERO_SPACE);
  1466. }
  1467. data <<= 1;
  1468. }
  1469. }
  1470. #endif
  1471. /**
  1472. * Aiwa system
  1473. * Remote control RC-T501
  1474. * Lirc file http://lirc.sourceforge.net/remotes/aiwa/RC-T501
  1475. *
  1476. */
  1477.  
  1478. #ifdef AIWA_RC_T501
  1479. void IRsend::sendAiwaRCT501(int code) {
  1480. // PRE-DATA, 26 bits, 0x227EEC0
  1481. long int pre = 0x227EEC0;
  1482. int i;
  1483.  
  1484. enableIROut(AIWA_RC_T501_HZ);
  1485.  
  1486. // HDR mark + HDR space
  1487. mark(AIWA_RC_T501_HDR_MARK);
  1488. space(AIWA_RC_T501_HDR_SPACE);
  1489.  
  1490. // Skip leading zero's
  1491. pre <<= 6;
  1492. // Send pre-data
  1493. for(i=0; i < 26; i++) {
  1494. mark(AIWA_RC_T501_BIT_MARK);
  1495. if(pre & TOPBIT) {
  1496. space(AIWA_RC_T501_ONE_SPACE);
  1497. } else {
  1498. space(AIWA_RC_T501_ZERO_SPACE);
  1499. }
  1500. pre <<= 1;
  1501. }
  1502.  
  1503. // Skip firts code bit
  1504. code <<= 1;
  1505. // Send code
  1506. for(i=0; i < 15; i++) {
  1507. mark(AIWA_RC_T501_BIT_MARK);
  1508. if(code & TOPBIT) {
  1509. space(AIWA_RC_T501_ONE_SPACE);
  1510. } else {
  1511. space(AIWA_RC_T501_ZERO_SPACE);
  1512. }
  1513. code <<= 1;
  1514. }
  1515. // POST-DATA, 1 bit, 0x0
  1516. mark(AIWA_RC_T501_BIT_MARK);
  1517. space(AIWA_RC_T501_ZERO_SPACE);
  1518.  
  1519. mark(AIWA_RC_T501_BIT_MARK);
  1520. space(0);
  1521. }
  1522. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement