Skip to content

Commit

Permalink
Display number of skipped values
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Jul 4, 2024
1 parent 5edac5d commit 7be18e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
28 changes: 8 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use color_eyre::eyre::Result;
use crossterm::event::KeyEvent;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::prelude::Rect;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -79,25 +79,13 @@ impl App {
tui::Event::Tick => action_tx.send(Action::Tick)?,
tui::Event::Render => action_tx.send(Action::Render)?,
tui::Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?,
tui::Event::Key(_key) => {
/*
if let Some(keymap) = self.config.keybindings.get(&self.mode) {
if let Some(action) = keymap.get(&vec![key]) {
log::info!("Got action: {action:?}");
action_tx.send(action.clone())?;
} else {
// If the key was not handled as a single key action,
// then consider it for multi-key combinations.
self.last_tick_key_events.push(key);
// Check for multi-key combinations
if let Some(action) = keymap.get(&self.last_tick_key_events) {
log::info!("Got action: {action:?}");
action_tx.send(action.clone())?;
}
}
};
*/
tui::Event::Key(key) => {
if key == KeyEvent::from(KeyCode::Char('q'))
|| key == KeyEvent::from(KeyCode::Esc)
|| key == KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)
{
action_tx.send(Action::Quit)?;
}
}
_ => {}
}
Expand Down
8 changes: 8 additions & 0 deletions src/components/sensors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ impl Component for Sensors {
.unwrap_or_default();
let fps = avg_duration.as_secs_f32().recip();

let skipped = self.receiver.get_skipped_by_sensor(&id);
let skipped = if skipped > 0 {
Span::styled(format!("/{skipped}"), Style::default().red())
} else {
"".into()
};

let mut lines = vec![
Span::styled(format!("{no}"), Style::default()),
": ".into(),
Span::styled(id.tag().to_string(), Style::default().yellow()),
":".into(),
Span::styled(frame.sensor_sequence.to_string(), Style::default().dim()),
skipped,
" ".into(),
Span::styled(
format!("{:02X}", frame.value.sensor_type_id()),
Expand Down
8 changes: 4 additions & 4 deletions src/components/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn frame_data_to_line(frame: &Version1DataFrame, line: &mut Vec<Span>) {

line.extend(vec![
Span::styled("acc", Style::default().cyan()),
" = (".into(),
" = (".into(),
axis_to_span(vec.x as f32 / 16384.0, highlight_x), // TODO: Don't assume normalization
", ".into(),
axis_to_span(vec.y as f32 / 16384.0, highlight_y), // TODO: Don't assume normalization
Expand All @@ -70,7 +70,7 @@ pub fn frame_data_to_line(frame: &Version1DataFrame, line: &mut Vec<Span>) {

line.extend(vec![
Span::styled("mag", Style::default().cyan()),
" = (".into(),
" = (".into(),
axis_to_span(vec.x as f32 / 1100.0, highlight_x), // TODO: Don't assume normalization
", ".into(),
axis_to_span(vec.y as f32 / 1100.0, highlight_y), // TODO: Don't assume normalization
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn frame_data_to_line_raw(frame: &Version1DataFrame, line: &mut Vec<Span>) {

line.extend(vec![
Span::styled("acc", Style::default().cyan()),
" = (".into(),
" = (".into(),
raw_to_span(vec.x, highlight_x),
", ".into(),
raw_to_span(vec.y, highlight_y),
Expand All @@ -112,7 +112,7 @@ pub fn frame_data_to_line_raw(frame: &Version1DataFrame, line: &mut Vec<Span>) {

line.extend(vec![
Span::styled("mag", Style::default().cyan()),
" = (".into(),
" = (".into(),
raw_to_span(vec.x, highlight_x),
", ".into(),
raw_to_span(vec.y, highlight_y),
Expand Down
21 changes: 20 additions & 1 deletion src/data_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{HashMap, VecDeque};
use std::default::Default;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
use std::sync::RwLock;
use std::time::Duration;

Expand All @@ -21,6 +21,8 @@ struct InnerSensorDataBuffer {
data: RwLock<VecDeque<Version1DataFrame>>,
len: AtomicUsize,
fps: FpsCounter,
sequence: AtomicU32,
num_skipped: AtomicU32,
}

impl Default for SensorDataBuffer {
Expand All @@ -40,6 +42,8 @@ impl Default for InnerSensorDataBuffer {
data: RwLock::new(VecDeque::with_capacity(capacity)),
len: AtomicUsize::new(0),
fps: FpsCounter::default(),
sequence: AtomicU32::new(0),
num_skipped: AtomicU32::new(0),
}
}
}
Expand Down Expand Up @@ -101,6 +105,11 @@ impl SensorDataBuffer {
let map = self.by_sensor.read().expect("failed to lock");
map.get(id).map(|entry| entry.average_duration())
}

pub fn get_skipped_by_sensor(&self, id: &SensorId) -> u32 {
let map = self.by_sensor.read().expect("failed to lock");
map.get(id).map(|entry| entry.skipped()).unwrap_or(0)
}
}

impl InnerSensorDataBuffer {
Expand All @@ -116,6 +125,12 @@ impl InnerSensorDataBuffer {

pub fn enqueue(&self, frame: Version1DataFrame) {
let mut data = self.data.write().expect("lock failed");

let previous = self.sequence.swap(frame.sensor_sequence, Ordering::SeqCst);
if frame.sensor_sequence != previous + 1 {
self.num_skipped.fetch_add(1, Ordering::SeqCst);
}

data.push_front(frame);
let max_len = self.capacity;
data.truncate(max_len);
Expand All @@ -130,6 +145,10 @@ impl InnerSensorDataBuffer {
length
}

pub fn skipped(&self) -> u32 {
self.num_skipped.load(Ordering::SeqCst)
}

/// Returns the average duration between elements.
#[allow(dead_code)]
pub fn average_duration(&self) -> Duration {
Expand Down

0 comments on commit 7be18e5

Please sign in to comment.