From b8281d4fe7d8a425f15334e964efdfe9ab27eac9 Mon Sep 17 00:00:00 2001 From: Stephen D <webmaster@scd31.com> Date: Thu, 18 Jul 2024 21:34:41 -0300 Subject: [PATCH] save brightness --- companion-software/src/gui/settings.rs | 6 ++--- companion-software/src/gui/slider.rs | 4 +-- companion-software/src/main.rs | 4 ++- companion-software/src/storage/settings.rs | 31 ++++++++++++++++++---- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/companion-software/src/gui/settings.rs b/companion-software/src/gui/settings.rs index 94348fa..bce8b99 100644 --- a/companion-software/src/gui/settings.rs +++ b/companion-software/src/gui/settings.rs @@ -77,8 +77,9 @@ impl SettingsView { freq.set_text(&settings.freq_string()).unwrap(); let brightness = Slider::new( - 0, + 1, 100, + settings.brightness(), TEXT_X, (200 - textbox::BORDER_WIDTH).try_into().unwrap(), ); @@ -105,8 +106,7 @@ impl SettingsView { settings.set_ssid(ssid); settings.set_icon(icon); settings.set_frequency(freq).unwrap(); // safe to unwrap because we already verified it's in range - - // settings.set_brightness(self.brightness); TODO + settings.set_brightness(self.brightness.value()); storage.save_settings(); true diff --git a/companion-software/src/gui/slider.rs b/companion-software/src/gui/slider.rs index 482a53b..9e3044d 100644 --- a/companion-software/src/gui/slider.rs +++ b/companion-software/src/gui/slider.rs @@ -30,11 +30,11 @@ pub struct Slider { } impl Slider { - pub fn new(min: u8, max: u8, x: i32, y: i32) -> Self { + pub fn new(min: u8, max: u8, cur: u8, x: i32, y: i32) -> Self { Self { min, max, - cur: max, + cur, x, y, tracker: ScrollTracker::new_with_threshold(10), diff --git a/companion-software/src/main.rs b/companion-software/src/main.rs index 3b45ab6..710a079 100644 --- a/companion-software/src/main.rs +++ b/companion-software/src/main.rs @@ -261,6 +261,8 @@ mod app { pins.gpio28.into_floating_disabled(), ); + let display_brightness = storage.settings().brightness(); + // Setup app let controller = Controller::new(storage, volt_meter, radio); @@ -276,7 +278,7 @@ mod app { keyboard, display, display_bl, - display_brightness: 100, + display_brightness, last_key_press: mono.now(), spi: Some(spi), peri_frequency: clocks.peripheral_clock.freq().to_Hz(), diff --git a/companion-software/src/storage/settings.rs b/companion-software/src/storage/settings.rs index 3ffc467..2952866 100644 --- a/companion-software/src/storage/settings.rs +++ b/companion-software/src/storage/settings.rs @@ -10,9 +10,10 @@ const SETTINGS_START: usize = MESSAGE_HEADER_END; // 1 byte for ssid // 2 bytes for icon // 4 bytes for frequency +// 1 byte for brightness // 1 byte for callsign length // 2 bytes for checksum -const SETTINGS_SIZE: usize = 1 + 2 + 4 + 1 + MAX_CONTACT_CALLSIGN_LENGTH + 2; +const SETTINGS_SIZE: usize = 1 + 2 + 4 + 1 + 1 + MAX_CONTACT_CALLSIGN_LENGTH + 2; #[derive(Clone)] pub struct Settings { @@ -20,6 +21,7 @@ pub struct Settings { ssid: u8, icon: u16, frequency: u32, + brightness: u8, } impl Settings { @@ -67,6 +69,17 @@ impl Settings { Ok(()) } + pub fn brightness(&self) -> u8 { + self.brightness + } + + pub fn set_brightness(&mut self, brightness: u8) { + assert!(brightness <= 100); + assert!(brightness > 0); + + self.brightness = brightness; + } + pub fn callsign(&self) -> &str { &self.callsign } @@ -112,11 +125,16 @@ impl Settings { return None; } - let callsign_len: usize = bytes[7].into(); + let brightness = bytes[7]; + if !(1..=100).contains(&brightness) { + return None; + } + + let callsign_len: usize = bytes[8].into(); if callsign_len > MAX_CONTACT_CALLSIGN_LENGTH { return None; } - let callsign_bytes = &bytes[8..][..callsign_len]; + let callsign_bytes = &bytes[9..][..callsign_len]; let callsign = String::from_utf8(Vec::from_slice(callsign_bytes).ok()?).ok()?; Some(Self { @@ -124,6 +142,7 @@ impl Settings { ssid, icon, frequency, + brightness, }) } @@ -132,10 +151,11 @@ impl Settings { buf[0] = self.ssid; buf[1..3].copy_from_slice(&self.icon.to_le_bytes()); buf[3..7].copy_from_slice(&self.frequency.to_le_bytes()); + buf[7] = self.brightness; let callsign_bytes = self.callsign.as_bytes(); - buf[7] = callsign_bytes.len().try_into().unwrap(); - buf[8..][..callsign_bytes.len()].copy_from_slice(callsign_bytes); + buf[8] = callsign_bytes.len().try_into().unwrap(); + buf[9..][..callsign_bytes.len()].copy_from_slice(callsign_bytes); let checksum = X25.checksum(&buf[..(SETTINGS_SIZE - 2)]); buf[(SETTINGS_SIZE - 2)..].copy_from_slice(&checksum.to_le_bytes()); @@ -151,6 +171,7 @@ impl Default for Settings { ssid: 0, icon: 0, frequency: 430_500_000, + brightness: 100, } } } -- GitLab