diff --git a/board_single/main/button.cpp b/board_single/main/button.cpp index 3f84205..62c532c 100644 --- a/board_single/main/button.cpp +++ b/board_single/main/button.cpp @@ -183,12 +183,15 @@ void buttonCommands::action (uint8_t count, bool lastPressLong){ // and takes the corresponding action // this function has to be started once in a separate task #define INPUT_TIMEOUT 500 // duration of no button events, after which action is run (implicitly also is 'long-press' time) +#define IGNORE_BUTTON_TIME_SINCE_LAST_ROTATE 800 // time that has to be passed since last encoder rotate click for button count command to be accepted (e.g. prevent long press action after PRESS+ROTATE was used) +#define IGNORE_ROTATE_COUNT 2 //amount of ignored clicks before action is actually taken (ignore accidental touches) void buttonCommands::startHandleLoop() { //-- variables -- static bool isPressed = false; static rotary_encoder_event_t event; // store event data int rotateCount = 0; // temporary count clicks encoder was rotated + uint32_t timestampLastRotate = 0; // int count = 0; (from class) while (1) @@ -222,6 +225,13 @@ void buttonCommands::startHandleLoop() isPressed = false; // rest stored state break; case RE_ET_CHANGED: + // ignore first clicks + if (++rotateCount < IGNORE_ROTATE_COUNT) + { + buzzer->beep(1, 60, 0); + break; + } + timestampLastRotate = esp_log_timestamp(); if (isPressed){ /////### scroll through status pages when PRESSED + ROTATED ### ///if (event.diff > 0) @@ -242,8 +252,6 @@ void buttonCommands::startHandleLoop() else { // increment target position each click - // TODO: ignore first 2 clicks - // TODO: control back rest too? if (event.diff > 0) legRest->setTargetPercent(legRest->getTargetPercent() - 5); else @@ -263,8 +271,12 @@ void buttonCommands::startHandleLoop() } else // timeout (no event received within TIMEOUT) { + rotateCount = 0; // reset rotate count + // ignore button click events when "ROTATE+PRESSED" was just used + if (count > 0 && (esp_log_timestamp() - timestampLastRotate < IGNORE_BUTTON_TIME_SINCE_LAST_ROTATE)) + ESP_LOGW(TAG, "ignoring button count %d because encoder was rotated less than %d ms ago", count, IGNORE_BUTTON_TIME_SINCE_LAST_ROTATE); // encoder was pressed - if (count > 0) + else if (count > 0) { //-- run action with count of presses -- ESP_LOGI(TAG, "timeout: count=%d, lastPressLong=%d -> running action", count, isPressed);