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.

125 lines
3.0 KiB

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