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