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.

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