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.

148 lines
3.7 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. typedef struct {
  9. unsigned char pin;
  10. bool reverse;
  11. } led_t;
  12. std::vector<led_t> _leds;
  13. bool ledAuto;
  14. bool ledStatus(unsigned char id) {
  15. if (id >= _leds.size()) return false;
  16. bool status = digitalRead(_leds[id].pin);
  17. return _leds[id].reverse ? !status : status;
  18. }
  19. bool ledStatus(unsigned char id, bool status) {
  20. if (id >= _leds.size()) return false;
  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. if (id >= _leds.size()) return false;
  27. return ledStatus(id, !ledStatus(id));
  28. }
  29. void ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
  30. if (id >= _leds.size()) return;
  31. static unsigned long next = millis();
  32. if (next < millis()) {
  33. next += (ledToggle(id) ? delayOn : delayOff);
  34. }
  35. }
  36. #if WIFI_LED
  37. void showStatus() {
  38. if (wifiConnected()) {
  39. if (WiFi.getMode() == WIFI_AP) {
  40. ledBlink(WIFI_LED - 1, 2500, 2500);
  41. } else {
  42. ledBlink(WIFI_LED - 1, 4900, 100);
  43. }
  44. } else {
  45. ledBlink(WIFI_LED - 1, 500, 500);
  46. }
  47. }
  48. #endif
  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. char * t = mqttSubtopic((char *) topic);
  60. if (strncmp(t, MQTT_LED_TOPIC, strlen(MQTT_LED_TOPIC)) != 0) return;
  61. int len = mqttSetter.length();
  62. if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) return;
  63. // Get led ID
  64. unsigned int ledID = topic[strlen(topic) - mqttSetter.length() - 1] - '0';
  65. if (ledID >= ledCount()) {
  66. DEBUG_MSG("[LED] Wrong ledID (%d)\n", ledID);
  67. return;
  68. }
  69. // get value
  70. unsigned int value = (char)payload[0] - '0';
  71. bool bitAuto = (value & 0x02) > 0;
  72. bool bitState = (value & 0x01) > 0;
  73. // Check ledAuto
  74. if (ledID == 0) {
  75. ledAuto = bitAuto ? bitState : false;
  76. setSetting("ledAuto", String() + (ledAuto ? "1" : "0"));
  77. if (bitAuto) return;
  78. }
  79. // Action to perform
  80. ledStatus(ledID, bitState);
  81. }
  82. }
  83. unsigned char ledCount() {
  84. return _leds.size();
  85. }
  86. void ledConfigure() {
  87. ledAuto = getSetting("ledAuto", String() + LED_AUTO).toInt() == 1;
  88. }
  89. void ledSetup() {
  90. #ifdef LED1_PIN
  91. _leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE });
  92. #endif
  93. #ifdef LED2_PIN
  94. _leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE });
  95. #endif
  96. #ifdef LED3_PIN
  97. _leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE });
  98. #endif
  99. #ifdef LED4_PIN
  100. _leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE });
  101. #endif
  102. for (unsigned int i=0; i < _leds.size(); i++) {
  103. pinMode(_leds[i].pin, OUTPUT);
  104. ledStatus(i, false);
  105. }
  106. ledConfigure();
  107. mqttRegister(ledMQTTCallback);
  108. DEBUG_MSG("[LED] Number of leds: %d\n", _leds.size());
  109. DEBUG_MSG("[LED] Led auto indicator is %s\n", ledAuto ? "ON" : "OFF" );
  110. }
  111. void ledLoop() {
  112. #if WIFI_LED
  113. if (ledAuto) showStatus();
  114. #endif
  115. }