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.

217 lines
5.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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. unsigned char mode;
  12. unsigned char relay;
  13. } led_t;
  14. std::vector<led_t> _leds;
  15. // -----------------------------------------------------------------------------
  16. bool _ledStatus(unsigned char id) {
  17. if (id >= _ledCount()) 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 >=_ledCount()) 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 >= _ledCount()) return false;
  29. return _ledStatus(id, !_ledStatus(id));
  30. }
  31. unsigned char _ledMode(unsigned char id) {
  32. if (id >= _ledCount()) return false;
  33. return _leds[id].mode;
  34. }
  35. void _ledMode(unsigned char id, unsigned char mode) {
  36. if (id >= _ledCount()) return;
  37. _leds[id].mode = mode;
  38. }
  39. void _ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
  40. if (id >= _ledCount()) return;
  41. static unsigned long next = millis();
  42. if (next < millis()) {
  43. next += (_ledToggle(id) ? delayOn : delayOff);
  44. }
  45. }
  46. #if WEB_SUPPORT
  47. void _ledWebSocketOnSend(JsonObject& root) {
  48. if (_ledCount() == 0) return;
  49. root["ledVisible"] = 1;
  50. root["ledMode0"] = _ledMode(0);
  51. }
  52. #endif
  53. #if MQTT_SUPPORT
  54. void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  55. static bool isFirstMessage = true;
  56. if (type == MQTT_CONNECT_EVENT) {
  57. char buffer[strlen(MQTT_TOPIC_LED) + 3];
  58. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_LED);
  59. mqttSubscribe(buffer);
  60. }
  61. if (type == MQTT_MESSAGE_EVENT) {
  62. // Match topic
  63. String t = mqttSubtopic((char *) topic);
  64. if (!t.startsWith(MQTT_TOPIC_LED)) return;
  65. // Get led ID
  66. unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt();
  67. if (ledID >= _ledCount()) {
  68. DEBUG_MSG_P(PSTR("[LED] Wrong ledID (%d)\n"), ledID);
  69. return;
  70. }
  71. // Check if LED is managed
  72. if (_ledMode(ledID) != LED_MODE_MQTT) return;
  73. // get value
  74. unsigned char value = relayParsePayload(payload);
  75. // Action to perform
  76. if (value == 2) {
  77. _ledToggle(ledID);
  78. } else {
  79. _ledStatus(ledID, value == 1);
  80. }
  81. }
  82. }
  83. #endif
  84. unsigned char _ledCount() {
  85. return _leds.size();
  86. }
  87. void _ledConfigure() {
  88. for (unsigned int i=0; i < _leds.size(); i++) {
  89. _ledMode(i, getSetting("ledMode", i, _ledMode(i)).toInt());
  90. }
  91. }
  92. // -----------------------------------------------------------------------------
  93. void ledSetup() {
  94. #ifdef LED1_PIN
  95. _leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE, LED1_MODE, LED1_RELAY });
  96. #endif
  97. #ifdef LED2_PIN
  98. _leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE, LED2_MODE, LED2_RELAY });
  99. #endif
  100. #ifdef LED3_PIN
  101. _leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE, LED3_MODE, LED3_RELAY });
  102. #endif
  103. #ifdef LED4_PIN
  104. _leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE, LED4_MODE, LED4_RELAY });
  105. #endif
  106. for (unsigned int i=0; i < _leds.size(); i++) {
  107. pinMode(_leds[i].pin, OUTPUT);
  108. _ledStatus(i, false);
  109. }
  110. _ledConfigure();
  111. #if MQTT_SUPPORT
  112. mqttRegister(_ledMQTTCallback);
  113. #endif
  114. #if WEB_SUPPORT
  115. wsOnSendRegister(_ledWebSocketOnSend);
  116. wsOnAfterParseRegister(_ledConfigure);
  117. #endif
  118. DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
  119. }
  120. void ledLoop() {
  121. for (unsigned char i=0; i<_leds.size(); i++) {
  122. if (_ledMode(i) == LED_MODE_WIFI) {
  123. if (wifiConnected()) {
  124. if (WiFi.getMode() == WIFI_AP) {
  125. _ledBlink(i, 900, 100);
  126. } else {
  127. _ledBlink(i, 4900, 100);
  128. }
  129. } else {
  130. _ledBlink(i, 500, 500);
  131. }
  132. }
  133. if (_ledMode(i) == LED_MODE_MIXED) {
  134. if (wifiConnected()) {
  135. if (relayStatus(_leds[i].relay-1)) {
  136. if (WiFi.getMode() == WIFI_AP) {
  137. _ledBlink(i, 900, 100);
  138. } else {
  139. _ledBlink(i, 4900, 100);
  140. }
  141. } else {
  142. if (WiFi.getMode() == WIFI_AP) {
  143. _ledBlink(i, 100, 900);
  144. } else {
  145. _ledBlink(i, 100, 4900);
  146. }
  147. }
  148. } else {
  149. _ledBlink(i, 500, 500);
  150. }
  151. }
  152. if (_ledMode(i) == LED_MODE_FOLLOW) {
  153. _ledStatus(i, relayStatus(_leds[i].relay-1));
  154. }
  155. if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) {
  156. _ledStatus(i, !relayStatus(_leds[i].relay-1));
  157. }
  158. if (_ledMode(i) == LED_MODE_FINDME) {
  159. bool status = true;
  160. for (unsigned char k=0; k<relayCount(); k++) {
  161. if (relayStatus(k)) {
  162. status = false;
  163. break;
  164. }
  165. }
  166. _ledStatus(i, status);
  167. }
  168. }
  169. }