Added option to set SpanButton type
Types include: GROUNDED, POWERED, and TOUCH. Also added configureTouch() to customize parameters used for touch sensors.
This commit is contained in:
parent
54b8374573
commit
7841081fda
|
|
@ -61,6 +61,8 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
|
|||
|
||||
statusLED.init(statusPin,0,autoOffLED);
|
||||
|
||||
PushButton::configureTouch(4000,1000,10); // set default parameters for any touch-style pushbuttons
|
||||
|
||||
if(requestedMaxCon<maxConnections) // if specific request for max connections is less than computed max connections
|
||||
maxConnections=requestedMaxCon; // over-ride max connections with requested value
|
||||
|
||||
|
|
@ -1961,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){
|
||||
SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, Button buttonType){
|
||||
|
||||
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);
|
||||
|
|
@ -1975,7 +1977,7 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t
|
|||
this->doubleTime=doubleTime;
|
||||
service=homeSpan.Accessories.back()->Services.back();
|
||||
|
||||
pushButton=new PushButton(pin); // create underlying PushButton
|
||||
pushButton=new PushButton(pin,buttonType); // create underlying PushButton
|
||||
homeSpan.PushButtons.push_back(this);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -713,7 +713,10 @@ class SpanButton{
|
|||
LONG=2
|
||||
};
|
||||
|
||||
SpanButton(int pin, uint16_t longTime=2000, uint16_t singleTime=5, uint16_t doubleTime=200);
|
||||
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){};
|
||||
|
||||
static void configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){PushButton::configureTouch(measureTime,sleepTime,thresh);}
|
||||
};
|
||||
|
||||
///////////////////////////////
|
||||
|
|
|
|||
|
|
@ -85,12 +85,30 @@ String Utils::mask(char *c, int n){
|
|||
// PushButton //
|
||||
////////////////////////////////
|
||||
|
||||
PushButton::PushButton(int pin){
|
||||
PushButton::PushButton(int pin, Button buttonType){
|
||||
|
||||
this->pin=pin;
|
||||
status=0;
|
||||
doubleCheck=false;
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
|
@ -108,7 +126,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
|||
return(true);
|
||||
}
|
||||
|
||||
if(!digitalRead(pin)){ // button is pressed
|
||||
if(pressed(pin)){ // button is pressed
|
||||
singleAlarm=cTime+singleTime;
|
||||
if(!doubleCheck){
|
||||
status=1;
|
||||
|
|
@ -122,7 +140,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
|||
|
||||
case 1:
|
||||
case 2:
|
||||
if(digitalRead(pin)){ // button is released
|
||||
if(!pressed(pin)){ // button is released
|
||||
status=0;
|
||||
if(cTime>singleAlarm){
|
||||
doubleCheck=true;
|
||||
|
|
@ -138,7 +156,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
|||
break;
|
||||
|
||||
case 3:
|
||||
if(digitalRead(pin)) // button has been released after a long press
|
||||
if(!pressed(pin)) // button has been released after a long press
|
||||
status=0;
|
||||
else if(cTime>longAlarm){
|
||||
longAlarm=cTime+longTime;
|
||||
|
|
@ -148,7 +166,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
|||
break;
|
||||
|
||||
case 4:
|
||||
if(digitalRead(pin)){ // button is released
|
||||
if(!pressed(pin)){ // button is released
|
||||
status=0;
|
||||
} else
|
||||
|
||||
|
|
@ -161,7 +179,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
|||
break;
|
||||
|
||||
case 5:
|
||||
if(digitalRead(pin)) // button has been released after double-click
|
||||
if(!pressed(pin)) // button has been released after double-click
|
||||
status=0;
|
||||
break;
|
||||
|
||||
|
|
@ -191,7 +209,7 @@ int PushButton::type(){
|
|||
//////////////////////////////////////
|
||||
|
||||
void PushButton::wait(){
|
||||
while(!digitalRead(pin));
|
||||
while(pressed(pin));
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
|
@ -200,6 +218,10 @@ void PushButton::reset(){
|
|||
status=0;
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
uint16_t PushButton::touchThreshold;
|
||||
|
||||
////////////////////////////////
|
||||
// Blinker //
|
||||
////////////////////////////////
|
||||
|
|
|
|||
19
src/Utils.h
19
src/Utils.h
|
|
@ -37,6 +37,12 @@ String mask(char *c, int n); // simply utility that creates a String fr
|
|||
|
||||
}
|
||||
|
||||
enum class Button {
|
||||
GROUNDED,
|
||||
POWERED,
|
||||
TOUCH
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Creates a temporary buffer that is freed after
|
||||
// going out of scope
|
||||
|
|
@ -80,6 +86,13 @@ class PushButton{
|
|||
uint32_t doubleAlarm;
|
||||
uint32_t longAlarm;
|
||||
int pressType;
|
||||
boolean (*pressed)(int pin);
|
||||
|
||||
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);}
|
||||
|
||||
public:
|
||||
|
||||
|
|
@ -89,7 +102,7 @@ class PushButton{
|
|||
LONG=2
|
||||
};
|
||||
|
||||
PushButton(int pin);
|
||||
PushButton(int pin, Button buttonType=Button::GROUNDED);
|
||||
|
||||
// Creates generic pushbutton functionality on specified pin.
|
||||
//
|
||||
|
|
@ -137,6 +150,10 @@ class PushButton{
|
|||
|
||||
// Returns pin number
|
||||
|
||||
static void configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){touchSetCycles(measureTime,sleepTime);touchThreshold=thresh;}
|
||||
|
||||
// Sets the measure time, sleep time, and lower threshold that triggers a touch - used only when buttonType=Button::TOUCH
|
||||
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
|
|
|
|||
|
|
@ -13,6 +13,13 @@ void setup() {
|
|||
|
||||
Serial.begin(115200);
|
||||
|
||||
touchSetCycles(4000,1000);
|
||||
|
||||
while(1){
|
||||
Serial.println(touchRead(4));
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
// homeSpan.setLogLevel(2);
|
||||
// homeSpan.setStatusPin(13);
|
||||
homeSpan.setControlPin(33);
|
||||
|
|
|
|||
Loading…
Reference in New Issue