From 61af08d4993014935a3a378952cd5562ee4e2e14 Mon Sep 17 00:00:00 2001 From: Malderin <52313714+Malderin@users.noreply.github.com> Date: Tue, 6 Apr 2021 04:39:57 +0300 Subject: [PATCH] MKS LVGL UI: Display/edit fan percentage (#21544) Co-authored-by: Scott Lahteine --- Marlin/src/lcd/extui/lib/mks_ui/draw_fan.cpp | 50 ++++++-------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/Marlin/src/lcd/extui/lib/mks_ui/draw_fan.cpp b/Marlin/src/lcd/extui/lib/mks_ui/draw_fan.cpp index af8b441f24..700471b4a3 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/draw_fan.cpp +++ b/Marlin/src/lcd/extui/lib/mks_ui/draw_fan.cpp @@ -32,8 +32,7 @@ #include "../../../../inc/MarlinConfig.h" extern lv_group_t *g; -static lv_obj_t *scr; -static lv_obj_t *fanText; +static lv_obj_t *scr, *fanText; enum { ID_F_ADD = 1, @@ -44,43 +43,23 @@ enum { ID_F_RETURN }; -static uint8_t fanSpeed; - static void event_handler(lv_obj_t *obj, lv_event_t event) { if (event != LV_EVENT_RELEASED) return; - + uint8_t fanPercent = map(thermalManager.fan_speed[0], 0, 255, 0, 100); switch (obj->mks_obj_id) { - case ID_F_ADD: - if (fanSpeed < 254) fanSpeed++; - break; - case ID_F_DEC: - if (fanSpeed > 0) fanSpeed--; - break; - case ID_F_HIGH: - fanSpeed = 255; - break; - case ID_F_MID: - fanSpeed = 127; - break; - case ID_F_OFF: - gcode.process_subcommands_now_P(PSTR("M107")); - return; - case ID_F_RETURN: - clear_cur_ui(); - draw_return_ui(); - return; + case ID_F_ADD: if (fanPercent < 100) fanPercent++; break; + case ID_F_DEC: if (fanPercent != 0) fanPercent--; break; + case ID_F_HIGH: fanPercent = 100; break; + case ID_F_MID: fanPercent = 50; break; + case ID_F_OFF: fanPercent = 0; break; + case ID_F_RETURN: clear_cur_ui(); draw_return_ui(); return; } - sprintf_P(public_buf_l, PSTR("M106 S%d"), fanSpeed); - gcode.process_subcommands_now(public_buf_l); + thermalManager.set_fan_speed(0, map(fanPercent, 0, 100, 0, 255)); } void lv_draw_fan() { lv_obj_t *buttonAdd; - #if HAS_FAN - fanSpeed = thermalManager.fan_speed[0]; - #endif - scr = lv_screen_create(FAN_UI); // Create an Image button buttonAdd = lv_big_button_create(scr, "F:/bmp_Add.bin", fan_menu.add, INTERVAL_V, titleHeight, event_handler, ID_F_ADD); @@ -97,12 +76,11 @@ void lv_draw_fan() { } void disp_fan_value() { - char buf1[10] = {0}; - public_buf_l[0] = '\0'; - strcat(public_buf_l, fan_menu.state); - strcat_P(public_buf_l, PSTR(": ")); - sprintf_P(buf1, PSTR("%3d"), thermalManager.fan_speed[0]); - strcat(public_buf_l, buf1); + #if HAS_FAN + sprintf_P(public_buf_l, PSTR("%s: %3d%%"), fan_menu.state, (int)map(thermalManager.fan_speed[0], 0, 255, 0, 100)); + #else + sprintf_P(public_buf_l, PSTR("%s: ---"), fan_menu.state); + #endif lv_label_set_text(fanText, public_buf_l); lv_obj_align(fanText, nullptr, LV_ALIGN_CENTER, 0, -65); }