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.

499 lines
14 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 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 <bitset>
  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. #include "libs/DebounceEvent.h"
  15. // -----------------------------------------------------------------------------
  16. button_event_delays_t::button_event_delays_t() :
  17. debounce(BUTTON_DEBOUNCE_DELAY),
  18. dblclick(BUTTON_DBLCLICK_DELAY),
  19. lngclick(BUTTON_LNGCLICK_DELAY),
  20. lnglngclick(BUTTON_LNGLNGCLICK_DELAY)
  21. {}
  22. button_event_delays_t::button_event_delays_t(unsigned long debounce, unsigned long dblclick, unsigned long lngclick, unsigned long lnglngclick) :
  23. debounce(debounce),
  24. dblclick(dblclick),
  25. lngclick(lngclick),
  26. lnglngclick(lnglngclick)
  27. {}
  28. button_t::button_t(std::shared_ptr<DebounceEvent::PinBase> pin, int mode, unsigned long actions, unsigned char relayID, button_event_delays_t delays) :
  29. event_handler(new DebounceEvent::DebounceEvent(pin, mode, delays.debounce, delays.dblclick)),
  30. event_delays(delays),
  31. actions(actions),
  32. relayID(relayID)
  33. {}
  34. bool button_t::state() {
  35. return event_handler->pressed();
  36. }
  37. std::vector<button_t> _buttons;
  38. // -----------------------------------------------------------------------------
  39. constexpr const uint8_t _buttonMapReleased(uint8_t count, uint8_t length, unsigned long lngclick_delay, unsigned long lnglngclick_delay) {
  40. return (
  41. (1 == count) ? (
  42. (length > lnglngclick_delay) ? BUTTON_EVENT_LNGLNGCLICK :
  43. (length > lngclick_delay) ? BUTTON_EVENT_LNGCLICK : BUTTON_EVENT_CLICK
  44. ) :
  45. (2 == count) ? BUTTON_EVENT_DBLCLICK :
  46. (3 == count) ? BUTTON_EVENT_TRIPLECLICK :
  47. BUTTON_EVENT_NONE
  48. );
  49. }
  50. const uint8_t _buttonMapEvent(button_t& button, DebounceEvent::Types::event_t event) {
  51. using namespace DebounceEvent;
  52. switch (event) {
  53. case Types::EventPressed:
  54. return BUTTON_EVENT_PRESSED;
  55. case Types::EventChanged:
  56. return BUTTON_EVENT_CLICK;
  57. case Types::EventReleased: {
  58. return _buttonMapReleased(
  59. button.event_handler->getEventCount(),
  60. button.event_handler->getEventLength(),
  61. button.event_delays.lngclick,
  62. button.event_delays.lnglngclick
  63. );
  64. }
  65. case Types::EventNone:
  66. default:
  67. return BUTTON_EVENT_NONE;
  68. }
  69. }
  70. unsigned char buttonCount() {
  71. return _buttons.size();
  72. }
  73. #if MQTT_SUPPORT
  74. std::bitset<BUTTONS_MAX> _buttons_mqtt_retain(
  75. (1 == BUTTON_MQTT_RETAIN) ? 0xFFFFFFFFUL : 0UL
  76. );
  77. std::bitset<BUTTONS_MAX> _buttons_mqtt_send_all(
  78. (1 == BUTTON_MQTT_SEND_ALL_EVENTS) ? 0xFFFFFFFFUL : 0UL
  79. );
  80. void buttonMQTT(unsigned char id, uint8_t event) {
  81. char payload[4] = {0};
  82. itoa(event, payload, 10);
  83. // mqttSend(topic, id, payload, force, retail)
  84. mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, _buttons_mqtt_retain[id]);
  85. }
  86. #endif
  87. #if WEB_SUPPORT
  88. void _buttonWebSocketOnVisible(JsonObject& root) {
  89. if (buttonCount() > 0) {
  90. root["btnVisible"] = 1;
  91. }
  92. }
  93. // XXX: unused! pending webui changes
  94. void _buttonWebSocketOnConnected(JsonObject& root) {
  95. #if 0
  96. if (buttonCount() < 1) return;
  97. JsonObject& module = root.createNestedObject("btn");
  98. // TODO: hardware can sometimes use a different event source
  99. // e.g. Sonoff Dual does not need `Pin`, `Mode` or any of `Del`
  100. // TODO: schema names are uppercase to easily match settings?
  101. // TODO: schema name->type map to generate WebUI elements?
  102. JsonArray& schema = module.createNestedArray("_schema");
  103. schema.add("Pin");
  104. schema.add("Mode");
  105. schema.add("Relay");
  106. schema.add("DebDel");
  107. schema.add("DblDel");
  108. schema.add("LngDel");
  109. schema.add("LngLngDel");
  110. #if MQTT_SUPPORT
  111. schema.add("MqttSnd");
  112. schema.add("MqttRetain");
  113. #endif
  114. JsonArray& buttons = module.createNestedArray("list");
  115. for (unsigned char i=0; i<buttonCount(); i++) {
  116. JsonArray& button = buttons.createNestedArray();
  117. button.add(_buttons[i].pin);
  118. button.add(_buttons[i].mode);
  119. button.add(_buttons[i].relayID);
  120. button.add(_buttons[i].debounceDelay);
  121. button.add(_buttons[i].doubleClickDelay);
  122. button.add(_buttons[i].longClickDelay);
  123. button.add(_buttons[i].longLongClickDelay);
  124. #if MQTT_SUPPORT
  125. button.add(_buttonMqttSendAllEvents(i));
  126. button.add(_buttonMqttRetain(i));
  127. #endif
  128. }
  129. #endif
  130. }
  131. bool _buttonWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  132. return (strncmp(key, "btn", 3) == 0);
  133. }
  134. #endif
  135. bool buttonState(unsigned char id) {
  136. if (id >= _buttons.size()) return false;
  137. return _buttons[id].state();
  138. }
  139. unsigned char buttonAction(unsigned char id, unsigned char event) {
  140. if (id >= _buttons.size()) return BUTTON_MODE_NONE;
  141. return _buttonDecodeEventAction(_buttons[id].actions, event);
  142. }
  143. void buttonEvent(unsigned char id, unsigned char event) {
  144. DEBUG_MSG_P(PSTR("[BUTTON] Button #%u event %u\n"), id, event);
  145. if (event == 0) return;
  146. auto& button = _buttons[id];
  147. unsigned char action = _buttonDecodeEventAction(button.actions, event);
  148. #if MQTT_SUPPORT
  149. if (action != BUTTON_MODE_NONE || _buttons_mqtt_send_all[id]) {
  150. buttonMQTT(id, event);
  151. }
  152. #endif
  153. if (BUTTON_MODE_TOGGLE == action) {
  154. relayToggle(button.relayID);
  155. }
  156. if (BUTTON_MODE_ON == action) {
  157. relayStatus(button.relayID, true);
  158. }
  159. if (BUTTON_MODE_OFF == action) {
  160. relayStatus(button.relayID, false);
  161. }
  162. if (BUTTON_MODE_AP == action) {
  163. if (wifiState() & WIFI_STATE_AP) {
  164. wifiStartSTA();
  165. } else {
  166. wifiStartAP();
  167. }
  168. }
  169. if (BUTTON_MODE_RESET == action) {
  170. deferredReset(100, CUSTOM_RESET_HARDWARE);
  171. }
  172. if (BUTTON_MODE_FACTORY == action) {
  173. DEBUG_MSG_P(PSTR("\n\nFACTORY RESET\n\n"));
  174. resetSettings();
  175. deferredReset(100, CUSTOM_RESET_FACTORY);
  176. }
  177. #if defined(JUSTWIFI_ENABLE_WPS)
  178. if (BUTTON_MODE_WPS == action) {
  179. wifiStartWPS();
  180. }
  181. #endif // defined(JUSTWIFI_ENABLE_WPS)
  182. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  183. if (BUTTON_MODE_SMART_CONFIG == action) {
  184. wifiStartSmartConfig();
  185. }
  186. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  187. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  188. if (BUTTON_MODE_DIM_UP == action) {
  189. lightBrightnessStep(1);
  190. lightUpdate(true, true);
  191. }
  192. if (BUTTON_MODE_DIM_DOWN == action) {
  193. lightBrightnessStep(-1);
  194. lightUpdate(true, true);
  195. }
  196. #endif // LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  197. }
  198. struct DummyPin : virtual public DebounceEvent::PinBase {
  199. DummyPin(unsigned char pin) : DebounceEvent::PinBase(pin) {}
  200. void digitalWrite(int8_t val) {}
  201. void pinMode(int8_t mode) {}
  202. int digitalRead() { return 0; }
  203. };
  204. unsigned char buttonAdd(unsigned char pin, unsigned char mode, unsigned long actions, unsigned char relayID) {
  205. const unsigned char index = _buttons.size();
  206. button_event_delays_t delays {
  207. getSetting({"btnDebDelay", index}, _buttonDebounceDelay(index)),
  208. getSetting({"btnDblCDelay", index}, _buttonDoubleClickDelay(index)),
  209. getSetting({"btnLngCDelay", index}, _buttonLongClickDelay(index)),
  210. getSetting({"btnLngLngCDelay", index}, _buttonLongLongClickDelay(index))
  211. };
  212. _buttons.emplace_back(std::make_shared<DummyPin>(GPIO_NONE), BUTTON_PUSHBUTTON, actions, relayID, delays);
  213. return _buttons.size() - 1;
  214. }
  215. void buttonSetup() {
  216. // Special hardware cases
  217. #if (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_ITEAD_SONOFF_DUAL) || \
  218. (BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_FOXEL_LIGHTFOX_DUAL)
  219. size_t buttons = 0;
  220. #if BUTTON1_RELAY != RELAY_NONE
  221. ++buttons;
  222. #endif
  223. #if BUTTON2_RELAY != RELAY_NONE
  224. ++buttons;
  225. #endif
  226. #if BUTTON3_RELAY != RELAY_NONE
  227. ++buttons;
  228. #endif
  229. #if BUTTON4_RELAY != RELAY_NONE
  230. ++buttons;
  231. #endif
  232. _buttons.reserve(buttons);
  233. // Ignore default button modes
  234. const auto actions = _buttonConstructActions(
  235. BUTTON_MODE_NONE, BUTTON_MODE_TOGGLE, BUTTON_MODE_NONE,
  236. BUTTON_MODE_NONE, BUTTON_MODE_NONE, BUTTON_MODE_NONE
  237. );
  238. for (unsigned char id = 0; id < buttons; ++id) {
  239. buttonAdd(
  240. GPIO_NONE, BUTTON_PUSHBUTTON,
  241. actions, getSetting({"btnRelay", id}, _buttonRelay(id))
  242. );
  243. }
  244. // Generic GPIO input handlers
  245. #elif BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_GENERIC
  246. size_t buttons = 0;
  247. // TODO: no real point of doing this when running with dynamic settings
  248. // if there is limit like RELAYS_MAX - use that
  249. // if not, try to allocate some reasonable amount
  250. #if BUTTON1_PIN != GPIO_NONE
  251. ++buttons;
  252. #endif
  253. #if BUTTON2_PIN != GPIO_NONE
  254. ++buttons;
  255. #endif
  256. #if BUTTON3_PIN != GPIO_NONE
  257. ++buttons;
  258. #endif
  259. #if BUTTON4_PIN != GPIO_NONE
  260. ++buttons;
  261. #endif
  262. #if BUTTON5_PIN != GPIO_NONE
  263. ++buttons;
  264. #endif
  265. #if BUTTON6_PIN != GPIO_NONE
  266. ++buttons;
  267. #endif
  268. #if BUTTON7_PIN != GPIO_NONE
  269. ++buttons;
  270. #endif
  271. #if BUTTON8_PIN != GPIO_NONE
  272. ++buttons;
  273. #endif
  274. _buttons.reserve(buttons);
  275. for (unsigned char index = 0; index < buttons; ++index) {
  276. const auto pin = getSetting({"btnGPIO", index}, _buttonPin(index));
  277. if (!gpioValid(pin)) {
  278. break;
  279. }
  280. button_event_delays_t delays {
  281. getSetting({"btnDebDel", index}, _buttonDebounceDelay(index)),
  282. getSetting({"btnDblCDel", index}, _buttonDoubleClickDelay(index)),
  283. getSetting({"btnLngCDel", index}, _buttonLongClickDelay(index)),
  284. getSetting({"btnLngLngCDel", index}, _buttonLongLongClickDelay(index))
  285. };
  286. // TODO: allow to change DebounceEvent::DigitalPin to something else based on config
  287. _buttons.emplace_back(
  288. std::make_shared<DebounceEvent::DigitalPin>(pin),
  289. getSetting({"btnMode", index}, _buttonMode(index)),
  290. getSetting({"btnActions", index}, _buttonConstructActions(index)),
  291. getSetting({"btnRelay", index}, _buttonRelay(index)),
  292. delays
  293. );
  294. }
  295. #endif
  296. DEBUG_MSG_P(PSTR("[BUTTON] Number of buttons: %u\n"), _buttons.size());
  297. #if MQTT_SUPPORT
  298. for (unsigned char index = 0; index < _buttons.size(); ++index) {
  299. _buttons_mqtt_send_all[index] = getSetting({"btnMqttSendAll", index}, _buttonMqttSendAllEvents(index));
  300. _buttons_mqtt_retain[index] = getSetting({"btnMqttRetain", index}, _buttonMqttRetain(index));
  301. }
  302. #endif
  303. // Websocket Callbacks
  304. #if WEB_SUPPORT
  305. wsRegister()
  306. .onConnected(_buttonWebSocketOnVisible)
  307. .onVisible(_buttonWebSocketOnVisible)
  308. .onKeyCheck(_buttonWebSocketOnKeyCheck);
  309. #endif
  310. // Register system callbacks
  311. espurnaRegisterLoop(buttonLoop);
  312. }
  313. // Sonoff Dual does not do real GPIO readings and we
  314. // depend on the external MCU to send us relay / button events
  315. // TODO: move this to a separate 'hardware' setup file?
  316. #if BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_ITEAD_SONOFF_DUAL
  317. void _buttonLoopSonoffDual() {
  318. if (Serial.available() < 4) {
  319. return;
  320. }
  321. unsigned char bytes[4] = {0};
  322. Serial.readBytes(bytes, 4);
  323. if ((bytes[0] != 0xA0) || (bytes[1] != 0x04) && (bytes[3] != 0xA1)) {
  324. return;
  325. }
  326. const unsigned char value = bytes[2];
  327. // RELAYs and BUTTONs are synchonized in the SIL F330
  328. // The on-board BUTTON2 should toggle RELAY0 value
  329. // Since we are not passing back RELAY2 value
  330. // (in the relayStatus method) it will only be present
  331. // here if it has actually been pressed
  332. if ((value & 4) == 4) {
  333. buttonEvent(2, BUTTON_EVENT_CLICK);
  334. return;
  335. }
  336. // Otherwise check if any of the other two BUTTONs
  337. // (in the header) has been pressed, but we should
  338. // ensure that we only toggle one of them to avoid
  339. // the synchronization going mad
  340. // This loop is generic for any PSB-04 module
  341. for (unsigned int i=0; i<relayCount(); i++) {
  342. bool status = (value & (1 << i)) > 0;
  343. // Check if the status for that relay has changed
  344. if (relayStatus(i) != status) {
  345. buttonEvent(i, BUTTON_EVENT_CLICK);
  346. break;
  347. }
  348. }
  349. }
  350. #endif // BUTTON_EVENTS_SOURCE_ITEAD_SONOFF_DUAL == 1
  351. // Lightfox uses the same protocol as Dual, but has slightly different actions
  352. // TODO: same as above, move from here someplace else
  353. #if BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_FOXEL_LIGHTFOX_DUAL
  354. void _buttonLoopFoxelLightfox() {
  355. if (Serial.available() < 4) {
  356. return;
  357. }
  358. unsigned char bytes[4] = {0};
  359. Serial.readBytes(bytes, 4);
  360. if ((bytes[0] != 0xA0) || (bytes[1] != 0x04) && (bytes[3] != 0xA1)) {
  361. return;
  362. }
  363. const unsigned char value = bytes[2];
  364. DEBUG_MSG_P(PSTR("[BUTTON] [LIGHTFOX] Received buttons mask: %u\n"), value);
  365. for (unsigned int i=0; i<_buttons.size(); i++) {
  366. bool clicked = (value & (1 << i)) > 0;
  367. if (clicked) {
  368. buttonEvent(i, BUTTON_EVENT_CLICK);
  369. }
  370. }
  371. }
  372. #endif // BUTTON_EVENTS_SOURCE_FOXEL_LIGHTFOX_DUAL == 1
  373. void _buttonLoopGeneric() {
  374. for (size_t id = 0; id < _buttons.size(); ++id) {
  375. auto& button = _buttons[id];
  376. auto event = button.event_handler->loop();
  377. if (event != DebounceEvent::Types::EventNone) {
  378. buttonEvent(id, _buttonMapEvent(button, event));
  379. }
  380. }
  381. }
  382. void buttonLoop() {
  383. #if BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_GENERIC
  384. _buttonLoopGeneric();
  385. #elif BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_ITEAD_SONOFF_DUAL
  386. _buttonLoopSonoffDual();
  387. #elif BUTTON_EVENTS_SOURCE == BUTTON_EVENTS_SOURCE_FOXEL_LIGHTFOX_DUAL
  388. _buttonLoopFoxelLightfox();
  389. #else
  390. #warning "Unknown value for BUTTON_EVENTS_SOURCE"
  391. #endif
  392. }
  393. #endif // BUTTON_SUPPORT