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.

152 lines
3.6 KiB

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