Skip to content

Commit

Permalink
111, 112 Backlight handling improve (#113)
Browse files Browse the repository at this point in the history
* Fix and enhancement
#111 Backlight timeout brightness is hardcoded and too high
#112 Backlight menu slider needs tweaking

* get close enough to 100%, adjust 9step to 8step

* use a step counter in the loop

* Modified to match clang-format
  • Loading branch information
hijae authored Aug 14, 2024
1 parent 4e44ff1 commit 88a993a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
35 changes: 28 additions & 7 deletions lib/M5ez/src/M5ez.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ void ezSettings::defaults() {
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() {
Expand All @@ -801,7 +804,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);
Expand All @@ -819,19 +834,25 @@ 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: {
ezProgressBar bl("Backlight brightness", "Set brightness", "Adjust#Back");
while (true) {
String b = ez.buttons.poll();
if (b == "Adjust") {
if (_brightness >= 48 && _brightness < 248)
_brightness += 20;
else
_brightness = 48;
if (_brightness >= _MinimumBrightness && _Step < _MaxSteps - 1) {
_Step++;
_brightness =
_MinimumBrightness + (_Step * (255 - _MinimumBrightness) / (_MaxSteps - 1));
} else {
_Step = 0;
_brightness = _MinimumBrightness;
}
}
float p = float(_brightness) / 2.48;
float p = ((float)_Step / (_MaxSteps - 1)) * 100.0f;
bl.value(p);
M5.Display.setBrightness(_brightness);
if (b == "Back")
Expand Down Expand Up @@ -907,7 +928,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())
Expand Down
3 changes: 3 additions & 0 deletions lib/M5ez/src/M5ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ class ezBacklight {
static uint8_t _brightness;
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;
//
};
Expand Down

0 comments on commit 88a993a

Please sign in to comment.