Incorporated logic to auto-calibrate touch sensors

Works with ESP32, ESP32-S2, and ESP32-S3 (ESP32-C3 does not support Touch Sensors).
This commit is contained in:
Gregg 2022-08-14 18:03:59 -05:00
parent dbcd9e267b
commit e8d40150ed
3 changed files with 29 additions and 9 deletions

View File

@ -61,8 +61,6 @@ 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

View File

@ -96,6 +96,21 @@ PushButton::PushButton(int pin, pressTest_t pressed){
pinMode(pin, INPUT_PULLUP); pinMode(pin, INPUT_PULLUP);
else if(pressed==POWERED) else if(pressed==POWERED)
pinMode(pin, INPUT_PULLDOWN); pinMode(pin, INPUT_PULLDOWN);
#if SOC_TOUCH_SENSOR_NUM > 0
else if (pressed==TOUCH && threshold==0){
for(int i=0;i<calibCount;i++)
threshold+=touchRead(pin);
threshold/=calibCount;
#if SOC_TOUCH_VERSION_1
threshold/=2;
Serial.printf("Touch Sensor at pin=%d used for calibration. Triggers when sensor reading < %d.\n",pin,threshold);
#elif SOC_TOUCH_VERSION_2
threshold*=2;
Serial.printf("Touch Sensor at pin=%d used for calibration. Triggers when sensor reading > %d.\n",pin,threshold);
#endif
}
#endif
} }
@ -209,15 +224,17 @@ void PushButton::reset(){
////////////////////////////////////// //////////////////////////////////////
void PushButton::configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){ void PushButton::configureTouch(uint16_t measureTime, uint16_t sleepTime, uint16_t thresh){
#ifndef CONFIG_IDF_TARGET_ESP32C3 #if SOC_TOUCH_SENSOR_NUM > 0
touchSetCycles(measureTime,sleepTime); touchSetCycles(measureTime,sleepTime);
touchThreshold=thresh; threshold=thresh;
#endif #endif
} }
////////////////////////////////////// //////////////////////////////////////
uint16_t PushButton::touchThreshold; #if SOC_TOUCH_SENSOR_NUM > 0
touch_value_t PushButton::threshold=0;
#endif
//////////////////////////////// ////////////////////////////////
// Blinker // // Blinker //

View File

@ -79,8 +79,11 @@ class PushButton{
uint32_t doubleAlarm; uint32_t doubleAlarm;
uint32_t longAlarm; uint32_t longAlarm;
int pressType; int pressType;
static uint16_t touchThreshold; #if SOC_TOUCH_SENSOR_NUM > 0
static touch_value_t threshold;
static const int calibCount=20;
#endif
protected: protected:
@ -100,8 +103,10 @@ class PushButton{
static boolean GROUNDED(int pin){return(!digitalRead(pin));} static boolean GROUNDED(int pin){return(!digitalRead(pin));}
static boolean POWERED(int pin){return(digitalRead(pin));} static boolean POWERED(int pin){return(digitalRead(pin));}
#ifndef CONFIG_IDF_TARGET_ESP32C3 #if SOC_TOUCH_VERSION_1 // ESP32
static boolean TOUCH(int pin){return(touchRead(pin)<touchThreshold);} static boolean TOUCH(int pin){return(touchRead(pin)<threshold);}
#elif SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3
static boolean TOUCH(int pin){return(touchRead(pin)>threshold);}
#endif #endif
PushButton(int pin, pressTest_t pressed=GROUNDED); PushButton(int pin, pressTest_t pressed=GROUNDED);