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