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.

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