From fb5912598255bc5621addf8295d449e5837445f1 Mon Sep 17 00:00:00 2001 From: Hijae Song Date: Sun, 11 Aug 2024 23:37:26 +0900 Subject: [PATCH 1/4] Fix and enhancement #111 Backlight timeout brightness is hardcoded and too high #112 Backlight menu slider needs tweaking --- lib/M5ez/src/M5ez.cpp | 23 ++++++++++++++++++----- lib/M5ez/src/M5ez.h | 1 + 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 0562f8f..61dca7b 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -793,6 +793,7 @@ void ezSettings::defaults() { uint8_t ezBacklight::_brightness; uint8_t ezBacklight::_inactivity; uint32_t ezBacklight::_last_activity; +uint8_t ezBacklight::_MinimumBrightness; bool ezBacklight::_backlight_off = false; void ezBacklight::begin() { @@ -801,7 +802,19 @@ void ezBacklight::begin() { Preferences prefs; prefs.begin("M5ez", true); // read-only _brightness = prefs.getUChar("brightness", 128); - if (_brightness < 48) { + switch (M5.getBoard()) { + case m5::board_t::board_M5StickCPlus2: + case m5::board_t::board_M5StackCore2: + _MinimumBrightness = 16; + break; + case m5::board_t::board_M5StickCPlus: + case m5::board_t::board_M5StickC: + _MinimumBrightness = 48; + break; + default: + _MinimumBrightness = 64; + } + if (_brightness < _MinimumBrightness) { _brightness = 100; } _inactivity = prefs.getUChar("inactivity", 1); @@ -826,10 +839,10 @@ void ezBacklight::menu() { while (true) { String b = ez.buttons.poll(); if (b == "Adjust") { - if (_brightness >= 48 && _brightness < 248) - _brightness += 20; + if (_brightness >= _MinimumBrightness && _brightness < (255 - _MinimumBrightness)) + _brightness += (255 - _MinimumBrightness) / 8; else - _brightness = 48; + _brightness = _MinimumBrightness + (255 - _MinimumBrightness) / 8; } float p = float(_brightness) / 2.48; bl.value(p); @@ -907,7 +920,7 @@ uint16_t ezBacklight::loop(void *private_data) { if (!_backlight_off && _inactivity) { if (millis() > _last_activity + 30000 * _inactivity) { _backlight_off = true; - M5.Display.setBrightness(64); + M5.Display.setBrightness(_MinimumBrightness); changeCpuPower(true); while (true) { if (M5.BtnA.wasClicked() || M5.BtnB.wasClicked()) diff --git a/lib/M5ez/src/M5ez.h b/lib/M5ez/src/M5ez.h index f447c04..06bc353 100644 --- a/lib/M5ez/src/M5ez.h +++ b/lib/M5ez/src/M5ez.h @@ -509,6 +509,7 @@ class ezBacklight { static uint8_t _brightness; static uint8_t _inactivity; static uint32_t _last_activity; + static uint8_t _MinimumBrightness; static bool _backlight_off; // }; From 6314033543f2bfdb34555774df3223eaa3ce7a32 Mon Sep 17 00:00:00 2001 From: Hijae Song Date: Tue, 13 Aug 2024 03:13:07 +0900 Subject: [PATCH 2/4] get close enough to 100%, adjust 9step to 8step --- lib/M5ez/src/M5ez.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 61dca7b..984aff7 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -840,11 +840,11 @@ void ezBacklight::menu() { String b = ez.buttons.poll(); if (b == "Adjust") { if (_brightness >= _MinimumBrightness && _brightness < (255 - _MinimumBrightness)) - _brightness += (255 - _MinimumBrightness) / 8; + _brightness += (255 - _MinimumBrightness) / 7; else - _brightness = _MinimumBrightness + (255 - _MinimumBrightness) / 8; + _brightness = _MinimumBrightness; } - float p = float(_brightness) / 2.48; + float p = float(_brightness - _MinimumBrightness) / (255 - _MinimumBrightness) * 100; bl.value(p); M5.Display.setBrightness(_brightness); if (b == "Back") From 1faf8930b2bf1964253a4ffad11cc5aaecf8ee7b Mon Sep 17 00:00:00 2001 From: Hijae Song Date: Tue, 13 Aug 2024 21:13:26 +0900 Subject: [PATCH 3/4] use a step counter in the loop --- lib/M5ez/src/M5ez.cpp | 15 ++++++++++++--- lib/M5ez/src/M5ez.h | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 984aff7..6d9fa92 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -794,6 +794,8 @@ uint8_t ezBacklight::_brightness; uint8_t ezBacklight::_inactivity; uint32_t ezBacklight::_last_activity; uint8_t ezBacklight::_MinimumBrightness; +uint8_t ezBacklight::_Step = 0; +uint8_t ezBacklight::_MaxSteps = 8; bool ezBacklight::_backlight_off = false; void ezBacklight::begin() { @@ -832,6 +834,7 @@ void ezBacklight::menu() { blmenu.addItem("Inactivity timeout"); blmenu.addItem("Back"); blmenu.downOnLast("first"); + _Step = (_brightness - _MinimumBrightness) * (_MaxSteps) / (256 - _MinimumBrightness); // Calculate step from brightness while (true) { switch (blmenu.runOnce()) { case 1: { @@ -839,12 +842,18 @@ void ezBacklight::menu() { while (true) { String b = ez.buttons.poll(); if (b == "Adjust") { - if (_brightness >= _MinimumBrightness && _brightness < (255 - _MinimumBrightness)) - _brightness += (255 - _MinimumBrightness) / 7; + if (_brightness >= _MinimumBrightness && _Step < _MaxSteps - 1) + { + _Step++; + _brightness = _MinimumBrightness + (_Step * (255 - _MinimumBrightness) / (_MaxSteps - 1)); + } else + { + _Step = 0; _brightness = _MinimumBrightness; + } } - float p = float(_brightness - _MinimumBrightness) / (255 - _MinimumBrightness) * 100; + float p = ((float)_Step / (_MaxSteps - 1)) * 100.0f; bl.value(p); M5.Display.setBrightness(_brightness); if (b == "Back") diff --git a/lib/M5ez/src/M5ez.h b/lib/M5ez/src/M5ez.h index 06bc353..56e86b5 100644 --- a/lib/M5ez/src/M5ez.h +++ b/lib/M5ez/src/M5ez.h @@ -510,6 +510,8 @@ class ezBacklight { static uint8_t _inactivity; static uint32_t _last_activity; static uint8_t _MinimumBrightness; + static uint8_t _Step; + static uint8_t _MaxSteps; static bool _backlight_off; // }; From 935f87c0897cf534abac8450b4fc80cc47b1b6c9 Mon Sep 17 00:00:00 2001 From: Hijae Song Date: Wed, 14 Aug 2024 00:11:13 +0900 Subject: [PATCH 4/4] Modified to match clang-format --- lib/M5ez/src/M5ez.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/M5ez/src/M5ez.cpp b/lib/M5ez/src/M5ez.cpp index 6d9fa92..3a99734 100644 --- a/lib/M5ez/src/M5ez.cpp +++ b/lib/M5ez/src/M5ez.cpp @@ -834,7 +834,8 @@ void ezBacklight::menu() { blmenu.addItem("Inactivity timeout"); blmenu.addItem("Back"); blmenu.downOnLast("first"); - _Step = (_brightness - _MinimumBrightness) * (_MaxSteps) / (256 - _MinimumBrightness); // Calculate step from brightness + _Step = (_brightness - _MinimumBrightness) * _MaxSteps + / (256 - _MinimumBrightness); // Calculate step from brightness while (true) { switch (blmenu.runOnce()) { case 1: { @@ -842,13 +843,11 @@ void ezBacklight::menu() { while (true) { String b = ez.buttons.poll(); if (b == "Adjust") { - if (_brightness >= _MinimumBrightness && _Step < _MaxSteps - 1) - { + if (_brightness >= _MinimumBrightness && _Step < _MaxSteps - 1) { _Step++; - _brightness = _MinimumBrightness + (_Step * (255 - _MinimumBrightness) / (_MaxSteps - 1)); - } - else - { + _brightness = + _MinimumBrightness + (_Step * (255 - _MinimumBrightness) / (_MaxSteps - 1)); + } else { _Step = 0; _brightness = _MinimumBrightness; }