mirror of
https://github.com/wled/WLED.git
synced 2025-07-22 18:26:32 +00:00
Button handling
- debug messages - verify touch GPIO
This commit is contained in:
parent
b176225910
commit
1b5cc2abf1
@ -99,7 +99,7 @@ bool isButtonPressed(uint8_t i)
|
|||||||
case BTN_TYPE_TOUCH:
|
case BTN_TYPE_TOUCH:
|
||||||
case BTN_TYPE_TOUCH_SWITCH:
|
case BTN_TYPE_TOUCH_SWITCH:
|
||||||
#if defined(ARDUINO_ARCH_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3)
|
#if defined(ARDUINO_ARCH_ESP32) && !defined(CONFIG_IDF_TARGET_ESP32C3)
|
||||||
if (touchRead(pin) <= touchThreshold) return true;
|
if (digitalPinToTouchChannel(btnPin[i]) >= 0 && touchRead(pin) <= touchThreshold) return true;
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -110,6 +110,7 @@ void handleSwitch(uint8_t b)
|
|||||||
{
|
{
|
||||||
// isButtonPressed() handles inverted/noninverted logic
|
// isButtonPressed() handles inverted/noninverted logic
|
||||||
if (buttonPressedBefore[b] != isButtonPressed(b)) {
|
if (buttonPressedBefore[b] != isButtonPressed(b)) {
|
||||||
|
DEBUG_PRINT(F("Switch: State changed ")); DEBUG_PRINTLN(b);
|
||||||
buttonPressedTime[b] = millis();
|
buttonPressedTime[b] = millis();
|
||||||
buttonPressedBefore[b] = !buttonPressedBefore[b];
|
buttonPressedBefore[b] = !buttonPressedBefore[b];
|
||||||
}
|
}
|
||||||
@ -117,12 +118,15 @@ void handleSwitch(uint8_t b)
|
|||||||
if (buttonLongPressed[b] == buttonPressedBefore[b]) return;
|
if (buttonLongPressed[b] == buttonPressedBefore[b]) return;
|
||||||
|
|
||||||
if (millis() - buttonPressedTime[b] > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce)
|
if (millis() - buttonPressedTime[b] > WLED_DEBOUNCE_THRESHOLD) { //fire edge event only after 50ms without change (debounce)
|
||||||
|
DEBUG_PRINT(F("Switch: Activating ")); DEBUG_PRINTLN(b);
|
||||||
if (!buttonPressedBefore[b]) { // on -> off
|
if (!buttonPressedBefore[b]) { // on -> off
|
||||||
|
DEBUG_PRINT(F("Switch: On -> Off ")); DEBUG_PRINTLN(b);
|
||||||
if (macroButton[b]) applyPreset(macroButton[b], CALL_MODE_BUTTON_PRESET);
|
if (macroButton[b]) applyPreset(macroButton[b], CALL_MODE_BUTTON_PRESET);
|
||||||
else { //turn on
|
else { //turn on
|
||||||
if (!bri) {toggleOnOff(); stateUpdated(CALL_MODE_BUTTON);}
|
if (!bri) {toggleOnOff(); stateUpdated(CALL_MODE_BUTTON);}
|
||||||
}
|
}
|
||||||
} else { // off -> on
|
} else { // off -> on
|
||||||
|
DEBUG_PRINT(F("Switch: Off -> On ")); DEBUG_PRINTLN(b);
|
||||||
if (macroLongPress[b]) applyPreset(macroLongPress[b], CALL_MODE_BUTTON_PRESET);
|
if (macroLongPress[b]) applyPreset(macroLongPress[b], CALL_MODE_BUTTON_PRESET);
|
||||||
else { //turn off
|
else { //turn off
|
||||||
if (bri) {toggleOnOff(); stateUpdated(CALL_MODE_BUTTON);}
|
if (bri) {toggleOnOff(); stateUpdated(CALL_MODE_BUTTON);}
|
||||||
@ -154,6 +158,8 @@ void handleAnalog(uint8_t b)
|
|||||||
static float filteredReading[WLED_MAX_BUTTONS] = {0.0f};
|
static float filteredReading[WLED_MAX_BUTTONS] = {0.0f};
|
||||||
uint16_t rawReading; // raw value from analogRead, scaled to 12bit
|
uint16_t rawReading; // raw value from analogRead, scaled to 12bit
|
||||||
|
|
||||||
|
DEBUG_PRINT(F("Analog: Reading button ")); DEBUG_PRINTLN(b);
|
||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
rawReading = analogRead(A0) << 2; // convert 10bit read to 12bit
|
rawReading = analogRead(A0) << 2; // convert 10bit read to 12bit
|
||||||
#else
|
#else
|
||||||
@ -162,6 +168,8 @@ void handleAnalog(uint8_t b)
|
|||||||
#endif
|
#endif
|
||||||
yield(); // keep WiFi task running - analog read may take several millis on ESP8266
|
yield(); // keep WiFi task running - analog read may take several millis on ESP8266
|
||||||
|
|
||||||
|
DEBUG_PRINT(F("Analog: Raw read = ")); DEBUG_PRINTLN(rawReading);
|
||||||
|
|
||||||
filteredReading[b] += POT_SMOOTHING * ((float(rawReading) / 16.0f) - filteredReading[b]); // filter raw input, and scale to [0..255]
|
filteredReading[b] += POT_SMOOTHING * ((float(rawReading) / 16.0f) - filteredReading[b]); // filter raw input, and scale to [0..255]
|
||||||
uint16_t aRead = max(min(int(filteredReading[b]), 255), 0); // squash into 8bit
|
uint16_t aRead = max(min(int(filteredReading[b]), 255), 0); // squash into 8bit
|
||||||
if(aRead <= POT_SENSITIVITY) aRead = 0; // make sure that 0 and 255 are used
|
if(aRead <= POT_SENSITIVITY) aRead = 0; // make sure that 0 and 255 are used
|
||||||
@ -172,6 +180,8 @@ void handleAnalog(uint8_t b)
|
|||||||
// remove noise & reduce frequency of UI updates
|
// remove noise & reduce frequency of UI updates
|
||||||
if (abs(int(aRead) - int(oldRead[b])) <= POT_SENSITIVITY) return; // no significant change in reading
|
if (abs(int(aRead) - int(oldRead[b])) <= POT_SENSITIVITY) return; // no significant change in reading
|
||||||
|
|
||||||
|
DEBUG_PRINT(F("Analog: Filtered read = ")); DEBUG_PRINTLN(aRead);
|
||||||
|
|
||||||
// Unomment the next lines if you still see flickering related to potentiometer
|
// Unomment the next lines if you still see flickering related to potentiometer
|
||||||
// This waits until strip finishes updating (why: strip was not updating at the start of handleButton() but may have started during analogRead()?)
|
// This waits until strip finishes updating (why: strip was not updating at the start of handleButton() but may have started during analogRead()?)
|
||||||
//unsigned long wait_started = millis();
|
//unsigned long wait_started = millis();
|
||||||
@ -184,6 +194,7 @@ void handleAnalog(uint8_t b)
|
|||||||
|
|
||||||
// if no macro for "short press" and "long press" is defined use brightness control
|
// if no macro for "short press" and "long press" is defined use brightness control
|
||||||
if (!macroButton[b] && !macroLongPress[b]) {
|
if (!macroButton[b] && !macroLongPress[b]) {
|
||||||
|
DEBUG_PRINT(F("Analog: Action = ")); DEBUG_PRINTLN(macroDoublePress[b]);
|
||||||
// if "double press" macro defines which option to change
|
// if "double press" macro defines which option to change
|
||||||
if (macroDoublePress[b] >= 250) {
|
if (macroDoublePress[b] >= 250) {
|
||||||
// global brightness
|
// global brightness
|
||||||
@ -219,6 +230,7 @@ void handleAnalog(uint8_t b)
|
|||||||
updateInterfaces(CALL_MODE_BUTTON);
|
updateInterfaces(CALL_MODE_BUTTON);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
DEBUG_PRINTLN(F("Analog: No action"));
|
||||||
//TODO:
|
//TODO:
|
||||||
// we can either trigger a preset depending on the level (between short and long entries)
|
// we can either trigger a preset depending on the level (between short and long entries)
|
||||||
// or use it for RGBW direct control
|
// or use it for RGBW direct control
|
||||||
|
@ -221,15 +221,22 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
|
|||||||
char bt[4] = "BT"; bt[2] = (i<10?48:55)+i; bt[3] = 0; // button pin (use A,B,C,... if WLED_MAX_BUTTONS>10)
|
char bt[4] = "BT"; bt[2] = (i<10?48:55)+i; bt[3] = 0; // button pin (use A,B,C,... if WLED_MAX_BUTTONS>10)
|
||||||
char be[4] = "BE"; be[2] = (i<10?48:55)+i; be[3] = 0; // button type (use A,B,C,... if WLED_MAX_BUTTONS>10)
|
char be[4] = "BE"; be[2] = (i<10?48:55)+i; be[3] = 0; // button type (use A,B,C,... if WLED_MAX_BUTTONS>10)
|
||||||
int hw_btn_pin = request->arg(bt).toInt();
|
int hw_btn_pin = request->arg(bt).toInt();
|
||||||
if (pinManager.allocatePin(hw_btn_pin,false,PinOwner::Button)) {
|
if (hw_btn_pin >= 0 && pinManager.allocatePin(hw_btn_pin,false,PinOwner::Button)) {
|
||||||
btnPin[i] = hw_btn_pin;
|
btnPin[i] = hw_btn_pin;
|
||||||
buttonType[i] = request->arg(be).toInt();
|
buttonType[i] = request->arg(be).toInt();
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
// ESP32 only: check that analog button pin is a valid ADC gpio
|
// ESP32 only: check that button pin is a valid gpio
|
||||||
if (((buttonType[i] == BTN_TYPE_ANALOG) || (buttonType[i] == BTN_TYPE_ANALOG_INVERTED)) && (digitalPinToAnalogChannel(btnPin[i]) < 0))
|
if (((buttonType[i] == BTN_TYPE_ANALOG) || (buttonType[i] == BTN_TYPE_ANALOG_INVERTED)) && (digitalPinToAnalogChannel(btnPin[i]) < 0))
|
||||||
{
|
{
|
||||||
// not an ADC analog pin
|
// not an ADC analog pin
|
||||||
if (btnPin[i] >= 0) DEBUG_PRINTF("PIN ALLOC error: GPIO%d for analog button #%d is not an analog pin!\n", btnPin[i], i);
|
DEBUG_PRINTF("PIN ALLOC error: GPIO%d for analog button #%d is not an analog pin!\n", btnPin[i], i);
|
||||||
|
btnPin[i] = -1;
|
||||||
|
pinManager.deallocatePin(hw_btn_pin,PinOwner::Button);
|
||||||
|
}
|
||||||
|
else if ((buttonType[i] == BTN_TYPE_TOUCH || buttonType[i] == BTN_TYPE_TOUCH_SWITCH) && digitalPinToTouchChannel(btnPin[i]) < 0)
|
||||||
|
{
|
||||||
|
// not a touch pin
|
||||||
|
DEBUG_PRINTF("PIN ALLOC error: GPIO%d for touch button #%d is not an touch pin!\n", btnPin[i], i);
|
||||||
btnPin[i] = -1;
|
btnPin[i] = -1;
|
||||||
pinManager.deallocatePin(hw_btn_pin,PinOwner::Button);
|
pinManager.deallocatePin(hw_btn_pin,PinOwner::Button);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user