Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct: Improved backlight handling and charging #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name=M5ez
version=2.3.0
version=2.3.1
author=Rop Gonggrijp
maintainer=Rop Gonggrijp
maintainer=Rop Gonggrijp, Roberto Coli
sentence=Complete interface builder for the M5Stack, an ESP32 based mini tinker-computer
paragraph=See more on https://github.com/M5ez/M5ez
paragraph=See more on https://github.com/antimix/M5ez
category=Display
url=https://github.com/M5ez/M5ez
url=https://github.com/antimix/M5ez
architectures=esp32
includes=M5ez.h
depends=ezTime, M5Stack
53 changes: 45 additions & 8 deletions src/M5ez.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,10 @@ void ezSettings::defaults() {
uint8_t ezBacklight::_inactivity;
uint32_t ezBacklight::_last_activity;
bool ezBacklight::_backlight_off = false;
bool ezBacklight::_inactive = false;
uint32_t ezBacklight::_ButA_LastChg = 0;
uint32_t ezBacklight::_ButB_LastChg = 0;
uint32_t ezBacklight::_ButC_LastChg = 0;

void ezBacklight::begin() {
ez.addEvent(ez.backlight.loop);
Expand Down Expand Up @@ -850,19 +854,25 @@ void ezSettings::defaults() {
}

uint16_t ezBacklight::loop() {
if (!_backlight_off && _inactivity) {
if (!_backlight_off && _inactivity && !_inactive) {
if (millis() > _last_activity + 30000 * _inactivity) {
_backlight_off = true;
m5.lcd.setBrightness(0);
while (true) {
if (m5.BtnA.wasPressed() || m5.BtnB.wasPressed() || m5.BtnC.wasPressed()) break;
ez.yield();
delay(10);
}
ez.buttons.releaseWait(); // Make sure the key pressed to wake display gets ignored
_inactive = true;
ez.yield();
_ButA_LastChg = M5.BtnA.lastChange();
_ButB_LastChg = M5.BtnB.lastChange();
_ButC_LastChg = M5.BtnC.lastChange();
}
}

if (_backlight_off || _inactive) {
ez.yield();
if (_ButA_LastChg != M5.BtnA.lastChange() || _ButB_LastChg != M5.BtnB.lastChange() || _ButC_LastChg != M5.BtnC.lastChange()) {
m5.lcd.setBrightness(_brightness);
activity();
_backlight_off = false;
_inactive = false;
}
}
return 1000;
Expand Down Expand Up @@ -2006,9 +2016,11 @@ void ezSettings::defaults() {
bool ezBattery::_on = false;
#define BATTERY_CHARGING_OFF (255)
uint8_t ezBattery::_numChargingBars = BATTERY_CHARGING_OFF;
bool ezBattery::_canControl = false;

void ezBattery::begin() {
Wire.begin();
_canControl = m5.Power.canControl();
ez.battery.readFlash();
ez.settings.menuObj.addItem("Battery settings", ez.battery.menu);
if (_on) {
Expand Down Expand Up @@ -2051,7 +2063,28 @@ void ezSettings::defaults() {
}
}


void ezBattery::adaptChargeMode() {
// If power management not available, ignore the routine
if(!_canControl) {
return;
}

// Disable the charging if the battery is fully charged
if(m5.Power.isChargeFull()) {
m5.Power.setCharge(false);
} else if (m5.Power.getBatteryLevel() < 76) {
m5.Power.setCharge(true);
}

// Define the shutdown time at 64s
m5.Power.setLowPowerShutdownTime(M5.Power.ShutdownTime::SHUTDOWN_64S);
}



uint16_t ezBattery::loop() {
adaptChargeMode();
if (!_on) return 0;
ez.header.draw("battery");
return (_numChargingBars != BATTERY_CHARGING_OFF ? 1000 : 5000);
Expand Down Expand Up @@ -2120,7 +2153,11 @@ void ezSettings::defaults() {
uint8_t top = ez.theme->header_height / 10;
uint8_t height = ez.theme->header_height * 0.8;
m5.lcd.fillRoundRect(left_offset, top, ez.theme->battery_bar_width, height, ez.theme->battery_bar_gap, ez.theme->header_bgcolor);
m5.lcd.drawRoundRect(left_offset, top, ez.theme->battery_bar_width, height, ez.theme->battery_bar_gap, ez.theme->header_fgcolor);
if (m5.Power.isCharging()) {
m5.lcd.drawRoundRect(left_offset, top, ez.theme->battery_bar_width, height, ez.theme->battery_bar_gap, RED);
} else {
m5.lcd.drawRoundRect(left_offset, top, ez.theme->battery_bar_width, height, ez.theme->battery_bar_gap, ez.theme->header_fgcolor);
}
uint8_t bar_width = (ez.theme->battery_bar_width - ez.theme->battery_bar_gap * 5) / 4.0;
uint8_t bar_height = height - ez.theme->battery_bar_gap * 2;
left_offset += ez.theme->battery_bar_gap;
Expand Down
6 changes: 6 additions & 0 deletions src/M5ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ class ezSettings {
static uint8_t _inactivity;
static uint32_t _last_activity;
static bool _backlight_off;
static bool _inactive;
static uint32_t _ButA_LastChg;
static uint32_t _ButB_LastChg;
static uint32_t _ButC_LastChg;
//
};
#endif
Expand Down Expand Up @@ -632,7 +636,9 @@ class ezSettings {
static uint16_t loop();
static uint8_t getTransformedBatteryLevel();
static uint32_t getBatteryBarColor(uint8_t batteryLevel);
static void adaptChargeMode();
private:
static bool _canControl;
static bool _on;
static void _refresh();
static void _drawWidget(uint16_t x, uint16_t w);
Expand Down