Skip to content

Commit

Permalink
Merge pull request #757 from Lamnxzp/ir_kaseikyo
Browse files Browse the repository at this point in the history
Add Kaseikyo Protocol (Custom IR)
  • Loading branch information
pr3y authored Feb 1, 2025
2 parents 689e711 + f8fc648 commit 23a3115
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
64 changes: 46 additions & 18 deletions src/modules/ir/custom_ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include "TV-B-Gone.h" // for checkIrTxPin()
#include <IRutils.h>

uint32_t swap32(uint32_t value) {
return ((value & 0x000000FF) << 24) |
((value & 0x0000FF00) << 8) |
((value & 0x00FF0000) >> 8) |
((value & 0xFF000000) >> 24);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Custom IR
Expand Down Expand Up @@ -267,7 +273,7 @@ void otherIRcodes() {
{"LittleFS", [&]() { fs=&LittleFS; }},
{"Menu", []() { }},
};
if(setupSdCard()) options.push_back({"SD Card", [&]() { fs=&SD; }});
if(setupSdCard()) options.insert(options.begin() + 1, {"SD Card", [&]() { fs=&SD; }});

loopOptions(options);

Expand Down Expand Up @@ -377,7 +383,7 @@ void sendIRCommand(IRCode *code) {
else if(code->protocol=="RC6") sendRC6Command(code->address, code->command);
else if(code->protocol=="Samsung32") sendSamsungCommand(code->address, code->command);
else if(code->protocol.startsWith("SIRC")) sendSonyCommand(code->address, code->command);
else if(code->protocol=="Kaseikyo" || code->protocol=="Panasonic") sendPanasonicCommand(code->address, code->command);
else if(code->protocol=="Kaseikyo") sendKaseikyoCommand(code->address, code->command);
else if(code->protocol!="") sendDecodedCommand(code->protocol, code->data, code->bits);
}

Expand Down Expand Up @@ -487,29 +493,51 @@ void sendSonyCommand(String address, String command) {
digitalWrite(bruceConfig.irTx, LED_OFF);
}

void sendPanasonicCommand(String address, String command) {
void sendKaseikyoCommand(String address, String command) {
IRsend irsend(bruceConfig.irTx); // Set the GPIO to be used to sending the message.
irsend.begin();
displayTextLine("Sending..");
uint8_t first_zero_byte_pos = address.indexOf("00", 2);
if(first_zero_byte_pos!=-1) address = address.substring(0, first_zero_byte_pos);
// needs to invert endianess
// address: "D3 C4 00 00" -> "C4 D3 00 00"
address = address.substring(3,4) + " " + address.substring(0,1) + " 00 00";
// command: "02 00 40 64" -> "64 40 00 02"
command = command.substring(9,10) + " " + command.substring(6,7) + " " + command.substring(3,4) + " " + command.substring(0,1);


address.replace(" ", "");
command.replace(" ", "");

uint16_t addressValue = strtoul(address.c_str(), nullptr, 16);
uint32_t addressValue = strtoul(address.c_str(), nullptr, 16);
uint32_t commandValue = strtoul(command.c_str(), nullptr, 16);
Serial.println(addressValue);
Serial.println(commandValue);
irsend.sendPanasonic(addressValue, commandValue, 48, 10);
// sendPanasonic(const uint16_t address, const uint32_t data, const uint16_t nbits = kPanasonicBits, const uint16_t repeat = kNoRepeat);
delay(20);
Serial.println("Sent Panasonic Command");

uint32_t newAddress = swap32(addressValue);
uint32_t newCommand = swap32(commandValue);

uint8_t id = (newAddress >> 24) & 0xFF;
uint16_t vendor_id = (newAddress >> 8) & 0xFFFF;
uint8_t genre1 = (newAddress >> 4) & 0x0F;
uint8_t genre2 = newAddress & 0x0F;

uint16_t data = newCommand & 0x3FF;

byte bytes[6];
bytes[0] = vendor_id & 0xFF;
bytes[1] = (vendor_id >> 8) & 0xFF;

uint8_t vendor_parity = bytes[0] ^ bytes[1];
vendor_parity = (vendor_parity & 0xF) ^ (vendor_parity >> 4);

bytes[2] = (genre1 << 4) | (vendor_parity & 0x0F);
bytes[3] = ((data & 0x0F) << 4) | genre2;
bytes[4] = ((id & 0x03) << 6) | ((data >> 4) & 0x3F);

bytes[5] = bytes[2] ^ bytes[3] ^ bytes[4];

uint64_t lsb_data = 0;
for (int i = 0; i < 6; i++) {
lsb_data |= (uint64_t)bytes[i] << (8 * i);
}

// LSB First --> MSB First
uint64_t msb_data = reverseBits(lsb_data, 48);

irsend.sendPanasonic64(msb_data, 48); // Sends MSB First

Serial.println("Sent Kaseikyo Command");
digitalWrite(bruceConfig.irTx, LED_OFF);
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/ir/custom_ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@



// custom Ir
// Custom IR
void sendRawCommand(uint16_t frequency, String rawData);
void sendNECCommand(String address, String command);
void sendNECextCommand(String address, String command);
void sendRC5Command(String address, String command);
void sendRC6Command(String address, String command);
void sendSamsungCommand(String address, String command);
void sendSonyCommand(String address, String command);
void sendPanasonicCommand(String address, String data);
void sendKaseikyoCommand(String address, String command);
bool sendDecodedCommand(String protocol, String value, uint8_t bits=32);
void otherIRcodes();
bool txIrFile(FS *fs, String filepath);

0 comments on commit 23a3115

Please sign in to comment.