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.

126 lines
3.2 KiB

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