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.

120 lines
2.9 KiB

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