mirror of
https://github.com/crankyoldgit/IRremoteESP8266.git
synced 2026-01-12 00:05:10 +08:00
372 lines
9.7 KiB
C++
372 lines
9.7 KiB
C++
/*
|
|
* IRremote
|
|
* Version 0.11 August, 2009
|
|
* Copyright 2009 Ken Shirriff
|
|
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
|
|
*
|
|
* Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
|
|
* Modified by Mitra Ardron <mitra@mitra.biz>
|
|
* Added Sanyo and Mitsubishi controllers
|
|
* Modified Sony to spot the repeat codes that some Sony's send
|
|
*
|
|
* Interrupt code based on NECIRrcv by Joe Knapp
|
|
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
|
|
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
|
|
*
|
|
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
|
|
* LG added by Darryl Smith (based on the JVC protocol)
|
|
* Whynter A/C ARC-110WD added by Francesco Meschia
|
|
*/
|
|
|
|
#include "IRremoteESP8266.h"
|
|
#include "IRremoteInt.h"
|
|
|
|
// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging.
|
|
// To use them, set DEBUG in IRremoteInt.h
|
|
// Normally macros are used for efficiency
|
|
#ifdef DEBUG
|
|
int MATCH(int measured, int desired) {
|
|
Serial.print("Testing: ");
|
|
Serial.print(TICKS_LOW(desired), DEC);
|
|
Serial.print(" <= ");
|
|
Serial.print(measured, DEC);
|
|
Serial.print(" <= ");
|
|
Serial.println(TICKS_HIGH(desired), DEC);
|
|
return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);
|
|
}
|
|
|
|
int MATCH_MARK(int measured_ticks, int desired_us) {
|
|
Serial.print("Testing mark ");
|
|
Serial.print(measured_ticks * USECPERTICK, DEC);
|
|
Serial.print(" vs ");
|
|
Serial.print(desired_us, DEC);
|
|
Serial.print(": ");
|
|
Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
|
|
Serial.print(" <= ");
|
|
Serial.print(measured_ticks, DEC);
|
|
Serial.print(" <= ");
|
|
Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
|
|
return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS);
|
|
}
|
|
|
|
int MATCH_SPACE(int measured_ticks, int desired_us) {
|
|
Serial.print("Testing space ");
|
|
Serial.print(measured_ticks * USECPERTICK, DEC);
|
|
Serial.print(" vs ");
|
|
Serial.print(desired_us, DEC);
|
|
Serial.print(": ");
|
|
Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
|
|
Serial.print(" <= ");
|
|
Serial.print(measured_ticks, DEC);
|
|
Serial.print(" <= ");
|
|
Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
|
|
return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS);
|
|
}
|
|
#else
|
|
int MATCH(int measured, int desired) {return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);}
|
|
int MATCH_MARK(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us + MARK_EXCESS));}
|
|
int MATCH_SPACE(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us - MARK_EXCESS));}
|
|
// Debugging versions are in IRremote.cpp
|
|
#endif
|
|
|
|
IRsend::IRsend(int IRsendPin)
|
|
{
|
|
IRpin = IRsendPin;
|
|
}
|
|
|
|
void IRsend::begin()
|
|
{
|
|
pinMode(IRpin, OUTPUT);
|
|
}
|
|
|
|
void IRsend::sendNEC(unsigned long data, int nbits)
|
|
{
|
|
enableIROut(38);
|
|
mark(NEC_HDR_MARK);
|
|
space(NEC_HDR_SPACE);
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & TOPBIT) {
|
|
mark(NEC_BIT_MARK);
|
|
space(NEC_ONE_SPACE);
|
|
}
|
|
else {
|
|
mark(NEC_BIT_MARK);
|
|
space(NEC_ZERO_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
mark(NEC_BIT_MARK);
|
|
space(0);
|
|
}
|
|
|
|
void IRsend::sendWhynter(unsigned long data, int nbits) {
|
|
enableIROut(38);
|
|
mark(WHYNTER_ZERO_MARK);
|
|
space(WHYNTER_ZERO_SPACE);
|
|
mark(WHYNTER_HDR_MARK);
|
|
space(WHYNTER_HDR_SPACE);
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & TOPBIT) {
|
|
mark(WHYNTER_ONE_MARK);
|
|
space(WHYNTER_ONE_SPACE);
|
|
}
|
|
else {
|
|
mark(WHYNTER_ZERO_MARK);
|
|
space(WHYNTER_ZERO_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
mark(WHYNTER_ZERO_MARK);
|
|
space(WHYNTER_ZERO_SPACE);
|
|
}
|
|
|
|
void IRsend::sendSony(unsigned long data, int nbits) {
|
|
enableIROut(40);
|
|
mark(SONY_HDR_MARK);
|
|
space(SONY_HDR_SPACE);
|
|
data = data << (32 - nbits);
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & TOPBIT) {
|
|
mark(SONY_ONE_MARK);
|
|
space(SONY_HDR_SPACE);
|
|
}
|
|
else {
|
|
mark(SONY_ZERO_MARK);
|
|
space(SONY_HDR_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
}
|
|
|
|
void IRsend::sendRaw(unsigned int buf[], int len, int hz)
|
|
{
|
|
enableIROut(hz);
|
|
for (int i = 0; i < len; i++) {
|
|
if (i & 1) {
|
|
space(buf[i]);
|
|
}
|
|
else {
|
|
mark(buf[i]);
|
|
}
|
|
}
|
|
space(0); // Just to be sure
|
|
}
|
|
|
|
// Note: first bit must be a one (start bit)
|
|
void IRsend::sendRC5(unsigned long data, int nbits)
|
|
{
|
|
enableIROut(36);
|
|
data = data << (32 - nbits);
|
|
mark(RC5_T1); // First start bit
|
|
space(RC5_T1); // Second start bit
|
|
mark(RC5_T1); // Second start bit
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & TOPBIT) {
|
|
space(RC5_T1); // 1 is space, then mark
|
|
mark(RC5_T1);
|
|
}
|
|
else {
|
|
mark(RC5_T1);
|
|
space(RC5_T1);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
space(0); // Turn off at end
|
|
}
|
|
|
|
// Caller needs to take care of flipping the toggle bit
|
|
void IRsend::sendRC6(unsigned long data, int nbits)
|
|
{
|
|
enableIROut(36);
|
|
data = data << (32 - nbits);
|
|
mark(RC6_HDR_MARK);
|
|
space(RC6_HDR_SPACE);
|
|
mark(RC6_T1); // start bit
|
|
space(RC6_T1);
|
|
int t;
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (i == 3) {
|
|
// double-wide trailer bit
|
|
t = 2 * RC6_T1;
|
|
}
|
|
else {
|
|
t = RC6_T1;
|
|
}
|
|
if (data & TOPBIT) {
|
|
mark(t);
|
|
space(t);
|
|
}
|
|
else {
|
|
space(t);
|
|
mark(t);
|
|
}
|
|
|
|
data <<= 1;
|
|
}
|
|
space(0); // Turn off at end
|
|
}
|
|
void IRsend::sendPanasonic(unsigned int address, unsigned long data) {
|
|
enableIROut(35);
|
|
mark(PANASONIC_HDR_MARK);
|
|
space(PANASONIC_HDR_SPACE);
|
|
|
|
for(int i=0;i<16;i++)
|
|
{
|
|
mark(PANASONIC_BIT_MARK);
|
|
if (address & 0x8000) {
|
|
space(PANASONIC_ONE_SPACE);
|
|
} else {
|
|
space(PANASONIC_ZERO_SPACE);
|
|
}
|
|
address <<= 1;
|
|
}
|
|
for (int i=0; i < 32; i++) {
|
|
mark(PANASONIC_BIT_MARK);
|
|
if (data & TOPBIT) {
|
|
space(PANASONIC_ONE_SPACE);
|
|
} else {
|
|
space(PANASONIC_ZERO_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
mark(PANASONIC_BIT_MARK);
|
|
space(0);
|
|
}
|
|
void IRsend::sendJVC(unsigned long data, int nbits, int repeat)
|
|
{
|
|
enableIROut(38);
|
|
data = data << (32 - nbits);
|
|
if (!repeat){
|
|
mark(JVC_HDR_MARK);
|
|
space(JVC_HDR_SPACE);
|
|
}
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & TOPBIT) {
|
|
mark(JVC_BIT_MARK);
|
|
space(JVC_ONE_SPACE);
|
|
}
|
|
else {
|
|
mark(JVC_BIT_MARK);
|
|
space(JVC_ZERO_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
mark(JVC_BIT_MARK);
|
|
space(0);
|
|
}
|
|
|
|
void IRsend::sendSAMSUNG(unsigned long data, int nbits)
|
|
{
|
|
enableIROut(38);
|
|
mark(SAMSUNG_HDR_MARK);
|
|
space(SAMSUNG_HDR_SPACE);
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & TOPBIT) {
|
|
mark(SAMSUNG_BIT_MARK);
|
|
space(SAMSUNG_ONE_SPACE);
|
|
}
|
|
else {
|
|
mark(SAMSUNG_BIT_MARK);
|
|
space(SAMSUNG_ZERO_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
mark(SAMSUNG_BIT_MARK);
|
|
space(0);
|
|
}
|
|
|
|
void IRsend::mark(int time) {
|
|
// Sends an IR mark for the specified number of microseconds.
|
|
// The mark output is modulated at the PWM frequency.
|
|
long beginning = micros();
|
|
while(micros() - beginning < time){
|
|
digitalWrite(IRpin, HIGH);
|
|
delayMicroseconds(halfPeriodicTime);
|
|
digitalWrite(IRpin, LOW);
|
|
delayMicroseconds(halfPeriodicTime); //38 kHz -> T = 26.31 microsec (periodic time), half of it is 13
|
|
}
|
|
}
|
|
|
|
/* Leave pin off for time (given in microseconds) */
|
|
void IRsend::space(int time) {
|
|
// Sends an IR space for the specified number of microseconds.
|
|
// A space is no output, so the PWM output is disabled.
|
|
digitalWrite(IRpin, LOW);
|
|
if (time > 0) delayMicroseconds(time);
|
|
}
|
|
|
|
void IRsend::enableIROut(int khz) {
|
|
// Enables IR output. The khz value controls the modulation frequency in kilohertz.
|
|
halfPeriodicTime = 500/khz; // T = 1/f but we need T/2 in microsecond and f is in kHz
|
|
}
|
|
|
|
|
|
/* Sharp and DISH support by Todd Treece ( http://unionbridge.org/design/ircommand )
|
|
|
|
The Dish send function needs to be repeated 4 times, and the Sharp function
|
|
has the necessary repeat built in because of the need to invert the signal.
|
|
|
|
Sharp protocol documentation:
|
|
http://www.sbprojects.com/knowledge/ir/sharp.htm
|
|
|
|
Here are the LIRC files that I found that seem to match the remote codes
|
|
from the oscilloscope:
|
|
|
|
Sharp LCD TV:
|
|
http://lirc.sourceforge.net/remotes/sharp/GA538WJSA
|
|
|
|
DISH NETWORK (echostar 301):
|
|
http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx
|
|
|
|
For the DISH codes, only send the last for characters of the hex.
|
|
i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the
|
|
linked LIRC file.
|
|
*/
|
|
|
|
void IRsend::sendSharpRaw(unsigned long data, int nbits) {
|
|
enableIROut(38);
|
|
|
|
// Sending codes in bursts of 3 (normal, inverted, normal) makes transmission
|
|
// much more reliable. That's the exact behaviour of CD-S6470 remote control.
|
|
for (int n = 0; n < 3; n++) {
|
|
for (int i = 1 << (nbits-1); i > 0; i>>=1) {
|
|
if (data & i) {
|
|
mark(SHARP_BIT_MARK);
|
|
space(SHARP_ONE_SPACE);
|
|
}
|
|
else {
|
|
mark(SHARP_BIT_MARK);
|
|
space(SHARP_ZERO_SPACE);
|
|
}
|
|
}
|
|
|
|
mark(SHARP_BIT_MARK);
|
|
space(SHARP_ZERO_SPACE);
|
|
delay(40);
|
|
|
|
data = data ^ SHARP_TOGGLE_MASK;
|
|
}
|
|
}
|
|
|
|
// Sharp send compatible with data obtained through decodeSharp
|
|
void IRsend::sendSharp(unsigned int address, unsigned int command) {
|
|
sendSharpRaw((address << 10) | (command << 2) | 2, 15);
|
|
}
|
|
|
|
void IRsend::sendDISH(unsigned long data, int nbits) {
|
|
enableIROut(56);
|
|
mark(DISH_HDR_MARK);
|
|
space(DISH_HDR_SPACE);
|
|
for (int i = 0; i < nbits; i++) {
|
|
if (data & DISH_TOP_BIT) {
|
|
mark(DISH_BIT_MARK);
|
|
space(DISH_ONE_SPACE);
|
|
}
|
|
else {
|
|
mark(DISH_BIT_MARK);
|
|
space(DISH_ZERO_SPACE);
|
|
}
|
|
data <<= 1;
|
|
}
|
|
}
|