add commands to touch pin button on ESP32

This commit is contained in:
Staars 2020-05-30 13:50:22 +02:00
parent ea48272135
commit 7e28e03d78
3 changed files with 65 additions and 6 deletions

View File

@ -319,6 +319,11 @@
#define D_CMND_HUMOFFSET "HumOffset" #define D_CMND_HUMOFFSET "HumOffset"
#define D_CMND_GLOBAL_TEMP "GlobalTemp" #define D_CMND_GLOBAL_TEMP "GlobalTemp"
#define D_CMND_GLOBAL_HUM "GlobalHum" #define D_CMND_GLOBAL_HUM "GlobalHum"
#ifdef ESP32
#define D_CMND_TOUCH_CAL "TouchCal"
#define D_CMND_TOUCH_THRES "TouchThres"
#define D_CMND_TOUCH_NUM "TouchNum"
#endif //ESP32
// Commands xdrv_01_mqtt.ino // Commands xdrv_01_mqtt.ino
#define D_CMND_MQTTLOG "MqttLog" #define D_CMND_MQTTLOG "MqttLog"

View File

@ -52,6 +52,14 @@ struct BUTTON {
uint8_t adc = 99; // ADC0 button number uint8_t adc = 99; // ADC0 button number
} Button; } Button;
#ifdef ESP32
struct TOUCH_BUTTON {
uint8_t pin_threshold = TOUCH_PIN_THRESHOLD;
uint8_t hit_threshold = TOUCH_HIT_THRESHOLD;
uint8_t calibration = 0; // Bitfield
} TOUCH_BUTTON;
#endif // ESP32
/********************************************************************************************/ /********************************************************************************************/
void ButtonPullupFlag(uint8 button_bit) void ButtonPullupFlag(uint8 button_bit)
@ -155,15 +163,15 @@ void ButtonHandler(void)
uint32_t _value = touchRead(Pin(GPIO_KEY1, button_index)); uint32_t _value = touchRead(Pin(GPIO_KEY1, button_index));
button = NOT_PRESSED; button = NOT_PRESSED;
if (_value != 0){ // probably read-error if (_value != 0){ // probably read-error
if(_value < TOUCH_PIN_THRESHOLD){ if(_value < TOUCH_BUTTON.pin_threshold){
if(++Button.touch_hits[button_index]>TOUCH_HIT_THRESHOLD){ if(++Button.touch_hits[button_index]>TOUCH_BUTTON.hit_threshold){
button = PRESSED; if (!bitRead(TOUCH_BUTTON.calibration, button_index+1)) 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 Button.touch_hits[button_index] = 0; else Button.touch_hits[button_index] = 0;
if (bitRead(TOUCH_BUTTON.calibration, button_index+1)) AddLog_P2(LOG_LEVEL_INFO, PSTR("PLOT: %u, %u, %u,"),button_index+1, _value, Button.touch_hits[button_index]); // button number (1..4) , value, continuous hits under threshold
} }
else{ // Normal button else{ // Normal button
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));

View File

@ -38,7 +38,11 @@ const char kTasmotaCommands[] PROGMEM = "|" // No prefix
#endif // USE_DEVICE_GROUPS_SEND #endif // USE_DEVICE_GROUPS_SEND
D_CMND_DEVGROUP_SHARE "|" D_CMND_DEVGROUPSTATUS "|" D_CMND_DEVGROUP_SHARE "|" D_CMND_DEVGROUPSTATUS "|"
#endif // USE_DEVICE_GROUPS #endif // USE_DEVICE_GROUPS
D_CMND_SENSOR "|" D_CMND_DRIVER; D_CMND_SENSOR "|" D_CMND_DRIVER
#ifdef ESP32
"|" D_CMND_TOUCH_CAL "|" D_CMND_TOUCH_THRES "|" D_CMND_TOUCH_NUM
#endif //ESP32
;
void (* const TasmotaCommand[])(void) PROGMEM = { void (* const TasmotaCommand[])(void) PROGMEM = {
&CmndBacklog, &CmndDelay, &CmndPower, &CmndStatus, &CmndState, &CmndSleep, &CmndUpgrade, &CmndUpgrade, &CmndOtaUrl, &CmndBacklog, &CmndDelay, &CmndPower, &CmndStatus, &CmndState, &CmndSleep, &CmndUpgrade, &CmndUpgrade, &CmndOtaUrl,
@ -61,7 +65,11 @@ void (* const TasmotaCommand[])(void) PROGMEM = {
#endif // USE_DEVICE_GROUPS_SEND #endif // USE_DEVICE_GROUPS_SEND
&CmndDevGroupShare, &CmndDevGroupStatus, &CmndDevGroupShare, &CmndDevGroupStatus,
#endif // USE_DEVICE_GROUPS #endif // USE_DEVICE_GROUPS
&CmndSensor, &CmndDriver }; &CmndSensor, &CmndDriver
#ifdef ESP32
,&CmndTouchCal, &CmndTouchThres, &CmndTouchNum
#endif //ESP32
};
const char kWifiConfig[] PROGMEM = const char kWifiConfig[] PROGMEM =
D_WCFG_0_RESTART "||" D_WCFG_2_WIFIMANAGER "||" D_WCFG_4_RETRY "|" D_WCFG_5_WAIT "|" D_WCFG_6_SERIAL "|" D_WCFG_7_WIFIMANAGER_RESET_ONLY; D_WCFG_0_RESTART "||" D_WCFG_2_WIFIMANAGER "||" D_WCFG_4_RETRY "|" D_WCFG_5_WAIT "|" D_WCFG_6_SERIAL "|" D_WCFG_7_WIFIMANAGER_RESET_ONLY;
@ -1946,3 +1954,41 @@ void CmndDriver(void)
{ {
XdrvCall(FUNC_COMMAND_DRIVER); XdrvCall(FUNC_COMMAND_DRIVER);
} }
#ifdef ESP32
void CmndTouchCal(void)
{
if (XdrvMailbox.payload >= 0) {
if (XdrvMailbox.payload < MAX_KEYS + 1) TOUCH_BUTTON.calibration = bitSet(TOUCH_BUTTON.calibration, XdrvMailbox.payload);
if (XdrvMailbox.payload == 0) TOUCH_BUTTON.calibration = 0;
if (XdrvMailbox.payload == 255) TOUCH_BUTTON.calibration = 255; // all pinss
}
Response_P(PSTR("{\"" D_CMND_TOUCH_CAL "\": %u"), TOUCH_BUTTON.calibration);
ResponseJsonEnd();
AddLog_P2(LOG_LEVEL_INFO, PSTR("Button Touchvalue Hits,"));
}
void CmndTouchThres(void)
{
if (XdrvMailbox.payload >= 0) {
if (XdrvMailbox.payload<256){
TOUCH_BUTTON.pin_threshold = XdrvMailbox.payload;
}
}
Response_P(PSTR("{\"" D_CMND_TOUCH_THRES "\": %u"), TOUCH_BUTTON.pin_threshold);
ResponseJsonEnd();
}
void CmndTouchNum(void)
{
if (XdrvMailbox.payload >= 0) {
if (XdrvMailbox.payload<32){
TOUCH_BUTTON.hit_threshold = XdrvMailbox.payload;
}
}
Response_P(PSTR("{\"" D_CMND_TOUCH_NUM "\": %u"), TOUCH_BUTTON.hit_threshold);
ResponseJsonEnd();
}
#endif //ESP32