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.

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