gc_decode.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Quick and dirty tool to decode GlobalCache (GC) codes
  2. // and ProntoHex codes
  3. // Copyright 2017 Jorge Cisneros
  4. #include <errno.h>
  5. #include <inttypes.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <string>
  9. #include "IRsend.h"
  10. #include "IRsend_test.h"
  11. #include "IRutils.h"
  12. const uint16_t kMaxGcCodeLength = 10000;
  13. void str_to_uint16(char *str, uint16_t *res, uint8_t base) {
  14. char *end;
  15. errno = 0;
  16. intmax_t val = strtoimax(str, &end, base);
  17. if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == str ||
  18. *end != '\0')
  19. return;
  20. *res = (uint16_t)val;
  21. }
  22. void usage_error(char *name) {
  23. std::cerr << "Usage: " << name << " [-raw] <global_code>" << std::endl
  24. << "Usage: " << name << " -prontohex [-raw] <prontohex_code>"
  25. << std::endl;
  26. }
  27. int main(int argc, char *argv[]) {
  28. int argv_offset = 1;
  29. bool dumpraw = false;
  30. bool prontohex = false;
  31. // Check the invocation/calling usage.
  32. if (argc < 2 || argc > 4) {
  33. usage_error(argv[0]);
  34. return 1;
  35. }
  36. if (strncmp("-prontohex", argv[argv_offset], 10) == 0) {
  37. prontohex = true;
  38. argv_offset++;
  39. }
  40. if (strncmp("-raw", argv[argv_offset], 4) == 0) {
  41. dumpraw = true;
  42. argv_offset++;
  43. }
  44. if (argc - argv_offset != 1) {
  45. usage_error(argv[0]);
  46. return 1;
  47. }
  48. uint16_t gc_test[kMaxGcCodeLength];
  49. int index = 0;
  50. char *pch;
  51. char *saveptr1;
  52. char *sep = const_cast<char *>(",");
  53. int codebase = 10;
  54. if (prontohex) {
  55. sep = const_cast<char *>(" ");
  56. codebase = 16;
  57. }
  58. pch = strtok_r(argv[argv_offset], sep, &saveptr1);
  59. while (pch != NULL && index < kMaxGcCodeLength) {
  60. str_to_uint16(pch, &gc_test[index], codebase);
  61. pch = strtok_r(NULL, sep, &saveptr1);
  62. index++;
  63. }
  64. IRsendTest irsend(4);
  65. IRrecv irrecv(4);
  66. irsend.begin();
  67. irsend.reset();
  68. if (prontohex) {
  69. irsend.sendPronto(gc_test, index);
  70. } else {
  71. irsend.sendGC(gc_test, index);
  72. }
  73. irsend.makeDecodeResult();
  74. irrecv.decode(&irsend.capture);
  75. std::cout << "Code length " << index << std::endl
  76. << "Code type " << irsend.capture.decode_type << " ("
  77. << typeToString(irsend.capture.decode_type) << ")" << std::endl
  78. << "Code bits " << irsend.capture.bits << std::endl;
  79. if (hasACState(irsend.capture.decode_type)) {
  80. std::cout << "State value 0x";
  81. for (uint16_t i = 0; i < irsend.capture.bits / 8; i++)
  82. printf("%02X", irsend.capture.state[i]);
  83. std::cout << std::endl;
  84. } else {
  85. std::cout << "Code value 0x" << std::hex << irsend.capture.value
  86. << std::endl
  87. << "Code address 0x" << std::hex << irsend.capture.address
  88. << std::endl
  89. << "Code command 0x" << std::hex << irsend.capture.command
  90. << std::endl;
  91. }
  92. if (dumpraw || irsend.capture.decode_type == UNKNOWN) irsend.dumpRawResult();
  93. return 0;
  94. }