Skip to content

Commit

Permalink
86 backlight doesnt turn off on the m5stickc plus2 (#88)
Browse files Browse the repository at this point in the history
* Change platform macros to match M5Unified and M5GFX libraries.

Also:
- update M5ez library dependency
- disable IMU, mic and speaker
- cleanup display rotation

* Fix M5StickC Plus2 backlight flicker during idle.

The M5StickC Plus and Core devices use PWM to control backlight, unlike
the M5StickC and M5StickCPlus.
During lightSleep() PWM is inactive, so backlight control goes crazy,
instead, reduce CPU frequency and just delay, avoid lightSleep.

Enable compiler warnings, fix warnings.

* Appease clang-format.

* Update platformio version.
  • Loading branch information
gkoh authored Mar 21, 2024
1 parent 89e2cf9 commit 5adb393
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/M5ez/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "https://github.com/ropg/M5ez"
},
"dependencies": {
"name": "M5Stack"
"name": "M5Unified"
},
"version": "2.1.2",
"framework": "arduino",
Expand Down
44 changes: 33 additions & 11 deletions lib/M5ez/src/M5ez.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,6 @@ void ezSettings::begin() {
ez.settings.menuObj.addItem("Theme chooser", ez.theme->menu);
}
ez.settings.menuObj.addItem("Factory defaults", ez.settings.defaults);

#ifdef M5STACK_CORE2
M5.Lcd.setRotation(1);
#endif
}

void ezSettings::menu() {
Expand Down Expand Up @@ -836,7 +832,6 @@ void ezBacklight::menu() {
_brightness = 48;
}
float p = float(_brightness) / 2.48;
Serial.println(_brightness);
bl.value(p);
M5.Display.setBrightness(_brightness);
if (b == "Back")
Expand Down Expand Up @@ -892,22 +887,44 @@ void ezBacklight::activity() {
_last_activity = millis();
}

void changeCpuPower(bool reduce) {
switch (M5.getBoard()) {
case m5::board_t::board_M5StickC:
case m5::board_t::board_M5StickCPlus:
// do nothing, lightSleep() works with backlight control
break;
default:
// Backlight is PWM controlled, lightSleep() stops PWM
if (reduce) {
setCpuFrequencyMhz(10);
} else {
setCpuFrequencyMhz(80);
}
}
}

uint16_t ezBacklight::loop(void *private_data) {
if (!_backlight_off && _inactivity) {
if (millis() > _last_activity + 30000 * _inactivity) {
_backlight_off = true;
M5.Display.setBrightness(64);
changeCpuPower(true);
while (true) {
if (M5.BtnA.wasClicked() || M5.BtnB.wasClicked())
break;
ez.yield();
#if M5STACK_CORE2
delay(100);
#else
M5.Power.lightSleep(100000);
#endif
switch (M5.getBoard()) {
case m5::board_t::board_M5StickC:
case m5::board_t::board_M5StickCPlus:
M5.Power.lightSleep(100000);
break;
default:
// PWM inactive during lightSleep, thus display backlight not controlled
delay(100);
}
}
ez.buttons.releaseWait(); // Make sure the key pressed to wake display gets ignored
changeCpuPower(false);
M5.Display.setBrightness(_brightness);
activity();
_backlight_off = false;
Expand Down Expand Up @@ -2229,8 +2246,13 @@ long M5ez::_text_cursor_millis;

void M5ez::begin() {
auto cfg = M5.config();
cfg.internal_imu = false;
cfg.internal_spk = false;
cfg.internal_mic = false;
M5.begin(cfg);
#if ARDUINO_M5STICK_C || ARDUINO_M5STICK_C_PLUS
M5.Lcd.setRotation(3);
#endif
ezTheme::begin();
ez.screen.begin();
ez.settings.begin();
Expand Down Expand Up @@ -2862,7 +2884,7 @@ void M5ez::setFont(const GFXfont *font) {
}

int16_t M5ez::fontHeight() {
#if M5STICKC_PLUS || M5STACK_CORE2
#if ARDUINO_M5STICK_C_PLUS || ARDUINO_M5STACK_CORE_ESP32
return M5.Lcd.fontHeight(M5.Lcd.getFont());
#else
return 11;
Expand Down
6 changes: 3 additions & 3 deletions lib/M5ez/src/M5ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

#define NO_COLOR TFT_TRANSPARENT

#ifdef M5STICKC_PLUS
#if ARDUINO_M5STICK_C_PLUS
#define TFT_W 240
#define TFT_H 135
#define TFT_FONT sans16
Expand All @@ -70,7 +70,7 @@
#define TFT_RADIUS 8
#endif

#ifdef M5STICKC
#if ARDUINO_M5STICK_C
#define TFT_W 160
#define TFT_H 80
#define TFT_FONT hzk16
Expand All @@ -79,7 +79,7 @@
#define TFT_RADIUS 3
#endif

#ifdef M5STACK_CORE2
#if ARDUINO_M5STACK_CORE_ESP32 || ARDUINO_M5STACK_CORE2
#define TFT_W 320
#define TFT_H 240
#define TFT_FONT (&FreeMono12pt7b)
Expand Down
10 changes: 5 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[furble]
build_flags = -D FURBLE_VERSION=\"${sysenv.FURBLE_VERSION}\"
build_flags = -Wall -D FURBLE_VERSION=\"${sysenv.FURBLE_VERSION}\"

[env]
platform = espressif32
Expand All @@ -13,16 +13,16 @@ lib_deps =

[env:m5stick-c]
board = m5stick-c
build_flags = ${furble.build_flags} -D M5STICKC
build_flags = ${furble.build_flags} -D ARDUINO_M5STICK_C=1

[env:m5stick-c-plus]
board = m5stick-c
build_flags = ${furble.build_flags} -D M5STICKC_PLUS
build_flags = ${furble.build_flags} -D ARDUINO_M5STICK_C_PLUS=1

[env:m5stack-core]
board = m5stack-core-esp32
build_flags = ${furble.build_flags} -D M5STACK_CORE2
build_flags = ${furble.build_flags} -D ARDUINO_M5STACK_CORE_ESP32=1

[env:m5stack-core2]
board = m5stack-core2
build_flags = ${furble.build_flags} -D M5STACK_CORE2
build_flags = ${furble.build_flags} -D ARDUINO_M5STACK_CORE2=1
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
platformio==6.1.5
platformio==6.1.13
8 changes: 4 additions & 4 deletions src/furble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ static void show_shutter_control(bool shutter_locked, unsigned long lock_start_m
unsigned long seconds = (total_ms / 1000) % 60;
prev_update_ms = now;

char duration[8] = {0x0};
snprintf(duration, 8, "%02lu:%02lu", minutes, seconds);
char duration[16] = {0x0};
snprintf(duration, 16, "%02lu:%02lu", minutes, seconds);

#ifdef M5STACK_CORE2
#if ARDUINO_M5STACK_CORE_ESP32 || ARDUINO_M5STACK_CORE2
ez.msgBox("Remote Shutter", "Shutter Locked|" + String(duration), "Unlock#Unlock#Back", false);
#else
ez.msgBox("Remote Shutter", "Shutter Locked|" + String(duration) + "||Back: Power",
"Unlock#Unlock", false);
#endif
} else {
#ifdef M5STACK_CORE2
#if ARDUINO_M5STACK_CORE_ESP32 || ARDUINO_M5STACK_CORE2
ez.msgBox("Remote Shutter", "Lock: Focus+Release", "Release#Focus#Back", false);
#else
ez.msgBox("Remote Shutter", "Lock: Focus+Release|Back: Power", "Release#Focus", false);
Expand Down
9 changes: 8 additions & 1 deletion src/furble_gps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#include "settings.h"

static const uint32_t GPS_BAUD = 9600;
#if ARDUINO_M5STACK_CORE_ESP32 || ARDUINO_M5STACK_CORE2
static const int8_t GPS_RX = 22;
static const int8_t GPS_TX = 21;
#else
static const int8_t GPS_RX = 33;
static const int8_t GPS_TX = 32;
#endif
static const uint16_t GPS_SERVICE_MS = 250;
static const uint32_t GPS_MAX_AGE_MS = 60 * 1000;

Expand Down Expand Up @@ -106,7 +113,7 @@ static uint16_t current_service(void *private_data) {

void furble_gps_init(void) {
furble_gps_enable = settings_load_gps();
Serial2.begin(GPS_BAUD, SERIAL_8N1, 33, 32);
Serial2.begin(GPS_BAUD, SERIAL_8N1, GPS_RX, GPS_TX);

uint8_t width = 4 * M5.Lcd.textWidth("5") + ez.theme->header_hmargin * 2;
ez.header.insert(CURRENT_POSITION, "current", width, current_draw_widget);
Expand Down
4 changes: 2 additions & 2 deletions src/interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ static void display_interval_msg(interval_state_t state,
case INTERVAL_DELAY:
statestr = "DELAY";
break;
case INTERVAL_EXIT:
break;
}

unsigned long rem = next - now;
Expand Down Expand Up @@ -65,8 +67,6 @@ static void do_interval(FurbleCtx *fctx, interval_t *interval) {
const unsigned long config_shutter = sv2ms(&interval->shutter);

unsigned int icount = 0;
unsigned long idelay = 0;
unsigned long ishutter = 0;

interval_state_t state = INTERVAL_SHUTTER_OPEN;

Expand Down
2 changes: 2 additions & 0 deletions src/spinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ unsigned long sv2ms(SpinValue *sv) {
return (sv->value * 1000);
case SPIN_UNIT_MS:
return (sv->value);
default:
return 0;
}
return 0;
}
Expand Down

0 comments on commit 5adb393

Please sign in to comment.