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.

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