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.

134 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
7 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. 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, unsigned char type) {
  17. // Check valid code
  18. unsigned long last_code = 0;
  19. unsigned long last_time = 0;
  20. if (code == 0xFFFFFFFF) return;
  21. if (type == 0xFF) return;
  22. if ((last_code == code) && (millis() - last_time < IR_DEBOUNCE)) return;
  23. last_code = code;
  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. bool _irKeyCheck(const char * key) {
  75. return (strncmp(key, "ir", 2) == 0);
  76. }
  77. // -----------------------------------------------------------------------------
  78. // PUBLIC API
  79. // -----------------------------------------------------------------------------
  80. void irSetup() {
  81. _ir_recv = new IRrecv(IR_RECEIVER_PIN);
  82. _ir_recv->enableIRIn();
  83. // Key Check
  84. settingsRegisterKeyCheck(_irKeyCheck);
  85. // Register loop
  86. espurnaRegisterLoop(irLoop);
  87. }
  88. void irLoop() {
  89. if (_ir_recv->decode(&_ir_results)) {
  90. _irProcessCode(_ir_results.value, _ir_results.decode_type);
  91. _ir_recv->resume(); // Receive the next value
  92. }
  93. }
  94. #endif // IR_SUPPORT