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.

145 lines
3.3 KiB

  1. /*
  2. ESPurna
  3. LED MODULE
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. // -----------------------------------------------------------------------------
  7. // LED
  8. // -----------------------------------------------------------------------------
  9. #ifdef LED1_PIN
  10. typedef struct {
  11. unsigned char pin;
  12. bool reverse;
  13. } led_t;
  14. std::vector<led_t> _leds;
  15. bool ledAuto;
  16. bool ledStatus(unsigned char id) {
  17. bool status = digitalRead(_leds[id].pin);
  18. return _leds[id].reverse ? !status : status;
  19. }
  20. bool ledStatus(unsigned char id, bool status) {
  21. bool s = _leds[id].reverse ? !status : status;
  22. digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status);
  23. return status;
  24. }
  25. bool ledToggle(unsigned char id) {
  26. return ledStatus(id, !ledStatus(id));
  27. }
  28. void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
  29. static unsigned long next = millis();
  30. if (next < millis()) {
  31. next += (ledToggle(id) ? delayOn : delayOff);
  32. }
  33. }
  34. void showStatus() {
  35. if (wifiConnected()) {
  36. if (WiFi.getMode() == WIFI_AP) {
  37. ledBlink(0, 2000, 2000);
  38. } else {
  39. ledBlink(0, 5000, 500);
  40. }
  41. } else {
  42. ledBlink(0, 500, 500);
  43. }
  44. }
  45. void ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  46. static bool isFirstMessage = true;
  47. String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
  48. if (type == MQTT_CONNECT_EVENT) {
  49. char buffer[strlen(MQTT_LED_TOPIC) + mqttSetter.length() + 3];
  50. sprintf(buffer, "%s/+%s", MQTT_LED_TOPIC, mqttSetter.c_str());
  51. mqttSubscribe(buffer);
  52. }
  53. if (type == MQTT_MESSAGE_EVENT) {
  54. // Match topic
  55. String t = String(topic);
  56. if (!t.startsWith(MQTT_LED_TOPIC)) return;
  57. if (!t.endsWith(mqttSetter)) return;
  58. // Get led ID
  59. unsigned int ledID = topic[strlen(MQTT_LED_TOPIC)+1] - '0';
  60. if (ledID >= ledCount()) ledID = 0;
  61. // get value
  62. unsigned int value = (char)payload[0] - '0';
  63. bool bitAuto = (value & 0x02) > 0;
  64. bool bitState = (value & 0x01) > 0;
  65. // Check ledAuto
  66. if (ledID == 0) {
  67. ledAuto = bitAuto ? bitState : false;
  68. setSetting("ledAuto", String() + (ledAuto ? "1" : "0"));
  69. if (bitAuto) return;
  70. }
  71. // Action to perform
  72. ledStatus(ledID, bitState);
  73. }
  74. }
  75. unsigned char ledCount() {
  76. return _leds.size();
  77. }
  78. void ledConfigure() {
  79. ledAuto = getSetting("ledAuto", String() + LED_AUTO).toInt() == 1;
  80. }
  81. void ledSetup() {
  82. #ifdef LED1_PIN
  83. _leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE });
  84. #endif
  85. #ifdef LED2_PIN
  86. _leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE });
  87. #endif
  88. #ifdef LED3_PIN
  89. _leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE });
  90. #endif
  91. #ifdef LED4_PIN
  92. _leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE });
  93. #endif
  94. for (unsigned int i=0; i < _leds.size(); i++) {
  95. pinMode(_leds[i].pin, OUTPUT);
  96. ledStatus(i, false);
  97. }
  98. ledConfigure();
  99. mqttRegister(ledMQTTCallback);
  100. DEBUG_MSG("[LED] Number of leds: %d\n", _leds.size());
  101. }
  102. void ledLoop() {
  103. if (ledAuto) showStatus();
  104. }
  105. #else
  106. void ledSetup() {};
  107. void ledLoop() {};
  108. #endif