From a0136d7b33e7477f441e35a453fe256391d55329 Mon Sep 17 00:00:00 2001 From: eadmaster <925171+eadmaster@users.noreply.github.com> Date: Tue, 16 Jul 2024 19:24:19 +0200 Subject: [PATCH] fixed sendNEC usage (#64) --- src/TV-B-Gone.cpp | 14 ++++++++--- src/main.cpp | 11 +-------- src/serialcmds.cpp | 60 ++++++++++++++++++++++++++-------------------- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/TV-B-Gone.cpp b/src/TV-B-Gone.cpp index 9e9bf23d..80e8ff2d 100644 --- a/src/TV-B-Gone.cpp +++ b/src/TV-B-Gone.cpp @@ -411,9 +411,17 @@ void sendNECCommand(String address, String command) { IRsend irsend(IrTx,true); // Set the GPIO to be used to sending the message. irsend.begin(); displayRedStripe("Sending..",TFT_WHITE,FGCOLOR); - uint32_t addressValue = strtoul(address.c_str(), nullptr, 16); - uint32_t commandValue = strtoul(command.c_str(), nullptr, 16); - irsend.sendNEC(addressValue, commandValue, 32); + //uint32_t addressValue = strtoul(address.c_str(), nullptr, 16); + //uint32_t commandValue = strtoul(command.c_str(), nullptr, 16); + //irsend.sendNEC(addressValue, commandValue, 32); + uint8_t first_zero_byte_pos = address.indexOf("00", 2); + if(first_zero_byte_pos!=-1) address = address.substring(0, first_zero_byte_pos); + first_zero_byte_pos = command.indexOf("00", 2); + if(first_zero_byte_pos!=-1) command = command.substring(0, first_zero_byte_pos); + uint16_t addressValue = strtoul(address.c_str(), nullptr, 16); + uint16_t commandValue = strtoul(command.c_str(), nullptr, 16); + uint64_t data = irsend.encodeNEC(addressValue, commandValue); + irsend.sendNEC(data, 32, 10); Serial.println("Sent1"); } diff --git a/src/main.cpp b/src/main.cpp index 04f3cba3..274965d2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,10 +75,7 @@ TFT_eSprite draw = TFT_eSprite(&tft); ** Where the devices are started and variables set *********************************************************************/ void setup() { - //Serial.setRxBufferSize(1000); Serial.begin(115200); - //delay(1000); // Waiting for serial monitor to catch up. - //Serial.println("bruce setup"); log_d("Total heap: %d", ESP.getHeapSize()); log_d("Free heap: %d", ESP.getFreeHeap()); @@ -149,13 +146,7 @@ void setup() { if((millis()-i>3400) && (millis()-i)<3600) tft.fillScreen(TFT_BLACK); if((millis()-i>3600)) tft.drawXBitmap(1,1,bits, bits_width, bits_height,TFT_BLACK,FGCOLOR); - //reinit needed? - //Serial.begin(115200); - //Serial.println("setup: serial init1"); - //log_d("setup: serial init2"); - //log_d("setup: serial init2"); - - + #if defined (CARDPUTER) // If any key is pressed, it'll jump the boot screen Keyboard.update(); if(Keyboard.isPressed()) diff --git a/src/serialcmds.cpp b/src/serialcmds.cpp index 991c8ca2..988e32cb 100644 --- a/src/serialcmds.cpp +++ b/src/serialcmds.cpp @@ -49,8 +49,12 @@ void handleSerialCommands() { if(cmd_str.startsWith("ir") ) { + if(IrTx==0) IrTx = 44; // init issue? LED on CARDPUTER + //IRsend irsend(IrTx); //inverted = false + //Serial.println(IrTx); IRsend irsend(IrTx,true); // Set the GPIO to be used to sending the message. + //IRsend irsend(IrTx); //inverted = false irsend.begin(); // ir tx
@@ -68,46 +72,44 @@ void handleSerialCommands() { if(cmd_str.startsWith("ir tx nec ")){ String address = cmd_str.substring(10, 10+8); String command = cmd_str.substring(19, 19+8); - Serial.println(address+","+command); - + //displayRedStripe("Sending..",TFT_WHITE,FGCOLOR); - uint64_t addressValue = strtoul(address.c_str(), nullptr, 16); - uint64_t commandValue = strtoul(command.c_str(), nullptr, 16); + // trim 0s from the right of the string + uint8_t first_zero_byte_pos = address.indexOf("00", 2); + if(first_zero_byte_pos!=-1) address = address.substring(0, first_zero_byte_pos); + first_zero_byte_pos = command.indexOf("00", 2); + if(first_zero_byte_pos!=-1) command = command.substring(0, first_zero_byte_pos); + //Serial.println(address+","+command); + + uint16_t addressValue = strtoul(address.c_str(), nullptr, 16); + uint16_t commandValue = strtoul(command.c_str(), nullptr, 16); uint64_t data = irsend.encodeNEC(addressValue, commandValue); + //Serial.println(addressValue); + //Serial.println(commandValue); SerialPrintHexString(data); irsend.sendNEC(data, 32, 10); - } // TODO: more protocols //if(cmd_str.startsWith("ir tx raw")){ - - if(cmd_str.startsWith("irnec")) { - // irnec 20DF10EF - String dataStr = cmd_str.substring(6, 6+8); - uint64_t data = strtoul(dataStr.c_str(), nullptr, 16); - SerialPrintHexString(data); - IRsend irsend(IrTx); //inverted = false - irsend.begin(); - irsend.sendNEC(data, 32, 10); - - } + if(cmd_str.startsWith("irsend")) { - // tasmota format https://tasmota.github.io/docs/Tasmota-IR/#sending-ir-commands + // tasmota json command https://tasmota.github.io/docs/Tasmota-IR/#sending-ir-commands // e.g. IRSend {"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF"} cJSON *root = cJSON_Parse(cmd_str.c_str() + 6); if (root == NULL) { Serial.println("This is NOT json format"); return; } - int bits = 32; + uint16_t bits = 32; // defaults to 32 bits const char *dataStr = ""; + String protocolStr = "nec"; // defaults to NEC protocol cJSON * protocolItem = cJSON_GetObjectItem(root,"protocol"); cJSON * dataItem = cJSON_GetObjectItem(root, "data"); cJSON * bitsItem = cJSON_GetObjectItem(root,"bits"); - //if(protocolItem) ... + if(protocolItem && cJSON_IsString(protocolItem)) protocolStr = protocolItem->valuestring; if(bitsItem && cJSON_IsNumber(bitsItem)) bits = bitsItem->valueint; if(dataItem && cJSON_IsString(dataItem)) { dataStr = dataItem->valuestring; @@ -117,15 +119,21 @@ void handleSerialCommands() { } //String dataStr = cmd_str.substring(36, 36+8); uint64_t data = strtoul(dataStr, nullptr, 16); - Serial.println(dataStr); - SerialPrintHexString(data); - Serial.println(bits); - + //Serial.println(dataStr); + //SerialPrintHexString(data); + //Serial.println(bits); + //Serial.println(protocolItem->valuestring); + cJSON_Delete(root); - - // sendNEC(uint64_t data, uint16_t nbits, uint16_t repeat) - irsend.sendNEC(data, bits, 10); + + if(protocolStr == "nec"){ + // sendNEC(uint64_t data, uint16_t nbits, uint16_t repeat) + irsend.sendNEC(data, bits, 10); + } + // TODO: more protocols + } + // turn off the led digitalWrite(IrTx, LED_OFF); //backToMenu(); return;