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.

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