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.

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