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.

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