X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fcommon%2Fshell.rs;h=ab814962ad0ea83368caec4546a5f2f7195f8a18;hb=aa0f0d612a03799423e87e9137765ecb90146f48;hp=09c79079d62178444b6d1f0dd82553dd4b616a92;hpb=5897e4ea6cc3ba413190f475a29bb3f745b66887;p=gps-watch.git diff --git a/src/common/shell.rs b/src/common/shell.rs index 09c7907..ab81496 100644 --- a/src/common/shell.rs +++ b/src/common/shell.rs @@ -22,6 +22,7 @@ */ use buffer::Buffer; +use storage::Storage; pub struct Shell<'a> { tx_buf: &'a mut Buffer, @@ -88,6 +89,10 @@ fn read_char() -> Option { } } +struct Context<'a> { + storage: &'a mut dyn Storage, +} + impl<'a> Shell<'a> { pub fn new(tx_buf: &mut Buffer) -> Shell { Shell { @@ -97,7 +102,11 @@ impl<'a> Shell<'a> { } } - pub fn update(&mut self) { + pub fn update(&mut self, storage: &mut dyn Storage) { + let mut context = Context { + storage: storage, + }; + while let Some(c) = read_char() { if c != b'\n' { if self.command_offset != self.command_buffer.len() { @@ -112,7 +121,7 @@ impl<'a> Shell<'a> { self.command_buffer[command_length] = b'\0'; if command_length != 0 { - self.dispatch(command_length); + self.dispatch(command_length, &mut context); } } else { self.command_buffer[self.command_offset] = b'\0'; @@ -126,7 +135,7 @@ impl<'a> Shell<'a> { } } - fn dispatch(&mut self, command_length: usize) { + fn dispatch(&mut self, command_length: usize, mut context: &mut Context) { let command : [u8; 32] = self.command_buffer; let mut args_iter = ArgumentIter { @@ -139,11 +148,14 @@ impl<'a> Shell<'a> { let usage = b"\ Supported commands: help Show this help message. + clear_storage Fully erase the flash's contents. "; self.tx_buf.write(usage); }, + Some(b"clear_storage") => self.run_clear_storage(&mut context), + Some(ref other) => { self.tx_buf.write(b"unknown_command: "); self.tx_buf.write(other); @@ -154,4 +166,8 @@ Supported commands: } } } + + fn run_clear_storage(&self, context: &mut Context) { + context.storage.clear(); + } }