common: Store distance travelled in Logger instance.
[gps-watch.git] / src / common / logger.rs
index 95f4a4342a1f0320f76a98b26d5909f217442663..ea84bc23d4e23ad71d73f6ec6859977e1f913a6b 100644 (file)
@@ -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,9 @@ pub struct Logger<'a> {
 
     recording_started: u32,
 
+    // The total distance logged of the currently running recording.
+    pub total_distance_cm: u32,
+
     // The number of slots filled in num_flight.
     num_in_flight: usize,
 
@@ -144,10 +148,22 @@ fn cmp_sector_header_indices(a: u16, b: u16,
     let header_a = &sector_header[a as usize];
     let header_b = &sector_header[b as usize];
 
-    // Latest entries come first.
-    if header_a.start_time > header_b.start_time {
+    if header_a.starts_recording() && header_b.starts_recording() {
+        // Latest entries come first.
+        if header_a.start_time > header_b.start_time {
+            -1
+        } else if header_a.start_time < header_b.start_time {
+            1
+        } else {
+            0
+        }
+    } else if header_a.starts_recording() {
+        -1
+    } else if header_b.starts_recording() {
+        1
+    } else if a < b {
         -1
-    } else if header_a.start_time < header_b.start_time {
+    } else if a > b {
         1
     } else {
         0
@@ -187,56 +203,11 @@ impl<'a> SectorHeaderIter<'a> {
             indices: [0; NUM_SECTORS]
         };
 
-        let mut num_used = 0;
-
-        // Put the indices of the used directory entries at the beginning
-        // of the array. Ignore the unused ones since we are not going
-        // to sort them anyway.
         for i in 0..NUM_SECTORS {
-            let sector_header = &iter.sector_header[i];
-
-            if sector_header.starts_recording() {
-                iter.indices[num_used] = i as u16;
-                num_used += 1;
-            }
+            iter.indices[i] = i as u16;
         }
 
-        let num_elts_to_sort = num_used;
-
-        if num_elts_to_sort != 0 {
-            // Sort the used directory entries.
-            iter.sort(num_elts_to_sort);
-        }
-
-        // Now put the indices of the unused directory entries in the array.
-        if num_used == 0 {
-            for i in 0..NUM_SECTORS {
-                iter.indices[i] = i as u16;
-            }
-        } else {
-            let latest_used = iter.indices[0] as usize;
-            let mut offset_unused = num_used;
-
-            // First put the entries that come after the latest one in use...
-            for i in (latest_used + 1)..NUM_SECTORS {
-                let sector_header = &iter.sector_header[i];
-
-                if !sector_header.is_in_use() {
-                    iter.indices[offset_unused] = i as u16;
-                    offset_unused += 1;
-                }
-            }
-
-            // ... then wrap around if necessary.
-            for i in 0..latest_used {
-                let sector_header = &iter.sector_header[i];
-
-                if !sector_header.is_in_use() {
-                    iter.indices[offset_unused] = i as u16;
-                    offset_unused += 1;
-                }
-            }
-        }
+        iter.sort(NUM_SECTORS);
 
         // XXX:
         // Need to handle those sectors that don't start recordings
@@ -322,6 +293,7 @@ impl<'a> Logger<'a> {
             recording_id: 0,
             first_sector: 0,
             recording_started: 0,
+            total_distance_cm: 0,
             num_in_flight: 0,
 
             in_flight: [InFlight::new(); 7],
@@ -389,16 +361,19 @@ impl<'a> Logger<'a> {
 
         self.sectors_written = 0;
         self.recording_started = tap.unix_time;
+        self.total_distance_cm = 0;
         self.num_in_flight = 0;
 
         self.prepare_write_buffer(true);
 
-        self.write_packet(0, tap.latitude, tap.longitude);
+        self.write_packet(0, tap.latitude_deg, tap.longitude_deg);
 
         self.recording_id
     }
 
     pub fn log(&mut self, prev_tap: &TimeAndPos, tap: &TimeAndPos) {
+        self.total_distance_cm += tap.distance_cm(&prev_tap) as u32;
+
         let d_time_ms = elapsed_ms(tap.system_time, prev_tap.system_time);
 
         // We know that our hardware cannot deliver updates more often
@@ -409,8 +384,8 @@ impl<'a> Logger<'a> {
         // the intervals to full seconds.
         let d_time_s = (d_time_ms + 500) / 1000;
 
-        let d_lat = tap.latitude - prev_tap.latitude;
-        let d_lon = tap.longitude - prev_tap.longitude;
+        let d_lat = tap.latitude_deg - prev_tap.latitude_deg;
+        let d_lon = tap.longitude_deg - prev_tap.longitude_deg;
 
         if self.write_packet(d_time_s, d_lat, d_lon) {
             self.flush_in_flight(false);
@@ -587,6 +562,17 @@ impl<'a> Logger<'a> {
                 continue;
             }
 
+            let mut num_data_sectors = 0;
+
+            for d in 1..NUM_SECTORS {
+                let wrapped_index = ((index + d) & (NUM_SECTORS - 1)) as usize;
+                let other_sector_header = &self.sector_header[wrapped_index];
+
+                if other_sector_header.belongs_to(sector_header.recording_id) {
+                    num_data_sectors += 1;
+                }
+            }
+
             let mut date_time_s = [b' '; 19];
 
             if let Some(tm) = Time::from_unix_time(sector_header.start_time) {
@@ -594,6 +580,13 @@ impl<'a> Logger<'a> {
                 tm.fmt_time(&mut date_time_s[11..]);
             }
 
+            let recording_size = (num_data_sectors + 1) * (SECTOR_SIZE >> 10);
+
+            let mut recording_size_s = [b'0'; 9];
+            let recording_size_s_len = fmt_u32_pad(&mut recording_size_s,
+                                                   recording_size as u32,
+                                                   8, b' ');
+
             let mut recording_id_s = [b'0'; 9];
             let recording_id_s_len =
                 fmt_u32_pad(&mut recording_id_s,
@@ -601,7 +594,8 @@ impl<'a> Logger<'a> {
                             8, b' ');
 
             tx_buf.write(&date_time_s);
-            tx_buf.write(b"       ");
+            tx_buf.write(&recording_size_s[0..recording_size_s_len]);
+            tx_buf.write(b"K");
             tx_buf.write(&recording_id_s[0..recording_id_s_len]);
             tx_buf.write(b"\n");
 
@@ -680,4 +674,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)
+        }
+    }
 }