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.

305 lines
7.6 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
  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 = true; // 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 _ledClear() {
  92. _leds.clear();
  93. }
  94. void _ledConfigure() {
  95. _ledClear();
  96. unsigned char index = 0;
  97. while (index < MAX_COMPONENTS) {
  98. unsigned char pin = getSetting("ledGPIO", index, GPIO_NONE).toInt();
  99. if (pin == GPIO_NONE) break;
  100. bool inverse = getSetting("ledLogic", index, 0).toInt() == 1;
  101. unsigned char mode = getSetting("ledMode", index, index==0 ? LED_MODE_WIFI : LED_MODE_MQTT).toInt();
  102. unsigned char relayId = getSetting("ledRelay", index, RELAY_NONE).toInt();
  103. _leds.push_back((led_t) { pin, inverse, mode, relayId });
  104. pinMode(pin, OUTPUT);
  105. _ledStatus(index, false);
  106. ++index;
  107. }
  108. DEBUG_MSG_P(PSTR("[LED] LEDs: %d\n"), _leds.size());
  109. _led_update = true;
  110. }
  111. void _ledLoop() {
  112. uint8_t wifi_state = wifiState();
  113. for (unsigned char i=0; i<_leds.size(); i++) {
  114. if (_ledMode(i) == LED_MODE_WIFI) {
  115. if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) {
  116. _ledBlink(i, 100, 100);
  117. } else if (wifi_state & WIFI_STATE_STA) {
  118. _ledBlink(i, 4900, 100);
  119. } else if (wifi_state & WIFI_STATE_AP) {
  120. _ledBlink(i, 900, 100);
  121. } else {
  122. _ledBlink(i, 500, 500);
  123. }
  124. continue;
  125. }
  126. if (_ledMode(i) == LED_MODE_FINDME_WIFI) {
  127. if (wifi_state & WIFI_STATE_WPS || wifi_state & WIFI_STATE_SMARTCONFIG) {
  128. _ledBlink(i, 100, 100);
  129. } else if (wifi_state & WIFI_STATE_STA) {
  130. if (relayStatus(_leds[i].relay-1)) {
  131. _ledBlink(i, 4900, 100);
  132. } else {
  133. _ledBlink(i, 100, 4900);
  134. }
  135. } else if (wifi_state & WIFI_STATE_AP) {
  136. if (relayStatus(_leds[i].relay-1)) {
  137. _ledBlink(i, 900, 100);
  138. } else {
  139. _ledBlink(i, 100, 900);
  140. }
  141. } else {
  142. _ledBlink(i, 500, 500);
  143. }
  144. continue;
  145. }
  146. if (_ledMode(i) == LED_MODE_RELAY_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. if (relayStatus(_leds[i].relay-1)) {
  151. _ledBlink(i, 100, 4900);
  152. } else {
  153. _ledBlink(i, 4900, 100);
  154. }
  155. } else if (wifi_state & WIFI_STATE_AP) {
  156. if (relayStatus(_leds[i].relay-1)) {
  157. _ledBlink(i, 100, 900);
  158. } else {
  159. _ledBlink(i, 900, 100);
  160. }
  161. } else {
  162. _ledBlink(i, 500, 500);
  163. }
  164. continue;
  165. }
  166. // Relay-based modes, update only if relays have been updated
  167. if (!_led_update) continue;
  168. if (_ledMode(i) == LED_MODE_FOLLOW) {
  169. if (RELAY_NONE != _leds[i].relay) {
  170. _ledStatus(i, relayStatus(_leds[i].relay));
  171. }
  172. continue;
  173. }
  174. if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) {
  175. if (RELAY_NONE != _leds[i].relay) {
  176. _ledStatus(i, !relayStatus(_leds[i].relay));
  177. }
  178. continue;
  179. }
  180. if (_ledMode(i) == LED_MODE_FINDME) {
  181. bool status = true;
  182. for (unsigned char k=0; k<relayCount(); k++) {
  183. if (relayStatus(k)) {
  184. status = false;
  185. break;
  186. }
  187. }
  188. _ledStatus(i, status);
  189. continue;
  190. }
  191. if (_ledMode(i) == LED_MODE_RELAY) {
  192. bool status = false;
  193. for (unsigned char k=0; k<relayCount(); k++) {
  194. if (relayStatus(k)) {
  195. status = true;
  196. break;
  197. }
  198. }
  199. _ledStatus(i, status);
  200. continue;
  201. }
  202. if (_ledMode(i) == LED_MODE_ON) {
  203. _ledStatus(i, true);
  204. continue;
  205. }
  206. if (_ledMode(i) == LED_MODE_OFF) {
  207. _ledStatus(i, false);
  208. continue;
  209. }
  210. }
  211. _led_update = false;
  212. }
  213. // -----------------------------------------------------------------------------
  214. void ledUpdate(bool value) {
  215. _led_update = value;
  216. }
  217. void ledSetup() {
  218. _ledConfigure();
  219. // Callbacks
  220. #if MQTT_SUPPORT
  221. mqttRegister(_ledMQTTCallback);
  222. #endif
  223. #if WEB_SUPPORT
  224. wsOnSendRegister(_ledWebSocketOnSend);
  225. #endif
  226. settingsRegisterKeyCheck(_ledKeyCheck);
  227. espurnaRegisterReload(_ledConfigure);
  228. espurnaRegisterLoop(_ledLoop);
  229. }
  230. #endif // LED_SUPPORT