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.

229 lines
5.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
  1. /*
  2. LED MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. // -----------------------------------------------------------------------------
  6. // LED
  7. // -----------------------------------------------------------------------------
  8. typedef struct {
  9. unsigned char pin;
  10. bool reverse;
  11. unsigned char mode;
  12. unsigned char relay;
  13. } led_t;
  14. std::vector<led_t> _leds;
  15. // -----------------------------------------------------------------------------
  16. bool _ledStatus(unsigned char id) {
  17. if (id >= _ledCount()) return false;
  18. bool status = digitalRead(_leds[id].pin);
  19. return _leds[id].reverse ? !status : status;
  20. }
  21. bool _ledStatus(unsigned char id, bool status) {
  22. if (id >=_ledCount()) return false;
  23. bool s = _leds[id].reverse ? !status : status;
  24. digitalWrite(_leds[id].pin, _leds[id].reverse ? !status : status);
  25. return status;
  26. }
  27. bool _ledToggle(unsigned char id) {
  28. if (id >= _ledCount()) return false;
  29. return _ledStatus(id, !_ledStatus(id));
  30. }
  31. unsigned char _ledMode(unsigned char id) {
  32. if (id >= _ledCount()) return false;
  33. return _leds[id].mode;
  34. }
  35. void _ledMode(unsigned char id, unsigned char mode) {
  36. if (id >= _ledCount()) return;
  37. _leds[id].mode = mode;
  38. }
  39. void _ledBlink(unsigned char id, unsigned long delayOff, unsigned long delayOn) {
  40. if (id >= _ledCount()) return;
  41. static unsigned long next = millis();
  42. if (next < millis()) {
  43. next += (_ledToggle(id) ? delayOn : delayOff);
  44. }
  45. }
  46. #if WEB_SUPPORT
  47. void _ledWebSocketOnSend(JsonObject& root) {
  48. if (_ledCount() == 0) return;
  49. root["ledVisible"] = 1;
  50. root["ledMode0"] = _ledMode(0);
  51. }
  52. #endif
  53. #if MQTT_SUPPORT
  54. void _ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  55. static bool isFirstMessage = true;
  56. if (type == MQTT_CONNECT_EVENT) {
  57. char buffer[strlen(MQTT_TOPIC_LED) + 3];
  58. snprintf_P(buffer, sizeof(buffer), PSTR("%s/+"), MQTT_TOPIC_LED);
  59. mqttSubscribe(buffer);
  60. }
  61. if (type == MQTT_MESSAGE_EVENT) {
  62. // Match topic
  63. String t = mqttSubtopic((char *) topic);
  64. if (!t.startsWith(MQTT_TOPIC_LED)) return;
  65. // Get led ID
  66. unsigned int ledID = t.substring(strlen(MQTT_TOPIC_LED)+1).toInt();
  67. if (ledID >= _ledCount()) {
  68. DEBUG_MSG_P(PSTR("[LED] Wrong ledID (%d)\n"), ledID);
  69. return;
  70. }
  71. // Check if LED is managed
  72. if (_ledMode(ledID) != LED_MODE_MQTT) return;
  73. // get value
  74. unsigned char value = relayParsePayload(payload);
  75. // Action to perform
  76. if (value == 2) {
  77. _ledToggle(ledID);
  78. } else {
  79. _ledStatus(ledID, value == 1);
  80. }
  81. }
  82. }
  83. #endif
  84. unsigned char _ledCount() {
  85. return _leds.size();
  86. }
  87. void _ledConfigure() {
  88. for (unsigned int i=0; i < _leds.size(); i++) {
  89. _ledMode(i, getSetting("ledMode", i, _ledMode(i)).toInt());
  90. }
  91. }
  92. // -----------------------------------------------------------------------------
  93. void ledSetup() {
  94. #ifdef LED1_PIN
  95. _leds.push_back((led_t) { LED1_PIN, LED1_PIN_INVERSE, LED1_MODE, LED1_RELAY });
  96. #endif
  97. #ifdef LED2_PIN
  98. _leds.push_back((led_t) { LED2_PIN, LED2_PIN_INVERSE, LED2_MODE, LED2_RELAY });
  99. #endif
  100. #ifdef LED3_PIN
  101. _leds.push_back((led_t) { LED3_PIN, LED3_PIN_INVERSE, LED3_MODE, LED3_RELAY });
  102. #endif
  103. #ifdef LED4_PIN
  104. _leds.push_back((led_t) { LED4_PIN, LED4_PIN_INVERSE, LED4_MODE, LED4_RELAY });
  105. #endif
  106. #ifdef LED5_PIN
  107. _leds.push_back((led_t) { LED5_PIN, LED5_PIN_INVERSE, LED5_MODE, LED5_RELAY });
  108. #endif
  109. #ifdef LED6_PIN
  110. _leds.push_back((led_t) { LED6_PIN, LED6_PIN_INVERSE, LED6_MODE, LED6_RELAY });
  111. #endif
  112. #ifdef LED7_PIN
  113. _leds.push_back((led_t) { LED7_PIN, LED7_PIN_INVERSE, LED7_MODE, LED7_RELAY });
  114. #endif
  115. #ifdef LED8_PIN
  116. _leds.push_back((led_t) { LED8_PIN, LED8_PIN_INVERSE, LED8_MODE, LED8_RELAY });
  117. #endif
  118. for (unsigned int i=0; i < _leds.size(); i++) {
  119. pinMode(_leds[i].pin, OUTPUT);
  120. _ledStatus(i, false);
  121. }
  122. _ledConfigure();
  123. #if MQTT_SUPPORT
  124. mqttRegister(_ledMQTTCallback);
  125. #endif
  126. #if WEB_SUPPORT
  127. wsOnSendRegister(_ledWebSocketOnSend);
  128. wsOnAfterParseRegister(_ledConfigure);
  129. #endif
  130. DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
  131. }
  132. void ledLoop() {
  133. for (unsigned char i=0; i<_leds.size(); i++) {
  134. if (_ledMode(i) == LED_MODE_WIFI) {
  135. if (wifiConnected()) {
  136. if (WiFi.getMode() == WIFI_AP) {
  137. _ledBlink(i, 900, 100);
  138. } else {
  139. _ledBlink(i, 4900, 100);
  140. }
  141. } else {
  142. _ledBlink(i, 500, 500);
  143. }
  144. }
  145. if (_ledMode(i) == LED_MODE_MIXED) {
  146. if (wifiConnected()) {
  147. if (relayStatus(_leds[i].relay-1)) {
  148. if (WiFi.getMode() == WIFI_AP) {
  149. _ledBlink(i, 900, 100);
  150. } else {
  151. _ledBlink(i, 4900, 100);
  152. }
  153. } else {
  154. if (WiFi.getMode() == WIFI_AP) {
  155. _ledBlink(i, 100, 900);
  156. } else {
  157. _ledBlink(i, 100, 4900);
  158. }
  159. }
  160. } else {
  161. _ledBlink(i, 500, 500);
  162. }
  163. }
  164. if (_ledMode(i) == LED_MODE_FOLLOW) {
  165. _ledStatus(i, relayStatus(_leds[i].relay-1));
  166. }
  167. if (_ledMode(i) == LED_MODE_FOLLOW_INVERSE) {
  168. _ledStatus(i, !relayStatus(_leds[i].relay-1));
  169. }
  170. if (_ledMode(i) == LED_MODE_FINDME) {
  171. bool status = true;
  172. for (unsigned char k=0; k<relayCount(); k++) {
  173. if (relayStatus(k)) {
  174. status = false;
  175. break;
  176. }
  177. }
  178. _ledStatus(i, status);
  179. }
  180. }
  181. }