Skip to content

Commit efaa093

Browse files
committed
feat(oled): make auto-power save optional and fix brightness control
- Add CONFIG_OLED_AUTO_POWER_SAVE Kconfig option (default off) for bread-compact-wifi to make screen auto-off optional per maintainer request - Make Backlight::SetBrightness virtual to allow OledBacklight to skip the PWM gradual transition, applying brightness changes immediately
1 parent 04d9e77 commit efaa093

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

main/Kconfig.projbuild

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ choice DISPLAY_OLED_TYPE
510510
bool "SH1106 128*64"
511511
endchoice
512512

513+
config OLED_AUTO_POWER_SAVE
514+
bool "Enable OLED Auto Power Save"
515+
depends on BOARD_TYPE_BREAD_COMPACT_WIFI
516+
help
517+
Automatically turn off the OLED display after a period of inactivity.
518+
The screen wakes up on button press or voice activity.
519+
513520
choice DISPLAY_LCD_TYPE
514521
depends on BOARD_TYPE_BREAD_COMPACT_WIFI_LCD || BOARD_TYPE_BREAD_COMPACT_ESP32_LCD || BOARD_TYPE_CGC || BOARD_TYPE_WAVESHARE_P4_NANO || BOARD_TYPE_WAVESHARE_P4_WIFI6_TOUCH_LCD_XC || BOARD_TYPE_BREAD_COMPACT_WIFI_CAM
515522
prompt "LCD Type"

main/boards/bread-compact-wifi/compact_wifi_board.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "lamp_controller.h"
1010
#include "led/single_led.h"
1111
#include "assets/lang_config.h"
12+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
1213
#include "power_save_timer.h"
14+
#endif
1315

1416
#include <esp_log.h>
1517
#include <driver/i2c_master.h>
@@ -22,6 +24,8 @@
2224

2325
#define TAG "CompactWifiBoard"
2426

27+
static constexpr int kPowerSaveTimeoutSeconds = 60;
28+
2529
class CompactWifiBoard : public WifiBoard {
2630
private:
2731
i2c_master_bus_handle_t display_i2c_bus_;
@@ -32,10 +36,11 @@ class CompactWifiBoard : public WifiBoard {
3236
Button touch_button_;
3337
Button volume_up_button_;
3438
Button volume_down_button_;
39+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
3540
PowerSaveTimer* power_save_timer_;
3641

3742
void InitializePowerSaveTimer() {
38-
power_save_timer_ = new PowerSaveTimer(-1, 60, -1);
43+
power_save_timer_ = new PowerSaveTimer(-1, kPowerSaveTimeoutSeconds, -1);
3944
power_save_timer_->OnEnterSleepMode([this]() {
4045
GetDisplay()->SetPowerSaveMode(true);
4146
});
@@ -44,6 +49,7 @@ class CompactWifiBoard : public WifiBoard {
4449
});
4550
power_save_timer_->SetEnabled(true);
4651
}
52+
#endif
4753

4854
void InitializeDisplayI2c() {
4955
i2c_master_bus_config_t bus_config = {
@@ -115,7 +121,9 @@ class CompactWifiBoard : public WifiBoard {
115121

116122
void InitializeButtons() {
117123
boot_button_.OnClick([this]() {
124+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
118125
power_save_timer_->WakeUp();
126+
#endif
119127
auto& app = Application::GetInstance();
120128
if (app.GetDeviceState() == kDeviceStateStarting) {
121129
EnterWifiConfigMode();
@@ -124,16 +132,22 @@ class CompactWifiBoard : public WifiBoard {
124132
app.ToggleChatState();
125133
});
126134
touch_button_.OnPressDown([this]() {
135+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
127136
power_save_timer_->WakeUp();
137+
#endif
128138
Application::GetInstance().StartListening();
129139
});
130140
touch_button_.OnPressUp([this]() {
141+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
131142
power_save_timer_->WakeUp();
143+
#endif
132144
Application::GetInstance().StopListening();
133145
});
134146

135147
volume_up_button_.OnClick([this]() {
148+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
136149
power_save_timer_->WakeUp();
150+
#endif
137151
auto codec = GetAudioCodec();
138152
auto volume = codec->output_volume() + 10;
139153
if (volume > 100) {
@@ -144,13 +158,17 @@ class CompactWifiBoard : public WifiBoard {
144158
});
145159

146160
volume_up_button_.OnLongPress([this]() {
161+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
147162
power_save_timer_->WakeUp();
163+
#endif
148164
GetAudioCodec()->SetOutputVolume(100);
149165
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
150166
});
151167

152168
volume_down_button_.OnClick([this]() {
169+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
153170
power_save_timer_->WakeUp();
171+
#endif
154172
auto codec = GetAudioCodec();
155173
auto volume = codec->output_volume() - 10;
156174
if (volume < 0) {
@@ -161,7 +179,9 @@ class CompactWifiBoard : public WifiBoard {
161179
});
162180

163181
volume_down_button_.OnLongPress([this]() {
182+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
164183
power_save_timer_->WakeUp();
184+
#endif
165185
GetAudioCodec()->SetOutputVolume(0);
166186
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
167187
});
@@ -180,7 +200,9 @@ class CompactWifiBoard : public WifiBoard {
180200
volume_down_button_(VOLUME_DOWN_BUTTON_GPIO) {
181201
InitializeDisplayI2c();
182202
InitializeSsd1306Display();
203+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
183204
InitializePowerSaveTimer();
205+
#endif
184206
InitializeButtons();
185207
InitializeTools();
186208
}
@@ -210,12 +232,14 @@ class CompactWifiBoard : public WifiBoard {
210232
return &backlight;
211233
}
212234

235+
#ifdef CONFIG_OLED_AUTO_POWER_SAVE
213236
virtual void SetPowerSaveLevel(PowerSaveLevel level) override {
214237
if (level != PowerSaveLevel::LOW_POWER) {
215238
power_save_timer_->WakeUp();
216239
}
217240
WifiBoard::SetPowerSaveLevel(level);
218241
}
242+
#endif
219243
};
220244

221245
DECLARE_BOARD(CompactWifiBoard);

main/boards/common/backlight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Backlight {
1313
~Backlight();
1414

1515
void RestoreBrightness();
16-
void SetBrightness(uint8_t brightness, bool permanent = false);
16+
virtual void SetBrightness(uint8_t brightness, bool permanent = false);
1717
inline uint8_t brightness() const { return brightness_; }
1818

1919
protected:

main/display/oled_display.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "oled_display.h"
2+
#include "settings.h"
23
#include "assets/lang_config.h"
34
#include "lvgl_theme.h"
45
#include "lvgl_font.h"
@@ -413,6 +414,25 @@ OledBacklight::OledBacklight(OledDisplay* display) : display_(display) {
413414
brightness_ = 50;
414415
}
415416

417+
void OledBacklight::SetBrightness(uint8_t brightness, bool permanent) {
418+
if (brightness > 100) {
419+
brightness = 100;
420+
}
421+
if (brightness_ == brightness) {
422+
return;
423+
}
424+
425+
if (permanent) {
426+
Settings settings("display", true);
427+
settings.SetInt("brightness", brightness);
428+
}
429+
430+
brightness_ = brightness;
431+
target_brightness_ = brightness;
432+
SetBrightnessImpl(brightness);
433+
ESP_LOGI(TAG, "Set OLED brightness to %d", brightness);
434+
}
435+
416436
void OledBacklight::SetBrightnessImpl(uint8_t brightness) {
417437
if (brightness <= 5) {
418438
display_->SetPowerSaveMode(true);

main/display/oled_display.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class OledDisplay : public LvglDisplay {
4343
class OledBacklight : public Backlight {
4444
public:
4545
OledBacklight(OledDisplay* display);
46-
virtual void SetBrightnessImpl(uint8_t brightness) override;
46+
void SetBrightness(uint8_t brightness, bool permanent = false) override;
47+
void SetBrightnessImpl(uint8_t brightness) override;
4748

4849
private:
4950
OledDisplay* display_;

0 commit comments

Comments
 (0)