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.

317 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. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(0));
  121. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(1));
  122. buttonAdd(GPIO_NONE, BUTTON_PUSHBUTTON, 0, _buttonRelay(2));
  123. #elif defined(FOXEL_LIGHTFOX_DUAL)
  124. const auto actions = _buttonConstructActions(
  125. BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE,
  126. BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE
  127. );
  128. for (unsigned char id = 0; id < 4; ++id) {
  129. buttonAdd(
  130. GPIO_NONE, BUTTON_PUSHBUTTON,
  131. actions, getSetting({"btnRelay", id}, _buttonRelay(id))
  132. );
  133. }
  134. // Generic GPIO input handlers
  135. #else
  136. size_t buttons = 0;
  137. #if BUTTON1_PIN != GPIO_NONE
  138. ++buttons;
  139. #endif
  140. #if BUTTON2_PIN != GPIO_NONE
  141. ++buttons;
  142. #endif
  143. #if BUTTON3_PIN != GPIO_NONE
  144. ++buttons;
  145. #endif
  146. #if BUTTON4_PIN != GPIO_NONE
  147. ++buttons;
  148. #endif
  149. #if BUTTON5_PIN != GPIO_NONE
  150. ++buttons;
  151. #endif
  152. #if BUTTON6_PIN != GPIO_NONE
  153. ++buttons;
  154. #endif
  155. #if BUTTON7_PIN != GPIO_NONE
  156. ++buttons;
  157. #endif
  158. #if BUTTON8_PIN != GPIO_NONE
  159. ++buttons;
  160. #endif
  161. // TODO: load based on index
  162. button_t::DebounceDelay = getSetting("btnDebounce", BUTTON_DEBOUNCE_DELAY);
  163. button_t::DblclickDelay = getSetting("btnDelay", BUTTON_DBLCLICK_DELAY);
  164. for (unsigned char id = 0; id < buttons; ++id) {
  165. _buttons.emplace_back(id);
  166. }
  167. #endif
  168. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  169. // Websocket Callbacks
  170. #if WEB_SUPPORT
  171. wsRegister()
  172. .onVisible(_buttonWebSocketOnVisible)
  173. .onKeyCheck(_buttonWebSocketOnKeyCheck);
  174. #endif
  175. // Register loop
  176. espurnaRegisterLoop(buttonLoop);
  177. }
  178. void buttonLoop() {
  179. #if defined(ITEAD_SONOFF_DUAL)
  180. if (Serial.available() >= 4) {
  181. if (Serial.read() == 0xA0) {
  182. if (Serial.read() == 0x04) {
  183. unsigned char value = Serial.read();
  184. if (Serial.read() == 0xA1) {
  185. // RELAYs and BUTTONs are synchonized in the SIL F330
  186. // The on-board BUTTON2 should toggle RELAY0 value
  187. // Since we are not passing back RELAY2 value
  188. // (in the relayStatus method) it will only be present
  189. // here if it has actually been pressed
  190. if ((value & 4) == 4) {
  191. buttonEvent(2, BUTTON_EVENT_CLICK);
  192. return;
  193. }
  194. // Otherwise check if any of the other two BUTTONs
  195. // (in the header) has been pressed, but we should
  196. // ensure that we only toggle one of them to avoid
  197. // the synchronization going mad
  198. // This loop is generic for any PSB-04 module
  199. for (unsigned int i=0; i<relayCount(); i++) {
  200. bool status = (value & (1 << i)) > 0;
  201. // Check if the status for that relay has changed
  202. if (relayStatus(i) != status) {
  203. buttonEvent(i, BUTTON_EVENT_CLICK);
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. #elif defined(FOXEL_LIGHTFOX_DUAL)
  212. if (Serial.available() >= 4) {
  213. if (Serial.read() == 0xA0) {
  214. if (Serial.read() == 0x04) {
  215. unsigned char value = Serial.read();
  216. if (Serial.read() == 0xA1) {
  217. DEBUG_MSG_P(PSTR("[BUTTON] [LIGHTFOX] Received buttons mask: %d\n"), value);
  218. for (unsigned int i=0; i<_buttons.size(); i++) {
  219. bool clicked = (value & (1 << i)) > 0;
  220. if (clicked) {
  221. buttonEvent(i, BUTTON_EVENT_CLICK);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. #else
  229. for (size_t id = 0; id < _buttons.size(); ++id) {
  230. auto& button = _buttons[id];
  231. if (auto event = button.event->loop()) {
  232. buttonEvent(id, _buttonMapEvent(
  233. event,
  234. button.event->getEventCount(),
  235. button.event->getEventLength()
  236. ));
  237. }
  238. }
  239. #endif
  240. }
  241. #endif // BUTTON_SUPPORT