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
30class Io;
31
32namespace Control {
33
34enum StateResults { PRESSED, RELEASED, TO_PRESSED, TO_RELEASED };
35
36class ButtonState {
37 public:
38 ButtonState(Supla::Io *io, int pin, bool pullUp, bool invertLogic);
39 ButtonState(int pin, bool pullUp, bool invertLogic);
40 enum StateResults update();
41 enum StateResults getLastState() const;
42 void init(int buttonNumber);
43
44 void setSwNoiseFilterDelay(unsigned int newDelayMs);
45 void setDebounceDelay(unsigned int newDelayMs);
46 int getGpio() const;
47
48 protected:
49 int valueOnPress() const;
50
51 Supla::Io *io = nullptr;
52
53 uint16_t debounceDelayMs = 50;
54 uint16_t swNoiseFilterDelayMs = 20;
55 uint32_t debounceTimestampMs = 0;
56 uint32_t filterTimestampMs = 0;
57 int16_t pin = -1;
58 int8_t newStatusCandidate = 0;
59 int8_t prevState = -1;
60 bool pullUp = false;
61 bool invertLogic = false;
62};
63
64class SimpleButton : public Element, public LocalAction {
65 public:
66 explicit SimpleButton(Supla::Io *io,
67 int pin,
68 bool pullUp = false,
69 bool invertLogic = false);
70 explicit SimpleButton(int pin, bool pullUp = false, bool invertLogic = false);
71
72 void onTimer() override;
73 void onInit() override;
74 void setSwNoiseFilterDelay(unsigned int newDelayMs);
75 void setDebounceDelay(unsigned int newDelayMs);
76
77 enum StateResults getLastState() const;
78
79 protected:
80 // Returns unique button number (current implementation returns configured
81 // GPIO)
82 virtual int8_t getButtonNumber() const;
83 ButtonState state;
84};
85
86}; // namespace Control
87}; // namespace Supla
88
89#endif // SRC_SUPLA_CONTROL_SIMPLE_BUTTON_H_
Definition simple_button.h:36
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition simple_button.cpp:105
void onTimer() override
Method called on timer interupt.
Definition simple_button.cpp:94
Definition io.h:33