Skip to content
Snippets Groups Projects
Commit b8281d4f authored by Stephen D's avatar Stephen D
Browse files

save brightness

parent b84c4fff
No related branches found
No related tags found
No related merge requests found
...@@ -77,8 +77,9 @@ impl SettingsView { ...@@ -77,8 +77,9 @@ impl SettingsView {
freq.set_text(&settings.freq_string()).unwrap(); freq.set_text(&settings.freq_string()).unwrap();
let brightness = Slider::new( let brightness = Slider::new(
0, 1,
100, 100,
settings.brightness(),
TEXT_X, TEXT_X,
(200 - textbox::BORDER_WIDTH).try_into().unwrap(), (200 - textbox::BORDER_WIDTH).try_into().unwrap(),
); );
...@@ -105,8 +106,7 @@ impl SettingsView { ...@@ -105,8 +106,7 @@ impl SettingsView {
settings.set_ssid(ssid); settings.set_ssid(ssid);
settings.set_icon(icon); settings.set_icon(icon);
settings.set_frequency(freq).unwrap(); // safe to unwrap because we already verified it's in range settings.set_frequency(freq).unwrap(); // safe to unwrap because we already verified it's in range
settings.set_brightness(self.brightness.value());
// settings.set_brightness(self.brightness); TODO
storage.save_settings(); storage.save_settings();
true true
......
...@@ -30,11 +30,11 @@ pub struct Slider { ...@@ -30,11 +30,11 @@ pub struct Slider {
} }
impl 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 { Self {
min, min,
max, max,
cur: max, cur,
x, x,
y, y,
tracker: ScrollTracker::new_with_threshold(10), tracker: ScrollTracker::new_with_threshold(10),
......
...@@ -261,6 +261,8 @@ mod app { ...@@ -261,6 +261,8 @@ mod app {
pins.gpio28.into_floating_disabled(), pins.gpio28.into_floating_disabled(),
); );
let display_brightness = storage.settings().brightness();
// Setup app // Setup app
let controller = Controller::new(storage, volt_meter, radio); let controller = Controller::new(storage, volt_meter, radio);
...@@ -276,7 +278,7 @@ mod app { ...@@ -276,7 +278,7 @@ mod app {
keyboard, keyboard,
display, display,
display_bl, display_bl,
display_brightness: 100, display_brightness,
last_key_press: mono.now(), last_key_press: mono.now(),
spi: Some(spi), spi: Some(spi),
peri_frequency: clocks.peripheral_clock.freq().to_Hz(), peri_frequency: clocks.peripheral_clock.freq().to_Hz(),
......
...@@ -10,9 +10,10 @@ const SETTINGS_START: usize = MESSAGE_HEADER_END; ...@@ -10,9 +10,10 @@ const SETTINGS_START: usize = MESSAGE_HEADER_END;
// 1 byte for ssid // 1 byte for ssid
// 2 bytes for icon // 2 bytes for icon
// 4 bytes for frequency // 4 bytes for frequency
// 1 byte for brightness
// 1 byte for callsign length // 1 byte for callsign length
// 2 bytes for checksum // 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)] #[derive(Clone)]
pub struct Settings { pub struct Settings {
...@@ -20,6 +21,7 @@ pub struct Settings { ...@@ -20,6 +21,7 @@ pub struct Settings {
ssid: u8, ssid: u8,
icon: u16, icon: u16,
frequency: u32, frequency: u32,
brightness: u8,
} }
impl Settings { impl Settings {
...@@ -67,6 +69,17 @@ impl Settings { ...@@ -67,6 +69,17 @@ impl Settings {
Ok(()) 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 { pub fn callsign(&self) -> &str {
&self.callsign &self.callsign
} }
...@@ -112,11 +125,16 @@ impl Settings { ...@@ -112,11 +125,16 @@ impl Settings {
return None; 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 { if callsign_len > MAX_CONTACT_CALLSIGN_LENGTH {
return None; 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()?; let callsign = String::from_utf8(Vec::from_slice(callsign_bytes).ok()?).ok()?;
Some(Self { Some(Self {
...@@ -124,6 +142,7 @@ impl Settings { ...@@ -124,6 +142,7 @@ impl Settings {
ssid, ssid,
icon, icon,
frequency, frequency,
brightness,
}) })
} }
...@@ -132,10 +151,11 @@ impl Settings { ...@@ -132,10 +151,11 @@ impl Settings {
buf[0] = self.ssid; buf[0] = self.ssid;
buf[1..3].copy_from_slice(&self.icon.to_le_bytes()); buf[1..3].copy_from_slice(&self.icon.to_le_bytes());
buf[3..7].copy_from_slice(&self.frequency.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(); let callsign_bytes = self.callsign.as_bytes();
buf[7] = callsign_bytes.len().try_into().unwrap(); buf[8] = callsign_bytes.len().try_into().unwrap();
buf[8..][..callsign_bytes.len()].copy_from_slice(callsign_bytes); buf[9..][..callsign_bytes.len()].copy_from_slice(callsign_bytes);
let checksum = X25.checksum(&buf[..(SETTINGS_SIZE - 2)]); let checksum = X25.checksum(&buf[..(SETTINGS_SIZE - 2)]);
buf[(SETTINGS_SIZE - 2)..].copy_from_slice(&checksum.to_le_bytes()); buf[(SETTINGS_SIZE - 2)..].copy_from_slice(&checksum.to_le_bytes());
...@@ -151,6 +171,7 @@ impl Default for Settings { ...@@ -151,6 +171,7 @@ impl Default for Settings {
ssid: 0, ssid: 0,
icon: 0, icon: 0,
frequency: 430_500_000, frequency: 430_500_000,
brightness: 100,
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment