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.

107 lines
2.6 KiB

  1. /*
  2. IR MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2017 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 LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  32. if (button_mode == IR_BUTTON_MODE_BRIGHTER) {
  33. lightBrightnessStep(button_value ? 1 : -1);
  34. delay(150); //debounce
  35. }
  36. if (button_mode == IR_BUTTON_MODE_RGB) {
  37. lightColor(button_value);
  38. }
  39. /*
  40. #if LIGHT_PROVIDER == LIGHT_PROVIDER_FASTLED
  41. if (button_mode == IR_BUTTON_MODE_EFFECT) {
  42. _buttonAnimMode(button_value);
  43. }
  44. #endif
  45. */
  46. /*
  47. if (button_mode == IR_BUTTON_MODE_HSV) {
  48. lightColor(button_value);
  49. }
  50. */
  51. lightUpdate(true, true);
  52. #endif
  53. found = true;
  54. last_code = code;
  55. break;
  56. }
  57. }
  58. if (!found) {
  59. DEBUG_MSG_P(PSTR("[IR] Ignoring code\n"));
  60. }
  61. }
  62. // -----------------------------------------------------------------------------
  63. // PUBLIC API
  64. // -----------------------------------------------------------------------------
  65. void irSetup() {
  66. _ir_recv = new IRrecv(IR_PIN);
  67. _ir_recv->enableIRIn();
  68. }
  69. void irLoop() {
  70. if (_ir_recv->decode(&_ir_results)) {
  71. unsigned long code = _ir_results.value;
  72. _irProcessCode(code);
  73. _ir_recv->resume(); // Receive the next value
  74. }
  75. }
  76. #endif // IR_SUPPORT