123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- #include <IRremote.h>
- const int LOG_LEVEL = 0;
- const int led = LED_BUILTIN;
- const int RECV_PIN = 7;
- String input = "";
- bool done = false;
- IRsend sendr;
- decode_results results;
- String result_code[] = {"", "", ""};
- void setup() {
-
- pinMode(led, OUTPUT);
- digitalWrite(led, LOW);
-
-
- input.reserve(60);
- done = false;
-
-
-
-
-
- Serial.begin(9600);
- while(!Serial) { delay(10); }
- Serial.println("Connected!");
- digitalWrite(led, LOW);
- }
- void sendCode(long code, int bit_len, bool repeat) {
-
- if(repeat) {
- for(int x = 0; x <= 3; x++) {
- sendr.sendNEC(code, bit_len);
- delay(40);
- }
- } else {
-
- sendr.sendNEC(code, bit_len);
- delay(40);
- for(int x = 0; x <= 2; x++) {
- sendr.sendNEC(0xFFFFFFFF, 32);
- delay(40);
- }
- }
- }
- void parse_by_comma(String data) {
-
-
-
-
- result_code[0] = "";
- result_code[1] = "";
- result_code[2] = "";
- int at = 0;
- for(char c : data) {
-
- if(at == 0) {
- if(c != ',') {
-
- result_code[0] += String(c);
- } else {
- at += 1;
- if (LOG_LEVEL >= 2) {
- Serial.print("> ");
-
- Serial.println(result_code[0]);
- }
- }
- } else if(at == 1) {
- if(c != ',') {
-
- result_code[1] += String(c);
- } else {
- at += 1;
- if (LOG_LEVEL >= 2) {
- Serial.print("> ");
-
- Serial.println(result_code[1]);
- }
- }
- } else if(at == 2) {
-
- result_code[2] += String(c);
- if (LOG_LEVEL >= 2) {
- Serial.print("> ");
-
- Serial.println(result_code[2]);
- }
- break;
- }
- }
-
- }
- void parse_order() {
- long code;
- int bit_size;
- bool repeat;
-
- if(result_code[2].toInt() == 1) {
- repeat = true;
- } else {
- repeat = false;
- }
-
-
-
- code = strtol(result_code[0].c_str(), NULL, 16);
-
- bit_size = result_code[1].toInt();
- if (LOG_LEVEL >= 1) {
- Serial.println(code);
- Serial.println(bit_size);
- Serial.println(repeat);
-
- Serial.print(result_code[0]);
- Serial.print(",");
- Serial.print(result_code[1]);
- Serial.print(",");;
- Serial.println(result_code[2]);
- }
-
- if (result_code[0] != "" and result_code[1] != "" and result_code[2] != "") {
- sendCode(code, bit_size, repeat);
- Serial.println("Sent!");
- } else {
- Serial.println("Failed to parse give command.");
- }
- }
- void action(String act){
-
- if(act == "r") {
- parse_order();
- } else {
- parse_by_comma(act);
- parse_order();
- }
-
-
- }
- void loop() {
-
- if(Serial.available() > 0) {
- char in = (char)Serial.read();
- if (LOG_LEVEL >= 3) {
- Serial.print(": ");
- Serial.println(in);
- }
- if (in == '\n') {
- done = true;
- } else {
- input += in;
- }
- }
- if(done){
- action(input);
- input = "";
- done = false;
- }
- }
|