From aae42db05a203e4655102239d447e5c2d8200258 Mon Sep 17 00:00:00 2001 From: Chen Xu Date: Wed, 8 Jan 2025 01:20:29 +0800 Subject: [PATCH] Add brightness config setting --- config.json.example | 3 +++ src/bin/app_main.rs | 23 +++++++++---------- src/config.rs | 19 ++++++++++++++- src/constants.rs | 6 ++--- .../graphical_indicator/m5atom_s3.rs | 6 +++-- src/indicator/smartled_indicator.rs | 18 +++++++++++---- 6 files changed, 52 insertions(+), 23 deletions(-) diff --git a/config.json.example b/config.json.example index 2bbdcd0..e69b999 100644 --- a/config.json.example +++ b/config.json.example @@ -16,6 +16,9 @@ "screen_height": 1080, // Set to `true` to reverse the direction of the mouse wheels, optional, default value is false "flip_wheel": false, + // Brightness, optional, 1-100, default value is 30, applied to both SmartLED and Graphical indicators. + // CAUTION: Higher value can consume more power and may cause overheat or being blocked by the host USB port, but too low value may cause the indicators not visible, especially to the graphics indicator on TFT LCD. Usually 10~50 is good for SmartLED, and 30~60 is good for TFT LCD. + "brightness": 30, // Static IP address, optional, omit to use DHCP, default value is null or omitted "ip_addr": "192.168.100.201/24", diff --git a/src/bin/app_main.rs b/src/bin/app_main.rs index ab98845..89ec73a 100644 --- a/src/bin/app_main.rs +++ b/src/bin/app_main.rs @@ -42,8 +42,7 @@ async fn main(spawner: Spawner) { ); // Load the configuration - let app_config = mk_static!(AppConfig, AppConfig::load()); - println!("Config: {:?}", app_config); + println!("Config: {:?}", AppConfig::get()); let peripherals = esp_hal::init({ let mut config = esp_hal::Config::default(); @@ -69,14 +68,14 @@ async fn main(spawner: Spawner) { let mut wdt1 = timg1.wdt; wdt1.set_timeout( MwdtStage::Stage0, - fugit::MicrosDurationU64::secs(app_config.watchdog_timeout as u64), + fugit::MicrosDurationU64::secs(AppConfig::get().watchdog_timeout as u64), ); wdt1.set_stage_action(MwdtStage::Stage0, MwdtStageAction::ResetSystem); wdt1.enable(); wdt1.feed(); // Setup HID task - let hid_sender = start_hid_task(spawner, app_config); + let hid_sender = start_hid_task(spawner, AppConfig::get()); #[cfg(feature = "clipboard")] { @@ -97,11 +96,11 @@ async fn main(spawner: Spawner) { esp_wifi::init(timg0.timer0, rng, peripherals.RADIO_CLK,).unwrap() ); - let config = match app_config.get_ip_addr() { + let config = match AppConfig::get().get_ip_addr() { Some(addr) => embassy_net::Config::ipv4_static(embassy_net::StaticConfigV4 { address: addr, - dns_servers: Vec::new(), // We don't really need DNS - gateway: app_config.get_gateway(), // Gateway is optional if server is on the same subnet + dns_servers: Vec::new(), // We don't really need DNS + gateway: AppConfig::get().get_gateway(), // Gateway is optional if server is on the same subnet }), None => embassy_net::Config::dhcpv4(Default::default()), }; @@ -129,8 +128,8 @@ async fn main(spawner: Spawner) { // Start WiFi connection task spawner.must_spawn(connection( controller, - app_config.ssid.to_owned().into(), - app_config.password.to_owned().into(), + AppConfig::get().ssid.to_owned().into(), + AppConfig::get().password.to_owned().into(), )); // Start network stack task spawner.must_spawn(net_task(stack)); @@ -156,10 +155,10 @@ async fn main(spawner: Spawner) { loop { // Start the Barrier client - let mut actuator = UsbActuator::new(app_config, indicator_sender, hid_sender); + let mut actuator = UsbActuator::new(AppConfig::get(), indicator_sender, hid_sender); start_barrier_client( - app_config.get_server_endpoint(), - &app_config.screen_name, + AppConfig::get().get_server_endpoint(), + &AppConfig::get().screen_name, stack, &mut actuator, &mut wdt1, diff --git a/src/config.rs b/src/config.rs index 060f819..73e673d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use core::{cmp::min, fmt, str::FromStr}; use embassy_net::{IpEndpoint, Ipv4Address, Ipv4Cidr}; +use embassy_sync::once_lock::OnceLock; use embedded_storage::ReadStorage; use esp_storage::FlashStorage; use heapless::String; @@ -38,6 +39,7 @@ impl AsRef for Secret { } } +#[allow(dead_code)] #[derive(Debug, Deserialize)] pub struct AppConfig { // These fields must be set @@ -54,6 +56,10 @@ pub struct AppConfig { #[serde(default)] pub flip_wheel: bool, + // Indicator brightness, used by both SmartLED and graphical indicators + #[serde(default = "get_default_brightness")] + pub brightness: u8, + // Network configuration // Static IP configuration, CIDR notation, optional @@ -89,6 +95,10 @@ fn get_default_screen_height() -> u16 { SCREEN_HEIGHT } +fn get_default_brightness() -> u8 { + BRIGHTNESS +} + fn get_default_vid() -> u16 { USB_VID } @@ -123,6 +133,7 @@ impl Default for AppConfig { screen_width: SCREEN_WIDTH, screen_height: SCREEN_HEIGHT, flip_wheel: REVERSED_WHEEL, + brightness: BRIGHTNESS, ip_addr: None, gateway: None, vid: USB_VID, @@ -135,8 +146,14 @@ impl Default for AppConfig { } } +static CONFIG: OnceLock = OnceLock::new(); + impl AppConfig { - pub fn load() -> Self { + pub fn get() -> &'static Self { + CONFIG.get_or_init(Self::load) + } + + fn load() -> Self { // TODO: Use proper way to read the config let mut bytes = [0u8; 4096]; let mut flash = FlashStorage::new(); diff --git a/src/constants.rs b/src/constants.rs index 6b76856..99f0eb2 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -14,10 +14,6 @@ pub const SMART_LED_PIN: u8 = 35; #[from_env] pub const SMART_LED_COUNT: usize = 1; -#[cfg(feature = "graphics")] -#[from_env] -pub const BRIGHTNESS: u8 = 50; - // Pin 41 is for M5Atom S3 and M5Atom S3 Lite #[cfg(feature = "clipboard")] #[from_env] @@ -43,6 +39,8 @@ pub const SCREEN_HEIGHT: u16 = 1080; #[from_env] pub const REVERSED_WHEEL: bool = false; #[from_env] +pub const BRIGHTNESS: u8 = 30; +#[from_env] pub const USB_VID: u16 = 0x0d0a; #[from_env] pub const USB_PID: u16 = 0xc0de; diff --git a/src/indicator/graphical_indicator/m5atom_s3.rs b/src/indicator/graphical_indicator/m5atom_s3.rs index 8b3a788..393a679 100644 --- a/src/indicator/graphical_indicator/m5atom_s3.rs +++ b/src/indicator/graphical_indicator/m5atom_s3.rs @@ -13,6 +13,7 @@ pub struct IndicatorConfig { pub backlight: AnyPin, pub color_inversion: ColorInversion, pub color_order: ColorOrder, + pub max_brightness: u8, } impl Default for IndicatorConfig { @@ -22,7 +23,7 @@ impl Default for IndicatorConfig { Self { width: 128, height: 128, - offset_x: 1, // M5Atom S3 has 1px offset on both x and y axis + offset_x: 2, // M5Atom S3 has 1px offset on both x and y axis offset_y: 1, spi: unsafe { SPI3::steal() }.into(), mosi: unsafe { GpioPin::<21>::steal() }.into(), @@ -33,6 +34,7 @@ impl Default for IndicatorConfig { backlight: unsafe { GpioPin::<16>::steal() }.into(), color_inversion: ColorInversion::Inverted, color_order: ColorOrder::Bgr, + max_brightness: crate::AppConfig::get().brightness, } } } @@ -62,7 +64,7 @@ pub fn init_display<'a>(config: IndicatorConfig) -> Display<'a> { channel0 .configure(channel::config::Config { timer: &lstimer0, - duty_pct: crate::constants::BRIGHTNESS, + duty_pct: config.max_brightness, pin_config: channel::config::PinConfig::PushPull, }) .unwrap(); diff --git a/src/indicator/smartled_indicator.rs b/src/indicator/smartled_indicator.rs index 44362d3..4409a2e 100644 --- a/src/indicator/smartled_indicator.rs +++ b/src/indicator/smartled_indicator.rs @@ -112,6 +112,7 @@ async fn fade_in_out( pub struct IndicatorConfig { pub rmt: RMT, pub pin: AnyPin, + pub max_brightness: u8, } impl Default for IndicatorConfig { @@ -119,6 +120,7 @@ impl Default for IndicatorConfig { Self { rmt: unsafe { RMT::steal() }, pin: unsafe { esp_hal::gpio::GpioPin::::steal() }.into(), + max_brightness: crate::AppConfig::get().brightness, } } } @@ -133,16 +135,24 @@ pub async fn start_indicator(config: IndicatorConfig, receiver: IndicatorReceive loop { match status { IndicatorStatus::WifiConnecting => { - status = fade_in_out(&mut led, RED, receiver, 0, 5, 1).await; + status = fade_in_out(&mut led, RED, receiver, 0, config.max_brightness, 1).await; } IndicatorStatus::WifiConnected => { - status = fade_in_out(&mut led, BLUE, receiver, 0, 5, 1).await; + status = fade_in_out(&mut led, BLUE, receiver, 0, config.max_brightness, 1).await; } IndicatorStatus::ServerConnected => { - status = fade_in_out(&mut led, YELLOW, receiver, 0, 5, 1).await; + status = fade_in_out(&mut led, YELLOW, receiver, 0, config.max_brightness, 1).await; } IndicatorStatus::Active => { - status = fade_in_out(&mut led, GREEN, receiver, 5, 5, 1).await; + status = fade_in_out( + &mut led, + GREEN, + receiver, + config.max_brightness, + config.max_brightness, + 1, + ) + .await; } } }