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.

119 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 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() - last > 250){
  34. relayToggle(button_value);
  35. }
  36. else{
  37. DEBUG_MSG_P(PSTR("[IR] Ignoring repeated code\n"));
  38. }
  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. last_code = code;
  64. break;
  65. }
  66. }
  67. if (!found) {
  68. DEBUG_MSG_P(PSTR("[IR] Ignoring code\n"));
  69. }
  70. }
  71. // -----------------------------------------------------------------------------
  72. // PUBLIC API
  73. // -----------------------------------------------------------------------------
  74. void irSetup() {
  75. _ir_recv = new IRrecv(IR_PIN);
  76. _ir_recv->enableIRIn();
  77. // Register loop
  78. espurnaRegisterLoop(irLoop);
  79. }
  80. void irLoop() {
  81. if (_ir_recv->decode(&_ir_results)) {
  82. unsigned long code = _ir_results.value;
  83. _irProcessCode(code);
  84. _ir_recv->resume(); // Receive the next value
  85. }
  86. }
  87. #endif // IR_SUPPORT