diff --git a/src/TV-B-Gone.cpp b/src/TV-B-Gone.cpp index ed2228f79..f0f685e5e 100644 --- a/src/TV-B-Gone.cpp +++ b/src/TV-B-Gone.cpp @@ -53,6 +53,7 @@ Distributed under Creative Commons 2.5 -- Attribution & Share Alike #include "TV-B-Gone.h" #include "display.h" #include "mykeyboard.h" +#include "sd_functions.h" #include "WORLD_IR_CODES.h" uint8_t read_bits(uint8_t count); @@ -64,6 +65,9 @@ uint16_t rawData[300]; #define putnum_ud(n) Serial.print(n, DEC) #define putnum_uh(n) Serial.print(n, HEX) +#define IR_DATA_BUFFER_SIZE 300 +File databaseFile; + #define MAX_WAIT_TIME 65535 //tens of us (ie: 655.350ms) IRsend irsend(LED); // Set the GPIO to be used to sending the message. @@ -401,3 +405,172 @@ void quickflashLEDx( uint8_t x ) { } } + + + + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Custom IR + +void otherIRcodes() { + //Serial.begin(115200); + String filepath = ""; + int total_codes = 0; + int codes_sent = 0; + filepath = loopSD(true); + tft.fillScreen(BGCOLOR); + drawMainMenu(4); + digitalWrite(LED, LED_ON); + pinMode(LED, OUTPUT); + + databaseFile = SD.open(filepath); + if (!databaseFile) { + Serial.println("Failed to open database file."); + displayError("Fail to open file"); + delay(2000); + return; + } + Serial.println("Opened database file."); + String line; + while (databaseFile.available()) { + line = databaseFile.readStringUntil('\n'); + if(line.startsWith("type:")) total_codes++; + } + databaseFile.seek(0); // comes back to first position + while (databaseFile.available()) { + progressHandler(codes_sent,total_codes); + line = databaseFile.readStringUntil('\n'); + if (line.startsWith("type:")) { + codes_sent++; + String type = line.substring(5); + type.trim(); + Serial.println("Type: "+type); + if (type == "raw") { + Serial.println("RAW"); + int frequency = 0; + String rawData = ""; + while (databaseFile.available()) { + line = databaseFile.readStringUntil('\n'); + if (line.startsWith("frequency:")) { + String frequencyString = line.substring(10); + frequencyString.trim(); + frequency = frequencyString.toInt(); + } else if (line.startsWith("data:")) { + rawData = line.substring(5); + rawData.trim(); + } else if (line.indexOf("#") != -1) { + Serial.println("Frequency: "+frequency); + Serial.println("RawData: "+rawData); + sendRawCommand(frequency, rawData); + rawData = ""; + frequency = 0; + type = ""; + line = ""; + break; + } + } + } else if (type == "parsed") { + String protocol = ""; + String address = ""; + String command = ""; + Serial.println("PARSED"); + while (databaseFile.available()) { + line = databaseFile.readStringUntil('\n'); + if (line.startsWith("protocol:")) { + protocol = line.substring(9); + protocol.trim(); + Serial.println("Protocol: "+protocol); + } else if (line.startsWith("address:")) { + address = line.substring(8); + address.trim(); + Serial.println("Address: "+address); + } else if (line.startsWith("command:")) { + command = line.substring(8); + command.trim(); + Serial.println("Command: "+command); + } else if (line.indexOf("#") != -1) { + if (protocol == "NECext") { + sendNECextCommand(address, command); + } else if (protocol == "NEC") { + sendNECCommand(address, command); + } else if (protocol == "RC5") { + sendRC5Command(address, command); + } else if (protocol == "Samsung") { + sendSamsungCommand(address, command); + } else if (protocol.startsWith("SIRC")) { + sendSonyCommand(address, command); + } + protocol = ""; + address = ""; + command = ""; + type = ""; + line = ""; + break; + } + } + } + } + } + databaseFile.close(); + Serial.println("closed"); + Serial.println("EXTRA finished"); + digitalWrite(LED, LED_OFF); +} + +//IR commands +void sendNECCommand(String address, String command) { + uint32_t addressValue = strtoul(address.c_str(), nullptr, 16); + uint32_t commandValue = strtoul(command.c_str(), nullptr, 16); + irsend.sendNEC(addressValue, commandValue, 32); + Serial.println("Sent1"); +} + +void sendNECextCommand(String address, String command) { + uint32_t addressValue = strtoul(address.c_str(), nullptr, 16); + uint32_t commandValue = strtoul(command.c_str(), nullptr, 16); + irsend.sendNEC(addressValue, commandValue, 32); + Serial.println("Sent2"); +} + +void sendRC5Command(String address, String command) { + uint32_t addressValue = strtoul(address.c_str(), nullptr, 16); + uint32_t commandValue = strtoul(command.c_str(), nullptr, 16); + irsend.sendRC5(addressValue, commandValue, 12); + Serial.println("Sent3"); +} + +void sendSamsungCommand(String address, String command) { + uint64_t data = ((uint64_t)strtoul(address.c_str(), nullptr, 16) << 32) | strtoul(command.c_str(), nullptr, 16); + irsend.sendSamsung36(data, 36); + Serial.println("Sent4"); +} + +void sendSonyCommand(String address, String command) { + uint16_t data = (uint16_t)strtoul(command.c_str(), nullptr, 16); + uint16_t addressValue = (uint16_t)strtoul(address.c_str(), nullptr, 16); + irsend.sendSony(addressValue, data); + Serial.println("Sent5"); +} + +void sendRawCommand(int frequency, String rawData) { + uint16_t dataBuffer[IR_DATA_BUFFER_SIZE]; + int count = 0; + + // Parse raw data string + while (rawData.length() > 0 && count < IR_DATA_BUFFER_SIZE) { + int delimiterIndex = rawData.indexOf(' '); + if (delimiterIndex == -1) { + delimiterIndex = rawData.length(); + } + String dataChunk = rawData.substring(0, delimiterIndex); + rawData.remove(0, delimiterIndex + 1); + dataBuffer[count++] = dataChunk.toInt(); + } + + Serial.println("Parsing raw data complete."); + + // Send raw command + irsend.sendRaw(dataBuffer, count, frequency); + + Serial.println("Sent6"); +} \ No newline at end of file diff --git a/src/TV-B-Gone.h b/src/TV-B-Gone.h index 8d0cd6eba..6dd13585d 100644 --- a/src/TV-B-Gone.h +++ b/src/TV-B-Gone.h @@ -53,7 +53,8 @@ Distributed under Creative Commons 2.5 -- Attribution & Share Alike #include #include #include - +#include +#include //void xmitCodeElement(uint16_t ontime, uint16_t offtime, uint8_t PWM_code ); void quickflashLEDx( uint8_t x ); void delay_ten_us(uint16_t us); @@ -61,3 +62,13 @@ void quickflashLED( void ); void StartTvBGone(); void sendAllCodes(); void chooseRegion(int reg); + + +// custom Ir +void sendRawCommand(int frequency, String rawData); +void sendNECextCommand(String address, String command); +void sendNECCommand(String address, String command); +void sendRC5Command(String address, String command); +void sendSamsungCommand(String address, String command); +void sendSonyCommand(String address, String command); +void otherIRcodes(); \ No newline at end of file diff --git a/src/bad_usb.cpp b/src/bad_usb.cpp index a4c35c559..41e3b64cd 100644 --- a/src/bad_usb.cpp +++ b/src/bad_usb.cpp @@ -225,6 +225,8 @@ void usb_setup() { bad_script = "/badpayload.txt"; bad_script = loopSD(true); + tft.fillScreen(BGCOLOR); + drawMainMenu(4); options = { {"US Inter", [=]() { chooseKb(KeyboardLayout_en_US); }}, diff --git a/src/main.cpp b/src/main.cpp index 4d3661fa8..6a2294ed9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,6 +43,7 @@ TFT_eSprite draw = TFT_eSprite(&tft); #include "mfrc522_i2c.h" #include "TV-B-Gone.h" + #ifdef CARDPUTER #include "bad_usb.h" #endif @@ -216,6 +217,7 @@ void loop() { {"SD Card", [=]() { loopSD(); }}, {"WebUI", [=]() { loopOptionsWebUi(); }}, }; + if(sdcardMounted) options.push_back({"Custom IR", [=]() { otherIRcodes(); }}); #ifdef CARDPUTER options.push_back({"BadUSB", [=]() { usb_setup(); }}); #endif