Infrared remote library for Arduino: send and receive infrared signals with multiple protocols Port from Arduino-IRremote https://github.com/z3t0/Arduino-IRremote

Dependents:   Lilnija_29012017 NucleoF042K6_IRReceiver

Committer:
yuhki50
Date:
Sat Jan 23 15:09:34 2016 +0000
Revision:
3:17440cf7ab90
Parent:
0:70c8e56bac45
Child:
6:ee990cddff48
porting

Who changed what in which revision?

UserRevisionLine numberNew 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 3:17440cf7ab90 24 _pwm.write(0.5); // Enable 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 3:17440cf7ab90 35 _pwm.write(0.0); // Disable 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 // Enables IR output. The khz value controls the modulation frequency in kilohertz.
yuhki50 0:70c8e56bac45 41 // The IR output will be on pin 3 (OC2B).
yuhki50 0:70c8e56bac45 42 // This routine is designed for 36-40KHz; if you use it for other values, it's up to you
yuhki50 0:70c8e56bac45 43 // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
yuhki50 0:70c8e56bac45 44 // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
yuhki50 0:70c8e56bac45 45 // controlling the duty cycle.
yuhki50 0:70c8e56bac45 46 // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
yuhki50 0:70c8e56bac45 47 // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
yuhki50 0:70c8e56bac45 48 // A few hours staring at the ATmega documentation and this will all make sense.
yuhki50 0:70c8e56bac45 49 // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
yuhki50 0:70c8e56bac45 50 //
yuhki50 0:70c8e56bac45 51 void IRsend::enableIROut (int khz)
yuhki50 0:70c8e56bac45 52 {
yuhki50 3:17440cf7ab90 53 _pwm.write(0.0);
yuhki50 3:17440cf7ab90 54 _pwm.period_us(1 / khz * 1000);
yuhki50 0:70c8e56bac45 55 }
yuhki50 0:70c8e56bac45 56
yuhki50 0:70c8e56bac45 57 //+=============================================================================
yuhki50 0:70c8e56bac45 58 // Custom delay function that circumvents Arduino's delayMicroseconds limit
yuhki50 0:70c8e56bac45 59
yuhki50 0:70c8e56bac45 60 void IRsend::custom_delay_usec(unsigned long uSecs) {
yuhki50 0:70c8e56bac45 61 if (uSecs > 4) {
yuhki50 3:17440cf7ab90 62 unsigned long start = us_ticker_read();
yuhki50 0:70c8e56bac45 63 unsigned long endMicros = start + uSecs - 4;
yuhki50 0:70c8e56bac45 64 if (endMicros < start) { // Check if overflow
yuhki50 3:17440cf7ab90 65 while ( us_ticker_read() > start ) {} // wait until overflow
yuhki50 0:70c8e56bac45 66 }
yuhki50 3:17440cf7ab90 67 while ( us_ticker_read() < endMicros ) {} // normal wait
yuhki50 0:70c8e56bac45 68 }
yuhki50 0:70c8e56bac45 69 //else {
yuhki50 0:70c8e56bac45 70 // __asm__("nop\n\t"); // must have or compiler optimizes out
yuhki50 0:70c8e56bac45 71 //}
yuhki50 0:70c8e56bac45 72 }