InfraTrans.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <IRremote.h>
  2. const int led = LED_BUILTIN; // On board LED, Pin 13
  3. const int RECV_PIN = 7; // IR Receiver
  4. //String input = "";
  5. long code = 0;
  6. int count = 0;
  7. bool repeat = false;
  8. int parse_item = 0;
  9. const int CODE_DELAY = 30;
  10. void reset_code() {
  11. code = 0;
  12. count = 0;
  13. repeat = false;
  14. parse_item = 0;
  15. }
  16. const char hexadecimal[] = "0123456789ABCDEF";
  17. int hextoi(char hexch) {
  18. char *pos = strchr(hexadecimal, toupper(hexch));
  19. if (pos == NULL) {
  20. printf("WARN: Invalid hex digit [%c/%d]\n", hexch, hexch);
  21. return 0;
  22. }
  23. return (pos - hexadecimal);
  24. }
  25. IRsend sendr; // Make Send Object, DANGER a fixed pin number is always used! (3)
  26. //IRrecv recvr(RECV_PIN); // Make Recever Object
  27. //decode_results results; // Make Results Object to store received codes
  28. //String result_code[] = {"", "", ""};
  29. void setup() {
  30. // Initalize on board LED
  31. pinMode(led, OUTPUT);
  32. digitalWrite(led, LOW);
  33. // Initalize input routine
  34. //input.reserve(60); // Max input of chars, to merge into string
  35. reset_code();
  36. // Initalize IR connections
  37. //recvr.enableIRIn();
  38. //recvr.blink13(false); // Disabled Pin 13 blinking on IR signal received
  39. // Initalize Output routine
  40. Serial.begin(9600); // Serial COM's Port
  41. while(!Serial) { delay(10); } // Wait for conection
  42. Serial.println("Ready!");
  43. digitalWrite(led, LOW);
  44. }
  45. void sendCode(long code, int bit_len, bool repeat) {
  46. // If repeat the code then repeat it 3 times
  47. if(repeat) {
  48. for(int x = 0; x <= 3; x++) {
  49. sendr.sendNEC(code, bit_len);
  50. delay(CODE_DELAY);
  51. }
  52. } else {
  53. // Else send the code then repeat a blank 2 times
  54. sendr.sendNEC(code, bit_len);
  55. delay(CODE_DELAY);
  56. for(int x = 0; x <= 2; x++) {
  57. sendr.sendNEC(0xFFFFFFFF, 32);
  58. delay(CODE_DELAY);
  59. }
  60. }
  61. }
  62. void loop() {
  63. //reset_code();
  64. // Are we sending/receiving data?
  65. while(Serial.available()) {
  66. char in = (char)Serial.read();
  67. #ifdef DEBUG_OUT
  68. Serial.print(": ");
  69. Serial.println(in);
  70. #endif
  71. if (in == ',') {
  72. parse_item++;
  73. if (parse_item > 2) {
  74. Serial.println("Error: Too many items!");
  75. while (Serial.read() != '\n') {
  76. }
  77. reset_code();
  78. break;
  79. }
  80. }
  81. if (in == '\n') {
  82. if (parse_item == 2) {
  83. Serial.print("Received: ");
  84. Serial.print(code, HEX);
  85. Serial.print(", ");
  86. Serial.print(count);
  87. Serial.print(", ");
  88. Serial.println(repeat);
  89. Serial.println("Sending...");
  90. sendCode(code, count, repeat);
  91. Serial.println("Ok");
  92. } else {
  93. Serial.print("Error: Missing items, I saw ");
  94. Serial.println(parse_item + 1);
  95. break;
  96. }
  97. reset_code();
  98. continue;
  99. }
  100. switch (parse_item) {
  101. case 0:
  102. #ifdef DEBUG_OUT
  103. Serial.print("Before: ");
  104. Serial.print(code, HEX);
  105. Serial.print(" ");
  106. Serial.print(in);
  107. Serial.println(hextoi(in));
  108. #endif
  109. code = code << 4;
  110. #ifdef DEBUG_OUT
  111. Serial.print("After: ");
  112. Serial.println(code, HEX);
  113. #endif
  114. code += hextoi(in);
  115. #ifdef DEBUG_OUT
  116. Serial.print("After: ");
  117. Serial.println(code, HEX);
  118. #endif
  119. break;
  120. case 1:
  121. count *= 10;
  122. count += hextoi(in);
  123. break;
  124. case 2:
  125. repeat *= 10;
  126. repeat += hextoi(in);
  127. break;
  128. }
  129. }
  130. }