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.

323 lines
8.7 KiB

7 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 (BUTTON_MODE_TOGGLE == action) {
  69. relayToggle(button.relayID);
  70. }
  71. if (BUTTON_MODE_ON == action) {
  72. relayStatus(button.relayID, true);
  73. }
  74. if (BUTTON_MODE_OFF == action) {
  75. relayStatus(button.relayID, false);
  76. }
  77. if (BUTTON_MODE_AP == action) {
  78. if (wifiState() & WIFI_STATE_AP) {
  79. wifiStartSTA();
  80. } else {
  81. wifiStartAP();
  82. }
  83. }
  84. if (BUTTON_MODE_RESET == action) {
  85. deferredReset(100, CUSTOM_RESET_HARDWARE);
  86. }
  87. if (BUTTON_MODE_FACTORY == action) {
  88. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  89. resetSettings();
  90. deferredReset(100, CUSTOM_RESET_FACTORY);
  91. }
  92. #if defined(JUSTWIFI_ENABLE_WPS)
  93. if (BUTTON_MODE_WPS == action) {
  94. wifiStartWPS();
  95. }
  96. #endif // defined(JUSTWIFI_ENABLE_WPS)
  97. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  98. if (BUTTON_MODE_SMART_CONFIG == action) {
  99. wifiStartSmartConfig();
  100. }
  101. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  102. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  103. if (BUTTON_MODE_DIM_UP == action) {
  104. lightBrightnessStep(1);
  105. lightUpdate(true, true);
  106. }
  107. if (BUTTON_MODE_DIM_DOWN == action) {
  108. lightBrightnessStep(-1);
  109. lightUpdate(true, true);
  110. }
  111. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  112. }
  113. unsigned char buttonAdd(unsigned char pin, unsigned char mode, unsigned long actions, unsigned char relayID) {
  114. _buttons.emplace_back(pin, mode, actions, relayID);
  115. return _buttons.size() - 1;
  116. }
  117. void buttonSetup() {
  118. // Special hardware cases
  119. #if defined(ITEAD_SONOFF_DUAL)
  120. _buttons.reserve(3);
  121. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(0));
  122. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(1));
  123. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(2));
  124. #elif defined(FOXEL_LIGHTFOX_DUAL)
  125. _buttons.reserve(4);
  126. const auto actions = _buttonConstructActions(
  127. BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE,
  128. BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE
  129. );
  130. for (unsigned char id = 0; id < 4; ++id) {
  131. buttonAdd(
  132. GPIO_NONE, BUTTON_PUSHBUTTON,
  133. actions, getSetting({"btnRelay", id}, _buttonRelay(id))
  134. );
  135. }
  136. // Generic GPIO input handlers
  137. #else
  138. size_t buttons = 0;
  139. #if BUTTON1_PIN != GPIO_NONE
  140. ++buttons;
  141. #endif
  142. #if BUTTON2_PIN != GPIO_NONE
  143. ++buttons;
  144. #endif
  145. #if BUTTON3_PIN != GPIO_NONE
  146. ++buttons;
  147. #endif
  148. #if BUTTON4_PIN != GPIO_NONE
  149. ++buttons;
  150. #endif
  151. #if BUTTON5_PIN != GPIO_NONE
  152. ++buttons;
  153. #endif
  154. #if BUTTON6_PIN != GPIO_NONE
  155. ++buttons;
  156. #endif
  157. #if BUTTON7_PIN != GPIO_NONE
  158. ++buttons;
  159. #endif
  160. #if BUTTON8_PIN != GPIO_NONE
  161. ++buttons;
  162. #endif
  163. _buttons.reserve(buttons);
  164. // TODO: load based on index
  165. button_t::DebounceDelay = getSetting("btnDebounce", BUTTON_DEBOUNCE_DELAY);
  166. button_t::DblclickDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY);
  167. for (unsigned char id = 0; id < buttons; ++id) {
  168. _buttons.emplace_back(id);
  169. }
  170. #endif
  171. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  172. // Websocket Callbacks
  173. #if WEB_SUPPORT
  174. wsRegister()
  175. .onVisible(_buttonWebSocketOnVisible)
  176. .onKeyCheck(_buttonWebSocketOnKeyCheck);
  177. #endif
  178. // Register loop
  179. espurnaRegisterLoop(buttonLoop);
  180. }
  181. void buttonLoop() {
  182. #if defined(ITEAD_SONOFF_DUAL)
  183. if (Serial.available() >= 4) {
  184. if (Serial.read() == 0xA0) {
  185. if (Serial.read() == 0x04) {
  186. unsigned char value = Serial.read();
  187. if (Serial.read() == 0xA1) {
  188. // RELAYs and BUTTONs are synchonized in the SIL F330
  189. // The on-board BUTTON2 should toggle RELAY0 value
  190. // Since we are not passing back RELAY2 value
  191. // (in the relayStatus method) it will only be present
  192. // here if it has actually been pressed
  193. if ((value & 4) == 4) {
  194. buttonEvent(2, BUTTON_EVENT_CLICK);
  195. return;
  196. }
  197. // Otherwise check if any of the other two BUTTONs
  198. // (in the header) has been pressed, but we should
  199. // ensure that we only toggle one of them to avoid
  200. // the synchronization going mad
  201. // This loop is generic for any PSB-04 module
  202. for (unsigned int i=0; i<relayCount(); i++) {
  203. bool status = (value & (1 << i)) > 0;
  204. // Check if the status for that relay has changed
  205. if (relayStatus(i) != status) {
  206. buttonEvent(i, BUTTON_EVENT_CLICK);
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. #elif defined(FOXEL_LIGHTFOX_DUAL)
  215. if (Serial.available() >= 4) {
  216. if (Serial.read() == 0xA0) {
  217. if (Serial.read() == 0x04) {
  218. unsigned char value = Serial.read();
  219. if (Serial.read() == 0xA1) {
  220. DEBUG_MSG_P(PSTR("[BUTTON] [LIGHTFOX] Received buttons mask: %d\n"), value);
  221. for (unsigned int i=0; i<_buttons.size(); i++) {
  222. bool clicked = (value & (1 << i)) > 0;
  223. if (clicked) {
  224. buttonEvent(i, BUTTON_EVENT_CLICK);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. #else
  232. for (size_t id = 0; id < _buttons.size(); ++id) {
  233. auto& button = _buttons[id];
  234. if (auto event = button.event->loop()) {
  235. buttonEvent(id, _buttonMapEvent(
  236. event,
  237. button.event->getEventCount(),
  238. button.event->getEventLength()
  239. ));
  240. }
  241. }
  242. #endif
  243. }
  244. #endif // BUTTON_SUPPORT