supla-device
Loading...
Searching...
No Matches
simple_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_SIMPLE_BUTTON_H_
5#define SRC_SUPLA_CONTROL_SIMPLE_BUTTON_H_
6
7#include <stdint.h>
8
9#include "../io.h"
10#include "../element.h"
11#include "../events.h"
12#include "../local_action.h"
13
14namespace Supla {
15namespace Control {
16
17enum StateResults { PRESSED, RELEASED, TO_PRESSED, TO_RELEASED };
18
20 public:
21 explicit ButtonState(Supla::Io::IoPin inputPin);
22 ButtonState(Supla::Io::Base *io, int pin, bool pullUp, bool invertLogic);
23 ButtonState(int pin, bool pullUp, bool invertLogic);
24 enum StateResults update();
25 enum StateResults getLastState() const;
26 void init(int buttonNumber);
27
28 void setSwNoiseFilterDelay(unsigned int newDelayMs);
29 void setDebounceDelay(unsigned int newDelayMs);
30 int getGpio() const;
31
32 bool isReady() const;
33
34 protected:
35 int valueOnPress() const;
36
37 Supla::Io::IoPin inputPin;
38
39 uint16_t debounceDelayMs = 50;
40 uint16_t swNoiseFilterDelayMs = 20;
41 uint32_t debounceTimestampMs = 0;
42 uint32_t filterTimestampMs = 0;
43 int8_t newStatusCandidate = 0;
44 int8_t prevState = -1;
45};
46
47class SimpleButton : public Element, public LocalAction {
48 public:
49 explicit SimpleButton(Supla::Io::IoPin inputPin);
50 explicit SimpleButton(Supla::Io::Base *io,
51 int pin,
52 bool pullUp = false,
53 bool invertLogic = false);
54 explicit SimpleButton(int pin, bool pullUp = false, bool invertLogic = false);
55
56 void onTimer() override;
57 void onInit() override;
58 void setSwNoiseFilterDelay(unsigned int newDelayMs);
59 void setDebounceDelay(unsigned int newDelayMs);
60
61 enum StateResults getLastState() const;
62
63 bool isReady() const;
64
65 protected:
66 // Returns unique button number (current implementation returns configured
67 // GPIO)
68 virtual int8_t getButtonNumber() const;
69 ButtonState state;
70};
71
72}; // namespace Control
73}; // namespace Supla
74
75#endif // SRC_SUPLA_CONTROL_SIMPLE_BUTTON_H_
Definition simple_button.h:19
Definition simple_button.h:47
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition simple_button.cpp:118
void onTimer() override
Method called on timer interupt.
Definition simple_button.cpp:104
Base class for all elements of SuplaDevice.
Definition element.h:27
Definition io.h:23
Definition local_action.h:42
Definition io_pin.h:14