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.

249 lines
6.4 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
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. bool _led_update = false; // For relay-based modes
  16. // -----------------------------------------------------------------------------
  17. bool _ledStatus(unsigned char id) {
  18. if (id >= _ledCount()) return false;
  19. bool status = digitalRead(_leds[id].pin);
  20. return _leds[id].reverse ? !status : status;
  21. }
  22. bool _ledStatus(unsigned char id, bool status) {
  23. if (id >=_ledCount()) return false;
  24. bool s = _leds[id].reverse ? !status : status;
  25. digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status);
  26. return status;
  27. }
  28. bool _ledToggle(unsigned char id) {
  29. if (id >= _ledCount()) return false;
  30. return _ledStatus(id, !_ledStatus(id));
  31. }
  32. unsigned char _ledMode(unsigned char id) {
  33. if (id >= _ledCount()) return false;
  34. return _leds[id].mode;
  35. }
  36. void _ledMode(unsigned char id, unsigned char mode) {
  37. if (id >= _ledCount()) return;
  38. _leds[id].mode = mode;
  39. }
  40. void _ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
  41. if (id >= _ledCount()) return;
  42. static unsigned long next = millis();
  43. if (next < millis()) {
  44. next += (_ledToggle(id) ? delayOn : delayOff);
  45. }
  46. }
  47. #if WEB_SUPPORT
  48. void _ledWebSocketOnSend(JsonObject& root) {
  49. if (_ledCount() == 0) return;
  50. root["ledVisible"] = 1;
  51. root["ledMode0"] = _ledMode(0);
  52. }
  53. #endif
  54. #if MQTT_SUPPORT
  55. void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  56. static bool isFirstMessage = true;
  57. if (type == MQTT_CONNECT_EVENT) {
  58. char buffer[strlen(MQTT_TOPIC_LED) + 3];
  59. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_LED);
  60. mqttSubscribe(buffer);
  61. }
  62. if (type == MQTT_MESSAGE_EVENT) {
  63. // Match topic
  64. String t = mqttSubtopic((char *) topic);
  65. if (!t.startsWith(MQTT_TOPIC_LED)) return;
  66. // Get led ID
  67. unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt();
  68. if (ledID >= _ledCount()) {
  69. DEBUG_MSG_P(PSTR("[LED] Wrong ledID (%d)\n"), ledID);
  70. return;
  71. }
  72. // Check if LED is managed
  73. if (_ledMode(ledID) != LED_MODE_MQTT) return;
  74. // get value
  75. unsigned char value = relayParsePayload(payload);
  76. // Action to perform
  77. if (value == 2) {
  78. _ledToggle(ledID);
  79. } else {
  80. _ledStatus(ledID, value == 1);
  81. }
  82. }
  83. }
  84. #endif
  85. unsigned char _ledCount() {
  86. return _leds.size();
  87. }
  88. void _ledConfigure() {
  89. for (unsigned int i=0; i < _leds.size(); i++) {
  90. _ledMode(i, getSetting("ledMode", i, _ledMode(i)).toInt());
  91. }
  92. _led_update = true;
  93. }
  94. // -----------------------------------------------------------------------------
  95. void ledUpdate(bool value) {
  96. _led_update = value;
  97. }
  98. void ledSetup() {
  99. #ifdef LED1_PIN
  100. _leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE, LED1_MODE, LED1_RELAY });
  101. #endif
  102. #ifdef LED2_PIN
  103. _leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE, LED2_MODE, LED2_RELAY });
  104. #endif
  105. #ifdef LED3_PIN
  106. _leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE, LED3_MODE, LED3_RELAY });
  107. #endif
  108. #ifdef LED4_PIN
  109. _leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE, LED4_MODE, LED4_RELAY });
  110. #endif
  111. #ifdef LED5_PIN
  112. _leds.push_back((led_t) { LED5_PIN, LED5_PIN_INVERSE, LED5_MODE, LED5_RELAY });
  113. #endif
  114. #ifdef LED6_PIN
  115. _leds.push_back((led_t) { LED6_PIN, LED6_PIN_INVERSE, LED6_MODE, LED6_RELAY });
  116. #endif
  117. #ifdef LED7_PIN
  118. _leds.push_back((led_t) { LED7_PIN, LED7_PIN_INVERSE, LED7_MODE, LED7_RELAY });
  119. #endif
  120. #ifdef LED8_PIN
  121. _leds.push_back((led_t) { LED8_PIN, LED8_PIN_INVERSE, LED8_MODE, LED8_RELAY });
  122. #endif
  123. for (unsigned int i=0; i < _leds.size(); i++) {
  124. pinMode(_leds[i].pin, OUTPUT);
  125. _ledStatus(i, false);
  126. }
  127. _ledConfigure();
  128. #if MQTT_SUPPORT
  129. mqttRegister(_ledMQTTCallback);
  130. #endif
  131. #if WEB_SUPPORT
  132. wsOnSendRegister(_ledWebSocketOnSend);
  133. wsOnAfterParseRegister(_ledConfigure);
  134. #endif
  135. DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
  136. }
  137. void ledLoop() {
  138. for (unsigned char i=0; i<_leds.size(); i++) {
  139. if (_ledMode(i) == LED_MODE_WIFI) {
  140. if (wifiConnected()) {
  141. if (WiFi.getMode() == WIFI_AP) {
  142. _ledBlink(i, 900, 100);
  143. } else {
  144. _ledBlink(i, 4900, 100);
  145. }
  146. } else {
  147. _ledBlink(i, 500, 500);
  148. }
  149. }
  150. if (_ledMode(i) == LED_MODE_MIXED) {
  151. if (wifiConnected()) {
  152. if (relayStatus(_leds[i].relay-1)) {
  153. if (WiFi.getMode() == WIFI_AP) {
  154. _ledBlink(i, 900, 100);
  155. } else {
  156. _ledBlink(i, 4900, 100);
  157. }
  158. } else {
  159. if (WiFi.getMode() == WIFI_AP) {
  160. _ledBlink(i, 100, 900);
  161. } else {
  162. _ledBlink(i, 100, 4900);
  163. }
  164. }
  165. } else {
  166. _ledBlink(i, 500, 500);
  167. }
  168. }
  169. // Relay-based modes, update only if relays have been updated
  170. if (!_led_update) continue;
  171. if (_ledMode(i) == LED_MODE_FOLLOW) {
  172. _ledStatus(i, relayStatus(_leds[i].relay-1));
  173. }
  174. if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) {
  175. _ledStatus(i, !relayStatus(_leds[i].relay-1));
  176. }
  177. if (_ledMode(i) == LED_MODE_FINDME) {
  178. bool status = true;
  179. for (unsigned char k=0; k<relayCount(); k++) {
  180. if (relayStatus(k)) {
  181. status = false;
  182. break;
  183. }
  184. }
  185. _ledStatus(i, status);
  186. }
  187. if (_ledMode(i) == LED_MODE_ON) {
  188. _ledStatus(i, true);
  189. }
  190. if (_ledMode(i) == LED_MODE_OFF) {
  191. _ledStatus(i, false);
  192. }
  193. }
  194. _led_update = false;
  195. }