From 7e56bca92992f3061870fb306e5d4533f1d5a95f Mon Sep 17 00:00:00 2001 From: eadmaster <925171+eadmaster@users.noreply.github.com> Date: Tue, 24 Sep 2024 08:30:23 +0200 Subject: [PATCH] fixed ir raw reading, esp32-s3-devkitc-1 build, added support for ir signals with state (#216) --- src/core/VectorDisplay.h | 3 ++ src/core/passwords.cpp | 4 +-- src/modules/ir/TV-B-Gone.cpp | 40 +++++++++++++++++------ src/modules/ir/ir_read.cpp | 62 +++++++++++++++++++++++++----------- src/modules/ir/ir_read.h | 3 +- 5 files changed, 80 insertions(+), 32 deletions(-) diff --git a/src/core/VectorDisplay.h b/src/core/VectorDisplay.h index b5f88487e..aeb6cc0e4 100755 --- a/src/core/VectorDisplay.h +++ b/src/core/VectorDisplay.h @@ -1310,6 +1310,9 @@ class SerialDisplayClass : public VectorDisplayClass { VectorDisplayClass::begin(width, height); } + bool getSwapBytes(void) { return false; } // stub + void setSwapBytes(bool swap) { return; } // stub + virtual void begin(int width=VECTOR_DISPLAY_DEFAULT_WIDTH, int height=VECTOR_DISPLAY_DEFAULT_HEIGHT) override { begin(115200, width, height); } diff --git a/src/core/passwords.cpp b/src/core/passwords.cpp index b12c28416..12aa302d2 100644 --- a/src/core/passwords.cpp +++ b/src/core/passwords.cpp @@ -6,7 +6,7 @@ #include "globals.h" #include "sd_functions.h" #include "mykeyboard.h" - +#include "modules/rf/rf.h" //for hexCharToDecimal String xorEncryptDecryptMD5(const String &input, const String &password, const int MD5_PASSES) { @@ -73,8 +73,6 @@ String readDecryptedFileOLD(FS &fs, String filepath) { return plaintext; } */ - -#include "modules/rf/rf.h" //for hexCharToDecimal String readDecryptedFile(FS &fs, String filepath) { diff --git a/src/modules/ir/TV-B-Gone.cpp b/src/modules/ir/TV-B-Gone.cpp index a747fea63..5442b7bd1 100644 --- a/src/modules/ir/TV-B-Gone.cpp +++ b/src/modules/ir/TV-B-Gone.cpp @@ -15,6 +15,7 @@ Distributed under Creative Commons 2.5 -- Attribution & Share Alike #include "core/settings.h" #include "WORLD_IR_CODES.h" #include "TV-B-Gone.h" +#include "modules/rf/rf.h" //for hexCharToDecimal #include char16_t FGCOLOR; @@ -292,6 +293,7 @@ bool txIrFile(FS *fs, String filepath) { String address = ""; String command = ""; String value = ""; + String state = ""; String bits = "32"; databaseFile.seek(0); // comes back to first position @@ -357,8 +359,8 @@ bool txIrFile(FS *fs, String filepath) { command = line.substring(8); command.trim(); Serial.println("Command: "+command); - } else if (line.startsWith("value:")) { - value = line.substring(strlen("value:")); + } else if (line.startsWith("value:") || line.startsWith("state:")) { + value = line.substring(6); value.trim(); Serial.println("Value: "+value); } else if (line.startsWith("bits:")) { @@ -386,6 +388,7 @@ bool txIrFile(FS *fs, String filepath) { command = ""; protocol = ""; value = ""; + state = ""; type = ""; line = ""; break; @@ -508,7 +511,7 @@ void otherIRcodes() { if(line.startsWith("frequency:")) codes[total_codes].frequency = txt.toInt(); //if(line.startsWith("duty_cycle:")) codes[total_codes].duty_cycle = txt.toFloat(); if(line.startsWith("command:")) { codes[total_codes].command = txt; total_codes++; } - if(line.startsWith("data:") || line.startsWith("value:")) { codes[total_codes].data = txt; total_codes++; } + if(line.startsWith("data:") || line.startsWith("value:") || line.startsWith("state:")) { codes[total_codes].data = txt; total_codes++; } } options = { }; bool exit = false; @@ -636,15 +639,34 @@ bool sendDecodedCommand(String protocol, String value, String bits) { decode_type_t type = strToDecodeType(protocol.c_str()); if(type == decode_type_t::UNKNOWN) return false; - value.replace(" ", ""); - uint64_t value_int = strtoull(value.c_str(), nullptr, 16); uint16_t nbit_int = bits.toInt(); - displayRedStripe("Sending..",TFT_WHITE,FGCOLOR); - IRsend irsend(IrTx); // Set the GPIO to be used to sending the message. irsend.begin(); - bool success = irsend.send(type, value_int, nbit_int); // bool send(const decode_type_t type, const uint64_t data, const uint16_t nbits, const uint16_t repeat = kNoRepeat); + bool success = false; + displayRedStripe("Sending..",TFT_WHITE,FGCOLOR); + + if(hasACState(type)) { + // need to send the state (passed from value) + uint8_t state[nbit_int / 8] = {0}; + uint16_t state_pos = 0; + for (uint16_t i = 0; i < value.length(); i += 3) { + // parse value -> state + uint8_t highNibble = hexCharToDecimal(value[i]); + uint8_t lowNibble = hexCharToDecimal(value[i + 1]); + state[state_pos] = (highNibble << 4) | lowNibble; + state_pos++; + } + //success = irsend.send(type, state, nbit_int / 8); + success = irsend.send(type, state, state_pos); // safer + + } else { + + value.replace(" ", ""); + uint64_t value_int = strtoull(value.c_str(), nullptr, 16); + + success = irsend.send(type, value_int, nbit_int); // bool send(const decode_type_t type, const uint64_t data, const uint16_t nbits, const uint16_t repeat = kNoRepeat); + } delay(20); Serial.println("Sent Decoded Command"); @@ -667,7 +689,7 @@ void sendRawCommand(uint16_t frequency, String rawData) { } String dataChunk = rawData.substring(0, delimiterIndex); rawData.remove(0, delimiterIndex + 1); - dataBuffer[count++] = (dataChunk.toInt())/2; + dataBuffer[count++] = (dataChunk.toInt()); } Serial.println("Parsing raw data complete."); diff --git a/src/modules/ir/ir_read.cpp b/src/modules/ir/ir_read.cpp index 5405d49db..113f8717e 100644 --- a/src/modules/ir/ir_read.cpp +++ b/src/modules/ir/ir_read.cpp @@ -32,6 +32,17 @@ String uint32ToString(uint32_t value) { return String(buffer); } +String uint32ToStringInverted(uint32_t value) { + char buffer[12] = {0}; // 8 hex digits + 3 spaces + 1 null terminator + snprintf(buffer, sizeof(buffer), "%02X %02X %02X %02X", + (value >> 24) & 0xFF, + (value >> 16) & 0xFF, + (value >> 8) & 0xFF, + value & 0xFF); + return String(buffer); +} + + IrRead::IrRead(bool headless_mode, bool raw_mode) { headless = headless_mode; @@ -158,6 +169,18 @@ void IrRead::save_signal() { } +String IrRead::parse_state_signal() { + String r = ""; + uint16_t state_len = (results.bits) / 8; + for (uint16_t i = 0; i < state_len; i++) { + //r += uint64ToString(results.state[i], 16) + " "; + r += ((results.state[i] < 0x10) ? "0" : ""); // adds 0 padding if necessary + r += String(results.state[i], HEX) + " "; + } + r.toUpperCase(); + return r; +} + String IrRead::parse_raw_signal() { //rawcode = new uint16_t[MAX_RAWBUF_SIZE]; //memset(rawcode, 0, MAX_RAWBUF_SIZE * sizeof(uint16_t)); @@ -165,26 +188,17 @@ String IrRead::parse_raw_signal() { // https://github.com/crankyoldgit/IRremoteESP8266/blob/master/examples/SmartIRRepeater/SmartIRRepeater.ino rawcode = resultToRawArray(&results); - // Find out how many elements are in the array. raw_data_len = getCorrectedRawLength(&results); String signal_code = ""; - //String signal_code2 = ""; - - // I HAVE NO FUCKING IDEA WHY WE NEED TO MULTIPLY BY 2, BUT WE DO. - for (uint16_t i = 1; i < raw_data_len; i++) { - //signal_code2 += String(results.rawbuf[i] * 2) + " "; - //rawcode[i - 1] = results.rawbuf[i] * 2; - //Serial.println(results.rawbuf[i]); - //Serial.println(rawcode[i]); - signal_code += String(rawcode[i] * 2) + " "; + + for (uint16_t i = 0; i < raw_data_len; i++) { + signal_code += String(rawcode[i]) + " "; } delete[] rawcode; rawcode = nullptr; signal_code.trim(); - //Serial.println(raw_data_len); - //Serial.println("data2 " + signal_code2); return signal_code; } @@ -254,19 +268,20 @@ void IrRead::append_to_file_str(String btn_name) { break; } } - // + strDeviceContent += "address: " + uint32ToString(results.address) + "\n"; strDeviceContent += "command: " + uint32ToString(results.command) + "\n"; // extra fields not supported on flipper - if(results.bits>32) - strDeviceContent += "value: " + uint32ToString(results.value >> 32) + " " + uint32ToString(results.value) + "\n"; // MEMO: from uint64_t - else - strDeviceContent += "value: " + uint32ToString(results.value) + "\n"; strDeviceContent += "bits: " + String(results.bits) + "\n"; - + if(hasACState(results.decode_type)) + strDeviceContent += "state: " + parse_state_signal() + "\n"; + else if(results.bits>32) + strDeviceContent += "value: " + uint32ToString(results.value) + " " + uint32ToString(results.value>> 32) + "\n"; // MEMO: from uint64_t + else + strDeviceContent += "value: " + uint32ToStringInverted(results.value) + "\n"; + /* - Serial.println("IR results:"); Serial.println(results.bits); Serial.println(results.address); Serial.println(results.command); @@ -275,6 +290,15 @@ void IrRead::append_to_file_str(String btn_name) { Serial.println("value:"); serialPrintUint64(results.address, HEX); serialPrintUint64(results.command, HEX); + * + Serial.print("resultToHexidecimal: "); + Serial.println(resultToHexidecimal(&results)); // 0x20DFC03F + Serial.println(results.value); + Serial.println(); + String value = uint32ToString(results.value ) + " " + uint32ToString(results.value>> 32); + value.replace(" ", ""); + uint64_t value_int = strtoull(value.c_str(), nullptr, 16); + Serial.println(value_int); */ } strDeviceContent += "#\n"; diff --git a/src/modules/ir/ir_read.h b/src/modules/ir/ir_read.h index d06bc1e7c..3f637f956 100644 --- a/src/modules/ir/ir_read.h +++ b/src/modules/ir/ir_read.h @@ -13,7 +13,7 @@ class IrRead { public: //IRrecv irrecv = IRrecv(IrRx); - IRrecv irrecv = IRrecv(IrRx, SAFE_STACK_BUFFER_SIZE, 50); + IRrecv irrecv = IRrecv(IrRx, SAFE_STACK_BUFFER_SIZE/2, 50); ///////////////////////////////////////////////////////////////////////////////////// @@ -57,4 +57,5 @@ class IrRead { void append_to_file_str(String btn_name); bool write_file(String filename, FS* fs); String parse_raw_signal(); + String parse_state_signal(); };