2
0
Fork 0

implement invert

This commit is contained in:
Siwat Sirichai 2024-07-07 11:51:51 +07:00
parent f2e6289330
commit 5b66d34d58
3 changed files with 8 additions and 4 deletions

View File

@ -1,12 +1,13 @@
#include "button.hpp"
Button::Button(DigitalInputCard *inputCard, uint8_t inputPin)
Button::Button(DigitalInputCard *inputCard, uint8_t inputPin, bool invert)
{
this->inputCard = inputCard;
this->inputPin = inputPin;
this->input_handler_id = -1; // initialize to MAX_UINT16
this->shortPressTime = 100; // default short press time
this->longPressTime = 2000; // default long press time
this->invert = invert;
}
Button::~Button()
@ -48,6 +49,8 @@ void Button::buttonChangedCallback(uint8_t pin, uint8_t value)
if (pin != this->inputPin)
return;
value = this->invert ? !value : value;
if (value) {
// button pressed
this->buttonState = true;

View File

@ -2,7 +2,7 @@
class Button {
public:
Button(DigitalInputCard* inputCard, uint8_t inputPin);
Button(DigitalInputCard* inputCard, uint8_t inputPin, bool invert = false);
~Button();
void setMinLongPressTime(uint32_t time);
void setMinShortPressTime(uint32_t time);
@ -19,6 +19,7 @@ class Button {
std::function<void()> longPressCallback;
std::function<void()> shortPressCallback;
uint32_t pressedTime;
bool invert;
bool buttonState;
bool longPressTriggered;
void buttonChangedCallback(uint8_t pin, uint8_t value);

View File

@ -5,9 +5,9 @@
ESPMegaPRO espmega = ESPMegaPRO();
ESPMegaDisplayOTA internalDisplayOTA = ESPMegaDisplayOTA();
Button rightOnButton(&espmega.inputs, RIGHT_ON_BUTTON_PIN);
Button rightOffButton(&espmega.inputs, RIGHT_OFF_BUTTON_PIN);
Button rightOffButton(&espmega.inputs, RIGHT_OFF_BUTTON_PIN, true);
Button leftOnButton(&espmega.inputs, LEFT_ON_BUTTON_PIN);
Button leftOffButton(&espmega.inputs, LEFT_OFF_BUTTON_PIN);
Button leftOffButton(&espmega.inputs, LEFT_OFF_BUTTON_PIN, true);
SmartVariable toiletLightsLock;
SmartVariable gymLightsLock;