From 42bc425ca9a626858da93865f8aec7b04d6cd4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Mon, 1 May 2023 18:25:12 +0200 Subject: [PATCH] reformat early_settings interactions so that it only writes if data is actually changed also abstract get/set off the early settings FLASH section, easier to maintain still todo: make hosted mode work again --- services/early_settings/src/main.rs | 57 ++++++++++++++--------------- services/status/src/main.rs | 12 +++--- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/services/early_settings/src/main.rs b/services/early_settings/src/main.rs index 3f99428c8..e724993b5 100644 --- a/services/early_settings/src/main.rs +++ b/services/early_settings/src/main.rs @@ -31,11 +31,34 @@ impl Slot { } } +// TODO(gsora): implement this so that it's a no-op on hosted mode struct State { settings_page: MemoryRange, spinor: Spinor, } +impl State { + fn set(&self, data: &[u8], slot: &Slot) { + let settings: &[u8] = self.settings_page.as_slice(); + + let new_data_u32 = u32::from_le_bytes(data.try_into().unwrap()); + + if new_data_u32 == self.get(slot) { + return; + } + + self.spinor + .patch(settings, xous::EARLY_SETTINGS, &data, slot.offset) + .expect("couldn't patch slot data"); + } + + fn get(&self, slot: &Slot) -> u32 { + let settings: &[u8] = self.settings_page.as_slice(); + + u32::from_le_bytes(settings[slot.range()].try_into().unwrap()) + } +} + fn main() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); @@ -63,46 +86,20 @@ fn main() -> ! { log::trace!("Message: {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { Some(Opcode::GetKeymap) => { - let settings: &[u8] = state.settings_page.as_slice(); - - let code = u32::from_le_bytes(settings[KEYMAP.range()].try_into().unwrap()); - - xous::return_scalar(msg.sender, code as usize).unwrap(); + xous::return_scalar(msg.sender, state.get(&KEYMAP) as usize).unwrap(); } Some(Opcode::SetKeymap) => msg_blocking_scalar_unpack!(msg, map, _, _, _, { let code = (map as u32).to_le_bytes(); - let settings: &[u8] = state.settings_page.as_slice(); - - state - .spinor - .patch(settings, xous::EARLY_SETTINGS, &code, KEYMAP.offset) - .expect("couldn't patch our keyboard code"); - - log::info!("writing early keymap: {}", map); - + state.set(&code, &KEYMAP); xous::return_scalar(msg.sender, 0).unwrap(); }), Some(Opcode::SetEarlySleep) => msg_blocking_scalar_unpack!(msg, value, _, _, _, { let code = (value as u32).to_le_bytes(); - let settings: &[u8] = state.settings_page.as_slice(); - - state - .spinor - .patch(settings, xous::EARLY_SETTINGS, &code, EARLY_SLEEP.offset) - .expect("couldn't patch early reboot flag"); - - log::info!("writing must sleep on reboot: {}", value); - + state.set(&code, &EARLY_SLEEP); xous::return_scalar(msg.sender, 0).unwrap(); }), Some(Opcode::EarlySleep) => { - let settings: &[u8] = state.settings_page.as_slice(); - - let value = u32::from_le_bytes(settings[EARLY_SLEEP.range()].try_into().unwrap()); - - log::info!("value read for early sleep: {}", value); - - xous::return_scalar(msg.sender, value as usize).unwrap(); + xous::return_scalar(msg.sender, state.get(&EARLY_SLEEP) as usize).unwrap(); } _ => log::warn!("unrecognized opcode"), } diff --git a/services/status/src/main.rs b/services/status/src/main.rs index a23571f21..8c1b9f494 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -629,11 +629,11 @@ fn wrapped_main() -> ! { // get the last status let must_sleep = early_settings.early_sleep().unwrap(); - - // reset it for good measure - early_settings.set_early_sleep(false).unwrap(); if must_sleep { + // reset it for good measure + early_settings.set_early_sleep(false).unwrap(); + while ((llio.adc_vbus().unwrap() as u32) * 503) > 150_000 { modals.show_notification(t!("mainmenu.cant_sleep", xous::LANG), None).expect("couldn't notify that power is plugged in"); } @@ -647,7 +647,7 @@ fn wrapped_main() -> ! { Err(_e) => { panic!("Unhandled error on suspend request"); } - } + } } // spawn a thread to auto-mount the PDDB @@ -1130,7 +1130,7 @@ fn wrapped_main() -> ! { early_settings.set_early_sleep(true).unwrap(); log::info!("forcing sleep on reboot"); - + pddb.pddb_halt(); susres.reboot(true).expect("couldn't issue reboot command"); } @@ -1605,4 +1605,4 @@ fn bars(g: &gam::Gam, g.draw_list(dl).unwrap(); -} \ No newline at end of file +}