Finalized new SpanButton and PushButton functionality
Streamlined interface for both functions. To Do: Update SpanButton documentation
This commit is contained in:
parent
9191b8664e
commit
ee55f268bd
|
|
@ -1963,7 +1963,7 @@ SpanRange::SpanRange(int min, int max, int step){
|
|||
// SpanButton //
|
||||
///////////////////////////////
|
||||
|
||||
SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, Button buttonType) : PushButton(pin, buttonType){
|
||||
SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, pressTest_t pressed) : PushButton(pin, pressed){
|
||||
|
||||
if(homeSpan.Accessories.empty() || homeSpan.Accessories.back()->Services.empty()){
|
||||
Serial.printf("\nFATAL ERROR! Can't create new SpanButton(%d,%u,%u,%u) without a defined Service ***\n",pin,longTime,singleTime,doubleTime);
|
||||
|
|
|
|||
|
|
@ -711,9 +711,8 @@ class SpanButton : PushButton {
|
|||
LONG=2
|
||||
};
|
||||
|
||||
SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200, Button buttonType=Button::GROUNDED);
|
||||
SpanButton(int pin, Button buttonType, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,buttonType){};
|
||||
SpanButton(int pin, boolean (*pressed)(int pin), uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,Button::CUSTOM){this->pressed=pressed;};
|
||||
SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200, pressTest_t pressed=GROUNDED);
|
||||
SpanButton(int pin, pressTest_t pressed=GROUNDED, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200) : SpanButton(pin,longTime,singleTime,doubleTime,pressed){};
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -85,40 +85,22 @@ String Utils::mask(char *c, int n){
|
|||
// PushButton //
|
||||
////////////////////////////////
|
||||
|
||||
PushButton::PushButton(int pin, Button buttonType){
|
||||
PushButton::PushButton(int pin, pressTest_t pressed){
|
||||
|
||||
this->pin=pin;
|
||||
this->pressed=pressed;
|
||||
status=0;
|
||||
doubleCheck=false;
|
||||
|
||||
switch(buttonType){
|
||||
|
||||
case Button::GROUNDED:
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
pressed=groundedButton;
|
||||
break;
|
||||
|
||||
case Button::POWERED:
|
||||
pinMode(pin, INPUT_PULLDOWN);
|
||||
pressed=poweredButton;
|
||||
break;
|
||||
|
||||
case Button::TOUCH:
|
||||
pressed=touchButton;
|
||||
break;
|
||||
|
||||
};
|
||||
if(pressed==GROUNDED)
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
else if(pressed==POWERED)
|
||||
pinMode(pin, INPUT_PULLDOWN);
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
PushButton::PushButton(int pin, boolean (*pressed)(int pin)) : PushButton(pin, Button::CUSTOM){
|
||||
this->pressed=pressed;
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t doubleTime){
|
||||
|
||||
unsigned long cTime=millis();
|
||||
|
|
|
|||
36
src/Utils.h
36
src/Utils.h
|
|
@ -37,13 +37,6 @@ String mask(char *c, int n); // simply utility that creates a String fr
|
|||
|
||||
}
|
||||
|
||||
enum class Button {
|
||||
GROUNDED,
|
||||
POWERED,
|
||||
TOUCH,
|
||||
CUSTOM
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Creates a temporary buffer that is freed after
|
||||
// going out of scope
|
||||
|
|
@ -88,15 +81,13 @@ class PushButton{
|
|||
int pressType;
|
||||
|
||||
static uint16_t touchThreshold;
|
||||
|
||||
static boolean groundedButton(int pin){return(!digitalRead(pin));}
|
||||
static boolean poweredButton(int pin){return(digitalRead(pin));}
|
||||
static boolean touchButton(int pin){return(touchRead(pin)<touchThreshold);}
|
||||
|
||||
protected:
|
||||
|
||||
typedef boolean (*pressTest_t)(int pin);
|
||||
|
||||
int pin;
|
||||
boolean (*pressed)(int pin);
|
||||
pressTest_t pressed;
|
||||
|
||||
public:
|
||||
|
||||
|
|
@ -106,19 +97,20 @@ class PushButton{
|
|||
LONG=2
|
||||
};
|
||||
|
||||
PushButton(int pin, Button buttonType=Button::GROUNDED);
|
||||
static boolean GROUNDED(int pin){return(!digitalRead(pin));}
|
||||
static boolean POWERED(int pin){return(digitalRead(pin));}
|
||||
static boolean TOUCH(int pin){return(touchRead(pin)<touchThreshold);}
|
||||
|
||||
PushButton(int pin, pressTest_t pressed=GROUNDED);
|
||||
|
||||
// Creates pushbutton of specified type on specified pin
|
||||
//
|
||||
// pin: Pin mumber to which pushbutton connects to ground when pressed
|
||||
// buttonType: sets button type to Button::GROUNDED, Button::POWERED, or Button::TOUCH
|
||||
|
||||
PushButton(int pin, boolean (*pressed)(int pin));
|
||||
|
||||
// Creates generic pushbutton using custom boolean function that accepts a single paramater
|
||||
//
|
||||
// pin: an arbitrary INT that is passed to the user-define BOOLEAN function. Usually used to represent a pin number
|
||||
// pressed: a user-defined BOOLEAN function that takes a single INT argument and returns TRUE if "button" is pressed, or FALSE if not
|
||||
// pin: pin number to which the button is connected
|
||||
// pressed: a function of of the form 'boolean f(int)' that is passed
|
||||
// the parameter *pin* and returns TRUE if the button associated
|
||||
// with *pin* is pressed, or FALSE if not. Can choose from 3 pre-specifed
|
||||
// pressTest_t functions (GROUNDED, POWERED, and TOUCH), or write your
|
||||
// own custom handler
|
||||
|
||||
void reset();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue