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.

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