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.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. 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 LED_WIFI
  37. void showStatus() {
  38. if (wifiConnected()) {
  39. if (WiFi.getMode() == WIFI_AP) {
  40. ledBlink(LED_WIFI - 1, 2500, 2500);
  41. } else {
  42. ledBlink(LED_WIFI - 1, 4900, 100);
  43. }
  44. } else {
  45. ledBlink(LED_WIFI - 1, 500, 500);
  46. }
  47. }
  48. #endif
  49. #if MQTT_SUPPORT
  50. void ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  51. static bool isFirstMessage = true;
  52. if (type == MQTT_CONNECT_EVENT) {
  53. char buffer[strlen(MQTT_TOPIC_LED) + 3];
  54. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_LED);
  55. mqttSubscribe(buffer);
  56. }
  57. if (type == MQTT_MESSAGE_EVENT) {
  58. // Match topic
  59. String t = mqttSubtopic((char *) topic);
  60. if (!t.startsWith(MQTT_TOPIC_LED)) return;
  61. // Get led ID
  62. unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt();
  63. if (ledID >= ledCount()) {
  64. DEBUG_MSG_P(PSTR("[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. #endif
  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. #if MQTT_SUPPORT
  107. mqttRegister(ledMQTTCallback);
  108. #endif
  109. DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
  110. DEBUG_MSG_P(PSTR("[LED] Led auto indicator is %s\n"), ledAuto ? "ON" : "OFF" );
  111. }
  112. void ledLoop() {
  113. #if LED_WIFI
  114. if (ledAuto) showStatus();
  115. #endif
  116. }