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);
|
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
|
if(requestedMaxCon<maxConnections) // if specific request for max connections is less than computed max connections
|
||||||
maxConnections=requestedMaxCon; // over-ride max connections with requested value
|
maxConnections=requestedMaxCon; // over-ride max connections with requested value
|
||||||
|
|
||||||
|
|
@ -1961,7 +1963,7 @@ SpanRange::SpanRange(int min, int max, int step){
|
||||||
// SpanButton //
|
// 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()){
|
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);
|
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;
|
this->doubleTime=doubleTime;
|
||||||
service=homeSpan.Accessories.back()->Services.back();
|
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);
|
homeSpan.PushButtons.push_back(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -713,7 +713,10 @@ class SpanButton{
|
||||||
LONG=2
|
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::PushButton(int pin){
|
PushButton::PushButton(int pin, Button buttonType){
|
||||||
|
|
||||||
this->pin=pin;
|
this->pin=pin;
|
||||||
status=0;
|
status=0;
|
||||||
doubleCheck=false;
|
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);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!digitalRead(pin)){ // button is pressed
|
if(pressed(pin)){ // button is pressed
|
||||||
singleAlarm=cTime+singleTime;
|
singleAlarm=cTime+singleTime;
|
||||||
if(!doubleCheck){
|
if(!doubleCheck){
|
||||||
status=1;
|
status=1;
|
||||||
|
|
@ -122,7 +140,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
if(digitalRead(pin)){ // button is released
|
if(!pressed(pin)){ // button is released
|
||||||
status=0;
|
status=0;
|
||||||
if(cTime>singleAlarm){
|
if(cTime>singleAlarm){
|
||||||
doubleCheck=true;
|
doubleCheck=true;
|
||||||
|
|
@ -138,7 +156,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
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;
|
status=0;
|
||||||
else if(cTime>longAlarm){
|
else if(cTime>longAlarm){
|
||||||
longAlarm=cTime+longTime;
|
longAlarm=cTime+longTime;
|
||||||
|
|
@ -148,7 +166,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 4:
|
case 4:
|
||||||
if(digitalRead(pin)){ // button is released
|
if(!pressed(pin)){ // button is released
|
||||||
status=0;
|
status=0;
|
||||||
} else
|
} else
|
||||||
|
|
||||||
|
|
@ -161,7 +179,7 @@ boolean PushButton::triggered(uint16_t singleTime, uint16_t longTime, uint16_t d
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 5:
|
case 5:
|
||||||
if(digitalRead(pin)) // button has been released after double-click
|
if(!pressed(pin)) // button has been released after double-click
|
||||||
status=0;
|
status=0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -191,7 +209,7 @@ int PushButton::type(){
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
||||||
void PushButton::wait(){
|
void PushButton::wait(){
|
||||||
while(!digitalRead(pin));
|
while(pressed(pin));
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
|
|
@ -200,6 +218,10 @@ void PushButton::reset(){
|
||||||
status=0;
|
status=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////
|
||||||
|
|
||||||
|
uint16_t PushButton::touchThreshold;
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
// Blinker //
|
// Blinker //
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|
|
||||||
21
src/Utils.h
21
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
|
// Creates a temporary buffer that is freed after
|
||||||
// going out of scope
|
// going out of scope
|
||||||
|
|
@ -80,7 +86,14 @@ class PushButton{
|
||||||
uint32_t doubleAlarm;
|
uint32_t doubleAlarm;
|
||||||
uint32_t longAlarm;
|
uint32_t longAlarm;
|
||||||
int pressType;
|
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:
|
public:
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
|
|
@ -88,8 +101,8 @@ class PushButton{
|
||||||
DOUBLE=1,
|
DOUBLE=1,
|
||||||
LONG=2
|
LONG=2
|
||||||
};
|
};
|
||||||
|
|
||||||
PushButton(int pin);
|
PushButton(int pin, Button buttonType=Button::GROUNDED);
|
||||||
|
|
||||||
// Creates generic pushbutton functionality on specified pin.
|
// Creates generic pushbutton functionality on specified pin.
|
||||||
//
|
//
|
||||||
|
|
@ -137,6 +150,10 @@ class PushButton{
|
||||||
|
|
||||||
// Returns pin number
|
// 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
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,13 @@ CUSTOM_CHAR_STRING(DarkMode, AAAAAAAA-BBBB-AAAA-AAAA-AAAAAAAAAAAA, PR, "MY_VALUE
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
touchSetCycles(4000,1000);
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
Serial.println(touchRead(4));
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
|
||||||
// homeSpan.setLogLevel(2);
|
// homeSpan.setLogLevel(2);
|
||||||
// homeSpan.setStatusPin(13);
|
// homeSpan.setStatusPin(13);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue