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 protected:
51 int valueOnPress() const;
52
53 Supla::Io::Base *io = nullptr;
54
55 uint16_t debounceDelayMs = 50;
56 uint16_t swNoiseFilterDelayMs = 20;
57 uint32_t debounceTimestampMs = 0;
58 uint32_t filterTimestampMs = 0;
59 int16_t pin = -1;
60 int8_t newStatusCandidate = 0;
61 int8_t prevState = -1;
62 bool pullUp = false;
63 bool invertLogic = false;
64};
65
66class SimpleButton : public Element, public LocalAction {
67 public:
68 explicit SimpleButton(Supla::Io::Base *io,
69 int pin,
70 bool pullUp = false,
71 bool invertLogic = false);
72 explicit SimpleButton(int pin, bool pullUp = false, bool invertLogic = false);
73
74 void onTimer() override;
75 void onInit() override;
76 void setSwNoiseFilterDelay(unsigned int newDelayMs);
77 void setDebounceDelay(unsigned int newDelayMs);
78
79 enum StateResults getLastState() const;
80
81 protected:
82 // Returns unique button number (current implementation returns configured
83 // GPIO)
84 virtual int8_t getButtonNumber() const;
85 ButtonState state;
86};
87
88}; // namespace Control
89}; // namespace Supla
90
91#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:111
void onTimer() override
Method called on timer interupt.
Definition simple_button.cpp:100
Definition io.h:35