supla-device
Loading...
Searching...
No Matches
simple_button.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_CONTROL_SIMPLE_BUTTON_H_
20#define SRC_SUPLA_CONTROL_SIMPLE_BUTTON_H_
21
22#include <stdint.h>
23
24#include "../element.h"
25#include "../events.h"
26#include "../local_action.h"
27
28namespace Supla {
29
30namespace Io {
31class Base;
32}
33
34namespace Control {
35
36enum StateResults { PRESSED, RELEASED, TO_PRESSED, TO_RELEASED };
37
38class ButtonState {
39 public:
40 ButtonState(Supla::Io::Base *io, int pin, bool pullUp, bool invertLogic);
41 ButtonState(int pin, bool pullUp, bool invertLogic);
42 enum StateResults update();
43 enum StateResults getLastState() const;
44 void init(int buttonNumber);
45
46 void setSwNoiseFilterDelay(unsigned int newDelayMs);
47 void setDebounceDelay(unsigned int newDelayMs);
48 int getGpio() const;
49
50 bool isReady() const;
51
52 protected:
53 int valueOnPress() const;
54
55 Supla::Io::Base *io = nullptr;
56
57 uint16_t debounceDelayMs = 50;
58 uint16_t swNoiseFilterDelayMs = 20;
59 uint32_t debounceTimestampMs = 0;
60 uint32_t filterTimestampMs = 0;
61 int16_t pin = -1;
62 int8_t newStatusCandidate = 0;
63 int8_t prevState = -1;
64 bool pullUp = false;
65 bool invertLogic = false;
66};
67
68class SimpleButton : public Element, public LocalAction {
69 public:
70 explicit SimpleButton(Supla::Io::Base *io,
71 int pin,
72 bool pullUp = false,
73 bool invertLogic = false);
74 explicit SimpleButton(int pin, bool pullUp = false, bool invertLogic = false);
75
76 void onTimer() override;
77 void onInit() override;
78 void setSwNoiseFilterDelay(unsigned int newDelayMs);
79 void setDebounceDelay(unsigned int newDelayMs);
80
81 enum StateResults getLastState() const;
82
83 bool isReady() const;
84
85 protected:
86 // Returns unique button number (current implementation returns configured
87 // GPIO)
88 virtual int8_t getButtonNumber() const;
89 ButtonState state;
90};
91
92}; // namespace Control
93}; // namespace Supla
94
95#endif // SRC_SUPLA_CONTROL_SIMPLE_BUTTON_H_
Definition simple_button.h:38
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition simple_button.cpp:121
void onTimer() override
Method called on timer interupt.
Definition simple_button.cpp:107
Definition io.h:35