X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fcommon%2Flogger.rs;h=98a857a660ed1cbcebdbecb06cf5c8f7738f34ca;hb=refs%2Fheads%2Fmaster;hp=8a1c05cd75f89b676635b7a37c991cde1c84688b;hpb=8131a71527b5279bca9b80aac37596f6264ea2a1;p=gps-watch.git diff --git a/src/common/logger.rs b/src/common/logger.rs index 8a1c05c..98a857a 100644 --- a/src/common/logger.rs +++ b/src/common/logger.rs @@ -38,6 +38,7 @@ const NUM_SECTORS: usize = MEMORY_SIZE / SECTOR_SIZE; #[derive(Clone, Copy, PartialEq, Debug)] pub enum Error { NoSuchRecording, + StorageError, } enum SectorFlag { @@ -115,6 +116,13 @@ pub struct Logger<'a> { recording_started: u32, + // The total distance logged of the currently running recording. + pub total_distance_cm: u32, + + split_distance_cm: u32, + split_duration_ms: u32, + pub pace_s: u32, + // The number of slots filled in num_flight. num_in_flight: usize, @@ -289,6 +297,10 @@ impl<'a> Logger<'a> { recording_id: 0, first_sector: 0, recording_started: 0, + total_distance_cm: 0, + split_distance_cm: 0, + split_duration_ms: 0, + pace_s: 0, num_in_flight: 0, in_flight: [InFlight::new(); 7], @@ -356,6 +368,10 @@ impl<'a> Logger<'a> { self.sectors_written = 0; self.recording_started = tap.unix_time; + self.total_distance_cm = 0; + self.split_distance_cm = 0; + self.split_duration_ms = 0; + self.pace_s = 0; self.num_in_flight = 0; self.prepare_write_buffer(true); @@ -382,6 +398,20 @@ impl<'a> Logger<'a> { if self.write_packet(d_time_s, d_lat, d_lon) { self.flush_in_flight(false); } + + let distance_cm = tap.distance_cm(&prev_tap) as u32; + + self.total_distance_cm += distance_cm; + self.split_distance_cm += distance_cm; + + self.split_duration_ms += d_time_ms; + + if self.split_distance_cm >= 100_000 { + self.split_distance_cm -= 100_000; + + self.pace_s = self.split_duration_ms / 1000; + self.split_duration_ms = 0; + } } pub fn stop_recording(&mut self, tap: &TimeAndPos) -> u16 { @@ -666,4 +696,40 @@ impl<'a> Logger<'a> { Err(Error::NoSuchRecording) } } + + /// + /// Remove recording @p recording_id. + pub fn remove_recording(&mut self, recording_id: u16) -> Result<(), Error> { + if let Some(found_index) = (0..NUM_SECTORS).find(|&index| { + let sector_header = &self.sector_header[index as usize]; + + sector_header.recording_id == recording_id && + sector_header.starts_recording() + }) { + let mut next_sector = found_index as usize; + + for _ in 0..NUM_SECTORS { + let address = next_sector * SECTOR_SIZE; + + if let Err(_) = self.storage.erase(address) { + return Err(Error::StorageError); + } + + // Mark this sector as eligible for the next recording + // and ensure it won't be picked up by list_recordings(). + self.read_sector_header(next_sector); + + next_sector += 1; + next_sector &= NUM_SECTORS - 1; + + if !self.sector_header[next_sector].belongs_to(recording_id) { + break; + } + } + + Ok(()) + } else { + Err(Error::NoSuchRecording) + } + } }