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.

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