/* * Library containing code for the APIs for IR encoding/decoding. * * Written by Eric B. Wertz * Last modified 2010/04/05 */ #include "ir.h" /* * PULSELEN * SPACERS---+-------+---+---+-------+---+ REPEAT * V V V V V V SPACER * ------+ +-+ +-+ +-+ +-+ +-+ +-+ +-------------+ +-+ +-+ +-+ +-+ +-+ +-+ +------... * | | | | | | | | | | | | | | | | | | | | | | | | | | | | * | | | | | | | | | | | | | | | | | | | | | | | | | | | | * | | | | | | | | | | | | | | | | | | | | | | | | | | | | * +----------+ +-----+ +-+ +-+ +-----+ +-+ +-+ +----------+ +-----+ +-+ +-+ +-----+ +-+ +-+ * PREAMBLE 1 0 0 1 0 0 PREAMBLE 1 0 0 1 0 0 * */ #define PULSELEN_PREAMBLE 2500 // # of usecs preamble is low #define PULSELEN_ZERO 700 // # of usecs 0-bit is low #define PULSELEN_ONE 1300 // # of usecs 1-bit is low #define PULSELEN_SPACER 500 // # of usecs between 0/1 bits #define PULSELEN_TIMEOUT 3500 // longer than any pulse should be #define PULSELEN_PREAMBLE_MIN (PULSELEN_PREAMBLE-300) #define PULSELEN_PREAMBLE_MAX (PULSELEN_PREAMBLE+300) #define PULSELEN_ZERO_MIN (PULSELEN_ZERO -200) #define PULSELEN_ZERO_MAX (PULSELEN_ZERO +200) #define PULSELEN_ONE_MIN (PULSELEN_ONE -200) #define PULSELEN_ONE_MAX (PULSELEN_ONE +200) #define isPulseAPreamble(t) (((t)>PULSELEN_PREAMBLE_MIN) && ((t)PULSELEN_ZERO_MIN) && ((t)PULSELEN_ONE_MIN) && ((t)0; i--) // -1 to skip preamble Serial.print(((pulseData >> (i-1)) & 0x1) ? '1' : '0'); Serial.print ('/'); Serial.println((int)(numPulseLengths-1)); } /* * A 38kHz frequency yields a 26.3us period. When you split the cycle in * half, each part should be roughly 13.15us -- however, because there's some * non-negligible time taken getting in-and-out of the function calls, it turns * out that 10 is the right parameter to get us closest to a 26.3us period. * * NB: This routine does not take into account different Arduino clock speeds * when picking the parameter to delayMicroseconds(). Hopefully the change in * overhead isn't too significant. * NB: This routine does not handle the case where the micros() counter * rolls-over (as mentioned in the micros() documentation). */ void IRSendPulse(byte pin, unsigned int usecs) { unsigned int i, numPulses=(usecs/26);// 38kHz has 26.3usec period for (i=0; i0; i--) { byte theBit = bitRead(data, i-1); IRSendPulse(pin, (theBit == 1) ? PULSELEN_ONE : PULSELEN_ZERO); if (i > 1) delayMicroseconds(PULSELEN_SPACER); // no extra delay after last pulse } } void IRSendDigit(byte pin, byte digit) { IRSend(pin, codesTVDigits[digit], CODETVDIGIT_BITLEN); delayMicroseconds(BURSTLEN_USECS_REPEAT); IRSend(pin, codesTVDigits[digit], CODETVDIGIT_BITLEN); delayMicroseconds(BURSTLEN_USECS_REPEAT); IRSend(pin, codesTVDigits[digit], CODETVDIGIT_BITLEN); }