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.

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