Skip to content

Commit

Permalink
Add brightness config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
windoze committed Jan 7, 2025
1 parent 31f6141 commit aae42db
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 23 deletions.
3 changes: 3 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 11 additions & 12 deletions src/bin/app_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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")]
{
Expand All @@ -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()),
};
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,6 +39,7 @@ impl<const N: usize> AsRef<str> for Secret<N> {
}
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct AppConfig {
// These fields must be set
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -135,8 +146,14 @@ impl Default for AppConfig {
}
}

static CONFIG: OnceLock<AppConfig> = 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();
Expand Down
6 changes: 2 additions & 4 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/indicator/graphical_indicator/m5atom_s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 14 additions & 4 deletions src/indicator/smartled_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ async fn fade_in_out<const N: usize>(
pub struct IndicatorConfig {
pub rmt: RMT,
pub pin: AnyPin,
pub max_brightness: u8,
}

impl Default for IndicatorConfig {
fn default() -> Self {
Self {
rmt: unsafe { RMT::steal() },
pin: unsafe { esp_hal::gpio::GpioPin::<SMART_LED_PIN>::steal() }.into(),
max_brightness: crate::AppConfig::get().brightness,
}
}
}
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit aae42db

Please sign in to comment.