Infrared remote library for Arduino: send and receive infrared signals with multiple protocols Port from Arduino-IRremote https://github.com/z3t0/Arduino-IRremote
irSend.cpp@0:70c8e56bac45, 2016-01-23 (annotated)
- Committer:
- yuhki50
- Date:
- Sat Jan 23 06:16:48 2016 +0000
- Revision:
- 0:70c8e56bac45
- Child:
- 3:17440cf7ab90
import https://github.com/z3t0/Arduino-IRremote e3ec11d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yuhki50 | 0:70c8e56bac45 | 1 | #include "IRremote.h" |
yuhki50 | 0:70c8e56bac45 | 2 | #include "IRremoteInt.h" |
yuhki50 | 0:70c8e56bac45 | 3 | |
yuhki50 | 0:70c8e56bac45 | 4 | //+============================================================================= |
yuhki50 | 0:70c8e56bac45 | 5 | void IRsend::sendRaw (unsigned int buf[], unsigned int len, unsigned int hz) |
yuhki50 | 0:70c8e56bac45 | 6 | { |
yuhki50 | 0:70c8e56bac45 | 7 | // Set IR carrier frequency |
yuhki50 | 0:70c8e56bac45 | 8 | enableIROut(hz); |
yuhki50 | 0:70c8e56bac45 | 9 | |
yuhki50 | 0:70c8e56bac45 | 10 | for (unsigned int i = 0; i < len; i++) { |
yuhki50 | 0:70c8e56bac45 | 11 | if (i & 1) space(buf[i]) ; |
yuhki50 | 0:70c8e56bac45 | 12 | else mark (buf[i]) ; |
yuhki50 | 0:70c8e56bac45 | 13 | } |
yuhki50 | 0:70c8e56bac45 | 14 | |
yuhki50 | 0:70c8e56bac45 | 15 | space(0); // Always end with the LED off |
yuhki50 | 0:70c8e56bac45 | 16 | } |
yuhki50 | 0:70c8e56bac45 | 17 | |
yuhki50 | 0:70c8e56bac45 | 18 | //+============================================================================= |
yuhki50 | 0:70c8e56bac45 | 19 | // Sends an IR mark for the specified number of microseconds. |
yuhki50 | 0:70c8e56bac45 | 20 | // The mark output is modulated at the PWM frequency. |
yuhki50 | 0:70c8e56bac45 | 21 | // |
yuhki50 | 0:70c8e56bac45 | 22 | void IRsend::mark (unsigned int time) |
yuhki50 | 0:70c8e56bac45 | 23 | { |
yuhki50 | 0:70c8e56bac45 | 24 | TIMER_ENABLE_PWM; // Enable pin 3 PWM output |
yuhki50 | 0:70c8e56bac45 | 25 | if (time > 0) custom_delay_usec(time); |
yuhki50 | 0:70c8e56bac45 | 26 | } |
yuhki50 | 0:70c8e56bac45 | 27 | |
yuhki50 | 0:70c8e56bac45 | 28 | //+============================================================================= |
yuhki50 | 0:70c8e56bac45 | 29 | // Leave pin off for time (given in microseconds) |
yuhki50 | 0:70c8e56bac45 | 30 | // Sends an IR space for the specified number of microseconds. |
yuhki50 | 0:70c8e56bac45 | 31 | // A space is no output, so the PWM output is disabled. |
yuhki50 | 0:70c8e56bac45 | 32 | // |
yuhki50 | 0:70c8e56bac45 | 33 | void IRsend::space (unsigned int time) |
yuhki50 | 0:70c8e56bac45 | 34 | { |
yuhki50 | 0:70c8e56bac45 | 35 | TIMER_DISABLE_PWM; // Disable pin 3 PWM output |
yuhki50 | 0:70c8e56bac45 | 36 | if (time > 0) IRsend::custom_delay_usec(time); |
yuhki50 | 0:70c8e56bac45 | 37 | } |
yuhki50 | 0:70c8e56bac45 | 38 | |
yuhki50 | 0:70c8e56bac45 | 39 | |
yuhki50 | 0:70c8e56bac45 | 40 | |
yuhki50 | 0:70c8e56bac45 | 41 | |
yuhki50 | 0:70c8e56bac45 | 42 | |
yuhki50 | 0:70c8e56bac45 | 43 | //+============================================================================= |
yuhki50 | 0:70c8e56bac45 | 44 | // Enables IR output. The khz value controls the modulation frequency in kilohertz. |
yuhki50 | 0:70c8e56bac45 | 45 | // The IR output will be on pin 3 (OC2B). |
yuhki50 | 0:70c8e56bac45 | 46 | // This routine is designed for 36-40KHz; if you use it for other values, it's up to you |
yuhki50 | 0:70c8e56bac45 | 47 | // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) |
yuhki50 | 0:70c8e56bac45 | 48 | // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B |
yuhki50 | 0:70c8e56bac45 | 49 | // controlling the duty cycle. |
yuhki50 | 0:70c8e56bac45 | 50 | // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) |
yuhki50 | 0:70c8e56bac45 | 51 | // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. |
yuhki50 | 0:70c8e56bac45 | 52 | // A few hours staring at the ATmega documentation and this will all make sense. |
yuhki50 | 0:70c8e56bac45 | 53 | // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details. |
yuhki50 | 0:70c8e56bac45 | 54 | // |
yuhki50 | 0:70c8e56bac45 | 55 | void IRsend::enableIROut (int khz) |
yuhki50 | 0:70c8e56bac45 | 56 | { |
yuhki50 | 0:70c8e56bac45 | 57 | // Disable the Timer2 Interrupt (which is used for receiving IR) |
yuhki50 | 0:70c8e56bac45 | 58 | TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt |
yuhki50 | 0:70c8e56bac45 | 59 | |
yuhki50 | 0:70c8e56bac45 | 60 | pinMode(TIMER_PWM_PIN, OUTPUT); |
yuhki50 | 0:70c8e56bac45 | 61 | digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low |
yuhki50 | 0:70c8e56bac45 | 62 | |
yuhki50 | 0:70c8e56bac45 | 63 | // COM2A = 00: disconnect OC2A |
yuhki50 | 0:70c8e56bac45 | 64 | // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted |
yuhki50 | 0:70c8e56bac45 | 65 | // WGM2 = 101: phase-correct PWM with OCRA as top |
yuhki50 | 0:70c8e56bac45 | 66 | // CS2 = 000: no prescaling |
yuhki50 | 0:70c8e56bac45 | 67 | // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A. |
yuhki50 | 0:70c8e56bac45 | 68 | TIMER_CONFIG_KHZ(khz); |
yuhki50 | 0:70c8e56bac45 | 69 | } |
yuhki50 | 0:70c8e56bac45 | 70 | |
yuhki50 | 0:70c8e56bac45 | 71 | //+============================================================================= |
yuhki50 | 0:70c8e56bac45 | 72 | // Custom delay function that circumvents Arduino's delayMicroseconds limit |
yuhki50 | 0:70c8e56bac45 | 73 | |
yuhki50 | 0:70c8e56bac45 | 74 | void IRsend::custom_delay_usec(unsigned long uSecs) { |
yuhki50 | 0:70c8e56bac45 | 75 | if (uSecs > 4) { |
yuhki50 | 0:70c8e56bac45 | 76 | unsigned long start = micros(); |
yuhki50 | 0:70c8e56bac45 | 77 | unsigned long endMicros = start + uSecs - 4; |
yuhki50 | 0:70c8e56bac45 | 78 | if (endMicros < start) { // Check if overflow |
yuhki50 | 0:70c8e56bac45 | 79 | while ( micros() > start ) {} // wait until overflow |
yuhki50 | 0:70c8e56bac45 | 80 | } |
yuhki50 | 0:70c8e56bac45 | 81 | while ( micros() < endMicros ) {} // normal wait |
yuhki50 | 0:70c8e56bac45 | 82 | } |
yuhki50 | 0:70c8e56bac45 | 83 | //else { |
yuhki50 | 0:70c8e56bac45 | 84 | // __asm__("nop\n\t"); // must have or compiler optimizes out |
yuhki50 | 0:70c8e56bac45 | 85 | //} |
yuhki50 | 0:70c8e56bac45 | 86 | } |
yuhki50 | 0:70c8e56bac45 | 87 |