Touch pin as button for ESP32

This commit is contained in:
Staars 2020-05-27 15:59:32 +02:00
parent 9f50b20c71
commit 55e56fee43
3 changed files with 46 additions and 5 deletions

View File

@ -24,6 +24,10 @@
\*********************************************************************************************/
#define MAX_RELAY_BUTTON1 5 // Max number of relay controlled by BUTTON1
#ifdef ESP32
#define TOUCH_PIN_THRESHOLD 12 // Smaller value will treated as button press
#define TOUCH_HIT_THRESHOLD 3 // successful hits to filter out noise
#endif // ESP32
const char kMultiPress[] PROGMEM =
"|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|";
@ -40,6 +44,10 @@ struct BUTTON {
uint8_t dual_receive_count = 0; // Sonoff dual input flag
uint8_t no_pullup_mask = 0; // key no pullup flag (1 = no pullup)
uint8_t inverted_mask = 0; // Key inverted flag (1 = inverted)
#ifdef ESP32
uint8_t touch_mask = 0; // Touch flag (1 = inverted)
uint8_t touch_hits[MAX_KEYS] = { 0 }; // Hits in a row to filter out noise
#endif // ESP32
uint8_t present = 0; // Number of buttons found flag
uint8_t adc = 99; // ADC0 button number
} Button;
@ -55,7 +63,12 @@ void ButtonInvertFlag(uint8 button_bit)
{
bitSet(Button.inverted_mask, button_bit);
}
#ifdef ESP32
void ButtonTouchFlag(uint8 button_bit)
{
bitSet(Button.touch_mask, button_bit);
}
#endif // ESP32
void ButtonInit(void)
{
Button.present = 0;
@ -131,11 +144,32 @@ void ButtonHandler(void)
}
}
else
#endif // ESP8266
if (PinUsed(GPIO_KEY1, button_index)) {
button_present = 1;
button = (digitalRead(Pin(GPIO_KEY1, button_index)) != bitRead(Button.inverted_mask, button_index));
}
#else
if (PinUsed(GPIO_KEY1, button_index)) {
button_present = 1;
if (bitRead(Button.touch_mask, button_index)){ // Touch
uint32_t _value = touchRead(Pin(GPIO_KEY1, button_index));
button = NOT_PRESSED;
if (_value != 0){ // probably read-error
if(_value < TOUCH_PIN_THRESHOLD){
if(++Button.touch_hits[button_index]>TOUCH_HIT_THRESHOLD){
button = PRESSED;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Touch value: %u hits: %u"), _value, Button.touch_hits[button_index]);
}
}
else Button.touch_hits[button_index] = 0;
}
else Button.touch_hits[button_index] = 0;
}
else{ // Normal button
button = (digitalRead(Pin(GPIO_KEY1, button_index)) != bitRead(Button.inverted_mask, button_index));
}
}
#endif // ESP8266
#ifndef USE_ADC_VCC
if (Button.adc == button_index) {
button_present = 1;
@ -147,7 +181,6 @@ void ButtonHandler(void)
}
}
#endif // USE_ADC_VCC
if (button_present) {
XdrvMailbox.index = button_index;
XdrvMailbox.payload = button;

View File

@ -1472,6 +1472,12 @@ void GpioInit(void)
ButtonInvertFlag(mpin - AGPIO(GPIO_KEY1_INV_NP)); // 0 .. 3
mpin -= (AGPIO(GPIO_KEY1_INV_NP) - AGPIO(GPIO_KEY1));
}
#ifdef ESP32
else if ((mpin >= AGPIO(GPIO_KEY1_TC)) && (mpin < (AGPIO(GPIO_KEY1_TC) + MAX_KEYS))) {
ButtonTouchFlag(mpin - AGPIO(GPIO_KEY1_TC)); // 0 .. 3
mpin -= (AGPIO(GPIO_KEY1_TC) - AGPIO(GPIO_KEY1));
}
#endif //ESP32
else if ((mpin >= AGPIO(GPIO_REL1_INV)) && (mpin < (AGPIO(GPIO_REL1_INV) + MAX_RELAYS))) {
bitSet(rel_inverted, mpin - AGPIO(GPIO_REL1_INV));
mpin -= (AGPIO(GPIO_REL1_INV) - AGPIO(GPIO_REL1));

View File

@ -44,7 +44,7 @@
enum UserSelectablePins {
GPIO_NONE, // Not used
GPIO_KEY1, GPIO_KEY1_NP, GPIO_KEY1_INV, GPIO_KEY1_INV_NP, // 4 x Button
GPIO_KEY1, GPIO_KEY1_NP, GPIO_KEY1_INV, GPIO_KEY1_INV_NP, GPIO_KEY1_TC, // 4 x Button + Touch
GPIO_SWT1, GPIO_SWT1_NP, // 8 x User connected external switches
GPIO_REL1, GPIO_REL1_INV, // 8 x Relays
GPIO_LED1, GPIO_LED1_INV, // 4 x Leds
@ -138,7 +138,7 @@ enum ProgramSelectablePins {
// Text in webpage Module Parameters and commands GPIOS and GPIO
const char kSensorNames[] PROGMEM =
D_SENSOR_NONE "|"
D_SENSOR_BUTTON "|" D_SENSOR_BUTTON "_n|" D_SENSOR_BUTTON "_i|" D_SENSOR_BUTTON "_in|"
D_SENSOR_BUTTON "|" D_SENSOR_BUTTON "_n|" D_SENSOR_BUTTON "_i|" D_SENSOR_BUTTON "_in|" D_SENSOR_BUTTON "_tc|"
D_SENSOR_SWITCH "|" D_SENSOR_SWITCH "_n|"
D_SENSOR_RELAY "|" D_SENSOR_RELAY "_i|"
D_SENSOR_LED "|" D_SENSOR_LED "_i|"
@ -230,6 +230,7 @@ const uint16_t kGpioNiceList[] PROGMEM = {
AGPIO(GPIO_KEY1_NP) + MAX_KEYS,
AGPIO(GPIO_KEY1_INV) + MAX_KEYS,
AGPIO(GPIO_KEY1_INV_NP) + MAX_KEYS,
AGPIO(GPIO_KEY1_TC) + MAX_KEYS,
AGPIO(GPIO_SWT1) + MAX_SWITCHES, // User connected external switches
AGPIO(GPIO_SWT1_NP) + MAX_SWITCHES,
AGPIO(GPIO_REL1) + MAX_RELAYS, // Relays
@ -549,6 +550,7 @@ const uint16_t kGpioNiceList[] PROGMEM = {
#define MAX_GPIO_PIN 40 // Number of supported GPIO
#define MIN_FLASH_PINS 4 // Number of flash chip pins unusable for configuration (GPIO6, 7, 8 and 11)
#define MAX_USER_PINS 36 // MAX_GPIO_PIN - MIN_FLASH_PINS
// #define MAX_TOUCH_PINS 10 // Number of supported TOUCH PINS
#define WEMOS_MODULE 0 // Wemos module
// 0 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839