Skip to content

Commit

Permalink
Added support to decode ProntoHex (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecis authored and crankyoldgit committed Jan 9, 2018
1 parent fe0fb62 commit 5a1a348
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions tools/gc_decode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Quick and dirty tool to decode GlobalCache (GC) codes
// and ProntoHex codes
// Copyright 2017 Jorge Cisneros

#include <errno.h>
Expand All @@ -12,29 +13,36 @@

#define MAX_GC_CODE_LENGTH 10000

void str_to_uint16(char *str, uint16_t *res) {
void str_to_uint16(char *str, uint16_t *res, uint8_t base) {
char *end;
errno = 0;
intmax_t val = strtoimax(str, &end, 10);
intmax_t val = strtoimax(str, &end, base);
if (errno == ERANGE || val < 0 || val > UINT16_MAX ||
end == str || *end != '\0')
return;
*res = (uint16_t) val;
}

void usage_error(char * name) {
std::cerr << "Usage: " << name << " [-raw] <global_code>" << std::endl;
std::cerr << "Usage: " << name << " [-raw] <global_code>" << std::endl
<< "Usage: " << name << " -prontohex [-raw] <prontohex_code>" << std::endl;
}

int main(int argc, char * argv[]) {
int argv_offset = 1;
bool dumpraw = false;
bool prontohex = false;

// Check the invocation/calling usage.
if (argc < 2 || argc > 3) {
if (argc < 2 || argc > 4) {
usage_error(argv[0]);
return 1;
}
if (strncmp("-prontohex", argv[argv_offset], 10) == 0) {
prontohex = true;
argv_offset++;
}

if (strncmp("-raw", argv[argv_offset], 4) == 0) {
dumpraw = true;
argv_offset++;
Expand All @@ -48,11 +56,18 @@ int main(int argc, char * argv[]) {
int index = 0;
char *pch;
char *saveptr1;
char *sep = const_cast<char *>(",");
int codebase = 10;

pch = strtok_r(argv[argv_offset], ",", &saveptr1);
if (prontohex) {
sep = const_cast<char *>(" ");
codebase = 16;
}

pch = strtok_r(argv[argv_offset], sep, &saveptr1);
while (pch != NULL && index < MAX_GC_CODE_LENGTH) {
str_to_uint16(pch, &gc_test[index]);
pch = strtok_r(NULL, ",", &saveptr1);
str_to_uint16(pch, &gc_test[index], codebase);
pch = strtok_r(NULL, sep, &saveptr1);
index++;
}

Expand All @@ -61,11 +76,15 @@ int main(int argc, char * argv[]) {
irsend.begin();
irsend.reset();

irsend.sendGC(gc_test, index);
if (prontohex) {
irsend.sendPronto(gc_test, index);
} else {
irsend.sendGC(gc_test, index);
}
irsend.makeDecodeResult();
irrecv.decode(&irsend.capture);

std::cout << "Code GC length " << index << std::endl
std::cout << "Code length " << index << std::endl
<< "Code type " << irsend.capture.decode_type
<< " (" << typeToString(irsend.capture.decode_type) << ")" << std::endl
<< "Code bits " << irsend.capture.bits << std::endl;
Expand Down

0 comments on commit 5a1a348

Please sign in to comment.