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.

104 lines
2.4 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%08X\n"), code);
  19. if (code == 0xFFFFFFFF) {
  20. DEBUG_MSG_P(PSTR("[IR] Processing 0x%08X\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 LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  29. if (button_mode == IR_BUTTON_MODE_BRIGHTER) {
  30. lightBrightnessStep(button_value ? 1 : -1);
  31. delay(150); //debounce
  32. }
  33. if (button_mode == IR_BUTTON_MODE_RGB) {
  34. lightColor(button_value);
  35. }
  36. /*
  37. if (button_mode == IR_BUTTON_MODE_HSV) {
  38. lightColor(button_value);
  39. }
  40. */
  41. #endif
  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. lightUpdate(true, true);
  50. found = true;
  51. last_code = code;
  52. break;
  53. }
  54. }
  55. if (!found) {
  56. last_code = 0;
  57. DEBUG_MSG_P(PSTR("[IR] Ignoring code\n"));
  58. }
  59. }
  60. // -----------------------------------------------------------------------------
  61. // PUBLIC API
  62. // -----------------------------------------------------------------------------
  63. void irSetup() {
  64. _ir_recv = new IRrecv(IR_PIN);
  65. _ir_recv->enableIRIn();
  66. }
  67. void irLoop() {
  68. if (_ir_recv->decode(&_ir_results)) {
  69. unsigned long code = _ir_results.value;
  70. _irProcessCode(code);
  71. _ir_recv->resume(); // Receive the next value
  72. }
  73. }
  74. #endif // IR_SUPPORT