Skip to content

Commit

Permalink
fixed sendNEC usage (pr3y#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Jul 16, 2024
1 parent 3fd6323 commit a0136d7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
14 changes: 11 additions & 3 deletions src/TV-B-Gone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
11 changes: 1 addition & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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())
Expand Down
60 changes: 34 additions & 26 deletions src/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <protocol> <address> <command>
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit a0136d7

Please sign in to comment.