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.

153 lines
3.6 KiB

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