Opponator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright (C) 2015 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. /*
  15. * This is more of an example than a program that is expected to be useful as-is.
  16. * It is a sort-of IR to serial translator.
  17. * It listens to an IR receiver, and transmits serial commands to an OPPO BluRay (or DVD) player,
  18. * (see http://download.oppodigital.com/BDP103/BDP103_RS232_Protocol_v1.2.2.pdf)
  19. * It can also issue query commands, and display the result on an LCD screen.
  20. */
  21. #include "config.h"
  22. #include <Arduino.h>
  23. #include <IrReceiverSampler.h>
  24. #include <Nec1Decoder.h>
  25. #include "GirsLib/LedLcdManager.h"
  26. #include "GirsLib/GirsUtils.h"
  27. #ifndef LCD
  28. #error LCD is presently required
  29. #endif
  30. #ifdef ARDUINO
  31. //#ifdef ARDUINO_AVR_MEGA2560
  32. //Stream& stream = Serial1;
  33. //#else // !ARDUINO_AVR_MEGA2560
  34. Stream& stream = Serial;
  35. //#endif // ! ARDUINO_AVR_MEGA2560
  36. #else // ! ARDUINO
  37. Stream stream(std::cout);
  38. #endif // ! ARDUINO
  39. #ifndef PROGNAME
  40. #define PROGNAME "Opponator"
  41. #endif
  42. #ifndef VERSION
  43. #include "GirsLib/version.h"
  44. #endif
  45. static IrReceiverSampler* irReceiver;
  46. void send(String payload) {
  47. stream.print("#" + payload + "\r");
  48. }
  49. void sendReceiveDisplay(String payload, String title) {
  50. send(payload);
  51. LedLcdManager::lcdPrint(title, true);
  52. #ifdef ARDUINO
  53. String answer = stream.readStringUntil('\r');
  54. answer.trim();
  55. LedLcdManager::lcdPrint(answer, false, 0, 1);
  56. #endif
  57. }
  58. void action(IrReader *irReader) {
  59. Nec1Decoder decoder(*irReader);
  60. if (decoder.isValid() && ! decoder.isDitto()
  61. && decoder.getD() == selectedD
  62. && decoder.getS() == selectedS) {
  63. LedLcdManager::lcdPrint("Signal " +
  64. #ifdef ARDUINO
  65. String(decoder.getF())
  66. #else
  67. std::to_string(decoder.getF())
  68. #endif
  69. );
  70. switch (decoder.getF()) {
  71. case 6: // Play
  72. send("PLA");
  73. break;
  74. case 86://14: // Power
  75. send("POW");
  76. break;
  77. case 18: // Goto
  78. sendReceiveDisplay("QEL", "Tot elapsed");
  79. break;
  80. case 26: // Eject
  81. send("EJT");
  82. break;
  83. case 64: // Title
  84. sendReceiveDisplay("QTK", "Title playing");
  85. break;
  86. case 80: // Display
  87. sendReceiveDisplay("QVR", "FW version"); // Query firmware version
  88. break;
  89. case 92: // ENTER
  90. break;
  91. default:
  92. // ...
  93. break;
  94. }
  95. }
  96. #ifdef DEBUG
  97. else {
  98. irReader->dump(stream);
  99. stream.println(decoder.isValid() ? "valid" : "invalid");
  100. stream.println(decoder.getD());
  101. stream.println(decoder.getS());
  102. stream.println(decoder.getF());
  103. }
  104. #endif
  105. }
  106. void loop() {
  107. irReceiver->enable();
  108. while (!irReceiver->isReady()) {
  109. LedLcdManager::checkTurnoff();
  110. }
  111. irReceiver->disable();
  112. action(irReceiver);
  113. }
  114. void setup() {
  115. LedLcdManager::setup(LCD_I2C_ADDRESS, LCD_WIDTH, LCD_HEIGHT);
  116. GirsUtils::setupReceivers();
  117. LedLcdManager::lcdPrint(F(PROGNAME));
  118. LedLcdManager::lcdPrint(F(VERSION), false, 0, 1);
  119. #ifdef ARDUINO
  120. Serial.begin(serialBaud);
  121. #endif
  122. irReceiver = IrReceiverSampler::newIrReceiverSampler(IrReader::defaultCaptureLength,
  123. IRRECEIVER_1_PIN, IRRECEIVER_1_PULLUP_VALUE, IRRECEIVER_MARK_EXCESS, DEFAULT_BEGINTIMEOUT, DEFAULT_ENDINGTIMEOUT);
  124. }
  125. #ifndef ARDUINO
  126. int main() {
  127. setup();
  128. while (true)
  129. loop();
  130. }
  131. #endif