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.

329 lines
8.9 KiB

6 years ago
7 years ago
  1. /*
  2. BUTTON MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if BUTTON_SUPPORT
  6. #include <DebounceEvent.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "system.h"
  10. #include "relay.h"
  11. #include "light.h"
  12. #include "button.h"
  13. #include "button_config.h"
  14. // -----------------------------------------------------------------------------
  15. // TODO: dblclick and debounce delays - right now a global setting, independent of ID
  16. unsigned long button_t::DebounceDelay = BUTTON_DEBOUNCE_DELAY;
  17. unsigned long button_t::DblclickDelay = BUTTON_DBLCLICK_DELAY;
  18. button_t::button_t(unsigned char pin, unsigned char mode, unsigned long actions, unsigned char relayID) :
  19. event(new DebounceEvent(pin, mode, DebounceDelay, DblclickDelay)),
  20. actions(actions),
  21. relayID(relayID)
  22. {}
  23. button_t::button_t(unsigned char index) :
  24. button_t(_buttonPin(index), _buttonMode(index), _buttonConstructActions(index), _buttonRelay(index))
  25. {}
  26. bool button_t::state() {
  27. return event->pressed();
  28. }
  29. std::vector<button_t> _buttons;
  30. unsigned char buttonCount() {
  31. return _buttons.size();
  32. }
  33. #if MQTT_SUPPORT
  34. void buttonMQTT(unsigned char id, uint8_t event) {
  35. char payload[4] = {0};
  36. itoa(event, payload, 10);
  37. mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain
  38. }
  39. #endif
  40. #if WEB_SUPPORT
  41. void _buttonWebSocketOnVisible(JsonObject& root) {
  42. if (buttonCount() > 0) {
  43. root["btnVisible"] = 1;
  44. }
  45. }
  46. bool _buttonWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  47. return (strncmp(key, "btn", 3) == 0);
  48. }
  49. #endif
  50. bool buttonState(unsigned char id) {
  51. if (id >= _buttons.size()) return false;
  52. return _buttons[id].state();
  53. }
  54. unsigned char buttonAction(unsigned char id, unsigned char event) {
  55. if (id >= _buttons.size()) return BUTTON_MODE_NONE;
  56. return _buttonDecodeEventAction(_buttons[id].actions, event);
  57. }
  58. void buttonEvent(unsigned char id, unsigned char event) {
  59. DEBUG_MSG_P(PSTR("[BUTTON] Button #%u event %u\n"), id, event);
  60. if (event == 0) return;
  61. auto& button = _buttons[id];
  62. unsigned char action = _buttonDecodeEventAction(button.actions, event);
  63. #if MQTT_SUPPORT
  64. if (action != BUTTON_MODE_NONE || BUTTON_MQTT_SEND_ALL_EVENTS) {
  65. buttonMQTT(id, event);
  66. }
  67. #endif
  68. #if THERMOSTAT_DISPLAY_SUPPORT
  69. if (BUTTON_MODE_DISPLAY_ON == action) {
  70. displayOn();
  71. }
  72. #endif
  73. if (BUTTON_MODE_TOGGLE == action) {
  74. relayToggle(button.relayID);
  75. }
  76. if (BUTTON_MODE_ON == action) {
  77. relayStatus(button.relayID, true);
  78. }
  79. if (BUTTON_MODE_OFF == action) {
  80. relayStatus(button.relayID, false);
  81. }
  82. if (BUTTON_MODE_AP == action) {
  83. if (wifiState() & WIFI_STATE_AP) {
  84. wifiStartSTA();
  85. } else {
  86. wifiStartAP();
  87. }
  88. }
  89. if (BUTTON_MODE_RESET == action) {
  90. deferredReset(100, CUSTOM_RESET_HARDWARE);
  91. }
  92. if (BUTTON_MODE_FACTORY == action) {
  93. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  94. resetSettings();
  95. deferredReset(100, CUSTOM_RESET_FACTORY);
  96. }
  97. #if defined(JUSTWIFI_ENABLE_WPS)
  98. if (BUTTON_MODE_WPS == action) {
  99. wifiStartWPS();
  100. }
  101. #endif // defined(JUSTWIFI_ENABLE_WPS)
  102. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  103. if (BUTTON_MODE_SMART_CONFIG == action) {
  104. wifiStartSmartConfig();
  105. }
  106. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  107. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  108. if (BUTTON_MODE_DIM_UP == action) {
  109. lightBrightnessStep(1);
  110. lightUpdate(true, true);
  111. }
  112. if (BUTTON_MODE_DIM_DOWN == action) {
  113. lightBrightnessStep(-1);
  114. lightUpdate(true, true);
  115. }
  116. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  117. }
  118. unsigned char buttonAdd(unsigned char pin, unsigned char mode, unsigned long actions, unsigned char relayID) {
  119. _buttons.emplace_back(pin, mode, actions, relayID);
  120. return _buttons.size() - 1;
  121. }
  122. void buttonSetup() {
  123. // Special hardware cases
  124. #if defined(ITEAD_SONOFF_DUAL)
  125. _buttons.reserve(3);
  126. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(0));
  127. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(1));
  128. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(2));
  129. #elif defined(FOXEL_LIGHTFOX_DUAL)
  130. _buttons.reserve(4);
  131. const auto actions = _buttonConstructActions(
  132. BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE,
  133. BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE
  134. );
  135. for (unsigned char id = 0; id < 4; ++id) {
  136. buttonAdd(
  137. GPIO_NONE, BUTTON_PUSHBUTTON,
  138. actions, getSetting({"btnRelay", id}, _buttonRelay(id))
  139. );
  140. }
  141. // Generic GPIO input handlers
  142. #else
  143. size_t buttons = 0;
  144. #if BUTTON1_PIN != GPIO_NONE
  145. ++buttons;
  146. #endif
  147. #if BUTTON2_PIN != GPIO_NONE
  148. ++buttons;
  149. #endif
  150. #if BUTTON3_PIN != GPIO_NONE
  151. ++buttons;
  152. #endif
  153. #if BUTTON4_PIN != GPIO_NONE
  154. ++buttons;
  155. #endif
  156. #if BUTTON5_PIN != GPIO_NONE
  157. ++buttons;
  158. #endif
  159. #if BUTTON6_PIN != GPIO_NONE
  160. ++buttons;
  161. #endif
  162. #if BUTTON7_PIN != GPIO_NONE
  163. ++buttons;
  164. #endif
  165. #if BUTTON8_PIN != GPIO_NONE
  166. ++buttons;
  167. #endif
  168. _buttons.reserve(buttons);
  169. // TODO: load based on index
  170. button_t::DebounceDelay = getSetting("btnDebounce", BUTTON_DEBOUNCE_DELAY);
  171. button_t::DblclickDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY);
  172. for (unsigned char id = 0; id < buttons; ++id) {
  173. _buttons.emplace_back(id);
  174. }
  175. #endif
  176. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  177. // Websocket Callbacks
  178. #if WEB_SUPPORT
  179. wsRegister()
  180. .onVisible(_buttonWebSocketOnVisible)
  181. .onKeyCheck(_buttonWebSocketOnKeyCheck);
  182. #endif
  183. // Register loop
  184. espurnaRegisterLoop(buttonLoop);
  185. }
  186. void buttonLoop() {
  187. #if defined(ITEAD_SONOFF_DUAL)
  188. if (Serial.available() >= 4) {
  189. if (Serial.read() == 0xA0) {
  190. if (Serial.read() == 0x04) {
  191. unsigned char value = Serial.read();
  192. if (Serial.read() == 0xA1) {
  193. // RELAYs and BUTTONs are synchonized in the SIL F330
  194. // The on-board BUTTON2 should toggle RELAY0 value
  195. // Since we are not passing back RELAY2 value
  196. // (in the relayStatus method) it will only be present
  197. // here if it has actually been pressed
  198. if ((value & 4) == 4) {
  199. buttonEvent(2, BUTTON_EVENT_CLICK);
  200. return;
  201. }
  202. // Otherwise check if any of the other two BUTTONs
  203. // (in the header) has been pressed, but we should
  204. // ensure that we only toggle one of them to avoid
  205. // the synchronization going mad
  206. // This loop is generic for any PSB-04 module
  207. for (unsigned int i=0; i<relayCount(); i++) {
  208. bool status = (value & (1 << i)) > 0;
  209. // Check if the status for that relay has changed
  210. if (relayStatus(i) != status) {
  211. buttonEvent(i, BUTTON_EVENT_CLICK);
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. #elif defined(FOXEL_LIGHTFOX_DUAL)
  220. if (Serial.available() >= 4) {
  221. if (Serial.read() == 0xA0) {
  222. if (Serial.read() == 0x04) {
  223. unsigned char value = Serial.read();
  224. if (Serial.read() == 0xA1) {
  225. DEBUG_MSG_P(PSTR("[BUTTON] [LIGHTFOX] Received buttons mask: %d\n"), value);
  226. for (unsigned int i=0; i<_buttons.size(); i++) {
  227. bool clicked = (value & (1 << i)) > 0;
  228. if (clicked) {
  229. buttonEvent(i, BUTTON_EVENT_CLICK);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. #else
  237. for (size_t id = 0; id < _buttons.size(); ++id) {
  238. auto& button = _buttons[id];
  239. if (auto event = button.event->loop()) {
  240. buttonEvent(id, _buttonMapEvent(
  241. event,
  242. button.event->getEventCount(),
  243. button.event->getEventLength()
  244. ));
  245. }
  246. }
  247. #endif
  248. }
  249. #endif // BUTTON_SUPPORT