Last active
November 17, 2015 03:33
-
-
Save neilisaac/078af863c87be01c5f17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IRremote.h> | |
IRrecv irrecv(A2); | |
IRsend irsend; // hard-coded to pin 3 | |
String buf; | |
String mode; | |
void setup() { | |
irrecv.enableIRIn(); | |
Serial.begin(9600); | |
buf = ""; | |
mode = "samsung"; | |
} | |
void send(unsigned long code) { | |
Serial.print("sending "); | |
Serial.println(code, HEX); | |
for (int i = 0; i < 3; i++) { | |
if (mode == "sony") | |
irsend.sendSony(code, 32); | |
else if (mode == "rc5") | |
irsend.sendRC6(code, 32); | |
else if (mode == "rc6") | |
irsend.sendRC5(code, 32); | |
else if (mode == "samsung") | |
irsend.sendSAMSUNG(code, 32); | |
else if (mode == "nec") | |
irsend.sendNEC(code, 32); | |
else | |
Serial.print("error: unknown mode " + mode + "\n"); | |
delay(40); | |
} | |
Serial.print("end\n"); | |
} | |
unsigned long convert(const char *data) { | |
unsigned long result = 0; | |
for (char c = *data; c != '\0'; c = *++data) { | |
int part = 0; | |
if (c >= '0' && c <= '9') | |
part = c - '0'; | |
else if (c>= 'a' && c <= 'f') | |
part = c - 'a' + 0xa; | |
else if (c >= 'A' && c <= 'F') | |
part = c - 'A' + 0xA; | |
result <<= 4; | |
result += part; | |
} | |
return result; | |
} | |
void sender() { | |
while (Serial.available() > 0) { | |
char c = Serial.read(); | |
if (c == '\n') { | |
if (buf == "sony" || buf == "nec" || buf == "samsung") { | |
mode = buf; | |
Serial.print("set mode to " + mode + "\n"); | |
} else { | |
send(convert(buf.c_str())); | |
} | |
buf = ""; | |
} else { | |
buf += c; | |
} | |
} | |
} | |
void listener() { | |
decode_results results; | |
if (irrecv.decode(&results)) { | |
Serial.print(results.decode_type); | |
Serial.print(" ("); | |
Serial.print(results.bits); | |
Serial.print("): "); | |
Serial.println(results.value, HEX); | |
irrecv.resume(); | |
} | |
} | |
void loop() { | |
sender(); | |
//listener(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment