Translator.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (C) 2018 Bengt Martensson.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at
  6. your option) any later version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program. If not, see http://www.gnu.org/licenses/.
  13. */
  14. #include "config.h"
  15. #include "GirsLib/LedLcdManager.h"
  16. #include "GirsLib/GirsUtils.h"
  17. #include <IrReceiverSampler.h>
  18. #include <IrSenderPwm.h>
  19. #include <Nec1Decoder.h>
  20. #include <Rc5Renderer.h>
  21. #ifdef DEBUG
  22. Stream& stream = Serial;
  23. #endif
  24. static IrReceiver *irReceiver = NULL;
  25. static IrSender *sender = NULL;
  26. #ifndef PROGNAME
  27. #define PROGNAME "Translator"
  28. #endif // ! PROGNAME
  29. #ifndef VERSION
  30. #include "GirsLib/version.h"
  31. #endif // VERSION
  32. static void transmit(const char *name, unsigned int D, unsigned int F, unsigned int count = 1) {
  33. LedLcdManager::lcdPrint("Command: " + String(name));
  34. #ifdef DEBUG
  35. stream.println("Command: " + String(name));
  36. #endif
  37. const IrSignal *signal = Rc5Renderer::newIrSignal(D, F);
  38. #ifdef TRANSMITLED
  39. LedLcdManager::setLogicLed(TRANSMITLED, LedLcdManager::on);
  40. #endif
  41. sender->sendIrSignal(*signal, count);
  42. #ifdef TRANSMITLED
  43. LedLcdManager::setLogicLed(TRANSMITLED, LedLcdManager::off);
  44. #endif
  45. delete(signal);
  46. }
  47. static void action() {
  48. Nec1Decoder decoder(*irReceiver);
  49. if (decoder.isValid() && !decoder.isDitto()) {
  50. int D = decoder.getD();
  51. int S = decoder.getS();
  52. int F = decoder.getF();
  53. #ifdef DEBUG
  54. stream.println("NEC1 " + String(D) + "/" + String(S) + "/" + String(F));
  55. #endif
  56. if (D == RECV_NEC1_DEVICE && S == RECV_NEC1_SUBDEVICE) {
  57. switch (decoder.getF()) {
  58. case RECV_VOLUME_UP_CMD:
  59. transmit("UP", SEND_DEVICE, SEND_VOLUME_UP_CMD);
  60. break;
  61. case RECV_VOLUME_DOWN_CMD:
  62. transmit("DOWN", SEND_DEVICE, SEND_VOLUME_DOWN_CMD);
  63. break;
  64. default:
  65. LedLcdManager::lcdPrint("Unknown: " + String(decoder.getF()));
  66. #ifdef DEBUG
  67. stream.println("Unknown command: " + String(decoder.getF()));
  68. #endif
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. void setup() {
  75. LedLcdManager::setupLedGroundPins();
  76. GirsUtils::setupReceivers();
  77. GirsUtils::setupLeds();
  78. LedLcdManager::setup(LCD_I2C_ADDRESS, LCD_WIDTH, LCD_HEIGHT,
  79. (const pin_t[]) {SIGNAL_LED_1, SIGNAL_LED_2, SIGNAL_LED_3, SIGNAL_LED_4,
  80. SIGNAL_LED_5, SIGNAL_LED_6, SIGNAL_LED_7, SIGNAL_LED_8 });
  81. LedLcdManager::selfTest(F(PROGNAME " " VERSION));
  82. LedLcdManager::lcdPrint(F("*************"), false, 0, 1);
  83. #ifdef LED
  84. LedLcdManager::setupShouldTimeout(RECEIVELED, false);
  85. LedLcdManager::setupShouldTimeout(TRANSMITLED, false);
  86. #endif
  87. #ifdef DEBUG
  88. Serial.begin(SERIALBAUD);
  89. Serial.setTimeout(SERIALTIMEOUT);
  90. #if defined(ARDUINO_AVR_LEONARDO) | defined(ARDUINO_AVR_MICRO)
  91. while (!Serial)
  92. ; // wait for serial port to connect. "Needed for Leonardo only"
  93. #endif
  94. stream.println(F(PROGNAME " " VERSION));
  95. #endif // DEBUG
  96. irReceiver = IrReceiverSampler::newIrReceiverSampler(CAPTURESIZE, IRRECEIVER_1_PIN,
  97. IRRECEIVER_1_PULLUP_VALUE, IRRECEIVER_MARK_EXCESS, BEGINTIMEOUT, ENDINGTIMEOUT);
  98. irReceiver->setEndingTimeout(ENDINGTIMEOUT);
  99. irReceiver->setBeginningTimeout(BEGINTIMEOUT);
  100. sender = IrSenderPwm::getInstance(true);
  101. }
  102. static void read() {
  103. #ifdef RECEIVELED
  104. LedLcdManager::setLogicLed(RECEIVELED, LedLcdManager::on);
  105. #endif
  106. irReceiver->enable();
  107. while (!irReceiver->isReady())
  108. LedLcdManager::checkTurnoff();
  109. irReceiver->disable();
  110. #ifdef RECEIVELED
  111. LedLcdManager::setLogicLed(RECEIVELED, LedLcdManager::off);
  112. #endif
  113. }
  114. static void loop() {
  115. read();
  116. action();
  117. }