mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-22 18:26:30 +00:00
Merge pull request #8562 from Staars/touch
Touch pin as button for ESP32
This commit is contained in:
commit
0dc0eda274
@ -24,6 +24,10 @@
|
|||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
#define MAX_RELAY_BUTTON1 5 // Max number of relay controlled by BUTTON1
|
#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 =
|
const char kMultiPress[] PROGMEM =
|
||||||
"|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|";
|
"|SINGLE|DOUBLE|TRIPLE|QUAD|PENTA|";
|
||||||
@ -40,6 +44,10 @@ struct BUTTON {
|
|||||||
uint8_t dual_receive_count = 0; // Sonoff dual input flag
|
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 no_pullup_mask = 0; // key no pullup flag (1 = no pullup)
|
||||||
uint8_t inverted_mask = 0; // Key inverted flag (1 = inverted)
|
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 present = 0; // Number of buttons found flag
|
||||||
uint8_t adc = 99; // ADC0 button number
|
uint8_t adc = 99; // ADC0 button number
|
||||||
} Button;
|
} Button;
|
||||||
@ -55,7 +63,12 @@ void ButtonInvertFlag(uint8 button_bit)
|
|||||||
{
|
{
|
||||||
bitSet(Button.inverted_mask, 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)
|
void ButtonInit(void)
|
||||||
{
|
{
|
||||||
Button.present = 0;
|
Button.present = 0;
|
||||||
@ -131,11 +144,32 @@ void ButtonHandler(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif // ESP8266
|
|
||||||
if (PinUsed(GPIO_KEY1, button_index)) {
|
if (PinUsed(GPIO_KEY1, button_index)) {
|
||||||
button_present = 1;
|
button_present = 1;
|
||||||
button = (digitalRead(Pin(GPIO_KEY1, button_index)) != bitRead(Button.inverted_mask, button_index));
|
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
|
#ifndef USE_ADC_VCC
|
||||||
if (Button.adc == button_index) {
|
if (Button.adc == button_index) {
|
||||||
button_present = 1;
|
button_present = 1;
|
||||||
@ -147,7 +181,6 @@ void ButtonHandler(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // USE_ADC_VCC
|
#endif // USE_ADC_VCC
|
||||||
|
|
||||||
if (button_present) {
|
if (button_present) {
|
||||||
XdrvMailbox.index = button_index;
|
XdrvMailbox.index = button_index;
|
||||||
XdrvMailbox.payload = button;
|
XdrvMailbox.payload = button;
|
||||||
|
@ -1472,6 +1472,12 @@ void GpioInit(void)
|
|||||||
ButtonInvertFlag(mpin - AGPIO(GPIO_KEY1_INV_NP)); // 0 .. 3
|
ButtonInvertFlag(mpin - AGPIO(GPIO_KEY1_INV_NP)); // 0 .. 3
|
||||||
mpin -= (AGPIO(GPIO_KEY1_INV_NP) - AGPIO(GPIO_KEY1));
|
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))) {
|
else if ((mpin >= AGPIO(GPIO_REL1_INV)) && (mpin < (AGPIO(GPIO_REL1_INV) + MAX_RELAYS))) {
|
||||||
bitSet(rel_inverted, mpin - AGPIO(GPIO_REL1_INV));
|
bitSet(rel_inverted, mpin - AGPIO(GPIO_REL1_INV));
|
||||||
mpin -= (AGPIO(GPIO_REL1_INV) - AGPIO(GPIO_REL1));
|
mpin -= (AGPIO(GPIO_REL1_INV) - AGPIO(GPIO_REL1));
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
|
|
||||||
enum UserSelectablePins {
|
enum UserSelectablePins {
|
||||||
GPIO_NONE, // Not used
|
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, // 4 x Button
|
||||||
GPIO_SWT1, GPIO_SWT1_NP, // 8 x User connected external switches
|
GPIO_SWT1, GPIO_SWT1_NP, // 8 x User connected external switches
|
||||||
GPIO_REL1, GPIO_REL1_INV, // 8 x Relays
|
GPIO_REL1, GPIO_REL1_INV, // 8 x Relays
|
||||||
GPIO_LED1, GPIO_LED1_INV, // 4 x Leds
|
GPIO_LED1, GPIO_LED1_INV, // 4 x Leds
|
||||||
@ -127,6 +127,7 @@ enum UserSelectablePins {
|
|||||||
GPIO_WEBCAM_PSRCS,
|
GPIO_WEBCAM_PSRCS,
|
||||||
GPIO_BOILER_OT_RX, GPIO_BOILER_OT_TX, // OpenTherm Boiler TX pin
|
GPIO_BOILER_OT_RX, GPIO_BOILER_OT_TX, // OpenTherm Boiler TX pin
|
||||||
GPIO_WINDMETER_SPEED, // WindMeter speed counter pin
|
GPIO_WINDMETER_SPEED, // WindMeter speed counter pin
|
||||||
|
GPIO_KEY1_TC, // Touch pin as button
|
||||||
GPIO_SENSOR_END };
|
GPIO_SENSOR_END };
|
||||||
|
|
||||||
enum ProgramSelectablePins {
|
enum ProgramSelectablePins {
|
||||||
@ -215,7 +216,7 @@ const char kSensorNames[] PROGMEM =
|
|||||||
D_GPIO_WEBCAM_HSD "|"
|
D_GPIO_WEBCAM_HSD "|"
|
||||||
D_GPIO_WEBCAM_PSRCS "|"
|
D_GPIO_WEBCAM_PSRCS "|"
|
||||||
D_SENSOR_BOILER_OT_RX "|" D_SENSOR_BOILER_OT_TX "|"
|
D_SENSOR_BOILER_OT_RX "|" D_SENSOR_BOILER_OT_TX "|"
|
||||||
D_SENSOR_WINDMETER_SPEED
|
D_SENSOR_WINDMETER_SPEED "|" D_SENSOR_BUTTON "_tc"
|
||||||
;
|
;
|
||||||
|
|
||||||
const char kSensorNamesFixed[] PROGMEM =
|
const char kSensorNamesFixed[] PROGMEM =
|
||||||
@ -542,6 +543,7 @@ const uint16_t kGpioNiceList[] PROGMEM = {
|
|||||||
AGPIO(GPIO_WEBCAM_HSD) + MAX_WEBCAM_HSD,
|
AGPIO(GPIO_WEBCAM_HSD) + MAX_WEBCAM_HSD,
|
||||||
AGPIO(GPIO_WEBCAM_PSRCS),
|
AGPIO(GPIO_WEBCAM_PSRCS),
|
||||||
#endif
|
#endif
|
||||||
|
AGPIO(GPIO_KEY1_TC) + MAX_KEYS
|
||||||
};
|
};
|
||||||
|
|
||||||
//********************************************************************************************
|
//********************************************************************************************
|
||||||
|
Loading…
x
Reference in New Issue
Block a user