From c19d0538670fdb6ba81f57f62b8aea00bf99463c Mon Sep 17 00:00:00 2001
From: jonny_l480 <jonny@wwad.de>
Date: Thu, 5 Sep 2024 10:47:37 +0200
Subject: [PATCH] Minor fixes + optimizations - test success

---
 board_single/main/button.cpp  | 14 +++++++-------
 board_single/main/display.cpp |  4 ++++
 board_single/main/main.cpp    |  2 +-
 common/chairAdjust.cpp        | 28 +++++++++++++++++++++-------
 4 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/board_single/main/button.cpp b/board_single/main/button.cpp
index 62c532c..718731c 100644
--- a/board_single/main/button.cpp
+++ b/board_single/main/button.cpp
@@ -182,9 +182,9 @@ void buttonCommands::action (uint8_t count, bool lastPressLong){
 // when not in MENU_SETTINGS mode, repeatedly receives events from encoder button
 // 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 INPUT_TIMEOUT 600 // 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)
+#define IGNORE_ROTATE_COUNT 1 //amount of ignored clicks before action is actually taken (ignore accidental touches)
 void buttonCommands::startHandleLoop()
 {
     //-- variables --
@@ -226,7 +226,7 @@ void buttonCommands::startHandleLoop()
                 break;
             case RE_ET_CHANGED:
                 // ignore first clicks
-                if (++rotateCount < IGNORE_ROTATE_COUNT)
+                if (rotateCount++ < IGNORE_ROTATE_COUNT)
                     {
                         buzzer->beep(1, 60, 0);
                         break;
@@ -245,8 +245,8 @@ void buttonCommands::startHandleLoop()
                         backRest->setTargetPercent(backRest->getTargetPercent() + 5);
                     // show temporary notification on display
                     char buf[8];
-                    snprintf(buf, 8, "%.1f%%", backRest->getTargetPercent());
-                    display_showNotification(1000, "moving Rest:", "LEG", buf);
+                    snprintf(buf, 8, "%.0f%%", backRest->getTargetPercent());
+                    display_showNotification(2500, "moving Rest:", "BACK", buf);
                 }
                 //### adjust leg support when ROTATED ###
                 else
@@ -258,8 +258,8 @@ void buttonCommands::startHandleLoop()
                         legRest->setTargetPercent(legRest->getTargetPercent() + 5);
                     // show temporary notification on display
                     char buf[8];
-                    snprintf(buf, 8, "%.1f%%", legRest->getTargetPercent());
-                    display_showNotification(1000, "moving Rest:", "LEG", buf);
+                    snprintf(buf, 8, "%.0f%%", legRest->getTargetPercent());
+                    display_showNotification(2500, "moving Rest:", "LEG", buf);
                 }
                 buzzer->beep(1, 90, 0);
                 break;
diff --git a/board_single/main/display.cpp b/board_single/main/display.cpp
index 52711fa..7a84df8 100644
--- a/board_single/main/display.cpp
+++ b/board_single/main/display.cpp
@@ -596,6 +596,10 @@ void handleStatusScreen(display_task_parameters_t *objects)
 // trigger custom notification that is shown in any mode for set amount of time
 void display_showNotification(uint32_t showDurationMs, const char *line1, const char *line2Large, const char *line3Large)
 {
+	// clear display when notification initially shown
+	if (notificationIsActive == false)
+		ssd1306_clear_screen(&dev, false);
+	// update state and timestamp for auto exit
 	timestampNotificationStop = esp_log_timestamp() + showDurationMs;
 	notificationIsActive = true;
 	// void displayTextLineCentered(SSD1306_t *display, int line, bool isLarge, bool inverted, const char *format, ...);
diff --git a/board_single/main/main.cpp b/board_single/main/main.cpp
index cd89a31..4e6774e 100644
--- a/board_single/main/main.cpp
+++ b/board_single/main/main.cpp
@@ -162,7 +162,7 @@ void createObjects()
 
     // create objects for controlling the chair position
     //                       gpio_up, gpio_down, travelDuration, name, defaultPosition
-    legRest = new cControlledRest(GPIO_NUM_2, GPIO_NUM_15, 12000, "legRest");
+    legRest = new cControlledRest(GPIO_NUM_2, GPIO_NUM_15, 11000, "legRest");
     backRest = new cControlledRest(GPIO_NUM_16, GPIO_NUM_4, 12000, "backRest", 100); //default position "100% up"
 
     // create control object (control.hpp)
diff --git a/common/chairAdjust.cpp b/common/chairAdjust.cpp
index 2fb9785..572457f 100644
--- a/common/chairAdjust.cpp
+++ b/common/chairAdjust.cpp
@@ -95,10 +95,18 @@ void cControlledRest::updatePosition(){
 //============================
 void cControlledRest::setState(restState_t targetState)
 {
-    //check if actually changed
-    if (targetState == state){
-        ESP_LOGV(TAG, "[%s] state already at '%s', nothing to do", name, restStateStr[state]);
-        return;
+    // TODO: drop this section?
+    // check if actually changed
+    if (targetState == state)
+    {
+    // update anyways when target is 0 or 100, to trigger movement threshold in case of position tracking is out of sync
+        if (positionTarget == 0 || positionTarget == 100)
+            ESP_LOGD(TAG, "[%s] state already at '%s', but updating anyway to trigger move to limit addition", name, restStateStr[state]);
+        else
+        {
+            ESP_LOGV(TAG, "[%s] state already at '%s', nothing to do", name, restStateStr[state]);
+            return;
+        }
     }
 
     // when switching direction without stop: update position first
@@ -147,8 +155,14 @@ void cControlledRest::setTargetPercent(float targetPercent){
         positionTarget = 100;
     else if (positionTarget < 0)
         positionTarget = 0;
-        
-        ESP_LOGI(TAG, "[%s] changed Target position from %.2f%% to %.2f%%", name, positionTargetPrev, positionTarget);
+
+    // ignore if unchanged
+    //if (positionTarget == positionTargetPrev){
+    //    ESP_LOGI(TAG, "[%s] Target position unchanged at %.2f%%", name, positionTarget);
+    //    return;
+    //}
+
+    ESP_LOGI(TAG, "[%s] changed Target position from %.2f%% to %.2f%%", name, positionTargetPrev, positionTarget);
 
     // start rest in required direction
     // TODO always run this check in handle()?
@@ -185,7 +199,7 @@ void cControlledRest::handle(){
 
     // target reached
     if (timeRan >= timeTarget){
-        ESP_LOGW(TAG, "[%s] reached target run-time (%dms/%dms) for position %.2f%% -> stopping", name, timeRan, timeTarget, positionTarget);
+        ESP_LOGW(TAG, "[%s] handle: reached target run-time (%dms/%dms) for position %.2f%% -> stopping", name, timeRan, timeTarget, positionTarget);
         setState(REST_OFF);
     }
 }