diff --git a/TFT/src/User/API/CaseLightControl.c b/TFT/src/User/API/CaseLightControl.c index 9bcf333f29..f9f461e552 100644 --- a/TFT/src/User/API/CaseLightControl.c +++ b/TFT/src/User/API/CaseLightControl.c @@ -1,22 +1,12 @@ #include "CaseLightControl.h" #include "includes.h" -static uint8_t caseLightBrightness = 0; -static uint8_t lastCaseLightBrightness = 0; -static bool lastCaseLightState = true; static bool caseLightState = true; +static uint8_t caseLightBrightness = 0; -static bool lightQueryWait = false; -static bool caseLight_applied = false; - -// Sends a query (M355) to the printer asking for the current brightness -void caseLightValueQuery(void) +bool caseLightGetState(void) { - if (infoMachineSettings.caseLightsBrightness == ENABLED && infoHost.connected && - !infoHost.wait && !lightQueryWait) - { - lightQueryWait = storeCmd("M355\n"); - } + return caseLightState; } void caseLightSetState(bool state) @@ -24,61 +14,23 @@ void caseLightSetState(bool state) caseLightState = state; } -void caseLightSetBrightness(uint8_t brightness) -{ - caseLightBrightness = NOBEYOND(0, brightness, 255); -} - -void caseLightChangeBrightness(int8_t brightness_delta) -{ - caseLightBrightness = NOBEYOND(0, caseLightBrightness + brightness_delta, 255); -} - -void caseLightChangeBrightnessPrecent(int8_t brightness_delta_percent) -{ - brightness_delta_percent = NOBEYOND(-100, brightness_delta_percent, 100); - caseLightBrightness = NOBEYOND(0, (caseLightGetBrightnessPercent() + brightness_delta_percent) * 255 / 100.0f, 255); -} - -void caseLightQuerySetWait(bool wait) -{ - lightQueryWait = wait; -} - uint8_t caseLightGetBrightness(void) { return caseLightBrightness; } -uint8_t caseLightGetBrightnessPercent(void) -{ - return (caseLightBrightness * 100.0f) / 255 + 0.5f; -} - -bool caseLightGetState(void) +void caseLightSetBrightness(uint8_t brightness) { - return caseLightState; + caseLightBrightness = NOBEYOND(0, brightness, 255); } -void caseLightApplied(bool applied) +uint8_t caseLightGetBrightnessPercent(void) { - caseLight_applied = applied; + return (caseLightBrightness * 100.0f) / 255 + 0.5f; } -void loopCaseLight(void) +void caseLightSetBrightnessPercent(int8_t unit) { - if (lastCaseLightBrightness != caseLightBrightness || lastCaseLightState != caseLightState) - { - if (caseLight_applied == false) - { - caseLight_applied = storeCmd("M355 S%d P%d\n", caseLightState ? 1 : 0, caseLightBrightness); - } - - if (caseLight_applied == true) - { - lastCaseLightBrightness = caseLightBrightness; - lastCaseLightState = caseLightState; - caseLight_applied = false; - } - } + unit = NOBEYOND(-100, unit, 100); + caseLightBrightness = NOBEYOND(0, (caseLightGetBrightnessPercent() + unit) * 255 / 100.0f, 255); } diff --git a/TFT/src/User/API/CaseLightControl.h b/TFT/src/User/API/CaseLightControl.h index 3f6da0267b..236dd7c50b 100644 --- a/TFT/src/User/API/CaseLightControl.h +++ b/TFT/src/User/API/CaseLightControl.h @@ -7,17 +7,14 @@ extern "C" { #include #include -void caseLightValueQuery(void); +bool caseLightGetState(void); void caseLightSetState(bool state); -void caseLightSetBrightness(uint8_t brightness); -void caseLightQuerySetWait(bool wait); + uint8_t caseLightGetBrightness(void); +void caseLightSetBrightness(uint8_t brightness); + uint8_t caseLightGetBrightnessPercent(void); -bool caseLightGetState(void); -void loopCaseLight(void); -void caseLightApplied(bool applied); -void caseLightChangeBrightness(int8_t brightness_delta); -void caseLightChangeBrightnessPrecent(int8_t brightness_delta_percent); +void caseLightSetBrightnessPercent(int8_t unit); #ifdef __cplusplus } diff --git a/TFT/src/User/API/interfaceCmd.c b/TFT/src/User/API/interfaceCmd.c index 96191c93c0..9c8762e01c 100644 --- a/TFT/src/User/API/interfaceCmd.c +++ b/TFT/src/User/API/interfaceCmd.c @@ -978,7 +978,6 @@ void sendQueueCmd(void) { caseLightSetBrightness(cmd_value()); } - caseLightApplied(true); break; } diff --git a/TFT/src/User/API/menu.c b/TFT/src/User/API/menu.c index 028b800464..fd94a86feb 100644 --- a/TFT/src/User/API/menu.c +++ b/TFT/src/User/API/menu.c @@ -1304,11 +1304,6 @@ void loopBackEnd(void) if (GET_BIT(infoSettings.general_settings, INDEX_EVENT_LED) == 1) LED_CheckEvent(); - if (infoMachineSettings.caseLightsBrightness == ENABLED) - { - loopCaseLight(); - } - // Query RRF status rrfStatusQuery(); } // loopBackEnd diff --git a/TFT/src/User/API/parseACK.c b/TFT/src/User/API/parseACK.c index 180d09aabe..02f1e83ffe 100644 --- a/TFT/src/User/API/parseACK.c +++ b/TFT/src/User/API/parseACK.c @@ -786,8 +786,6 @@ void parseACK(void) caseLightSetState(true); caseLightSetBrightness(ack_value()); } - caseLightQuerySetWait(false); - caseLightApplied(true); } // parse and store M420 V1 T1, mesh data (e.g. from Mesh Editor menu) // diff --git a/TFT/src/User/Menu/CaseLight.c b/TFT/src/User/Menu/CaseLight.c index a277566fab..3cb952b716 100644 --- a/TFT/src/User/Menu/CaseLight.c +++ b/TFT/src/User/Menu/CaseLight.c @@ -1,15 +1,20 @@ #include "CaseLight.h" #include "includes.h" +#define CASE_LIGHT_UPDATE_TIME 1000 // 1 seconds is 1000 + +// Icons list for Case Light const ITEM itemCaseLight[2] = { // icon label {ICON_RGB_OFF, LABEL_OFF}, {ICON_RGB_WHITE, LABEL_ON}, }; +static uint8_t curUnit_index = 1; + static inline void updateCaseLightIcon(MENUITEMS * curmenu, bool state) { - curmenu->items[KEY_ICON_4] = itemCaseLight[state ? 1 : 0]; + curmenu->items[KEY_ICON_5] = itemCaseLight[state ? 1 : 0]; } void caseLightBrightnessReDraw() @@ -34,24 +39,25 @@ void menuCaseLight(void) {ICON_NULL, LABEL_NULL}, {ICON_NULL, LABEL_NULL}, {ICON_INC, LABEL_INC}, + {ICON_E_5_PERCENT, LABEL_5_PERCENT}, {ICON_RGB_WHITE, LABEL_ON}, {ICON_NULL, LABEL_NULL}, - {ICON_NULL, LABEL_NULL}, {ICON_BACK, LABEL_BACK}, } }; KEY_VALUES key_num = KEY_IDLE; - - // Initiate query. Value will be compared in while loop - caseLightValueQuery(); - bool currentCaseLightState = caseLightGetState(); bool previousCaseLightState = currentCaseLightState; uint8_t currentCaseLightBrightness = caseLightGetBrightness(); uint8_t previousCaseLightBrightness = currentCaseLightBrightness; + bool sendingNeeded = false; + storeCmd("M355\n"); // retrieve the case light's state and brightness. Value will be compared in while loop + + caseLightItems.items[KEY_ICON_4] = itemPercent[curUnit_index]; updateCaseLightIcon(&caseLightItems, currentCaseLightState); + menuDrawPage(&caseLightItems); caseLightBrightnessReDraw(); @@ -61,19 +67,29 @@ void menuCaseLight(void) switch (key_num) { + // decrease case light case KEY_ICON_0: - caseLightChangeBrightnessPrecent(-10); - caseLightBrightnessReDraw(); + case KEY_DECREASE: + caseLightSetBrightnessPercent(- percentSteps[curUnit_index]); break; + // increase case light case KEY_ICON_3: - caseLightChangeBrightnessPrecent(10); - caseLightBrightnessReDraw(); + case KEY_INCREASE: + caseLightSetBrightnessPercent(+ percentSteps[curUnit_index]); break; + // change unit case KEY_ICON_4: + curUnit_index = (curUnit_index + 1) % ITEM_PERCENT_STEPS_NUM; + + caseLightItems.items[key_num] = itemPercent[curUnit_index]; + menuDrawItem(&caseLightItems.items[key_num], key_num); + break; + + // switch on/off case light + case KEY_ICON_5: caseLightSetState(!currentCaseLightState); - caseLightBrightnessReDraw(); break; case KEY_ICON_7: @@ -81,32 +97,35 @@ void menuCaseLight(void) break; default: - #if LCD_ENCODER_SUPPORT - if (encoderPosition) - { - caseLightChangeBrightnessPrecent(encoderPosition); - caseLightBrightnessReDraw(); - encoderPosition = 0; - } - #endif break; } currentCaseLightState = caseLightGetState(); - if (previousCaseLightState != currentCaseLightState) + if (previousCaseLightState != currentCaseLightState) // dynamically change the light on/off icon based on the current state { - // Dynamically change the light on/off icon based on the current state previousCaseLightState = currentCaseLightState; + updateCaseLightIcon(&caseLightItems, currentCaseLightState); - menuDrawItem(&caseLightItems.items[KEY_ICON_4], KEY_ICON_4); - caseLightBrightnessReDraw(); + menuDrawItem(&caseLightItems.items[KEY_ICON_5], KEY_ICON_5); + + sendingNeeded = true; } currentCaseLightBrightness = caseLightGetBrightness(); if (previousCaseLightBrightness != currentCaseLightBrightness) { previousCaseLightBrightness = currentCaseLightBrightness; + caseLightBrightnessReDraw(); + + sendingNeeded = true; + } + + if (sendingNeeded && nextScreenUpdate(CASE_LIGHT_UPDATE_TIME)) + { + storeCmd("M355 S%d P%d\n", currentCaseLightState ? 1 : 0, currentCaseLightBrightness); + + sendingNeeded = false; } loopProcess();