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.

296 lines
7.9 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
6 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-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // LED
  7. // -----------------------------------------------------------------------------
  8. #if LED_SUPPORT
  9. typedef struct {
  10. unsigned char pin;
  11. bool reverse;
  12. unsigned char mode;
  13. unsigned char relay;
  14. } led_t;
  15. std::vector<led_t> _leds;
  16. bool _led_update = false; // For relay-based modes
  17. // -----------------------------------------------------------------------------
  18. bool _ledStatus(unsigned char id) {
  19. if (id >= _ledCount()) return false;
  20. bool status = digitalRead(_leds[id].pin);
  21. return _leds[id].reverse ? !status : status;
  22. }
  23. bool _ledStatus(unsigned char id, bool status) {
  24. if (id >=_ledCount()) return false;
  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. bool _ledWebSocketOnReceive(const char * key, JsonVariant& value) {
  49. return (strncmp(key, "led", 3) == 0);
  50. }
  51. void _ledWebSocketOnSend(JsonObject& root) {
  52. if (_ledCount() == 0) return;
  53. root["ledVisible"] = 1;
  54. root["ledMode0"] = _ledMode(0);
  55. }
  56. #endif
  57. #if MQTT_SUPPORT
  58. void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  59. if (type == MQTT_CONNECT_EVENT) {
  60. char buffer[strlen(MQTT_TOPIC_LED) + 3];
  61. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_LED);
  62. mqttSubscribe(buffer);
  63. }
  64. if (type == MQTT_MESSAGE_EVENT) {
  65. // Match topic
  66. String t = mqttMagnitude((char *) topic);
  67. if (!t.startsWith(MQTT_TOPIC_LED)) return;
  68. // Get led ID
  69. unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt();
  70. if (ledID >= _ledCount()) {
  71. DEBUG_MSG_P(PSTR("[LED] Wrong ledID (%d)\n"), ledID);
  72. return;
  73. }
  74. // Check if LED is managed
  75. if (_ledMode(ledID) != LED_MODE_MQTT) return;
  76. // get value
  77. unsigned char value = relayParsePayload(payload);
  78. // Action to perform
  79. if (value == 2) {
  80. _ledToggle(ledID);
  81. } else {
  82. _ledStatus(ledID, value == 1);
  83. }
  84. }
  85. }
  86. #endif
  87. unsigned char _ledCount() {
  88. return _leds.size();
  89. }
  90. void _ledConfigure() {
  91. for (unsigned int i=0; i < _leds.size(); i++) {
  92. _ledMode(i, getSetting("ledMode", i, _ledMode(i)).toInt());
  93. }
  94. _led_update = true;
  95. }
  96. // -----------------------------------------------------------------------------
  97. void ledUpdate(bool value) {
  98. _led_update = value;
  99. }
  100. void ledSetup() {
  101. #if LED1_PIN != GPIO_NONE
  102. _leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE, LED1_MODE, LED1_RELAY });
  103. #endif
  104. #if LED2_PIN != GPIO_NONE
  105. _leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE, LED2_MODE, LED2_RELAY });
  106. #endif
  107. #if LED3_PIN != GPIO_NONE
  108. _leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE, LED3_MODE, LED3_RELAY });
  109. #endif
  110. #if LED4_PIN != GPIO_NONE
  111. _leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE, LED4_MODE, LED4_RELAY });
  112. #endif
  113. #if LED5_PIN != GPIO_NONE
  114. _leds.push_back((led_t) { LED5_PIN, LED5_PIN_INVERSE, LED5_MODE, LED5_RELAY });
  115. #endif
  116. #if LED6_PIN != GPIO_NONE
  117. _leds.push_back((led_t) { LED6_PIN, LED6_PIN_INVERSE, LED6_MODE, LED6_RELAY });
  118. #endif
  119. #if LED7_PIN != GPIO_NONE
  120. _leds.push_back((led_t) { LED7_PIN, LED7_PIN_INVERSE, LED7_MODE, LED7_RELAY });
  121. #endif
  122. #if LED8_PIN != GPIO_NONE
  123. _leds.push_back((led_t) { LED8_PIN, LED8_PIN_INVERSE, LED8_MODE, LED8_RELAY });
  124. #endif
  125. for (unsigned int i=0; i < _leds.size(); i++) {
  126. pinMode(_leds[i].pin, OUTPUT);
  127. _ledStatus(i, false);
  128. }
  129. _ledConfigure();
  130. #if MQTT_SUPPORT
  131. mqttRegister(_ledMQTTCallback);
  132. #endif
  133. #if WEB_SUPPORT
  134. wsOnSendRegister(_ledWebSocketOnSend);
  135. wsOnAfterParseRegister(_ledConfigure);
  136. wsOnReceiveRegister(_ledWebSocketOnReceive);
  137. #endif
  138. DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
  139. // Register loop
  140. espurnaRegisterLoop(ledLoop);
  141. }
  142. void ledLoop() {
  143. uint8_t wifi_state = wifiState();
  144. for (unsigned char i=0; i<_leds.size(); i++) {
  145. if (_ledMode(i) == LED_MODE_WIFI) {
  146. if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) {
  147. _ledBlink(i, 100, 100);
  148. } else if (wifi_state & WIFI_STATE_STA) {
  149. _ledBlink(i, 4900, 100);
  150. } else if (wifi_state & WIFI_STATE_AP) {
  151. _ledBlink(i, 900, 100);
  152. } else {
  153. _ledBlink(i, 500, 500);
  154. }
  155. }
  156. if (_ledMode(i) == LED_MODE_FINDME_WIFI) {
  157. if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) {
  158. _ledBlink(i, 100, 100);
  159. } else if (wifi_state & WIFI_STATE_STA) {
  160. if (relayStatus(_leds[i].relay-1)) {
  161. _ledBlink(i, 4900, 100);
  162. } else {
  163. _ledBlink(i, 100, 4900);
  164. }
  165. } else if (wifi_state & WIFI_STATE_AP) {
  166. if (relayStatus(_leds[i].relay-1)) {
  167. _ledBlink(i, 900, 100);
  168. } else {
  169. _ledBlink(i, 100, 900);
  170. }
  171. } else {
  172. _ledBlink(i, 500, 500);
  173. }
  174. }
  175. if (_ledMode(i) == LED_MODE_RELAY_WIFI) {
  176. if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) {
  177. _ledBlink(i, 100, 100);
  178. } else if (wifi_state & WIFI_STATE_STA) {
  179. if (relayStatus(_leds[i].relay-1)) {
  180. _ledBlink(i, 100, 4900);
  181. } else {
  182. _ledBlink(i, 4900, 100);
  183. }
  184. } else if (wifi_state & WIFI_STATE_AP) {
  185. if (relayStatus(_leds[i].relay-1)) {
  186. _ledBlink(i, 100, 900);
  187. } else {
  188. _ledBlink(i, 900, 100);
  189. }
  190. } else {
  191. _ledBlink(i, 500, 500);
  192. }
  193. }
  194. // Relay-based modes, update only if relays have been updated
  195. if (!_led_update) continue;
  196. if (_ledMode(i) == LED_MODE_FOLLOW) {
  197. _ledStatus(i, relayStatus(_leds[i].relay-1));
  198. }
  199. if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) {
  200. _ledStatus(i, !relayStatus(_leds[i].relay-1));
  201. }
  202. if (_ledMode(i) == LED_MODE_FINDME) {
  203. bool status = true;
  204. for (unsigned char k=0; k<relayCount(); k++) {
  205. if (relayStatus(k)) {
  206. status = false;
  207. break;
  208. }
  209. }
  210. _ledStatus(i, status);
  211. }
  212. if (_ledMode(i) == LED_MODE_RELAY) {
  213. bool status = false;
  214. for (unsigned char k=0; k<relayCount(); k++) {
  215. if (relayStatus(k)) {
  216. status = true;
  217. break;
  218. }
  219. }
  220. _ledStatus(i, status);
  221. }
  222. if (_ledMode(i) == LED_MODE_ON) {
  223. _ledStatus(i, true);
  224. }
  225. if (_ledMode(i) == LED_MODE_OFF) {
  226. _ledStatus(i, false);
  227. }
  228. }
  229. _led_update = false;
  230. }
  231. #endif // LED_SUPPORT