Skip to content

Commit

Permalink
Merge pull request #5 from bmorcelli/main
Browse files Browse the repository at this point in the history
customIr
  • Loading branch information
pr3y authored May 7, 2024
2 parents eceb24f + c087d5b commit d53a9b8
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 1 deletion.
173 changes: 173 additions & 0 deletions src/TV-B-Gone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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");
}
13 changes: 12 additions & 1 deletion src/TV-B-Gone.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,22 @@ Distributed under Creative Commons 2.5 -- Attribution & Share Alike
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>

#include <FS.h>
#include <SD.h>
//void xmitCodeElement(uint16_t ontime, uint16_t offtime, uint8_t PWM_code );
void quickflashLEDx( uint8_t x );
void delay_ten_us(uint16_t us);
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();
2 changes: 2 additions & 0 deletions src/bad_usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }},
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d53a9b8

Please sign in to comment.