supla-device
Loading...
Searching...
No Matches
button.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_CONTROL_BUTTON_H_
5#define SRC_SUPLA_CONTROL_BUTTON_H_
6
7#include <stdint.h>
8#include <supla/action_handler.h>
9#include "action_trigger.h"
10#include "simple_button.h"
11
13
14namespace Supla {
15namespace Io {
16class Base;
17} // namespace Io
18
19namespace Control {
20
21class Button : public SimpleButton, public ActionHandler {
22 public:
23 friend class ActionTrigger;
24 enum class ButtonType : uint8_t {
25 MONOSTABLE,
26 BISTABLE,
27 MOTION_SENSOR,
28 CENTRAL_CONTROL
29 };
30
31 enum class OnLoadConfigType : uint8_t {
32 LOAD_FULL_CONFIG,
33 LOAD_BUTTON_SETUP_ONLY,
34 DONT_LOAD_CONFIG
35 };
36
37 explicit Button(Supla::Io::IoPin inputPin);
38 explicit Button(Supla::Io::Base *io,
39 int pin,
40 bool pullUp = false,
41 bool invertLogic = false);
42 explicit Button(int pin, bool pullUp = false, bool invertLogic = false);
43
44 void onTimer() override;
45 void onLoadConfig(SuplaDeviceClass *) override;
46 void onInit() override;
47 void addAction(uint16_t action, ActionHandler &client, uint16_t event,
48 bool alwaysEnabled = false) override;
49 void addAction(uint16_t action, ActionHandler *client, uint16_t event,
50 bool alwaysEnabled = false) override;
51 void disableAction(int32_t action,
52 ActionHandler *client,
53 int32_t event) override;
54 void enableAction(int32_t action,
55 ActionHandler *client,
56 int32_t event) override;
57
58 void setHoldTime(unsigned int timeMs);
59 void repeatOnHoldEvery(unsigned int timeMs);
60
61 // setting of bistableButton is for backward compatiblity.
62 // Use setButtonType instaed.
63 void setMulticlickTime(unsigned int timeMs, bool bistableButton = false);
64
65 void setButtonType(const ButtonType type);
66 bool isBistable() const;
67 bool isMonostable() const;
68 bool isMotionSensor() const;
69 bool isCentral() const;
70
71 virtual void configureAsConfigButton(SuplaDeviceClass *sdc);
72 bool disableActionsInConfigMode() override;
73 void dontUseOnLoadConfig();
74 void setOnLoadConfigType(OnLoadConfigType type);
75
76 uint8_t getMaxMulticlickValue();
77 int8_t getButtonNumber() const override;
78 void setButtonNumber(int8_t number);
79
80 void handleAction(int event, int action) override;
81
82 void disableButton();
83 void enableButton();
84 void waitForRelease();
85
86 uint32_t getLastStateChange() const;
87
88 void setAllowHoldOnPowerOn(bool allow) {
89 runtimeFlags.allowHoldOnPowerOn = allow;
90 }
91
92 protected:
93 // A local copy of the active AT expiry guards timer-driven button events.
94 void setTemporaryUnlockDeadline(uint32_t startMs, uint32_t durationMs);
95 struct RuntimeFlags {
96 uint16_t actionTriggerModeLocked : 1;
97 uint16_t actionTriggerLocalUnlockAllowed : 1;
98 uint16_t keepConfigButtonTriggerAlwaysAvailable : 1;
99 uint16_t actionTriggerSuppressActionsUntilRelease : 1;
100 uint16_t actionTriggerDispatching : 1;
101 uint16_t suppressActionsUntilRelease : 1;
102 uint16_t repeatOnHoldEnabled : 1;
103 uint16_t configButton : 1;
104 uint16_t disabled : 1;
105 uint16_t allowHoldOnPowerOn : 1;
106 uint16_t waitingForRelease : 1;
107 uint16_t conditionalActionsOnClick1 : 1;
108 uint16_t temporaryUnlockDeadlineActive : 1;
109 uint16_t reserved : 3;
110 };
111
112 static_assert(sizeof(RuntimeFlags) == sizeof(uint16_t),
113 "Button runtime flags must fit in uint16_t");
114
115 void setActionTriggerModeLocked(bool locked);
116 void setActionTriggerModeLocked(bool locked,
117 bool localUnlockAllowed,
118 bool keepConfigButtonTriggerAlwaysAvailable);
119 bool runActionWithActionTriggerPolicy(uint16_t event);
120 void evaluateMaxMulticlickValue();
121 // Used by ActionTrigger for bistable directional press/release pairs.
122 // Counting clicks (including CFG x10) remains independent of this policy.
123 // Reapplying the same policy preserves pending clicks and release timing.
124 void setConditionalActionsOnClick1(bool enabled);
125 // disbles repeating "on hold" if repeat time is lower than threshold
126 // threshold 0 disables always
127 void disableRepeatOnHold(uint32_t threshold = 0);
128 void enableRepeatOnHold();
129 const char *getButtonTypeName(ButtonType type) const;
130 uint32_t lastStateChangeMs = 0;
131 uint32_t temporaryUnlockDeadlineMs = 0;
132 uint16_t repeatOnHoldMs = 0;
133 uint16_t holdSend = 0;
134 uint16_t holdTimeMs = 0;
135 uint16_t multiclickTimeMs = 0;
136 RuntimeFlags runtimeFlags = {};
137 ButtonType buttonType = ButtonType::MONOSTABLE;
138 enum OnLoadConfigType onLoadConfigType = OnLoadConfigType::LOAD_FULL_CONFIG;
139
140 uint8_t clickCounter = 0;
141 uint8_t maxMulticlickValueConfigured = 0;
142 int8_t buttonNumber = -1;
143
144 static int buttonCounter;
145};
146
147}; // namespace Control
148}; // namespace Supla
149
150#endif // SRC_SUPLA_CONTROL_BUTTON_H_
Definition SuplaDevice.h:167
Definition action_handler.h:8
Definition action_trigger.h:33
Definition button.h:21
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition button.cpp:44
void onTimer() override
Method called on timer interupt.
Definition button.cpp:130
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition button.cpp:505
Definition simple_button.h:48
Definition io.h:23
Definition io_pin.h:14