Andrea L / IRremote

Dependents:   ConditionlyDaikin

Committer:
alittera
Date:
Mon May 27 11:13:55 2019 +0000
Revision:
9:4e06441ceecf
Parent:
6:ee990cddff48
Final revision.; Tested with led on D13, also visible with the built in Led on Nucleo-F401RE;

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 6:ee990cddff48 3 #include "cmath"
yuhki50 0:70c8e56bac45 4
yuhki50 0:70c8e56bac45 5 //+=============================================================================
yuhki50 0:70c8e56bac45 6 void IRsend::sendRaw (unsigned int buf[], unsigned int len, unsigned int hz)
yuhki50 0:70c8e56bac45 7 {
yuhki50 0:70c8e56bac45 8 // Set IR carrier frequency
yuhki50 0:70c8e56bac45 9 enableIROut(hz);
yuhki50 0:70c8e56bac45 10
yuhki50 0:70c8e56bac45 11 for (unsigned int i = 0; i < len; i++) {
yuhki50 0:70c8e56bac45 12 if (i & 1) space(buf[i]) ;
yuhki50 0:70c8e56bac45 13 else mark (buf[i]) ;
yuhki50 0:70c8e56bac45 14 }
yuhki50 0:70c8e56bac45 15
yuhki50 0:70c8e56bac45 16 space(0); // Always end with the LED off
yuhki50 0:70c8e56bac45 17 }
yuhki50 0:70c8e56bac45 18
yuhki50 0:70c8e56bac45 19 //+=============================================================================
yuhki50 0:70c8e56bac45 20 // Sends an IR mark for the specified number of microseconds.
yuhki50 0:70c8e56bac45 21 // The mark output is modulated at the PWM frequency.
yuhki50 0:70c8e56bac45 22 //
yuhki50 0:70c8e56bac45 23 void IRsend::mark (unsigned int time)
yuhki50 0:70c8e56bac45 24 {
yuhki50 3:17440cf7ab90 25 _pwm.write(0.5); // Enable PWM output
yuhki50 0:70c8e56bac45 26 if (time > 0) custom_delay_usec(time);
yuhki50 0:70c8e56bac45 27 }
yuhki50 0:70c8e56bac45 28
alittera 9:4e06441ceecf 29
alittera 9:4e06441ceecf 30
yuhki50 0:70c8e56bac45 31 //+=============================================================================
yuhki50 0:70c8e56bac45 32 // Leave pin off for time (given in microseconds)
yuhki50 0:70c8e56bac45 33 // Sends an IR space for the specified number of microseconds.
yuhki50 0:70c8e56bac45 34 // A space is no output, so the PWM output is disabled.
yuhki50 0:70c8e56bac45 35 //
yuhki50 0:70c8e56bac45 36 void IRsend::space (unsigned int time)
yuhki50 0:70c8e56bac45 37 {
yuhki50 3:17440cf7ab90 38 _pwm.write(0.0); // Disable PWM output
yuhki50 0:70c8e56bac45 39 if (time > 0) IRsend::custom_delay_usec(time);
yuhki50 0:70c8e56bac45 40 }
yuhki50 0:70c8e56bac45 41
yuhki50 0:70c8e56bac45 42 //+=============================================================================
yuhki50 0:70c8e56bac45 43 // Enables IR output. The khz value controls the modulation frequency in kilohertz.
yuhki50 0:70c8e56bac45 44 // The IR output will be on pin 3 (OC2B).
yuhki50 0:70c8e56bac45 45 // This routine is designed for 36-40KHz; if you use it for other values, it's up to you
yuhki50 0:70c8e56bac45 46 // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
yuhki50 0:70c8e56bac45 47 // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
yuhki50 0:70c8e56bac45 48 // controlling the duty cycle.
yuhki50 0:70c8e56bac45 49 // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
yuhki50 0:70c8e56bac45 50 // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
yuhki50 0:70c8e56bac45 51 // A few hours staring at the ATmega documentation and this will all make sense.
yuhki50 0:70c8e56bac45 52 // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
yuhki50 0:70c8e56bac45 53 //
yuhki50 0:70c8e56bac45 54 void IRsend::enableIROut (int khz)
yuhki50 0:70c8e56bac45 55 {
yuhki50 3:17440cf7ab90 56 _pwm.write(0.0);
yuhki50 6:ee990cddff48 57 _pwm.period_us(std::floor((1.0 / khz * 1000) + 0.5)); // round
yuhki50 0:70c8e56bac45 58 }
yuhki50 0:70c8e56bac45 59
yuhki50 0:70c8e56bac45 60 //+=============================================================================
yuhki50 0:70c8e56bac45 61 // Custom delay function that circumvents Arduino's delayMicroseconds limit
yuhki50 0:70c8e56bac45 62
yuhki50 0:70c8e56bac45 63 void IRsend::custom_delay_usec(unsigned long uSecs) {
yuhki50 0:70c8e56bac45 64 if (uSecs > 4) {
yuhki50 3:17440cf7ab90 65 unsigned long start = us_ticker_read();
yuhki50 0:70c8e56bac45 66 unsigned long endMicros = start + uSecs - 4;
yuhki50 0:70c8e56bac45 67 if (endMicros < start) { // Check if overflow
yuhki50 3:17440cf7ab90 68 while ( us_ticker_read() > start ) {} // wait until overflow
yuhki50 0:70c8e56bac45 69 }
yuhki50 3:17440cf7ab90 70 while ( us_ticker_read() < endMicros ) {} // normal wait
yuhki50 0:70c8e56bac45 71 }
yuhki50 0:70c8e56bac45 72 //else {
yuhki50 0:70c8e56bac45 73 // __asm__("nop\n\t"); // must have or compiler optimizes out
yuhki50 0:70c8e56bac45 74 //}
yuhki50 0:70c8e56bac45 75 }