From ed56d89bbb0692755ed06f18d227d4d1e39e6b05 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Thu, 9 Jan 2020 12:05:07 +0100 Subject: [PATCH] application: Set up a Logger instance. This also passes a mutable Logger reference to Shell::update() since we no longer can pass another mutable Storage reference to it: doing so would break the requirement of only having one mutable reference to any instance at any time, and Logger requires a mutable storage reference, too. --- src/application/main.rs | 6 +++++- src/common/shell.rs | 30 +++++++++++------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/application/main.rs b/src/application/main.rs index 444ffd4..e11aaf7 100644 --- a/src/application/main.rs +++ b/src/application/main.rs @@ -46,6 +46,7 @@ use common::screen; use common::time::Time; use common::mx25l::Mx25l; use common::shell::Shell; +use common::logger::Logger; extern { fn enable_interrupts(); @@ -207,6 +208,9 @@ pub unsafe extern "C" fn _start() -> ! { let mut mx25l = Mx25l::new(gpio::GPIOD, 1 << 0); + let mut logger = Logger::new(&mut mx25l); + logger.init(); + let mut gps = gps::Gps::new(); let mut gps_has_fix = false; @@ -282,7 +286,7 @@ pub unsafe extern "C" fn _start() -> ! { } }); - shell.update(&mut mx25l); + shell.update(&mut logger); if reset_requested() { nvic::system_reset(); diff --git a/src/common/shell.rs b/src/common/shell.rs index 730a0a8..bb59b62 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -22,7 +22,7 @@ */ use buffer::Buffer; -use storage::Storage; +use logger::Logger; use yencode::Yencode; use systick; @@ -108,10 +108,6 @@ fn read_char_delay_ms(limit_ms: u32) -> Option { None } -struct Context<'a> { - storage: &'a mut dyn Storage, -} - impl<'a> Shell<'a> { pub fn new(tx_buf: &mut Buffer) -> Shell { Shell { @@ -121,11 +117,7 @@ impl<'a> Shell<'a> { } } - pub fn update(&mut self, storage: &mut dyn Storage) { - let mut context = Context { - storage: storage, - }; - + pub fn update(&mut self, logger: &mut Logger) { while let Some(c) = read_char() { if c != b'\n' { if self.command_offset != self.command_buffer.len() { @@ -140,7 +132,7 @@ impl<'a> Shell<'a> { self.command_buffer[command_length] = b'\0'; if command_length != 0 { - self.dispatch(command_length, &mut context); + self.dispatch(command_length, logger); } } else { self.command_buffer[self.command_offset] = b'\0'; @@ -154,7 +146,7 @@ impl<'a> Shell<'a> { } } - fn dispatch(&mut self, command_length: usize, mut context: &mut Context) { + fn dispatch(&mut self, command_length: usize, logger: &mut Logger) { let command : [u8; 32] = self.command_buffer; let mut args_iter = ArgumentIter { @@ -174,8 +166,8 @@ Supported commands: self.tx_buf.write(usage); }, - Some(b"clear_storage") => self.run_clear_storage(&mut context), - Some(b"dump_storage") => self.run_dump_storage(&mut context), + Some(b"clear_storage") => self.run_clear_storage(logger), + Some(b"dump_storage") => self.run_dump_storage(logger), Some(ref other) => { self.tx_buf.write(b"unknown_command: "); @@ -188,11 +180,11 @@ Supported commands: } } - fn run_clear_storage(&self, context: &mut Context) { - context.storage.clear(); + fn run_clear_storage(&self, logger: &mut Logger) { + logger.storage.clear(); } - fn run_dump_storage(&mut self, context: &mut Context) { + fn run_dump_storage(&mut self, logger: &mut Logger) { self.tx_buf.write(b"waiting for receiver to start...\n"); self.tx_buf.flush(); @@ -206,12 +198,12 @@ Supported commands: yenc.start(b"gps-watch-storage.bin"); const CHUNK_SIZE: usize = 1024; - let num_chunks = context.storage.size() / CHUNK_SIZE; + let num_chunks = logger.storage.size() / CHUNK_SIZE; for i in 0..num_chunks { let mut buf = [0u8; CHUNK_SIZE]; - context.storage.read(i * CHUNK_SIZE, &mut buf); + logger.storage.read(i * CHUNK_SIZE, &mut buf); yenc.data(&buf); } -- 2.30.2