Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.7 KiB

7 years ago
7 years ago
  1. /*
  2. IR MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2017-2018 by François Déchery
  5. */
  6. #if IR_SUPPORT
  7. #include <IRremoteESP8266.h>
  8. #include <IRrecv.h>
  9. IRrecv * _ir_recv;
  10. decode_results _ir_results;
  11. // -----------------------------------------------------------------------------
  12. // PRIVATE
  13. // -----------------------------------------------------------------------------
  14. void _irProcessCode(unsigned long code) {
  15. static unsigned long last_code;
  16. boolean found = false;
  17. // Repeat last valid code
  18. DEBUG_MSG_P(PSTR("[IR] Received 0x%06X\n"), code);
  19. if (code == 0xFFFFFFFF) {
  20. DEBUG_MSG_P(PSTR("[IR] Processing 0x%06X\n"), code);
  21. code = last_code;
  22. }
  23. for (unsigned char i = 0; i < IR_BUTTON_COUNT ; i++) {
  24. unsigned long button_code = pgm_read_dword(&IR_BUTTON[i][0]);
  25. if (code == button_code) {
  26. unsigned long button_mode = pgm_read_dword(&IR_BUTTON[i][1]);
  27. unsigned long button_value = pgm_read_dword(&IR_BUTTON[i][2]);
  28. if (button_mode == IR_BUTTON_MODE_STATE) {
  29. relayStatus(0, button_value);
  30. }
  31. if (button_mode == IR_BUTTON_MODE_TOGGLE) {
  32. relayToggle(button_value);
  33. }
  34. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  35. if (button_mode == IR_BUTTON_MODE_BRIGHTER) {
  36. lightBrightnessStep(button_value ? 1 : -1);
  37. nice_delay(150); //debounce
  38. }
  39. if (button_mode == IR_BUTTON_MODE_RGB) {
  40. lightColor(button_value);
  41. }
  42. /*
  43. #if LIGHT_PROVIDER == LIGHT_PROVIDER_FASTLED
  44. if (button_mode == IR_BUTTON_MODE_EFFECT) {
  45. _buttonAnimMode(button_value);
  46. }
  47. #endif
  48. */
  49. /*
  50. if (button_mode == IR_BUTTON_MODE_HSV) {
  51. lightColor(button_value);
  52. }
  53. */
  54. lightUpdate(true, true);
  55. #endif
  56. found = true;
  57. last_code = code;
  58. break;
  59. }
  60. }
  61. if (!found) {
  62. DEBUG_MSG_P(PSTR("[IR] Ignoring code\n"));
  63. }
  64. }
  65. // -----------------------------------------------------------------------------
  66. // PUBLIC API
  67. // -----------------------------------------------------------------------------
  68. void irSetup() {
  69. _ir_recv = new IRrecv(IR_PIN);
  70. _ir_recv->enableIRIn();
  71. // Register loop
  72. espurnaRegisterLoop(irLoop);
  73. }
  74. void irLoop() {
  75. if (_ir_recv->decode(&_ir_results)) {
  76. unsigned long code = _ir_results.value;
  77. _irProcessCode(code);
  78. _ir_recv->resume(); // Receive the next value
  79. }
  80. }
  81. #endif // IR_SUPPORT