Adding generic SpanButton function
This commit is contained in:
parent
7841081fda
commit
9191b8664e
|
|
@ -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){
|
||||
SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t doubleTime, Button buttonType) : PushButton(pin, 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);
|
||||
|
|
@ -1971,13 +1971,11 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t
|
|||
while(1);
|
||||
}
|
||||
|
||||
this->pin=pin;
|
||||
this->longTime=longTime;
|
||||
this->singleTime=singleTime;
|
||||
this->doubleTime=doubleTime;
|
||||
service=homeSpan.Accessories.back()->Services.back();
|
||||
|
||||
pushButton=new PushButton(pin,buttonType); // create underlying PushButton
|
||||
homeSpan.PushButtons.push_back(this);
|
||||
}
|
||||
|
||||
|
|
@ -1985,8 +1983,8 @@ SpanButton::SpanButton(int pin, uint16_t longTime, uint16_t singleTime, uint16_t
|
|||
|
||||
void SpanButton::check(){
|
||||
|
||||
if(pushButton->triggered(singleTime,longTime,doubleTime)) // if the underlying PushButton is triggered
|
||||
service->button(pin,pushButton->type()); // call the Service's button() routine with pin and type as parameters
|
||||
if(triggered(singleTime,longTime,doubleTime)) // if the underlying PushButton is triggered
|
||||
service->button(pin,type()); // call the Service's button() routine with pin and type as parameters
|
||||
}
|
||||
|
||||
///////////////////////////////
|
||||
|
|
|
|||
|
|
@ -691,17 +691,15 @@ struct [[deprecated("Please use Characteristic::setRange() method instead.")]] S
|
|||
|
||||
///////////////////////////////
|
||||
|
||||
class SpanButton{
|
||||
class SpanButton : PushButton {
|
||||
|
||||
friend class Span;
|
||||
friend class SpanService;
|
||||
|
||||
int pin; // pin number
|
||||
uint16_t singleTime; // minimum time (in millis) required to register a single press
|
||||
uint16_t longTime; // minimum time (in millis) required to register a long press
|
||||
uint16_t doubleTime; // maximum time (in millis) between single presses to register a double press instead
|
||||
SpanService *service; // Service to which this PushButton is attached
|
||||
PushButton *pushButton; // PushButton associated with this SpanButton
|
||||
|
||||
void check(); // check PushButton and call button() if pressed
|
||||
|
||||
|
|
@ -715,8 +713,8 @@ class SpanButton{
|
|||
|
||||
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;};
|
||||
|
||||
static void configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){PushButton::configureTouch(measureTime,sleepTime,thresh);}
|
||||
};
|
||||
|
||||
///////////////////////////////
|
||||
|
|
|
|||
|
|
@ -113,6 +113,12 @@ PushButton::PushButton(int pin, Button buttonType){
|
|||
|
||||
//////////////////////////////////////
|
||||
|
||||
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();
|
||||
|
|
@ -220,6 +226,13 @@ void PushButton::reset(){
|
|||
|
||||
//////////////////////////////////////
|
||||
|
||||
void PushButton::configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){
|
||||
touchSetCycles(measureTime,sleepTime);
|
||||
touchThreshold=thresh;
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
||||
uint16_t PushButton::touchThreshold;
|
||||
|
||||
////////////////////////////////
|
||||
|
|
|
|||
26
src/Utils.h
26
src/Utils.h
|
|
@ -40,7 +40,8 @@ String mask(char *c, int n); // simply utility that creates a String fr
|
|||
enum class Button {
|
||||
GROUNDED,
|
||||
POWERED,
|
||||
TOUCH
|
||||
TOUCH,
|
||||
CUSTOM
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
|
|
@ -80,13 +81,11 @@ struct TempBuffer {
|
|||
class PushButton{
|
||||
|
||||
int status;
|
||||
int pin;
|
||||
boolean doubleCheck;
|
||||
uint32_t singleAlarm;
|
||||
uint32_t doubleAlarm;
|
||||
uint32_t longAlarm;
|
||||
int pressType;
|
||||
boolean (*pressed)(int pin);
|
||||
|
||||
static uint16_t touchThreshold;
|
||||
|
||||
|
|
@ -94,6 +93,11 @@ class PushButton{
|
|||
static boolean poweredButton(int pin){return(digitalRead(pin));}
|
||||
static boolean touchButton(int pin){return(touchRead(pin)<touchThreshold);}
|
||||
|
||||
protected:
|
||||
|
||||
int pin;
|
||||
boolean (*pressed)(int pin);
|
||||
|
||||
public:
|
||||
|
||||
enum {
|
||||
|
|
@ -104,10 +108,18 @@ class PushButton{
|
|||
|
||||
PushButton(int pin, Button buttonType=Button::GROUNDED);
|
||||
|
||||
// Creates generic pushbutton functionality on specified pin.
|
||||
// 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
|
||||
|
||||
void reset();
|
||||
|
||||
// Resets state of PushButton. Should be called once before any loops that will
|
||||
|
|
@ -150,10 +162,14 @@ class PushButton{
|
|||
|
||||
// Returns pin number
|
||||
|
||||
static void configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){touchSetCycles(measureTime,sleepTime);touchThreshold=thresh;}
|
||||
static void configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh);
|
||||
|
||||
// Sets the measure time, sleep time, and lower threshold that triggers a touch - used only when buttonType=Button::TOUCH
|
||||
|
||||
// measureTime: duration of measurement time of all touch sensors in number of clock cycles
|
||||
// sleepTime: duration of sleep time (between measurements) of all touch sensors number of clock cycles
|
||||
// touchThreshhold: the read value of touch sensors, below which sensors are considered touched (i.e. "pressed")
|
||||
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
|
|
|
|||
Loading…
Reference in New Issue