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 struct RuntimeFlags {
94 uint16_t actionTriggerModeLocked : 1;
95 uint16_t actionTriggerLocalUnlockAllowed : 1;
96 uint16_t keepConfigButtonTriggerAlwaysAvailable : 1;
97 uint16_t actionTriggerSuppressActionsUntilRelease : 1;
98 uint16_t actionTriggerDispatching : 1;
99 uint16_t suppressActionsUntilRelease : 1;
100 uint16_t repeatOnHoldEnabled : 1;
101 uint16_t configButton : 1;
102 uint16_t disabled : 1;
103 uint16_t allowHoldOnPowerOn : 1;
104 uint16_t waitingForRelease : 1;
105 uint16_t conditionalActionsOnClick1 : 1;
106 uint16_t reserved : 4;
107 };
108
109 static_assert(sizeof(RuntimeFlags) == sizeof(uint16_t),
110 "Button runtime flags must fit in uint16_t");
111
112 void setActionTriggerModeLocked(bool locked);
113 void setActionTriggerModeLocked(bool locked,
114 bool localUnlockAllowed,
115 bool keepConfigButtonTriggerAlwaysAvailable);
116 bool runActionWithActionTriggerPolicy(uint16_t event);
117 void evaluateMaxMulticlickValue();
118 // Used by ActionTrigger for bistable directional press/release pairs.
119 // Counting clicks (including CFG x10) remains independent of this policy.
120 // Reapplying the same policy preserves pending clicks and release timing.
121 void setConditionalActionsOnClick1(bool enabled);
122 // disbles repeating "on hold" if repeat time is lower than threshold
123 // threshold 0 disables always
124 void disableRepeatOnHold(uint32_t threshold = 0);
125 void enableRepeatOnHold();
126 const char *getButtonTypeName(ButtonType type) const;
127 uint32_t lastStateChangeMs = 0;
128 uint16_t repeatOnHoldMs = 0;
129 uint16_t holdSend = 0;
130 uint16_t holdTimeMs = 0;
131 uint16_t multiclickTimeMs = 0;
132 RuntimeFlags runtimeFlags = {};
133 ButtonType buttonType = ButtonType::MONOSTABLE;
134 enum OnLoadConfigType onLoadConfigType = OnLoadConfigType::LOAD_FULL_CONFIG;
135
136 uint8_t clickCounter = 0;
137 uint8_t maxMulticlickValueConfigured = 0;
138 int8_t buttonNumber = -1;
139
140 static int buttonCounter;
141};
142
143}; // namespace Control
144}; // namespace Supla
145
146#endif // SRC_SUPLA_CONTROL_BUTTON_H_
Definition SuplaDevice.h:167
Definition action_handler.h:8
Definition action_trigger.h:32
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:125
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition button.cpp:490
Definition simple_button.h:48
Definition io.h:23
Definition io_pin.h:14