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.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. #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. char * t = mqttSubtopic((char *) topic);
  59. if (strncmp(t, MQTT_LED_TOPIC, strlen(MQTT_LED_TOPIC)) != 0) return;
  60. int len = mqttSetter.length();
  61. if (strncmp(t + strlen(t) - len, mqttSetter.c_str(), len) != 0) 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