|
@@ -1,5 +1,6 @@
|
|
|
#include <IRremote.h>
|
|
|
|
|
|
+const int LOG_LEVEL = 1; // Just a general setting so we can control Serial output
|
|
|
const int led = LED_BUILTIN; // On board LED, Pin 13
|
|
|
const int RECV_PIN = 7; // IR Receiver
|
|
|
String input = "";
|
|
@@ -7,6 +8,9 @@ bool done = false;
|
|
|
IRsend sendr; // Make Send Object, DANGER a fixed pin number is always used! (3)
|
|
|
//IRrecv recvr(RECV_PIN); // Make Recever Object
|
|
|
decode_results results; // Make Results Object to store received codes
|
|
|
+String result_0 = "";
|
|
|
+String result_1 = "";
|
|
|
+String result_2 = "";
|
|
|
|
|
|
void setup() {
|
|
|
// Initalize on board LED
|
|
@@ -24,7 +28,7 @@ void setup() {
|
|
|
// Initalize Output routine
|
|
|
Serial.begin(9600); // Serial COM's Port
|
|
|
while(!Serial) { delay(10); } // Wait for conection
|
|
|
- Serial.println("Serial Connected!");
|
|
|
+ Serial.println("Connected!");
|
|
|
digitalWrite(led, LOW);
|
|
|
}
|
|
|
|
|
@@ -46,16 +50,94 @@ void sendCode(long code, int bit_len, bool repeat) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void parse_by_comma(String data) {
|
|
|
+ result_0 = "";
|
|
|
+ result_1 = "";
|
|
|
+ result_2 = "";
|
|
|
+ int at = 0;
|
|
|
+ for(char c : data) {
|
|
|
+ //Serial.println(c);
|
|
|
+ if(at == 0) {
|
|
|
+ if(c != ',') {
|
|
|
+ result_0 += String(c);
|
|
|
+ } else {
|
|
|
+ at += 1;
|
|
|
+ if (LOG_LEVEL >= 2) {
|
|
|
+ Serial.print("> ");
|
|
|
+ Serial.println(result_0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if(at == 1) {
|
|
|
+ if(c != ',') {
|
|
|
+ result_1 += String(c);
|
|
|
+ } else {
|
|
|
+ at += 1;
|
|
|
+ if (LOG_LEVEL >= 2) {
|
|
|
+ Serial.print("> ");
|
|
|
+ Serial.println(result_1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if(at == 2) {
|
|
|
+ result_2 += String(c);
|
|
|
+ if (LOG_LEVEL >= 2) {
|
|
|
+ Serial.print("> ");
|
|
|
+ Serial.println(result_2);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void parse_order() {
|
|
|
+ long code;
|
|
|
+ int bit_size;
|
|
|
+ bool repeat;
|
|
|
+ if(result_2.toInt() == 1) {
|
|
|
+ repeat = true;
|
|
|
+ } else {
|
|
|
+ repeat = false;
|
|
|
+ }
|
|
|
+ // Converting a string into a long
|
|
|
+ // https://stackoverflow.com/questions/29547115/how-to-convert-string-to-hex-value-in-c/29547549#29547549
|
|
|
+ code = strtol(result_0.c_str(), NULL, 16);
|
|
|
+ bit_size = result_1.toInt();
|
|
|
+ if (result_0 != "" and result_1 != "" and result_2 != "") {
|
|
|
+ sendCode(code, bit_size, repeat);
|
|
|
+ if (LOG_LEVEL >= 1) {
|
|
|
+ Serial.println(code);
|
|
|
+ Serial.println(bit_size);
|
|
|
+ Serial.println(repeat);
|
|
|
+ }
|
|
|
+ if (LOG_LEVEL >= 3) {
|
|
|
+ Serial.print(result_0);
|
|
|
+ Serial.print(",");
|
|
|
+ Serial.print(result_1);
|
|
|
+ Serial.print(",");
|
|
|
+ Serial.println(result_2);
|
|
|
+ }
|
|
|
+ Serial.println("Sent!");
|
|
|
+ } else {
|
|
|
+ Serial.println("Failed to parse give command.");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
void action(String act){
|
|
|
// Given String execute action
|
|
|
- if(act == "tv p"){
|
|
|
+ if(act == "r") { // If it's r, let's repeat the last code we sent out
|
|
|
+ parse_order();
|
|
|
+ } else { // If it's not any other command then let's assume it's a new code
|
|
|
+ parse_by_comma(act);
|
|
|
+ parse_order();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*if(act == "tv p"){
|
|
|
sendCode(0x57E3E817, 32, true);
|
|
|
- /*for(int x = 0; x <= 3; x++) {
|
|
|
+ for(int x = 0; x <= 3; x++) {
|
|
|
sendr.sendNEC(0x57E3E817, 32);
|
|
|
delay(40);
|
|
|
- }*/
|
|
|
+ }
|
|
|
Serial.println("tv power");
|
|
|
- }/* else if(act == "m p"){
|
|
|
+ } else if(act == "m p"){
|
|
|
sendCode(0x807F02FD, 32, false);
|
|
|
Serial.println("movie power");
|
|
|
}*/
|
|
@@ -65,10 +147,12 @@ void loop() {
|
|
|
// Are we sending/receiving data?
|
|
|
if(Serial.available() > 0) {
|
|
|
char in = (char)Serial.read();
|
|
|
+ if (LOG_LEVEL >= 3) {
|
|
|
+ Serial.print(": ");
|
|
|
+ Serial.println(in);
|
|
|
+ }
|
|
|
if (in == '\n') {
|
|
|
done = true;
|
|
|
- } else if (in == '\n') {
|
|
|
- done = true;
|
|
|
} else {
|
|
|
input += in;
|
|
|
}
|