7 Commits

Author SHA1 Message Date
lenn
d93a6694cf fix: correct 3D fingertip sampling and PCB mapping 2026-07-27 17:25:30 +08:00
lenn
0d1296c482 更新ui中 2026-07-21 17:36:46 +08:00
lenn
0a9fea0f0a Improve panel layout and per-user installer 2026-07-02 13:38:00 +08:00
lenn
da77f2f194 Improve hand mode panels and recording 2026-07-01 17:08:07 +08:00
lenn
1ed729f8da Prepare Windows installer release 2026-06-30 17:43:02 +08:00
lenn
444c20f233 Add animated force panels 2026-06-30 11:10:29 +08:00
lenn
d4f160af75 Integrate hand gateway and spatial force rendering 2026-06-29 18:55:42 +08:00
30 changed files with 3742 additions and 765 deletions

View File

@@ -1,35 +0,0 @@
{
"hooks": {
"pre-exec": [
{
"matcher": "",
"command": "scale gate pre-tool Bash --args-json \"$ARGS\" --session-id \"$SESSION_ID\""
},
{
"matcher": "edit|write",
"command": "scale gate pre-tool Edit --args-json \"$ARGS\" --session-id \"$SESSION_ID\""
}
],
"post-exec": [
{
"matcher": "edit|write",
"command": "scale gate post-tool Edit --args-json \"$ARGS\" --exit-code \"$EXIT_CODE\" --session-id \"$SESSION_ID\""
},
{
"matcher": "",
"command": "scale gate post-tool Bash --args-json \"$ARGS\" --exit-code \"$EXIT_CODE\" --session-id \"$SESSION_ID\""
}
],
"before-stop": [
{
"matcher": "",
"command": "scale gate before-stop --session-id \"$SESSION_ID\""
}
]
},
"permissions": {
"allow": [
"scale:*"
]
}
}

2
Cargo.lock generated
View File

@@ -1304,7 +1304,7 @@ checksum = "dea2df4cf52843e0452895c455a1a2cfbb842a1e7329671acf418fdc53ed4c59"
[[package]] [[package]]
name = "eskin-model-player" name = "eskin-model-player"
version = "0.5.0" version = "5.0.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytemuck", "bytemuck",

View File

@@ -1,9 +1,14 @@
[package] [package]
name = "eskin-model-player" name = "eskin-model-player"
version = "0.5.0" version = "5.0.0"
edition = "2024" edition = "2024"
authors = ["JOYSONQUIN"]
description = "Desktop pressure sensor visualization and playback application."
build = "build.rs" build = "build.rs"
[package.metadata.wix]
eula = false
[dependencies] [dependencies]
eframe = { version = "0.34.2", features = ["default", "wgpu", "__screenshot"] } eframe = { version = "0.34.2", features = ["default", "wgpu", "__screenshot"] }
env_logger = { version = "0.11.10", features = ["auto-color", "humantime"] } env_logger = { version = "0.11.10", features = ["auto-color", "humantime"] }
@@ -22,3 +27,8 @@ gltf = "1.4.1"
[build-dependencies] [build-dependencies]
anyhow = "1.0.102" anyhow = "1.0.102"
fs_extra = "1.3.0" fs_extra = "1.3.0"
[[bin]]
name = "ESkinPlayer"
path = "src/main.rs"

1060
docs/cargo-wix-guide.md Normal file

File diff suppressed because it is too large Load Diff

BIN
res/hand.glb Normal file

Binary file not shown.

View File

@@ -1,34 +1,35 @@
use crate::breakout::{BreakoutGame, control_from_matrix}; use crate::breakout::{BreakoutGame, control_from_matrix};
use crate::connection::ConnectionManager; use crate::connection::ConnectionManager;
use crate::force::{ForceEstimatorState, HudSpatialForce};
use crate::recording::Recorder; use crate::recording::Recorder;
use crate::render::{ActiveMode, FingerMode, HandGatewayMode}; use crate::render::{ActiveMode, FingerMode, HandGatewayMode};
use crate::style::{ONE_DARK_PRO, apply_fonts, apply_theme, layout}; use crate::style::{self, ONE_DARK_PRO, apply_fonts, apply_theme, dim_text, layout};
use crate::ui::SerialMode; use crate::ui::SerialMode;
use crate::{ use crate::{
matrix::{MATRIX_COLS, MATRIX_ROWS}, matrix::{MATRIX_COLS, MATRIX_ROWS, UNFOLDED_SENSOR_SEGMENT_COUNTS},
render::{ render::{
BackgroundRenderResources, PRESSURE_CELL_COUNT, PressureFrame, WgpuBackgroundCallback, BackgroundRenderResources, PRESSURE_CELL_COUNT, PressureFrame, PressureSamples,
WgpuBackgroundCallback,
}, },
ui::{ ui::{
ConfigPanelState, ConnectPanelState, FloatingPanelState, MatrixConfigState, ConfigPanelState, ConnectPanelState, FloatingPanelState, MatrixConfigState,
draw_config_panel, draw_export_panel, draw_matrix_config_panel, draw_stats_panel, draw_config_panel, draw_export_panel, draw_hand_force_panels, draw_stats_panel,
panel_restore_item, panel_restore_item,
}, },
}; };
use eframe::{egui, egui_wgpu}; use eframe::{egui, egui_wgpu};
use std::sync::Arc; use std::sync::Arc;
const DATA_LOG_EVERY_FRAMES: u64 = 30;
const SUMMARY_POINTS_PER_SERIES: usize = 42; const SUMMARY_POINTS_PER_SERIES: usize = 42;
const MIN_DISPLAY_FORCE_N: f32 = 0.1; const HAND_FORCE_PANEL_COUNT: usize = UNFOLDED_SENSOR_SEGMENT_COUNTS.len();
const MAX_DISPLAY_FORCE_N: f32 = 25.6; const HAND_FORCE_SEGMENT_COUNTS: [usize; HAND_FORCE_PANEL_COUNT] = UNFOLDED_SENSOR_SEGMENT_COUNTS;
pub struct EskinDesktopApp { pub struct EskinDesktopApp {
connect_panel: FloatingPanelState, connect_panel: FloatingPanelState,
connect_state: ConnectPanelState, connect_state: ConnectPanelState,
connection: Arc<ConnectionManager>, connection: Arc<ConnectionManager>,
pressure_matrix: PressureFrame, pressure_matrix: PressureFrame,
data_log_frame: u64, hand_pressure: PressureSamples,
scene_panel: FloatingPanelState, scene_panel: FloatingPanelState,
config_panel: FloatingPanelState, config_panel: FloatingPanelState,
config_state: ConfigPanelState, config_state: ConfigPanelState,
@@ -40,6 +41,9 @@ pub struct EskinDesktopApp {
matrix_config_panel: FloatingPanelState, matrix_config_panel: FloatingPanelState,
matrix_config: MatrixConfigState, matrix_config: MatrixConfigState,
signal_history: Vec<f32>, signal_history: Vec<f32>,
hand_signal_histories: [Vec<f32>; HAND_FORCE_PANEL_COUNT],
force_estimator: ForceEstimatorState,
latest_spatial_force: Option<HudSpatialForce>,
latest_raw_matrix: Vec<u32>, latest_raw_matrix: Vec<u32>,
latest_matrix_rows: u32, latest_matrix_rows: u32,
latest_matrix_cols: u32, latest_matrix_cols: u32,
@@ -77,7 +81,7 @@ impl EskinDesktopApp {
connect_state: ConnectPanelState::default(), connect_state: ConnectPanelState::default(),
connection: Arc::new(ConnectionManager::new()), connection: Arc::new(ConnectionManager::new()),
pressure_matrix: [[0.0, 0.0]; PRESSURE_CELL_COUNT], pressure_matrix: [[0.0, 0.0]; PRESSURE_CELL_COUNT],
data_log_frame: 0, hand_pressure: Vec::new(),
scene_panel: FloatingPanelState::new( scene_panel: FloatingPanelState::new(
[layout::LEFT_X, layout::TOP_Y], [layout::LEFT_X, layout::TOP_Y],
[layout::LEFT_TAG_X, layout::TOP_Y], [layout::LEFT_TAG_X, layout::TOP_Y],
@@ -103,6 +107,9 @@ impl EskinDesktopApp {
), ),
matrix_config: MatrixConfigState::default(), matrix_config: MatrixConfigState::default(),
signal_history: Vec::with_capacity(128), signal_history: Vec::with_capacity(128),
hand_signal_histories: std::array::from_fn(|_| Vec::with_capacity(128)),
force_estimator: ForceEstimatorState::new(),
latest_spatial_force: None,
latest_raw_matrix: Vec::new(), latest_raw_matrix: Vec::new(),
latest_matrix_rows: MATRIX_ROWS, latest_matrix_rows: MATRIX_ROWS,
latest_matrix_cols: MATRIX_COLS, latest_matrix_cols: MATRIX_COLS,
@@ -129,9 +136,40 @@ impl EskinDesktopApp {
width, width,
height, height,
pressure: self.pressure_matrix, pressure: self.pressure_matrix,
hand_pressure: self.hand_pressure.clone(),
active_mode: self.active_mode.clone(), active_mode: self.active_mode.clone(),
}, },
)); ));
self.paint_spatial_force_overlay(ui, rect);
}
fn paint_spatial_force_overlay(&self, ui: &egui::Ui, rect: egui::Rect) {
let Some(force) = self.latest_spatial_force else {
return;
};
let painter = ui.painter();
let center = rect.center();
let magnitude = force.magnitude.clamp(0.0, 1.8);
let length = 34.0 + magnitude * 54.0;
let angle = force.angle_deg.to_radians();
let direction = egui::vec2(angle.cos(), angle.sin());
let end = center + direction * length;
let side = egui::vec2(-direction.y, direction.x);
let color = egui::Color32::from_rgb(255, 196, 54);
let glow = egui::Color32::from_rgba_unmultiplied(255, 196, 54, 58);
painter.line_segment([center, end], egui::Stroke::new(9.0_f32, glow));
painter.line_segment([center, end], egui::Stroke::new(2.4_f32, color));
painter.line_segment(
[end, end - direction * 14.0 + side * 7.0],
egui::Stroke::new(2.4_f32, color),
);
painter.line_segment(
[end, end - direction * 14.0 - side * 7.0],
egui::Stroke::new(2.4_f32, color),
);
painter.circle_filled(center, 4.2, color);
} }
fn draw_workspace(&mut self, ui: &mut egui::Ui) { fn draw_workspace(&mut self, ui: &mut egui::Ui) {
@@ -180,41 +218,38 @@ impl EskinDesktopApp {
fn update_pressure_matrix(&mut self) { fn update_pressure_matrix(&mut self) {
if let Some(sample) = self.connection.take_latest_sample() { if let Some(sample) = self.connection.take_latest_sample() {
if self.config_state.mode == SerialMode::Finger3D {
eprintln!(
"[3d-rawdata] rows={} cols={} values={:?}",
sample.rows, sample.cols, sample.matrix
);
}
normalize_pressure_sample( normalize_pressure_sample(
&sample.matrix, &sample.matrix,
sample.rows, sample.rows,
sample.cols, sample.cols,
&mut self.pressure_matrix, &mut self.pressure_matrix,
); );
self.hand_pressure = normalize_pressure_samples(&sample.matrix);
self.latest_raw_matrix.clear(); self.latest_raw_matrix.clear();
self.latest_raw_matrix.extend_from_slice(&sample.matrix); self.latest_raw_matrix.extend_from_slice(&sample.matrix);
self.latest_matrix_rows = sample.rows; self.latest_matrix_rows = sample.rows;
self.latest_matrix_cols = sample.cols; self.latest_matrix_cols = sample.cols;
// Feed data to recorder self.latest_spatial_force = self.force_estimator.analyze(&sample.matrix);
self.recorder.add_frame(&sample.matrix);
// JE-Skin summary logic: sum all cells first, then convert raw summary to force. // Keep JE-Skin's summary path separate from the optional spatial-force vector.
let raw_total = sample let raw_total = sample
.matrix .matrix
.iter() .iter()
.fold(0_u64, |sum, value| sum + *value as u64) .fold(0_u64, |sum, value| sum + *value as u64)
.min(u32::MAX as u64) as u32; .min(u32::MAX as u64) as u32;
let force = raw_to_g1(raw_total).min(MAX_DISPLAY_FORCE_N); let force = raw_to_g1(raw_total).min(25.6);
let force = if force <= MIN_DISPLAY_FORCE_N { let force = if force <= 0.1 { 0.0 } else { force };
0.0
} else {
force
};
self.signal_history.push(force); self.signal_history.push(force);
if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES { if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES {
self.signal_history.remove(0); self.signal_history.remove(0);
} }
self.data_log_frame += 1;
if self.data_log_frame % DATA_LOG_EVERY_FRAMES == 0 {
log_pressure_sample(&sample.matrix, sample.rows, sample.cols);
}
} }
} }
@@ -232,7 +267,7 @@ impl EskinDesktopApp {
); );
ui.painter().line_segment( ui.painter().line_segment(
[title_bar_rect.left_bottom(), title_bar_rect.right_bottom()], [title_bar_rect.left_bottom(), title_bar_rect.right_bottom()],
egui::Stroke::new(1.0, ONE_DARK_PRO.border), egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border),
); );
// Drag-to-move: double-click to maximize, drag to move // Drag-to-move: double-click to maximize, drag to move
@@ -273,14 +308,14 @@ impl EskinDesktopApp {
btn_close_center + egui::vec2(-3.0, -3.0), btn_close_center + egui::vec2(-3.0, -3.0),
btn_close_center + egui::vec2(3.0, 3.0), btn_close_center + egui::vec2(3.0, 3.0),
], ],
egui::Stroke::new(1.5, egui::Color32::from_rgb(80, 0, 0)), egui::Stroke::new(1.5_f32, egui::Color32::from_rgb(80, 0, 0)),
); );
ui.painter().line_segment( ui.painter().line_segment(
[ [
btn_close_center + egui::vec2(3.0, -3.0), btn_close_center + egui::vec2(3.0, -3.0),
btn_close_center + egui::vec2(-3.0, 3.0), btn_close_center + egui::vec2(-3.0, 3.0),
], ],
egui::Stroke::new(1.5, egui::Color32::from_rgb(80, 0, 0)), egui::Stroke::new(1.5_f32, egui::Color32::from_rgb(80, 0, 0)),
); );
} }
if close_resp.clicked() { if close_resp.clicked() {
@@ -299,7 +334,7 @@ impl EskinDesktopApp {
btn_min_center + egui::vec2(-3.0, 0.0), btn_min_center + egui::vec2(-3.0, 0.0),
btn_min_center + egui::vec2(3.0, 0.0), btn_min_center + egui::vec2(3.0, 0.0),
], ],
egui::Stroke::new(1.5, egui::Color32::from_rgb(120, 80, 0)), egui::Stroke::new(1.5_f32, egui::Color32::from_rgb(120, 80, 0)),
); );
} }
if min_resp.clicked() { if min_resp.clicked() {
@@ -318,7 +353,7 @@ impl EskinDesktopApp {
ui.painter().rect_stroke( ui.painter().rect_stroke(
egui::Rect::from_center_size(btn_max_center, egui::vec2(s * 2.0, s * 2.0)), egui::Rect::from_center_size(btn_max_center, egui::vec2(s * 2.0, s * 2.0)),
egui::CornerRadius::same(1), egui::CornerRadius::same(1),
egui::Stroke::new(1.5, egui::Color32::from_rgb(0, 80, 10)), egui::Stroke::new(1.5_f32, egui::Color32::from_rgb(0, 80, 10)),
egui::StrokeKind::Outside, egui::StrokeKind::Outside,
); );
} }
@@ -329,24 +364,58 @@ impl EskinDesktopApp {
} }
} }
fn draw_floating_panels(&mut self, ctx: &egui::Context) { fn draw_floating_panels(
&mut self,
ctx: &egui::Context,
stats: crate::serial_core::serial::SerialIoStats,
) {
// draw_scene_panel(ctx, &mut self.scene_panel); // draw_scene_panel(ctx, &mut self.scene_panel);
// if self.config_state.mode == SerialMode::Hand && self.stats_panel.visible {
// self.config_panel.visible = false;
// self.export_panel.visible = false;
// self.matrix_config_panel.visible = false;
// }
if let Some(next_mode) = draw_config_panel( if let Some(next_mode) = draw_config_panel(
ctx, ctx,
&mut self.config_panel, &mut self.config_panel,
&mut self.config_state, &mut self.config_state,
&self.connection, &self.connection,
&self.recorder,
stats,
100.0,
100.0,
) { ) {
self.switch_mode(next_mode); self.switch_mode(next_mode);
} }
draw_stats_panel(ctx, &mut self.stats_panel, &self.signal_history); match self.config_state.mode {
SerialMode::Finger => {
draw_stats_panel(
ctx,
&mut self.stats_panel,
&self.signal_history,
self.latest_spatial_force,
);
}
SerialMode::Finger3D => {
draw_stats_panel(
ctx,
&mut self.stats_panel,
&self.signal_history,
self.latest_spatial_force,
);
}
SerialMode::Hand => {
draw_hand_force_panels(ctx, self.stats_panel.visible, &self.hand_signal_histories);
}
}
draw_export_panel( draw_export_panel(
ctx, ctx,
&mut self.export_panel, &mut self.export_panel,
&self.recorder, &self.recorder,
&mut self.export_path, &mut self.export_path,
); );
draw_matrix_config_panel(ctx, &mut self.matrix_config_panel, &mut self.matrix_config); // draw_matrix_config_panel(ctx, &mut self.matrix_config_panel, &mut self.matrix_config);
} }
fn draw_panel_context_menu(&mut self, ctx: &egui::Context) { fn draw_panel_context_menu(&mut self, ctx: &egui::Context) {
@@ -382,15 +451,40 @@ impl EskinDesktopApp {
panel_restore_item(ui, "配置", &mut self.config_panel); panel_restore_item(ui, "配置", &mut self.config_panel);
panel_restore_item(ui, "录制", &mut self.export_panel); panel_restore_item(ui, "录制", &mut self.export_panel);
panel_restore_item(ui, "矩阵", &mut self.matrix_config_panel); panel_restore_item(ui, "矩阵", &mut self.matrix_config_panel);
if self.stats_panel.visible {
if ui
.add_sized(
egui::vec2(ui.available_width(), 0.0),
egui::Button::new("隐藏 统计"),
)
.clicked()
{
self.stats_panel.visible = false;
close_menu = true;
}
} else {
panel_restore_item(ui, "统计", &mut self.stats_panel); panel_restore_item(ui, "统计", &mut self.stats_panel);
if ui.button("打砖块").clicked() { }
if ui
.add_sized(
egui::vec2(ui.available_width(), 0.0),
egui::Button::new("打砖块"),
)
.clicked()
{
self.breakout_visible = true; self.breakout_visible = true;
close_menu = true; close_menu = true;
} }
ui.separator(); ui.separator();
if ui.button("全部显示").clicked() { if ui
.add_sized(
egui::vec2(ui.available_width(), 0.0),
egui::Button::new("全部显示"),
)
.clicked()
{
self.config_panel.visible = true; self.config_panel.visible = true;
self.export_panel.visible = true; self.export_panel.visible = true;
self.matrix_config_panel.visible = true; self.matrix_config_panel.visible = true;
@@ -414,11 +508,27 @@ impl EskinDesktopApp {
} }
fn switch_mode(&mut self, next: SerialMode) { fn switch_mode(&mut self, next: SerialMode) {
if next == SerialMode::Finger3D {
// The 3D fingertip PCB sends one 12x9 TactileA frame (108 cells).
self.matrix_config = MatrixConfigState {
rows: 12,
cols: 9,
color_min: 0.0,
color_max: 7000.0,
};
}
self.connect_state.mode = next; self.connect_state.mode = next;
self.config_state.mode = next; self.config_state.mode = next;
self.config_state.baud_rate = next.baud_rate();
self.connection.disconnect(); self.connection.disconnect();
self.pressure_matrix.fill([0.0, 0.0]); self.pressure_matrix.fill([0.0, 0.0]);
self.data_log_frame = 0; self.hand_pressure.clear();
self.force_estimator.reset();
self.latest_spatial_force = None;
self.signal_history.clear();
self.hand_signal_histories
.iter_mut()
.for_each(|history| history.clear());
self.active_mode = match next { self.active_mode = match next {
SerialMode::Finger => ActiveMode::Finger(FingerMode { SerialMode::Finger => ActiveMode::Finger(FingerMode {
@@ -427,26 +537,35 @@ impl EskinDesktopApp {
range: 0..7000, range: 0..7000,
dot: true, dot: true,
}), }),
SerialMode::Hand => ActiveMode::Hand(HandGatewayMode { range: 0..7000 }), SerialMode::Finger3D | SerialMode::Hand => {
ActiveMode::Hand(HandGatewayMode { range: 0..7000 })
} }
};
} }
} }
fn log_pressure_sample(raw: &[u32], rows: u32, cols: u32) { fn update_hand_signal_histories(
let max = raw.iter().copied().max().unwrap_or(0); histories: &mut [Vec<f32>; HAND_FORCE_PANEL_COUNT],
let sum: u64 = raw.iter().map(|value| *value as u64).sum(); raw_values: &[u32],
let non_zero = raw.iter().filter(|value| **value != 0).count(); ) {
let preview = raw let mut offset = 0usize;
for (history, sample_count) in histories.iter_mut().zip(HAND_FORCE_SEGMENT_COUNTS) {
let raw_total = raw_values
.get(offset..offset + sample_count)
.unwrap_or(&[])
.iter() .iter()
.take(12) .fold(0_u64, |sum, value| sum + *value as u64)
.map(u32::to_string) .min(u32::MAX as u64) as u32;
.collect::<Vec<_>>() let force = raw_to_g1(raw_total).min(25.6);
.join(", "); let force = if force <= 0.1 { 0.0 } else { force };
println!( history.push(force);
"[pressure] {rows}x{cols} cells={} non_zero={non_zero} max={max} sum={sum} first=[{preview}]", if history.len() > SUMMARY_POINTS_PER_SERIES {
raw.len() history.remove(0);
); }
offset += sample_count;
}
} }
fn raw_to_g1(raw: u32) -> f32 { fn raw_to_g1(raw: u32) -> f32 {
@@ -465,7 +584,6 @@ fn raw_to_g1(raw: u32) -> f32 {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0; return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
} }
// Keep the same lookup behavior as JE-Skin so both apps report the same force.
let mut left = 0usize; let mut left = 0usize;
let mut right = RAW.len() - 1; let mut right = RAW.len() - 1;
while left + 1 < right { while left + 1 < right {
@@ -500,7 +618,7 @@ fn paint_split_viewport_shell(
painter.rect_stroke( painter.rect_stroke(
rect, rect,
egui::CornerRadius::same(8), egui::CornerRadius::same(8),
egui::Stroke::new(1.0, ONE_DARK_PRO.border_soft), egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border_soft),
egui::StrokeKind::Outside, egui::StrokeKind::Outside,
); );
painter.line_segment( painter.line_segment(
@@ -508,7 +626,7 @@ fn paint_split_viewport_shell(
rect.left_top() + egui::vec2(12.0, 34.0), rect.left_top() + egui::vec2(12.0, 34.0),
rect.right_top() + egui::vec2(-12.0, 34.0), rect.right_top() + egui::vec2(-12.0, 34.0),
], ],
egui::Stroke::new(1.0, ONE_DARK_PRO.border_soft), egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border_soft),
); );
painter.text( painter.text(
rect.left_top() + egui::vec2(14.0, 16.0), rect.left_top() + egui::vec2(14.0, 16.0),
@@ -534,9 +652,6 @@ fn split_viewport_body(rect: egui::Rect) -> egui::Rect {
} }
fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut PressureFrame) { fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut PressureFrame) {
const RANGE_MIN: f32 = 0.0;
const RANGE_MAX: f32 = 7000.0;
normalized.fill([0.0, 0.0]); normalized.fill([0.0, 0.0]);
let src_cols = cols.max(1); let src_cols = cols.max(1);
@@ -548,27 +663,69 @@ fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut
let src_index = (row * src_cols + col) as usize; let src_index = (row * src_cols + col) as usize;
let dst_index = (row * MATRIX_COLS + col) as usize; let dst_index = (row * MATRIX_COLS + col) as usize;
if let Some(value) = raw.get(src_index) { if let Some(value) = raw.get(src_index) {
let raw_value = *value as f32; normalized[dst_index] = normalize_pressure_value(*value);
}
}
}
}
fn normalize_pressure_samples(raw: &[u32]) -> PressureSamples {
raw.iter().copied().map(normalize_pressure_value).collect()
}
fn normalize_pressure_value(value: u32) -> [f32; 2] {
const RANGE_MIN: f32 = 0.0;
const RANGE_MAX: f32 = 7000.0;
let raw_value = value as f32;
let mapped = ((raw_value - RANGE_MIN) / (RANGE_MAX - RANGE_MIN)).clamp(0.0, 1.0); let mapped = ((raw_value - RANGE_MIN) / (RANGE_MAX - RANGE_MIN)).clamp(0.0, 1.0);
let display_value = if raw_value <= RANGE_MIN + 4.0 { let display_value = if raw_value <= RANGE_MIN + 4.0 {
0.0 0.0
} else { } else {
raw_value.round().min(9999.0) raw_value.round().min(9999.0)
}; };
normalized[dst_index] = [mapped, display_value];
[mapped, display_value]
} }
fn draw_config_bar_mode(ui: &mut egui::Ui, config: &mut ConfigPanelState) -> Option<SerialMode> {
let mut changed_to = None;
ui.horizontal(|ui| {
ui.colored_label(dim_text(), "单面指尖");
if mode_button(ui, &mut config.mode, SerialMode::Finger, "单面指尖") {
changed_to = Some(SerialMode::Finger);
} }
});
changed_to
}
fn mode_button(
ui: &mut egui::Ui,
mode: &mut SerialMode,
value: SerialMode,
label: &'static str,
) -> bool {
let clicked = ui.add(style::mode_button(label, *mode == value)).clicked();
if clicked && *mode != value {
*mode = value;
true
} else {
false
} }
} }
impl eframe::App for EskinDesktopApp { impl eframe::App for EskinDesktopApp {
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
let ctx = ui.ctx().clone(); let ctx = ui.ctx().clone();
let stats = self.connection.stats();
self.update_pressure_matrix(); self.update_pressure_matrix();
self.draw_workspace(ui); self.draw_workspace(ui);
self.draw_title_bar(ui, frame); self.draw_title_bar(ui, frame);
self.draw_floating_panels(&ctx); self.draw_floating_panels(&ctx, stats);
self.draw_panel_context_menu(&ctx); self.draw_panel_context_menu(&ctx);
// Keep repainting while the wgpu background is a realtime viewport. // Keep repainting while the wgpu background is a realtime viewport.

View File

@@ -249,7 +249,11 @@ impl BreakoutGame {
} }
fn launch_ball(&mut self) { fn launch_ball(&mut self) {
let direction = if self.level % 2 == 0 { -0.24 } else { 0.24 }; let direction = if self.level.is_multiple_of(2) {
-0.24
} else {
0.24
};
self.ball_pos = egui::pos2(self.paddle_x, PADDLE_Y - PADDLE_H - BALL_RADIUS); self.ball_pos = egui::pos2(self.paddle_x, PADDLE_Y - PADDLE_H - BALL_RADIUS);
self.ball_vel = egui::vec2(direction, -1.0).normalized() * BALL_SPEED; self.ball_vel = egui::vec2(direction, -1.0).normalized() * BALL_SPEED;
} }
@@ -372,7 +376,7 @@ impl BreakoutGame {
painter.rect_stroke( painter.rect_stroke(
rect, rect,
egui::CornerRadius::same(6), egui::CornerRadius::same(6),
egui::Stroke::new(1.0, color_alpha(ONE_DARK_PRO.accent, 110)), egui::Stroke::new(1.0_f32, color_alpha(ONE_DARK_PRO.accent, 110)),
egui::StrokeKind::Outside, egui::StrokeKind::Outside,
); );
@@ -469,7 +473,7 @@ fn circle_hits_rect(center: egui::Pos2, radius: f32, rect: egui::Rect) -> bool {
fn status_chip(ui: &mut egui::Ui, label: &'static str, value: impl ToString, color: egui::Color32) { fn status_chip(ui: &mut egui::Ui, label: &'static str, value: impl ToString, color: egui::Color32) {
egui::Frame::new() egui::Frame::new()
.fill(color_alpha(ONE_DARK_PRO.panel_deep, 190)) .fill(color_alpha(ONE_DARK_PRO.panel_deep, 190))
.stroke(egui::Stroke::new(1.0, color_alpha(color, 92))) .stroke(egui::Stroke::new(1.0_f32, color_alpha(color, 92)))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.inner_margin(egui::Margin::symmetric(8, 4)) .inner_margin(egui::Margin::symmetric(8, 4))
.show(ui, |ui| { .show(ui, |ui| {
@@ -514,7 +518,7 @@ fn paint_arena_grid(painter: &egui::Painter, rect: egui::Rect) {
painter.rect_stroke( painter.rect_stroke(
rect, rect,
egui::CornerRadius::same(6), egui::CornerRadius::same(6),
egui::Stroke::new(1.0, color_alpha(ONE_DARK_PRO.accent, 70)), egui::Stroke::new(1.0_f32, color_alpha(ONE_DARK_PRO.accent, 70)),
egui::StrokeKind::Inside, egui::StrokeKind::Inside,
); );
} }
@@ -531,7 +535,7 @@ fn paint_brick(painter: &egui::Painter, arena: egui::Rect, brick: &Brick, index:
painter.rect_stroke( painter.rect_stroke(
rect.expand(4.0 * brick.flash), rect.expand(4.0 * brick.flash),
egui::CornerRadius::same(4), egui::CornerRadius::same(4),
egui::Stroke::new(1.4, color_alpha(ONE_DARK_PRO.accent_hot, alpha)), egui::Stroke::new(1.4_f32, color_alpha(ONE_DARK_PRO.accent_hot, alpha)),
egui::StrokeKind::Outside, egui::StrokeKind::Outside,
); );
} }
@@ -564,7 +568,7 @@ fn paint_control_meter(painter: &egui::Painter, arena: egui::Rect, control: Brea
painter.rect_stroke( painter.rect_stroke(
meter, meter,
egui::CornerRadius::same(4), egui::CornerRadius::same(4),
egui::Stroke::new(1.0, color_alpha(ONE_DARK_PRO.border, 120)), egui::Stroke::new(1.0_f32, color_alpha(ONE_DARK_PRO.border, 120)),
egui::StrokeKind::Outside, egui::StrokeKind::Outside,
); );
let center_x = meter.center().x; let center_x = meter.center().x;
@@ -573,7 +577,7 @@ fn paint_control_meter(painter: &egui::Painter, arena: egui::Rect, control: Brea
egui::pos2(center_x, meter.top()), egui::pos2(center_x, meter.top()),
egui::pos2(center_x, meter.bottom()), egui::pos2(center_x, meter.bottom()),
], ],
egui::Stroke::new(1.0, ONE_DARK_PRO.border_soft), egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border_soft),
); );
let marker_x = center_x + control.axis.clamp(-1.0, 1.0) * meter.width() * 0.45; let marker_x = center_x + control.axis.clamp(-1.0, 1.0) * meter.width() * 0.45;
painter.circle_filled( painter.circle_filled(

View File

@@ -4,7 +4,10 @@ use std::time::Duration;
use crossbeam_channel::{self, Receiver, Sender, TryRecvError}; use crossbeam_channel::{self, Receiver, Sender, TryRecvError};
use crate::serial_core::serial::{SerialIoStats, SerialPortReadWrite, run_serial_loop}; use crate::recording::Recorder;
use crate::serial_core::serial::{
SerialIoStats, SerialPortReadWrite, SerialProtocol, run_serial_loop,
};
/// Connection state visible to the UI. /// Connection state visible to the UI.
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -28,6 +31,8 @@ struct Session {
handle: JoinHandle<()>, handle: JoinHandle<()>,
sample_rx: Receiver<Vec<i32>>, sample_rx: Receiver<Vec<i32>>,
stats_rx: Receiver<SerialIoStats>, stats_rx: Receiver<SerialIoStats>,
rows: u32,
cols: u32,
} }
/// Thread-safe connection manager that the UI and renderer can share. /// Thread-safe connection manager that the UI and renderer can share.
@@ -72,7 +77,15 @@ impl ConnectionManager {
} }
/// Connect to the given serial port and start streaming in a background thread. /// Connect to the given serial port and start streaming in a background thread.
pub fn connect(&self, port_name: &str, rows: u32, cols: u32) { pub fn connect(
&self,
port_name: &str,
rows: u32,
cols: u32,
baud_rate: u32,
protocol: SerialProtocol,
recorder: Recorder,
) {
self.disconnect(); self.disconnect();
self.set_state(ConnectionState::Connecting); self.set_state(ConnectionState::Connecting);
@@ -90,11 +103,14 @@ impl ConnectionManager {
&port, &port,
rows, rows,
cols, cols,
baud_rate,
protocol,
&state, &state,
&cancel_rx, &cancel_rx,
&sample_tx, &sample_tx,
&stats_tx, &stats_tx,
&latest_sample, &latest_sample,
recorder,
); );
if let Err(e) = result { if let Err(e) = result {
eprintln!("[connection] device loop error: {e}"); eprintln!("[connection] device loop error: {e}");
@@ -107,6 +123,8 @@ impl ConnectionManager {
handle, handle,
sample_rx, sample_rx,
stats_rx, stats_rx,
rows,
cols,
}); });
} }
@@ -135,10 +153,12 @@ impl ConnectionManager {
loop { loop {
match session.sample_rx.try_recv() { match session.sample_rx.try_recv() {
Ok(vals) => { Ok(vals) => {
let rows = 12u32;
let cols = 7u32;
let matrix = vals.iter().map(|v| (*v).max(0) as u32).collect(); let matrix = vals.iter().map(|v| (*v).max(0) as u32).collect();
last = Some(PressureSample { matrix, rows, cols }); last = Some(PressureSample {
matrix,
rows: session.rows,
cols: session.cols,
});
} }
Err(TryRecvError::Empty) => break, Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => break, Err(TryRecvError::Disconnected) => break,
@@ -159,18 +179,22 @@ impl Default for ConnectionManager {
} }
/// The blocking device loop that runs on a background thread. /// The blocking device loop that runs on a background thread.
#[allow(clippy::too_many_arguments)]
fn run_device_loop( fn run_device_loop(
port_name: &str, port_name: &str,
rows: u32, rows: u32,
cols: u32, cols: u32,
baud_rate: u32,
protocol: SerialProtocol,
state: &Arc<Mutex<ConnectionState>>, state: &Arc<Mutex<ConnectionState>>,
cancel_rx: &Receiver<()>, cancel_rx: &Receiver<()>,
sample_tx: &Sender<Vec<i32>>, sample_tx: &Sender<Vec<i32>>,
stats_tx: &Sender<SerialIoStats>, stats_tx: &Sender<SerialIoStats>,
latest_sample: &Arc<Mutex<Option<PressureSample>>>, latest_sample: &Arc<Mutex<Option<PressureSample>>>,
recorder: Recorder,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let port = serialport::new(port_name, 921_600) let port = serialport::new(port_name, baud_rate)
.timeout(Duration::from_millis(100)) .timeout(Duration::from_millis(1))
.open()?; .open()?;
*state.lock().unwrap() = ConnectionState::Connected; *state.lock().unwrap() = ConnectionState::Connected;
@@ -182,9 +206,11 @@ fn run_device_loop(
&mut rw, &mut rw,
rows as usize, rows as usize,
cols as usize, cols as usize,
protocol,
cancel_rx, cancel_rx,
sample_tx, sample_tx,
Some(stats_tx), Some(stats_tx),
Some(&recorder),
); );
if let Ok(mut latest) = latest_sample.lock() { if let Ok(mut latest) = latest_sample.lock() {

49
src/force.rs Normal file
View File

@@ -0,0 +1,49 @@
use crate::serial_core::multi_dim_force::PztProcessor;
const FINGER_SAMPLE_COUNT: usize = 84;
const MIN_TANGENTIAL_MAGNITUDE: f32 = 0.02;
#[derive(Debug, Clone, Copy)]
pub struct HudSpatialForce {
pub angle_deg: f32,
pub magnitude: f32,
}
pub struct ForceEstimatorState {
pzt_processor: PztProcessor,
}
impl ForceEstimatorState {
pub fn new() -> Self {
Self {
pzt_processor: PztProcessor::new(),
}
}
pub fn reset(&mut self) {
self.pzt_processor.reset_baseline();
}
pub fn analyze(&mut self, values: &[u32]) -> Option<HudSpatialForce> {
if values.len() != FINGER_SAMPLE_COUNT {
return None;
}
let pzt_values = values.iter().map(|value| *value as f32).collect::<Vec<_>>();
self.pzt_processor
.get_pzt_analysis(&pzt_values)
.ok()
.filter(|analysis| analysis.magnitude > MIN_TANGENTIAL_MAGNITUDE)
.map(|analysis| HudSpatialForce {
angle_deg: analysis.angle_deg,
magnitude: analysis.magnitude,
})
}
}
impl Default for ForceEstimatorState {
fn default() -> Self {
Self::new()
}
}

View File

@@ -1,6 +1,9 @@
#![allow(dead_code)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod app; mod app;
mod breakout; mod breakout;
mod connection; mod connection;
mod force;
mod matrix; mod matrix;
mod model; mod model;
mod recording; mod recording;

View File

@@ -1,5 +1,31 @@
pub const MATRIX_ROWS: u32 = 12; pub const MATRIX_ROWS: u32 = 12;
pub const MATRIX_COLS: u32 = 7; pub const MATRIX_COLS: u32 = 7;
pub const UNFOLDED_L_CHANNEL_COUNT: usize = 8;
pub const UNFOLDED_H_CHANNEL_COUNT: usize = 13;
pub const UNFOLDED_SENSOR_COUNT: usize = UNFOLDED_L_CHANNEL_COUNT * UNFOLDED_H_CHANNEL_COUNT;
pub const UNFOLDED_SENSOR_SEGMENT_COUNTS: [usize; 10] = [8, 3, 5, 8, 10, 44, 3, 5, 8, 10];
pub const fn unfolded_lh_sample_index(l: usize, h: usize) -> usize {
debug_assert!(l < UNFOLDED_L_CHANNEL_COUNT);
debug_assert!(h < UNFOLDED_H_CHANNEL_COUNT);
h * UNFOLDED_L_CHANNEL_COUNT + l
}
#[cfg(test)]
mod adc_scan_tests {
use super::*;
#[test]
fn scan_changes_l_before_advancing_h() {
let first_h0_scan = (0..UNFOLDED_L_CHANNEL_COUNT)
.map(|l| unfolded_lh_sample_index(l, 0))
.collect::<Vec<_>>();
assert_eq!(first_h0_scan, (0..8).collect::<Vec<_>>());
assert_eq!(unfolded_lh_sample_index(0, 1), 8);
assert_eq!(unfolded_lh_sample_index(7, 12), 103);
}
}
const BASE_MATRIX_SPAN: f32 = 24.0; const BASE_MATRIX_SPAN: f32 = 24.0;
const MATRIX_SPAN_GROWTH: f32 = 0.6; const MATRIX_SPAN_GROWTH: f32 = 0.6;

View File

@@ -145,11 +145,11 @@ impl Recorder {
r.state == RecordingState::Recording || r.state == RecordingState::Paused, r.state == RecordingState::Recording || r.state == RecordingState::Paused,
"nothing to stop" "nothing to stop"
); );
if r.state == RecordingState::Paused { if r.state == RecordingState::Paused
if let Some(ps) = r.pause_start.take() { && let Some(ps) = r.pause_start.take()
{
r.paused_duration_ms += ps.elapsed().as_millis() as u64; r.paused_duration_ms += ps.elapsed().as_millis() as u64;
} }
}
r.state = RecordingState::Idle; r.state = RecordingState::Idle;
Ok(()) Ok(())
} }
@@ -317,16 +317,15 @@ impl Recorder {
} }
// Set the start instant so duration_ms() reports the imported span // Set the start instant so duration_ms() reports the imported span
if !r.frames.is_empty() { if !r.frames.is_empty()
if let Some(last) = r.frames.last() { && let Some(last) = r.frames.last()
{
// Pretend the recording happened `last.timestamp_ms` ago // Pretend the recording happened `last.timestamp_ms` ago
// so that elapsed_ms() would return that value. // so that elapsed_ms() would return that value.
// We store a "fake" start by noting the offset. // We store a "fake" start by noting the offset.
r.start = r.start = Some(Instant::now() - std::time::Duration::from_millis(last.timestamp_ms));
Some(Instant::now() - std::time::Duration::from_millis(last.timestamp_ms));
r.paused_duration_ms = 0; r.paused_duration_ms = 0;
} }
}
Ok(()) Ok(())
} }

View File

@@ -1,5 +1,8 @@
use crate::{ use crate::{
matrix::{MatrixLayout, build_view_projection, glyph_world_position}, matrix::{
MatrixLayout, UNFOLDED_SENSOR_COUNT, build_view_projection, glyph_world_position,
unfolded_lh_sample_index,
},
model::{AlphaMode, InstanceRaw, ModelVertex, Vertex}, model::{AlphaMode, InstanceRaw, ModelVertex, Vertex},
resources, texture, resources, texture,
}; };
@@ -13,11 +16,13 @@ use std::ops::Range;
pub const PRESSURE_CELL_COUNT: usize = pub const PRESSURE_CELL_COUNT: usize =
(crate::matrix::MATRIX_ROWS * crate::matrix::MATRIX_COLS) as usize; (crate::matrix::MATRIX_ROWS * crate::matrix::MATRIX_COLS) as usize;
pub type PressureFrame = [[f32; 2]; PRESSURE_CELL_COUNT]; pub type PressureFrame = [[f32; 2]; PRESSURE_CELL_COUNT];
pub type PressureSamples = Vec<[f32; 2]>;
pub struct WgpuBackgroundCallback { pub struct WgpuBackgroundCallback {
pub width: f32, pub width: f32,
pub height: f32, pub height: f32,
pub pressure: PressureFrame, pub pressure: PressureFrame,
pub hand_pressure: PressureSamples,
pub active_mode: ActiveMode, pub active_mode: ActiveMode,
} }
@@ -68,23 +73,97 @@ const HAND_TIP_MATRICES: [HandTipMatrix; 5] = [
}, },
]; ];
const HAND_PALM_CHIPS: [HandPalmChip; 2] = [ const UNFOLDED_CANVAS_SIZE: [f32; 2] = [850.0, 750.0];
const UNFOLDED_CELL_SPACING_PX: f32 = 42.0;
const UNFOLDED_SENSOR_CHIPS: [HandPalmChip; 10] = [
// Top cap: 2 rows x 4 columns.
HandPalmChip { HandPalmChip {
center_px: [538.0, 608.0], center_px: [425.0, 165.0],
size_px: [248.0, 82.0], size_px: [176.0, 88.0],
angle_rad: 0.06, angle_rad: 0.0,
rows: 5, rows: 2,
cols: 14, cols: 4,
sample_offset: 0,
},
// Left wing, authored from the outside towards the 11x4 center block.
HandPalmChip {
center_px: [140.0, 578.0],
size_px: [46.0, 132.0],
angle_rad: 0.0,
rows: 3,
cols: 1,
sample_offset: 8,
}, },
HandPalmChip { HandPalmChip {
center_px: [606.0, 780.0], center_px: [188.0, 534.0],
size_px: [72.0, 214.0], size_px: [46.0, 220.0],
angle_rad: 0.05, angle_rad: 0.0,
rows: 5,
cols: 1,
sample_offset: 11,
},
HandPalmChip {
center_px: [236.0, 468.0],
size_px: [46.0, 352.0],
angle_rad: 0.0,
rows: 8,
cols: 1,
sample_offset: 16,
},
HandPalmChip {
center_px: [284.0, 424.0],
size_px: [46.0, 440.0],
angle_rad: 0.0,
rows: 10,
cols: 1,
sample_offset: 24,
},
// Center spine: 11 rows x 4 columns.
HandPalmChip {
center_px: [425.0, 444.0],
size_px: [176.0, 484.0],
angle_rad: 0.0,
rows: 11, rows: 11,
cols: 4, cols: 4,
sample_offset: 34,
},
// Right wing mirrors the left wing. Data remains ordered 3, 5, 8, 10.
HandPalmChip {
center_px: [710.0, 578.0],
size_px: [46.0, 132.0],
angle_rad: 0.0,
rows: 3,
cols: 1,
sample_offset: 78,
},
HandPalmChip {
center_px: [662.0, 534.0],
size_px: [46.0, 220.0],
angle_rad: 0.0,
rows: 5,
cols: 1,
sample_offset: 81,
},
HandPalmChip {
center_px: [614.0, 468.0],
size_px: [46.0, 352.0],
angle_rad: 0.0,
rows: 8,
cols: 1,
sample_offset: 86,
},
HandPalmChip {
center_px: [566.0, 424.0],
size_px: [46.0, 440.0],
angle_rad: 0.0,
rows: 10,
cols: 1,
sample_offset: 94,
}, },
]; ];
const HAND_FINGER_SENSOR_CELLS: usize = 12 * 7;
// Each entry pins one miniature matrix to a fingertip in hand.png. // Each entry pins one miniature matrix to a fingertip in hand.png.
// Coordinates are authored in source-image pixels so they are easy to tune by eye. // Coordinates are authored in source-image pixels so they are easy to tune by eye.
// size_px keeps the same 7:12 aspect as the Finger-mode 7 columns x 12 rows matrix. // size_px keeps the same 7:12 aspect as the Finger-mode 7 columns x 12 rows matrix.
@@ -94,14 +173,15 @@ struct HandTipMatrix {
angle_rad: f32, angle_rad: f32,
} }
// Palm chips follow the hand layout: one horizontal 5x14 matrix and one vertical // One block in the flat 270-degree sensor layout. Coordinates use a dedicated
// 11x4 matrix, rendered as dark inset chip tiles on the palm. // 850x750 canvas so the unfolded shape stays prominent across window sizes.
struct HandPalmChip { struct HandPalmChip {
center_px: [f32; 2], center_px: [f32; 2],
size_px: [f32; 2], size_px: [f32; 2],
angle_rad: f32, angle_rad: f32,
rows: u32, rows: u32,
cols: u32, cols: u32,
sample_offset: usize,
} }
impl egui_wgpu::CallbackTrait for WgpuBackgroundCallback { impl egui_wgpu::CallbackTrait for WgpuBackgroundCallback {
@@ -114,7 +194,13 @@ impl egui_wgpu::CallbackTrait for WgpuBackgroundCallback {
resources: &mut egui_wgpu::CallbackResources, resources: &mut egui_wgpu::CallbackResources,
) -> Vec<wgpu::CommandBuffer> { ) -> Vec<wgpu::CommandBuffer> {
let resources: &mut BackgroundRenderResources = resources.get_mut().unwrap(); let resources: &mut BackgroundRenderResources = resources.get_mut().unwrap();
resources.prepare(queue, self.width, self.height, &self.pressure); resources.prepare(
queue,
self.width,
self.height,
&self.pressure,
&self.hand_pressure,
);
Vec::new() Vec::new()
} }
@@ -144,7 +230,6 @@ pub struct BackgroundRenderResources {
dot_pipeline: wgpu::RenderPipeline, dot_pipeline: wgpu::RenderPipeline,
hand_membrane_pipeline: wgpu::RenderPipeline, hand_membrane_pipeline: wgpu::RenderPipeline,
hand_dot_pipeline: wgpu::RenderPipeline, hand_dot_pipeline: wgpu::RenderPipeline,
hand_palm_chip_pipeline: wgpu::RenderPipeline,
hand_palm_dot_pipeline: wgpu::RenderPipeline, hand_palm_dot_pipeline: wgpu::RenderPipeline,
hand_image_bind_group: wgpu::BindGroup, hand_image_bind_group: wgpu::BindGroup,
hand_image_texture: texture::Texture, hand_image_texture: texture::Texture,
@@ -156,8 +241,6 @@ pub struct BackgroundRenderResources {
hand_membrane_instances: Vec<GlyphInstance>, hand_membrane_instances: Vec<GlyphInstance>,
hand_dot_instance_buffer: wgpu::Buffer, hand_dot_instance_buffer: wgpu::Buffer,
hand_dot_instances: Vec<GlyphInstance>, hand_dot_instances: Vec<GlyphInstance>,
hand_palm_chip_instance_buffer: wgpu::Buffer,
hand_palm_chip_instances: Vec<GlyphInstance>,
hand_palm_dot_instance_buffer: wgpu::Buffer, hand_palm_dot_instance_buffer: wgpu::Buffer,
hand_palm_dot_instances: Vec<GlyphInstance>, hand_palm_dot_instances: Vec<GlyphInstance>,
render_options: RenderOptions, render_options: RenderOptions,
@@ -265,10 +348,7 @@ impl BackgroundRenderResources {
build_view_projection(1.0, &layout), build_view_projection(1.0, &layout),
surface_is_srgb, surface_is_srgb,
render_options, render_options,
[ UNFOLDED_CANVAS_SIZE,
hand_image_texture.width as f32,
hand_image_texture.height as f32,
],
); );
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Pressure Matrix Uniform Buffer"), label: Some("Pressure Matrix Uniform Buffer"),
@@ -485,8 +565,6 @@ impl BackgroundRenderResources {
create_hand_membrane_pipeline(device, target_format, &shader, &pipeline_layout); create_hand_membrane_pipeline(device, target_format, &shader, &pipeline_layout);
let hand_dot_pipeline = let hand_dot_pipeline =
create_hand_dot_pipeline(device, target_format, &shader, &pipeline_layout); create_hand_dot_pipeline(device, target_format, &shader, &pipeline_layout);
let hand_palm_chip_pipeline =
create_hand_palm_chip_pipeline(device, target_format, &shader, &pipeline_layout);
let hand_palm_dot_pipeline = let hand_palm_dot_pipeline =
create_hand_palm_dot_pipeline(device, target_format, &shader, &pipeline_layout); create_hand_palm_dot_pipeline(device, target_format, &shader, &pipeline_layout);
@@ -539,23 +617,8 @@ impl BackgroundRenderResources {
contents: bytemuck::cast_slice(&hand_dot_instances), contents: bytemuck::cast_slice(&hand_dot_instances),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
}); });
let hand_palm_chip_instances = build_hand_palm_chip_instances( let hand_palm_dot_instances =
hand_image_texture.width as f32, build_hand_palm_dot_instances(rows, cols, &[[0.0, 0.0]; PRESSURE_CELL_COUNT]);
hand_image_texture.height as f32,
);
let hand_palm_chip_instance_buffer =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Hand Palm Chip Instance Buffer"),
contents: bytemuck::cast_slice(&hand_palm_chip_instances),
usage: wgpu::BufferUsages::VERTEX,
});
let hand_palm_dot_instances = build_hand_palm_dot_instances(
rows,
cols,
hand_image_texture.width as f32,
hand_image_texture.height as f32,
&[[0.0, 0.0]; PRESSURE_CELL_COUNT],
);
let hand_palm_dot_instance_buffer = let hand_palm_dot_instance_buffer =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor { device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Hand Palm Chip Dot Instance Buffer"), label: Some("Hand Palm Chip Dot Instance Buffer"),
@@ -577,7 +640,6 @@ impl BackgroundRenderResources {
dot_pipeline, dot_pipeline,
hand_membrane_pipeline, hand_membrane_pipeline,
hand_dot_pipeline, hand_dot_pipeline,
hand_palm_chip_pipeline,
hand_palm_dot_pipeline, hand_palm_dot_pipeline,
hand_image_bind_group, hand_image_bind_group,
hand_image_texture, hand_image_texture,
@@ -588,15 +650,20 @@ impl BackgroundRenderResources {
hand_membrane_instances, hand_membrane_instances,
hand_dot_instance_buffer, hand_dot_instance_buffer,
hand_dot_instances, hand_dot_instances,
hand_palm_chip_instance_buffer,
hand_palm_chip_instances,
hand_palm_dot_instance_buffer, hand_palm_dot_instance_buffer,
hand_palm_dot_instances, hand_palm_dot_instances,
render_options, render_options,
} }
} }
fn prepare(&mut self, queue: &wgpu::Queue, width: f32, height: f32, pressure: &PressureFrame) { fn prepare(
&mut self,
queue: &wgpu::Queue,
width: f32,
height: f32,
pressure: &PressureFrame,
hand_pressure: &[[f32; 2]],
) {
let aspect = width / height.max(1.0); let aspect = width / height.max(1.0);
self.uniform = MatrixUniform::new( self.uniform = MatrixUniform::new(
width, width,
@@ -604,10 +671,7 @@ impl BackgroundRenderResources {
build_view_projection(aspect, &self.layout), build_view_projection(aspect, &self.layout),
self.surface_is_srgb, self.surface_is_srgb,
self.render_options, self.render_options,
[ UNFOLDED_CANVAS_SIZE,
self.hand_image_texture.width as f32,
self.hand_image_texture.height as f32,
],
); );
queue.write_buffer( queue.write_buffer(
&self.uniform_buffer, &self.uniform_buffer,
@@ -628,14 +692,20 @@ impl BackgroundRenderResources {
bytemuck::cast_slice(&self.glyph_instances), bytemuck::cast_slice(&self.glyph_instances),
); );
// Hand mode uses five UV-anchored fingertip matrices over hand.png. let hand_pressure = if hand_pressure.is_empty() {
// Rebuild their instance positions here so pressure colors update every frame. pressure.as_slice()
} else {
hand_pressure
};
// Keep legacy fingertip buffers current while both hardware modes share
// the same renderer resources.
self.hand_dot_instances = build_hand_dot_instances( self.hand_dot_instances = build_hand_dot_instances(
self.rows, self.rows,
self.cols, self.cols,
self.hand_image_texture.width as f32, self.hand_image_texture.width as f32,
self.hand_image_texture.height as f32, self.hand_image_texture.height as f32,
pressure, hand_pressure,
); );
queue.write_buffer( queue.write_buffer(
&self.hand_dot_instance_buffer, &self.hand_dot_instance_buffer,
@@ -643,15 +713,9 @@ impl BackgroundRenderResources {
bytemuck::cast_slice(&self.hand_dot_instances), bytemuck::cast_slice(&self.hand_dot_instances),
); );
// Palm chips reuse the same live 12x7 pressure frame, but draw it as // Rebuild the 104-cell unfolded layout with the latest gateway samples.
// embedded micro-pixels inside dark chip tiles. self.hand_palm_dot_instances =
self.hand_palm_dot_instances = build_hand_palm_dot_instances( build_hand_palm_dot_instances(self.rows, self.cols, hand_pressure);
self.rows,
self.cols,
self.hand_image_texture.width as f32,
self.hand_image_texture.height as f32,
pressure,
);
queue.write_buffer( queue.write_buffer(
&self.hand_palm_dot_instance_buffer, &self.hand_palm_dot_instance_buffer,
0, 0,
@@ -667,12 +731,7 @@ impl BackgroundRenderResources {
match active_mode { match active_mode {
ActiveMode::Finger(mode) => self.paint_finger(render_pass, mode), ActiveMode::Finger(mode) => self.paint_finger(render_pass, mode),
ActiveMode::Hand(mode) => { ActiveMode::Hand(mode) => self.paint_hand(render_pass, mode),
render_pass.set_pipeline(&self.hand_image_pipeline);
render_pass.set_bind_group(1, &self.hand_image_bind_group, &[]);
render_pass.draw(0..6, 0..1);
self.paint_hand(render_pass, mode);
}
} }
} }
@@ -694,22 +753,7 @@ impl BackgroundRenderResources {
fn paint_hand(&self, render_pass: &mut wgpu::RenderPass<'_>, mode: &HandGatewayMode) { fn paint_hand(&self, render_pass: &mut wgpu::RenderPass<'_>, mode: &HandGatewayMode) {
let _range = mode.range.clone(); let _range = mode.range.clone();
// First draw the translucent sensor membranes, then draw live pressure beads on their grid. // Match Finger mode: draw only the 104 independent pressure dots.
render_pass.set_pipeline(&self.hand_membrane_pipeline);
render_pass.set_vertex_buffer(0, self.glyph_vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.hand_membrane_instance_buffer.slice(..));
render_pass.draw(0..6, 0..self.hand_membrane_instances.len() as u32);
render_pass.set_pipeline(&self.hand_dot_pipeline);
render_pass.set_vertex_buffer(0, self.glyph_vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.hand_dot_instance_buffer.slice(..));
render_pass.draw(0..6, 0..self.hand_dot_instances.len() as u32);
render_pass.set_pipeline(&self.hand_palm_chip_pipeline);
render_pass.set_vertex_buffer(0, self.glyph_vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.hand_palm_chip_instance_buffer.slice(..));
render_pass.draw(0..6, 0..self.hand_palm_chip_instances.len() as u32);
render_pass.set_pipeline(&self.hand_palm_dot_pipeline); render_pass.set_pipeline(&self.hand_palm_dot_pipeline);
render_pass.set_vertex_buffer(0, self.glyph_vertex_buffer.slice(..)); render_pass.set_vertex_buffer(0, self.glyph_vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.hand_palm_dot_instance_buffer.slice(..)); render_pass.set_vertex_buffer(1, self.hand_palm_dot_instance_buffer.slice(..));
@@ -1047,39 +1091,6 @@ fn create_hand_dot_pipeline(
}) })
} }
fn create_hand_palm_chip_pipeline(
device: &wgpu::Device,
target_format: &wgpu::TextureFormat,
shader: &wgpu::ShaderModule,
layout: &wgpu::PipelineLayout,
) -> wgpu::RenderPipeline {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Hand Palm Embedded Chip Pipeline"),
layout: Some(layout),
vertex: wgpu::VertexState {
module: shader,
entry_point: Some("vs_hand_palm_chip"),
compilation_options: Default::default(),
buffers: &[GlyphVertex::desc(), GlyphInstance::desc()],
},
fragment: Some(wgpu::FragmentState {
module: shader,
entry_point: Some("fs_hand_palm_chip"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: *target_format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview_mask: None,
cache: None,
})
}
fn create_hand_palm_dot_pipeline( fn create_hand_palm_dot_pipeline(
device: &wgpu::Device, device: &wgpu::Device,
target_format: &wgpu::TextureFormat, target_format: &wgpu::TextureFormat,
@@ -1131,46 +1142,27 @@ fn build_hand_membrane_instances(image_width: f32, image_height: f32) -> Vec<Gly
.collect() .collect()
} }
fn build_hand_palm_chip_instances(image_width: f32, image_height: f32) -> Vec<GlyphInstance> {
HAND_PALM_CHIPS
.iter()
.map(|chip| {
let uv_x = chip.center_px[0] / image_width.max(1.0);
let uv_y = chip.center_px[1] / image_height.max(1.0);
GlyphInstance {
// Palm chip shaders read xy as hand.png UV.
world_position: [uv_x, uv_y, 0.0, 1.0],
// Store chip angle, source-image pixel size, and matrix shape for the shader grid.
style: [
chip.angle_rad,
chip.size_px[0],
chip.size_px[1],
(chip.rows * 100 + chip.cols) as f32,
],
}
})
.collect()
}
fn build_hand_dot_instances( fn build_hand_dot_instances(
rows: u32, rows: u32,
cols: u32, cols: u32,
image_width: f32, image_width: f32,
image_height: f32, image_height: f32,
pressure: &PressureFrame, pressure: &[[f32; 2]],
) -> Vec<GlyphInstance> { ) -> Vec<GlyphInstance> {
let mut instances = Vec::with_capacity(HAND_TIP_MATRICES.len() * rows as usize * cols as usize); let mut instances = Vec::with_capacity(HAND_TIP_MATRICES.len() * rows as usize * cols as usize);
for tip in HAND_TIP_MATRICES { for (tip_index, tip) in HAND_TIP_MATRICES.into_iter().enumerate() {
let cos = tip.angle_rad.cos(); let cos = tip.angle_rad.cos();
let sin = tip.angle_rad.sin(); let sin = tip.angle_rad.sin();
for row in 0..rows { for row in 0..rows {
for col in 0..cols { for col in 0..cols {
let index = (row * cols + col) as usize; let index = (row * cols + col) as usize;
let [normalized, display_value] = let [normalized, display_value] = sample_pressure_at(
pressure.get(index).copied().unwrap_or([0.0, 0.0]); pressure,
tip_index * HAND_FINGER_SENSOR_CELLS + index,
index,
);
// Lay out a rows x cols matrix in fingertip-local pixel space. // Lay out a rows x cols matrix in fingertip-local pixel space.
let local_x = (col as f32 - cols as f32 / 2.0 + 0.5) / cols as f32 * tip.size_px[0]; let local_x = (col as f32 - cols as f32 / 2.0 + 0.5) / cols as f32 * tip.size_px[0];
@@ -1195,46 +1187,28 @@ fn build_hand_dot_instances(
} }
fn build_hand_palm_dot_instances( fn build_hand_palm_dot_instances(
rows: u32, _rows: u32,
cols: u32, _cols: u32,
image_width: f32, pressure: &[[f32; 2]],
image_height: f32,
pressure: &PressureFrame,
) -> Vec<GlyphInstance> { ) -> Vec<GlyphInstance> {
let chip_dot_count: usize = HAND_PALM_CHIPS let chip_dot_count: usize = UNFOLDED_SENSOR_CHIPS
.iter() .iter()
.map(|chip| (chip.rows * chip.cols) as usize) .map(|chip| (chip.rows * chip.cols) as usize)
.sum(); .sum();
debug_assert_eq!(chip_dot_count, UNFOLDED_SENSOR_COUNT);
let mut instances = Vec::with_capacity(chip_dot_count); let mut instances = Vec::with_capacity(chip_dot_count);
for chip in HAND_PALM_CHIPS { for (chip_index, chip) in UNFOLDED_SENSOR_CHIPS.iter().enumerate() {
let cos = chip.angle_rad.cos();
let sin = chip.angle_rad.sin();
// Leave a bevel around the chip so the matrix reads as embedded pixels.
let active_size = [chip.size_px[0] * 0.72, chip.size_px[1] * 0.76];
for row in 0..chip.rows { for row in 0..chip.rows {
for col in 0..chip.cols { for col in 0..chip.cols {
// Current live data is still the device pressure frame. Sample it by let index = unfolded_adc_sample_index(chip_index, row, col);
// proportion so the palm's 5x14 and 11x4 layouts get coherent values. let [normalized, display_value] = sample_pressure_at(pressure, index, index);
let source_row = resample_index(row, chip.rows, rows); let [x, y] = unfolded_cell_position(&chip, row, col);
let source_col = resample_index(col, chip.cols, cols);
let index = (source_row * cols + source_col) as usize;
let [normalized, display_value] =
pressure.get(index).copied().unwrap_or([0.0, 0.0]);
let local_x =
(col as f32 - chip.cols as f32 / 2.0 + 0.5) / chip.cols as f32 * active_size[0];
let local_y =
(row as f32 - chip.rows as f32 / 2.0 + 0.5) / chip.rows as f32 * active_size[1];
let x = chip.center_px[0] + local_x * cos - local_y * sin;
let y = chip.center_px[1] + local_x * sin + local_y * cos;
instances.push(GlyphInstance { instances.push(GlyphInstance {
world_position: [ world_position: [
x / image_width.max(1.0), x / UNFOLDED_CANVAS_SIZE[0],
y / image_height.max(1.0), y / UNFOLDED_CANVAS_SIZE[1],
0.0, 0.0,
1.0, 1.0,
], ],
@@ -1247,13 +1221,272 @@ fn build_hand_palm_dot_instances(
instances instances
} }
fn resample_index(index: u32, source_count: u32, target_count: u32) -> u32 { fn unfolded_adc_sample_index(chip_index: usize, row: u32, col: u32) -> usize {
if source_count <= 1 || target_count <= 1 { // Raw sample 0 is L0H0. The scan advances L first:
return 0; // L0H0, L1H0, …, L7H0, L0H1, …, L7H12.
// The PCB unfolds those channel pairs into the ten visual regions below.
let (l, h) = match chip_index {
0 => (5 - col, 12 - row), // top cap: L5…L2 × H12…H11
1 => (6, row), // left outer tip: L6 × H0…H2
2 => (7, folded_five_row_h(row)), // left folded wing
3 => (7, 10 - row), // left inner wing: L7 × H10…H3
4 => (6, 12 - row), // left inner spine: L6 × H12…H3
5 => (5 - col, 10 - row), // center: L5…L2 × H10…H0
6 => (1, row), // right outer tip: L1 × H0…H2
7 => (0, folded_five_row_h(row)), // right folded wing
8 => (0, 10 - row), // right inner wing: L0 × H10…H3
9 => (1, 12 - row), // right inner spine: L1 × H12…H3
_ => unreachable!("invalid unfolded PCB region"),
};
unfolded_lh_sample_index(l as usize, h as usize)
} }
let mapped = index as f32 / (source_count - 1) as f32 * (target_count - 1) as f32; fn folded_five_row_h(row: u32) -> u32 {
mapped.round().clamp(0.0, (target_count - 1) as f32) as u32 const PCB_H_ROUTE: [u32; 5] = [12, 11, 0, 1, 2];
PCB_H_ROUTE[row as usize]
}
fn unfolded_cell_position(chip: &HandPalmChip, row: u32, col: u32) -> [f32; 2] {
let cos = chip.angle_rad.cos();
let sin = chip.angle_rad.sin();
// Author every region on the same point grid. Using the last row as the
// vertical anchor keeps the stepped wing columns exactly bottom-aligned.
let bottom_center_y = chip.center_px[1] + chip.size_px[1] * 0.5 - 22.0;
let local_x = (col as f32 - chip.cols as f32 / 2.0 + 0.5) * UNFOLDED_CELL_SPACING_PX;
let y_from_bottom = (chip.rows.saturating_sub(row + 1)) as f32 * UNFOLDED_CELL_SPACING_PX;
let local_y = -y_from_bottom;
[
chip.center_px[0] + local_x * cos - local_y * sin,
bottom_center_y + local_x * sin + local_y * cos,
]
}
fn sample_pressure_at(pressure: &[[f32; 2]], index: usize, fallback_index: usize) -> [f32; 2] {
pressure
.get(index)
.or_else(|| pressure.get(fallback_index))
.copied()
.unwrap_or([0.0, 0.0])
}
#[cfg(test)]
mod unfolded_layout_tests {
use super::*;
use crate::matrix::UNFOLDED_SENSOR_SEGMENT_COUNTS;
#[test]
fn unfolded_layout_maps_every_lh_adc_channel_once() {
let mut seen = [false; UNFOLDED_SENSOR_COUNT];
for (chip_index, chip) in UNFOLDED_SENSOR_CHIPS.iter().enumerate() {
for row in 0..chip.rows {
for col in 0..chip.cols {
let index = unfolded_adc_sample_index(chip_index, row, col);
assert!(index < UNFOLDED_SENSOR_COUNT);
assert!(!seen[index], "duplicate ADC sample index {index}");
seen[index] = true;
}
}
}
assert!(seen.into_iter().all(|mapped| mapped));
assert_eq!(unfolded_lh_sample_index(0, 0), 0); // first raw sample
assert_eq!(unfolded_lh_sample_index(1, 0), 1); // L changes first
assert_eq!(unfolded_lh_sample_index(7, 0), 7);
assert_eq!(unfolded_lh_sample_index(0, 1), 8); // then H advances
assert_eq!(unfolded_adc_sample_index(7, 0, 0), 96); // L0H12
assert_eq!(unfolded_adc_sample_index(6, 0, 0), 1); // L1H0
assert_eq!(unfolded_lh_sample_index(7, 12), 103); // last rendered sample
}
#[test]
fn unfolded_layout_matches_annotated_adc_intersections() {
// Right folded/outer intersections: 1=L0H0, 2=L1H0,
// 9=L0H1, 10=L1H1, 17=L0H2, 18=L1H2.
assert_eq!(unfolded_adc_sample_index(7, 2, 0) + 1, 1);
assert_eq!(unfolded_adc_sample_index(6, 0, 0) + 1, 2);
assert_eq!(unfolded_adc_sample_index(7, 3, 0) + 1, 9);
assert_eq!(unfolded_adc_sample_index(6, 1, 0) + 1, 10);
assert_eq!(unfolded_adc_sample_index(7, 4, 0) + 1, 17);
assert_eq!(unfolded_adc_sample_index(6, 2, 0) + 1, 18);
// Right inner routes continue upward from H3.
assert_eq!(
(0..8)
.map(|row| unfolded_adc_sample_index(8, row, 0) + 1)
.collect::<Vec<_>>(),
vec![81, 73, 65, 57, 49, 41, 33, 25]
);
assert_eq!(
(0..10)
.map(|row| unfolded_adc_sample_index(9, row, 0) + 1)
.collect::<Vec<_>>(),
vec![98, 90, 82, 74, 66, 58, 50, 42, 34, 26]
);
// Left routes mirror the right side's PCB continuation.
assert_eq!(
(0..3)
.map(|row| unfolded_adc_sample_index(1, row, 0) + 1)
.collect::<Vec<_>>(),
vec![7, 15, 23]
);
assert_eq!(
(0..5)
.map(|row| unfolded_adc_sample_index(2, row, 0) + 1)
.collect::<Vec<_>>(),
vec![104, 96, 8, 16, 24]
);
assert_eq!(
(0..8)
.map(|row| unfolded_adc_sample_index(3, row, 0) + 1)
.collect::<Vec<_>>(),
vec![88, 80, 72, 64, 56, 48, 40, 32]
);
assert_eq!(
(0..10)
.map(|row| unfolded_adc_sample_index(4, row, 0) + 1)
.collect::<Vec<_>>(),
vec![103, 95, 87, 79, 71, 63, 55, 47, 39, 31]
);
// Center rows H0…H7, each ordered L5, L4, L3, L2.
for h in 0..=7 {
let row = 10 - h;
let expected = (2..=5)
.rev()
.map(|l| unfolded_lh_sample_index(l, h as usize) + 1)
.collect::<Vec<_>>();
let rendered = (0..4)
.map(|col| unfolded_adc_sample_index(5, row, col) + 1)
.collect::<Vec<_>>();
assert_eq!(rendered, expected);
}
}
#[test]
fn annotated_folded_points_share_the_same_physical_rows() {
let same_y = |left_chip: usize, left_row: u32, right_chip: usize, right_row: u32| {
let left = unfolded_cell_position(&UNFOLDED_SENSOR_CHIPS[left_chip], left_row, 0)[1];
let right = unfolded_cell_position(&UNFOLDED_SENSOR_CHIPS[right_chip], right_row, 0)[1];
assert!((left - right).abs() < 0.01);
};
// Left outer pairs: 7/8, 15/16, 23/24.
for row in 0..3 {
same_y(1, row, 2, row + 2);
}
// Right outer pairs: 2/1, 10/9, 18/17.
for row in 0..3 {
same_y(6, row, 7, row + 2);
}
// Inner continuations align by H despite belonging to different strips.
for row in 0..8 {
same_y(3, row, 4, row + 2);
same_y(8, row, 9, row + 2);
}
}
#[test]
fn unfolded_layout_matches_requested_shapes() {
let shapes = UNFOLDED_SENSOR_CHIPS.map(|chip| (chip.rows, chip.cols));
let sample_counts = UNFOLDED_SENSOR_CHIPS.map(|chip| (chip.rows * chip.cols) as usize);
assert_eq!(
shapes,
[
(2, 4),
(3, 1),
(5, 1),
(8, 1),
(10, 1),
(11, 4),
(3, 1),
(5, 1),
(8, 1),
(10, 1),
]
);
assert_eq!(sample_counts, UNFOLDED_SENSOR_SEGMENT_COUNTS);
}
#[test]
fn unfolded_wings_are_mirrored_around_center() {
let center_x = UNFOLDED_SENSOR_CHIPS[5].center_px[0];
for (left, right) in UNFOLDED_SENSOR_CHIPS[1..5]
.iter()
.zip(UNFOLDED_SENSOR_CHIPS[6..10].iter())
{
assert_eq!((left.rows, left.cols), (right.rows, right.cols));
assert_eq!(left.center_px[1], right.center_px[1]);
assert!(
((center_x - left.center_px[0]) - (right.center_px[0] - center_x)).abs() < 0.01
);
}
}
#[test]
fn unfolded_layout_fits_its_canvas() {
for chip in UNFOLDED_SENSOR_CHIPS {
let half_width = chip.size_px[0] * 0.5;
let half_height = chip.size_px[1] * 0.5;
assert!(chip.center_px[0] - half_width >= 0.0);
assert!(chip.center_px[0] + half_width <= UNFOLDED_CANVAS_SIZE[0]);
assert!(chip.center_px[1] - half_height >= 0.0);
assert!(chip.center_px[1] + half_height <= UNFOLDED_CANVAS_SIZE[1]);
}
}
#[test]
fn unfolded_wing_rows_share_one_bottom_baseline() {
let wing_indices = [1usize, 2, 3, 4, 6, 7, 8, 9];
let expected_y = unfolded_cell_position(
&UNFOLDED_SENSOR_CHIPS[wing_indices[0]],
UNFOLDED_SENSOR_CHIPS[wing_indices[0]].rows - 1,
0,
)[1];
for index in wing_indices {
let chip = &UNFOLDED_SENSOR_CHIPS[index];
let bottom_y = unfolded_cell_position(chip, chip.rows - 1, 0)[1];
assert!((bottom_y - expected_y).abs() < 0.01);
}
}
#[test]
fn unfolded_wings_are_compact_with_more_space_at_center() {
let left_x = UNFOLDED_SENSOR_CHIPS[1..5]
.iter()
.map(|chip| chip.center_px[0])
.collect::<Vec<_>>();
let wing_gap = left_x[1] - left_x[0];
assert!((wing_gap - 48.0).abs() < 0.01);
assert!(
left_x
.windows(2)
.all(|pair| (pair[1] - pair[0] - wing_gap).abs() < 0.01)
);
let center_left_x = unfolded_cell_position(&UNFOLDED_SENSOR_CHIPS[5], 0, 0)[0];
let left_inner_x = unfolded_cell_position(&UNFOLDED_SENSOR_CHIPS[4], 0, 0)[0];
assert!(center_left_x - left_inner_x > wing_gap);
}
#[test]
fn unfolded_top_block_sits_close_to_center_block() {
let top = &UNFOLDED_SENSOR_CHIPS[0];
let center = &UNFOLDED_SENSOR_CHIPS[5];
let top_bottom_y = unfolded_cell_position(top, top.rows - 1, 0)[1];
let center_top_y = unfolded_cell_position(center, 0, 0)[1];
assert!(center_top_y > top_bottom_y);
assert!(center_top_y - top_bottom_y < UNFOLDED_CELL_SPACING_PX * 2.0);
}
} }
fn build_glyph_instances( fn build_glyph_instances(

View File

@@ -457,6 +457,7 @@ fn create_color_material(
)) ))
} }
#[allow(clippy::too_many_arguments)]
fn create_material( fn create_material(
device: &wgpu::Device, device: &wgpu::Device,
texture_bind_group_layout: &wgpu::BindGroupLayout, texture_bind_group_layout: &wgpu::BindGroupLayout,
@@ -702,6 +703,7 @@ fn append_gltf_node_meshes(
Ok(()) Ok(())
} }
#[allow(clippy::too_many_arguments)]
fn append_gltf_primitive_mesh( fn append_gltf_primitive_mesh(
mesh_name: &str, mesh_name: &str,
primitive_index: usize, primitive_index: usize,
@@ -969,7 +971,7 @@ fn rgba_from_chunks(
read_component: fn(&[u8]) -> u8, read_component: fn(&[u8]) -> u8,
) -> anyhow::Result<Vec<u8>> { ) -> anyhow::Result<Vec<u8>> {
let pixel_width = channels * component_width; let pixel_width = channels * component_width;
if pixel_width == 0 || pixels.len() % pixel_width != 0 { if pixel_width == 0 || !pixels.len().is_multiple_of(pixel_width) {
bail!("invalid glTF image byte length for {channels} channels"); bail!("invalid glTF image byte length for {channels} channels");
} }

View File

@@ -0,0 +1,404 @@
use crate::serial_core::codec::Codec;
use crate::serial_core::error::CodecError;
use crate::serial_core::utils::{calc_crc8_itu, elapsed_millis};
use std::time::Instant;
const FRAME_HEADER: [u8; 2] = [0x55, 0xAA];
const RESPONSE_DATA_CMD: u8 = 0x81;
const RESPONSE_DATA_LEGACY_FIXED_LENGTH: usize = 16;
const RESPONSE_DATA_LEGACY_PREFIX_LENGTH: usize = 19;
const RESPONSE_DATA_TEMP_FIXED_LENGTH: usize = 20;
const RESPONSE_DATA_TEMP_PREFIX_LENGTH: usize = 23;
const MIN_FRAME_LENGTH: usize = 7;
const DEFAULT_MAX_FRAME_LENGTH: usize = 64 * 1024;
#[derive(Debug, Clone, Copy)]
struct HandGatewayResponseLayout {
fixed_length: usize,
prefix_length: usize,
timestamp_len: usize,
config_offset: usize,
valid_config_offset: usize,
block_count_offset: usize,
}
const TEMP_RESPONSE_LAYOUT: HandGatewayResponseLayout = HandGatewayResponseLayout {
fixed_length: RESPONSE_DATA_TEMP_FIXED_LENGTH,
prefix_length: RESPONSE_DATA_TEMP_PREFIX_LENGTH,
timestamp_len: 8,
config_offset: 14,
valid_config_offset: 18,
block_count_offset: 22,
};
const LEGACY_RESPONSE_LAYOUT: HandGatewayResponseLayout = HandGatewayResponseLayout {
fixed_length: RESPONSE_DATA_LEGACY_FIXED_LENGTH,
prefix_length: RESPONSE_DATA_LEGACY_PREFIX_LENGTH,
timestamp_len: 4,
config_offset: 10,
valid_config_offset: 14,
block_count_offset: 18,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandGatewayNodeConfig {
pub config_mask: u32,
pub sample_count: usize,
}
impl HandGatewayNodeConfig {
pub const fn new(config_mask: u32, sample_count: usize) -> Self {
Self {
config_mask,
sample_count,
}
}
fn payload_len(&self, bytes_per_sample: usize) -> Option<usize> {
self.sample_count.checked_mul(bytes_per_sample)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandGatewayConfig {
pub protocol_version: u8,
pub bytes_per_sample: usize,
pub max_frame_length: usize,
pub nodes: Vec<HandGatewayNodeConfig>,
}
impl HandGatewayConfig {
pub fn new(nodes: Vec<HandGatewayNodeConfig>) -> Self {
Self {
protocol_version: 0x01,
bytes_per_sample: 2,
max_frame_length: DEFAULT_MAX_FRAME_LENGTH,
nodes,
}
}
fn validate(&self) -> Result<(), CodecError> {
if self.bytes_per_sample == 0 || self.max_frame_length < MIN_FRAME_LENGTH {
return Err(CodecError::InvalidLength);
}
let mut used_masks = 0u32;
for node in &self.nodes {
if node.config_mask == 0
|| node.config_mask.count_ones() != 1
|| used_masks & node.config_mask != 0
|| node.payload_len(self.bytes_per_sample).is_none()
{
return Err(CodecError::InvalidLength);
}
used_masks |= node.config_mask;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandGatewayFrameNode {
pub config_mask: u32,
pub valid: bool,
pub payload: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandGatewayDataRepFrame {
pub timestamp: u64,
pub config: u32,
pub valid_config: u32,
pub block_count: u8,
pub raw: Vec<u8>,
pub nodes: Vec<HandGatewayFrameNode>,
pub dts_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HandGatewayFrame {
DataRep(HandGatewayDataRepFrame),
}
pub struct HandGatewayCodec {
buffer: Vec<u8>,
config: HandGatewayConfig,
}
impl HandGatewayCodec {
pub fn new(node_sample_counts: &[u16]) -> Self {
let nodes = node_sample_counts
.iter()
.enumerate()
.map(|(index, &sample_count)| {
HandGatewayNodeConfig::new(
1u32.checked_shl(index as u32).unwrap_or(0),
sample_count as usize,
)
})
.collect();
Self {
buffer: Vec::new(),
config: HandGatewayConfig::new(nodes),
}
}
pub fn parse_node_payload(data: &[u8]) -> Result<Vec<u16>, CodecError> {
if !data.len().is_multiple_of(2) {
return Err(CodecError::InvalidLength);
}
Ok(data
.chunks_exact(2)
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect())
}
fn retain_possible_header_prefix(&mut self) {
if self.buffer.last() == Some(&FRAME_HEADER[0]) {
self.buffer.drain(..self.buffer.len() - 1);
} else {
self.buffer.clear();
}
}
fn parse_data_response_with_layout(
&self,
frame: &[u8],
length: u16,
session_started_at: Instant,
layout: HandGatewayResponseLayout,
) -> Result<HandGatewayFrame, CodecError> {
let payload_len = usize::from(length)
.checked_sub(layout.fixed_length)
.ok_or(CodecError::InvalidLength)?;
let payload_end = layout
.prefix_length
.checked_add(payload_len)
.ok_or(CodecError::PayloadTooLarge)?;
if payload_end + 1 != frame.len() {
return Err(CodecError::InvalidLength);
}
let version = frame[5];
if version != self.config.protocol_version {
return Err(CodecError::InvalidFrameType);
}
let timestamp = match layout.timestamp_len {
4 => u32::from_le_bytes(frame[6..10].try_into().unwrap()) as u64,
8 => u64::from_le_bytes(frame[6..14].try_into().unwrap()),
_ => return Err(CodecError::InvalidLength),
};
let config = u32::from_le_bytes(
frame[layout.config_offset..layout.config_offset + 4]
.try_into()
.unwrap(),
);
let valid_config = u32::from_le_bytes(
frame[layout.valid_config_offset..layout.valid_config_offset + 4]
.try_into()
.unwrap(),
);
let block_count = frame[layout.block_count_offset];
let active_nodes = self
.config
.nodes
.iter()
.filter(|node| config & node.config_mask != 0)
.collect::<Vec<_>>();
if active_nodes.len() != usize::from(block_count) {
return Err(CodecError::InvalidLength);
}
let expected_payload_len = active_nodes.iter().try_fold(0usize, |total, node| {
let node_len = node
.payload_len(self.config.bytes_per_sample)
.ok_or(CodecError::PayloadTooLarge)?;
total
.checked_add(node_len)
.ok_or(CodecError::PayloadTooLarge)
})?;
if expected_payload_len != payload_len {
return Err(CodecError::InvalidLength);
}
let mut cursor = layout.prefix_length;
let mut nodes = Vec::with_capacity(active_nodes.len());
for node in active_nodes {
let node_len = node
.payload_len(self.config.bytes_per_sample)
.ok_or(CodecError::PayloadTooLarge)?;
let next = cursor + node_len;
nodes.push(HandGatewayFrameNode {
config_mask: node.config_mask,
valid: valid_config & node.config_mask != 0,
payload: frame[cursor..next].to_vec(),
});
cursor = next;
}
Ok(HandGatewayFrame::DataRep(HandGatewayDataRepFrame {
timestamp,
config,
valid_config,
block_count,
raw: frame.to_vec(),
nodes,
dts_ms: elapsed_millis(session_started_at),
}))
}
fn parse_data_response(
&self,
frame: &[u8],
length: u16,
session_started_at: Instant,
) -> Result<HandGatewayFrame, CodecError> {
self.parse_data_response_with_layout(
frame,
length,
session_started_at,
TEMP_RESPONSE_LAYOUT,
)
.or_else(|_| {
self.parse_data_response_with_layout(
frame,
length,
session_started_at,
LEGACY_RESPONSE_LAYOUT,
)
})
}
}
impl Codec<HandGatewayFrame> for HandGatewayCodec {
fn decode(
&mut self,
input: &[u8],
session_started_at: Instant,
) -> Result<Vec<HandGatewayFrame>, CodecError> {
self.config.validate()?;
self.buffer.extend_from_slice(input);
let mut frames = Vec::new();
loop {
let Some(header_pos) = self
.buffer
.windows(FRAME_HEADER.len())
.position(|window| window == FRAME_HEADER)
else {
self.retain_possible_header_prefix();
break;
};
if header_pos > 0 {
self.buffer.drain(..header_pos);
}
if self.buffer.len() < 4 {
break;
}
let length = u16::from_le_bytes([self.buffer[2], self.buffer[3]]);
let frame_len = usize::from(length)
.checked_add(4)
.ok_or(CodecError::PayloadTooLarge)?;
if frame_len < MIN_FRAME_LENGTH || frame_len > self.config.max_frame_length {
log::debug!("invalid hand gateway frame length: {frame_len}");
self.buffer.drain(..1);
continue;
}
if self.buffer.len() < frame_len {
break;
}
let expected_checksum = calc_crc8_itu(&self.buffer[..frame_len - 1]);
let received_checksum = self.buffer[frame_len - 1];
if expected_checksum != received_checksum {
log::debug!(
"hand gateway checksum mismatch: expected {expected_checksum:02X}, got {received_checksum:02X}"
);
self.buffer.drain(..1);
continue;
}
if self.buffer[4] == RESPONSE_DATA_CMD {
match self.parse_data_response(
&self.buffer[..frame_len],
length,
session_started_at,
) {
Ok(frame) => frames.push(frame),
Err(error) => log::debug!("invalid hand gateway data response: {error}"),
}
}
self.buffer.drain(..frame_len);
}
Ok(frames)
}
fn encode(&self, frame: &HandGatewayFrame) -> Result<Vec<u8>, CodecError> {
let _ = frame;
Err(CodecError::InvalidFrameType)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn data_response(config: u32, valid_config: u32, payload: &[u8]) -> Vec<u8> {
let block_count = config.count_ones() as u8;
let length = (RESPONSE_DATA_LEGACY_FIXED_LENGTH + payload.len()) as u16;
let mut frame = Vec::new();
frame.extend_from_slice(&FRAME_HEADER);
frame.extend_from_slice(&length.to_le_bytes());
frame.push(RESPONSE_DATA_CMD);
frame.push(0x01);
frame.extend_from_slice(&0x0102_0304u32.to_le_bytes());
frame.extend_from_slice(&config.to_le_bytes());
frame.extend_from_slice(&valid_config.to_le_bytes());
frame.push(block_count);
frame.extend_from_slice(payload);
let checksum = calc_crc8_itu(&frame);
frame.push(checksum);
frame
}
#[test]
fn decodes_data_response_frame() {
let payload = [0x10, 0x00, 0x34, 0x12, 0x08, 0x00];
let bytes = data_response(0b11, 0b01, &payload);
let frames = HandGatewayCodec::new(&[2, 1])
.decode(&bytes, Instant::now())
.unwrap();
assert_eq!(frames.len(), 1);
let HandGatewayFrame::DataRep(frame) = &frames[0];
assert_eq!(frame.timestamp, 0x0102_0304);
assert_eq!(frame.config, 0b11);
assert_eq!(frame.valid_config, 0b01);
assert_eq!(frame.block_count, 2);
assert_eq!(frame.raw, bytes);
assert_eq!(frame.nodes.len(), 2);
assert_eq!(
HandGatewayCodec::parse_node_payload(&frame.nodes[0].payload).unwrap(),
vec![16, 0x1234]
);
assert_eq!(
HandGatewayCodec::parse_node_payload(&frame.nodes[1].payload).unwrap(),
vec![8]
);
}
}

View File

@@ -1 +1,2 @@
pub mod hand_gateway;
pub mod tactile_a; pub mod tactile_a;

View File

@@ -31,7 +31,7 @@ impl TactileACodec {
} }
pub fn parse_data_frame(data: &[u8]) -> Result<Vec<i32>, CodecError> { pub fn parse_data_frame(data: &[u8]) -> Result<Vec<i32>, CodecError> {
if data.len() % 2 != 0 { if !data.len().is_multiple_of(2) {
return Err(CodecError::InvalidLength); return Err(CodecError::InvalidLength);
} }
@@ -132,7 +132,7 @@ impl Codec<TactileAFrame> for TactileACodec {
let need_check_data = self.buffer[0..14 + except_data_len].to_vec(); let need_check_data = self.buffer[0..14 + except_data_len].to_vec();
let payload = self.buffer[14..14 + except_data_len].to_vec(); let payload = self.buffer[14..14 + except_data_len].to_vec();
let crc8_itu_alg = crc::Crc::<u8>::new(&crc::CRC_8_I_432_1); let crc8_itu_alg = crc::Crc::<u8>::new(&crc::CRC_8_I_432_1);
let checksum = crc8_itu_alg.checksum(&need_check_data.as_slice()); let checksum = crc8_itu_alg.checksum(need_check_data.as_slice());
if self.buffer[frame_length - 1] != checksum { if self.buffer[frame_length - 1] != checksum {
log::debug!( log::debug!(
"checksum mismatch: expected {:02X}, got {:02X}, frame_len={}", "checksum mismatch: expected {:02X}, got {:02X}, frame_len={}",
@@ -188,3 +188,61 @@ impl Codec<TactileAFrame> for TactileACodec {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use crate::serial_core::codec::Codec;
use std::time::Instant;
const FINGER_3D_ROWS: usize = 12;
const FINGER_3D_COLS: usize = 9;
const FINGER_3D_DATA_LEN: usize = FINGER_3D_ROWS * FINGER_3D_COLS * 2;
#[test]
fn finger_3d_request_asks_for_108_samples() {
let codec = TactileACodec::new(FINGER_3D_COLS, FINGER_3D_ROWS);
let frame = TactileACodec::build_req_frame(FINGER_3D_COLS, FINGER_3D_ROWS).unwrap();
let encoded = codec.encode(&frame).unwrap();
assert_eq!(
u16::from_le_bytes([encoded[11], encoded[12]]) as usize,
FINGER_3D_DATA_LEN
);
}
#[test]
fn finger_3d_decode_accepts_108_samples_across_reads() {
let mut codec = TactileACodec::new(FINGER_3D_COLS, FINGER_3D_ROWS);
let payload = (0..FINGER_3D_ROWS * FINGER_3D_COLS)
.flat_map(|value| (value as u16).to_le_bytes())
.collect::<Vec<_>>();
let mut response = Vec::new();
response.extend_from_slice(&[0xAA, 0x55]);
response.extend_from_slice(&9_u16.to_le_bytes());
response.extend_from_slice(&[0x34, 0x00, 0xFB]);
response.extend_from_slice(&7168_u32.to_le_bytes());
response.extend_from_slice(&(FINGER_3D_DATA_LEN as u16).to_le_bytes());
response.push(0);
response.extend_from_slice(&payload);
response.push(calc_crc8_itu(&response));
let split = response.len() / 2;
assert!(
codec
.decode(&response[..split], Instant::now())
.unwrap()
.is_empty()
);
let frames = codec.decode(&response[split..], Instant::now()).unwrap();
let TactileAFrame::Rep(rep) = &frames[0] else {
panic!("expected response frame");
};
assert_eq!(rep.payload.len(), FINGER_3D_DATA_LEN);
assert_eq!(
TactileACodec::parse_data_frame(&rep.payload).unwrap().len(),
108
);
}
}

View File

@@ -2,5 +2,6 @@ pub mod codec;
pub mod codecs; pub mod codecs;
pub mod error; pub mod error;
pub mod frame; pub mod frame;
pub mod multi_dim_force;
pub mod serial; pub mod serial;
pub mod utils; pub mod utils;

View File

@@ -0,0 +1,234 @@
const SENSOR_ROWS: usize = 12;
const SENSOR_COLS: usize = 7;
const SENSOR_COUNT: usize = SENSOR_ROWS * SENSOR_COLS;
const TOTAL_PRESSURE_LOW_THRESHOLD: f32 = 500.0;
const COP_STABILITY_FRAMES_REQUIRED: usize = 15;
const POST_INIT_WINDOW_CNT: usize = 100;
const POST_INIT_STABLE_CNT: usize = 50;
const POST_INIT_STABLE_THRESH: f32 = 0.1;
#[derive(Debug, Clone, Copy)]
pub struct PztSpatialAnalysis {
pub angle_deg: f32,
pub magnitude: f32,
pub planar_x: f32,
pub planar_y: f32,
}
pub struct PztProcessor {
first_frame: Option<Vec<f32>>,
first_contact_cop_x: Option<f32>,
first_contact_cop_y: Option<f32>,
contact_initialized: bool,
total_pressure_low_counter: usize,
cop_init_x_buf: Vec<f32>,
cop_init_y_buf: Vec<f32>,
post_init_frame_cnt: usize,
post_stable_cnt: usize,
post_refined_flag: bool,
post_cand_x: Option<f32>,
post_cand_y: Option<f32>,
}
impl PztProcessor {
pub fn new() -> Self {
Self {
first_frame: None,
first_contact_cop_x: None,
first_contact_cop_y: None,
contact_initialized: false,
total_pressure_low_counter: 0,
cop_init_x_buf: Vec::with_capacity(COP_STABILITY_FRAMES_REQUIRED),
cop_init_y_buf: Vec::with_capacity(COP_STABILITY_FRAMES_REQUIRED),
post_init_frame_cnt: 0,
post_stable_cnt: 0,
post_refined_flag: false,
post_cand_x: None,
post_cand_y: None,
}
}
fn subtract_baseline(&mut self, current_frame: &[f32]) -> Vec<f32> {
if self.first_frame.is_none() {
self.first_frame = Some(current_frame.to_vec());
}
let baseline = self.first_frame.as_ref().unwrap();
current_frame
.iter()
.zip(baseline.iter())
.map(|(current, baseline)| (current - baseline).max(0.0))
.collect()
}
fn reset_cop_state(&mut self) {
self.first_contact_cop_x = None;
self.first_contact_cop_y = None;
self.contact_initialized = false;
self.total_pressure_low_counter = 0;
self.cop_init_x_buf.clear();
self.cop_init_y_buf.clear();
self.post_init_frame_cnt = 0;
self.post_stable_cnt = 0;
self.post_refined_flag = false;
self.post_cand_x = None;
self.post_cand_y = None;
}
fn compute_median(sorted: &[f32]) -> f32 {
let n = sorted.len();
if n == 0 {
return 0.0;
}
if n.is_multiple_of(2) {
(sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
} else {
sorted[n / 2]
}
}
fn compute_pressure_direction(&mut self, frame: &[f32]) -> (f32, f32) {
let total_pressure = frame.iter().sum::<f32>();
if total_pressure < TOTAL_PRESSURE_LOW_THRESHOLD {
self.total_pressure_low_counter += 1;
} else {
self.total_pressure_low_counter = 0;
}
if self.total_pressure_low_counter >= COP_STABILITY_FRAMES_REQUIRED {
self.reset_cop_state();
return (0.0, 0.0);
}
if total_pressure == 0.0 {
return (0.0, 0.0);
}
let mut sum_x = 0.0;
let mut sum_y = 0.0;
for row in 0..SENSOR_ROWS {
for col in 0..SENSOR_COLS {
let value = frame[row * SENSOR_COLS + col];
sum_x += value * col as f32;
sum_y += value * row as f32;
}
}
let cop_x = sum_x / total_pressure;
let cop_y = sum_y / total_pressure;
if !self.contact_initialized {
self.cop_init_x_buf.push(cop_x);
self.cop_init_y_buf.push(cop_y);
if self.cop_init_x_buf.len() >= COP_STABILITY_FRAMES_REQUIRED {
let mut xs = self.cop_init_x_buf.clone();
xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mut ys = self.cop_init_y_buf.clone();
ys.sort_by(|a, b| a.partial_cmp(b).unwrap());
self.first_contact_cop_x = Some(Self::compute_median(&xs));
self.first_contact_cop_y = Some(Self::compute_median(&ys));
self.contact_initialized = true;
self.cop_init_x_buf.clear();
self.cop_init_y_buf.clear();
}
return (0.0, 0.0);
}
self.post_init_frame_cnt += 1;
if !self.post_refined_flag && self.post_init_frame_cnt <= POST_INIT_WINDOW_CNT {
if let (Some(cx), Some(cy)) = (self.post_cand_x, self.post_cand_y) {
let dist = ((cop_x - cx).powi(2) + (cop_y - cy).powi(2)).sqrt();
if dist <= POST_INIT_STABLE_THRESH {
self.post_stable_cnt += 1;
} else {
self.post_cand_x = Some(cop_x);
self.post_cand_y = Some(cop_y);
self.post_stable_cnt = 1;
}
} else {
self.post_cand_x = Some(cop_x);
self.post_cand_y = Some(cop_y);
self.post_stable_cnt = 1;
}
if self.post_stable_cnt >= POST_INIT_STABLE_CNT {
self.first_contact_cop_x = self.post_cand_x;
self.first_contact_cop_y = self.post_cand_y;
self.post_refined_flag = true;
}
} else {
self.post_refined_flag = true;
}
let base_x = self.first_contact_cop_x.unwrap_or(cop_x);
let base_y = self.first_contact_cop_y.unwrap_or(cop_y);
let delta_x = cop_x - base_x;
let delta_y = base_y - cop_y;
(delta_x, delta_y)
}
fn compute_vector_angle(x: f32, y: f32) -> (f32, f32) {
let epsilon = 1e-8f32;
let magnitude = (x * x + y * y).sqrt();
let mut angle = y.atan2(x + epsilon).to_degrees();
if angle < 0.0 {
angle += 360.0;
}
(angle, magnitude)
}
pub fn get_pzt_analysis(
&mut self,
adc_data: &[f32],
) -> Result<PztSpatialAnalysis, &'static str> {
if adc_data.len() != SENSOR_COUNT {
return Err("ADC data length must be 84");
}
let baseline = self.subtract_baseline(adc_data);
let (dx, dy) = self.compute_pressure_direction(&baseline);
let planar_x = dx;
let planar_y = -dy;
let (angle_deg, magnitude) = Self::compute_vector_angle(planar_x, planar_y);
Ok(PztSpatialAnalysis {
angle_deg,
magnitude,
planar_x,
planar_y,
})
}
pub fn get_pzt_angle(&mut self, adc_data: &[f32]) -> Result<f32, &'static str> {
Ok(self.get_pzt_analysis(adc_data)?.angle_deg)
}
pub fn reset_baseline(&mut self) {
self.first_frame = None;
self.reset_cop_state();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn idle_frame_returns_zero() {
let mut processor = PztProcessor::new();
let frame = [0.0f32; SENSOR_COUNT];
let analysis = processor.get_pzt_analysis(&frame).unwrap();
assert_eq!(analysis.magnitude, 0.0);
assert_eq!(analysis.angle_deg, 0.0);
}
}

View File

@@ -1,11 +1,14 @@
use crate::recording::Recorder;
use crate::serial_core::codec::Codec; use crate::serial_core::codec::Codec;
use crate::serial_core::codecs::hand_gateway::{HandGatewayCodec, HandGatewayFrame};
use crate::serial_core::codecs::tactile_a::TactileACodec; use crate::serial_core::codecs::tactile_a::TactileACodec;
use crate::serial_core::frame::TactileAFrame; use crate::serial_core::frame::TactileAFrame;
use crossbeam_channel::{Receiver, Sender}; use crossbeam_channel::{Receiver, Sender};
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
const POLL_INTERVAL_MS: u64 = 10; const POLL_INTERVAL_MS: u64 = 5;
const DEFAULT_HAND_GATEWAY_NODE_SAMPLE_COUNTS: &[u16] = &[84, 84, 84, 84, 84, 70, 44];
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct SerialIoStats { pub struct SerialIoStats {
@@ -13,15 +16,43 @@ pub struct SerialIoStats {
pub tx_bytes: u64, pub tx_bytes: u64,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SerialProtocol {
TactileA,
HandGateway,
}
/// Runs the serial polling loop on the calling (background) thread. /// Runs the serial polling loop on the calling (background) thread.
/// Sends decoded pressure matrix data (Vec<i32>) to the output channel. /// Sends decoded pressure matrix data (Vec<i32>) to the output channel.
#[allow(clippy::too_many_arguments)]
pub fn run_serial_loop( pub fn run_serial_loop(
port: &mut dyn ReadWrite,
rows: usize,
cols: usize,
protocol: SerialProtocol,
cancel_rx: &Receiver<()>,
sample_tx: &Sender<Vec<i32>>,
stats_tx: Option<&Sender<SerialIoStats>>,
recorder: Option<&Recorder>,
) {
match protocol {
SerialProtocol::TactileA => {
run_tactile_a_loop(port, rows, cols, cancel_rx, sample_tx, stats_tx, recorder)
}
SerialProtocol::HandGateway => {
run_hand_gateway_loop(port, cancel_rx, sample_tx, stats_tx, recorder)
}
}
}
fn run_tactile_a_loop(
port: &mut dyn ReadWrite, port: &mut dyn ReadWrite,
rows: usize, rows: usize,
cols: usize, cols: usize,
cancel_rx: &Receiver<()>, cancel_rx: &Receiver<()>,
sample_tx: &Sender<Vec<i32>>, sample_tx: &Sender<Vec<i32>>,
stats_tx: Option<&Sender<SerialIoStats>>, stats_tx: Option<&Sender<SerialIoStats>>,
recorder: Option<&Recorder>,
) { ) {
let session_started_at = Instant::now(); let session_started_at = Instant::now();
let mut codec = TactileACodec::new(cols, rows); let mut codec = TactileACodec::new(cols, rows);
@@ -43,12 +74,12 @@ pub fn run_serial_loop(
} }
// Send poll request // Send poll request
if let Ok(req_bytes) = codec.encode(&req_frame) { if let Ok(req_bytes) = codec.encode(&req_frame)
if port.write_all(&req_bytes).is_ok() { && port.write_all(&req_bytes).is_ok()
{
io_stats.tx_bytes += req_bytes.len() as u64; io_stats.tx_bytes += req_bytes.len() as u64;
publish_stats(stats_tx, io_stats); publish_stats(stats_tx, io_stats);
} }
}
// Read response with poll interval // Read response with poll interval
let deadline = Instant::now() + poll_interval; let deadline = Instant::now() + poll_interval;
@@ -63,10 +94,15 @@ pub fn run_serial_loop(
publish_stats(stats_tx, io_stats); publish_stats(stats_tx, io_stats);
if let Ok(frames) = codec.decode(&buffer[..n], session_started_at) { if let Ok(frames) = codec.decode(&buffer[..n], session_started_at) {
for frame in frames { for frame in frames {
if let TactileAFrame::Rep(rep) = frame { if let TactileAFrame::Rep(rep) = frame
if let Ok(vals) = TactileACodec::parse_data_frame(&rep.payload) { && let Ok(vals) = TactileACodec::parse_data_frame(&rep.payload)
let _ = sample_tx.try_send(vals); {
if let Some(recorder) = recorder {
let pressures: Vec<u32> =
vals.iter().map(|v| (*v).max(0) as u32).collect();
recorder.add_frame(&pressures);
} }
let _ = sample_tx.try_send(vals);
} }
} }
} }
@@ -86,6 +122,103 @@ pub fn run_serial_loop(
} }
} }
fn run_hand_gateway_loop(
port: &mut dyn ReadWrite,
cancel_rx: &Receiver<()>,
sample_tx: &Sender<Vec<i32>>,
stats_tx: Option<&Sender<SerialIoStats>>,
recorder: Option<&Recorder>,
) {
let session_started_at = Instant::now();
let mut codec = HandGatewayCodec::new(DEFAULT_HAND_GATEWAY_NODE_SAMPLE_COUNTS);
let mut buffer = [0u8; 1024];
let poll_interval = Duration::from_millis(POLL_INTERVAL_MS);
let mut io_stats = SerialIoStats::default();
loop {
if cancel_rx.try_recv().is_ok() {
break;
}
let deadline = Instant::now() + poll_interval;
loop {
if Instant::now() >= deadline {
break;
}
match port.read(&mut buffer) {
Ok(n) if n > 0 => {
io_stats.rx_bytes += n as u64;
publish_stats(stats_tx, io_stats);
if let Ok(frames) = codec.decode(&buffer[..n], session_started_at) {
for frame in frames {
let HandGatewayFrame::DataRep(rep) = frame;
// println!(
// "[hand-packet-raw] bytes={} timestamp_us={} config=0x{:08X} valid_config=0x{:08X} block_count={} payload_bytes={} raw={}",
// rep.raw.len(),
// rep.timestamp,
// rep.config,
// rep.valid_config,
// rep.block_count,
// rep.nodes
// .iter()
// .map(|node| node.payload.len())
// .sum::<usize>(),
// format_hex_bytes(&rep.raw)
// );
let mut vals = Vec::new();
let mut parse_ok = true;
for node in &rep.nodes {
match HandGatewayCodec::parse_node_payload(&node.payload) {
Ok(node_values) => {
vals.extend(node_values.into_iter().map(|raw| {
let raw = raw as i32;
if raw < 15 { 0 } else { raw }
}));
}
Err(err) => {
parse_ok = false;
eprintln!("[hand-packet-values] parse error: {err}");
break;
}
}
}
if parse_ok {
// println!("[hand-packet-values] samples={}", vals.len());
if let Some(recorder) = recorder {
let pressures: Vec<u32> =
vals.iter().map(|v| (*v).max(0) as u32).collect();
recorder.add_frame(&pressures);
}
let _ = sample_tx.try_send(vals);
}
}
}
}
Ok(_) => {
std::thread::sleep(Duration::from_millis(1));
}
Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut => {
continue;
}
Err(e) => {
eprintln!("[serial] hand gateway read error: {e}");
return;
}
}
}
}
}
fn format_hex_bytes(bytes: &[u8]) -> String {
bytes
.iter()
.map(|byte| format!("{byte:02X}"))
.collect::<Vec<_>>()
.join(" ")
}
fn publish_stats(stats_tx: Option<&Sender<SerialIoStats>>, stats: SerialIoStats) { fn publish_stats(stats_tx: Option<&Sender<SerialIoStats>>, stats: SerialIoStats) {
if let Some(tx) = stats_tx { if let Some(tx) = stats_tx {
let _ = tx.try_send(stats); let _ = tx.try_send(stats);

View File

@@ -26,13 +26,17 @@ pub fn calc_crc8_itu(c: &[u8]) -> u8 {
crc8_itu_alg.checksum(c) crc8_itu_alg.checksum(c)
} }
pub fn calc_crc8_itu_xor55(c: &[u8]) -> u8 {
calc_crc8_itu(c) ^ 0x55
}
pub fn elapsed_millis(start_at: Instant) -> u64 { pub fn elapsed_millis(start_at: Instant) -> u64 {
start_at.elapsed().as_millis() as u64 start_at.elapsed().as_millis() as u64
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::serial_core::utils::{calc_crc8_itu, calc_crc8_smbus}; use crate::serial_core::utils::{calc_crc8_itu, calc_crc8_itu_xor55, calc_crc8_smbus};
#[test] #[test]
fn test_crc8_itu() { fn test_crc8_itu() {
@@ -51,4 +55,10 @@ mod test {
let checksum = calc_crc8_smbus(req_vec.as_slice()); let checksum = calc_crc8_smbus(req_vec.as_slice());
assert_eq!(checksum, 0x2F); assert_eq!(checksum, 0x2F);
} }
#[test]
fn test_crc8_itu_xor55() {
let req_vec = vec![0x55, 0xAA, 0x07, 0x00, 0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF];
assert_eq!(calc_crc8_itu_xor55(req_vec.as_slice()), 0xFD);
}
} }

View File

@@ -1,4 +1,4 @@
use eframe::egui; use eframe::egui::{self, Color32};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct AppTheme { pub struct AppTheme {
@@ -29,7 +29,7 @@ pub struct DesignMetrics {
} }
pub const ONE_DARK_PRO: AppTheme = AppTheme { pub const ONE_DARK_PRO: AppTheme = AppTheme {
bg: egui::Color32::from_rgb(30, 40, 50), bg: egui::Color32::BLACK,
panel: egui::Color32::from_rgb(22, 28, 35), panel: egui::Color32::from_rgb(22, 28, 35),
panel_strong: egui::Color32::from_rgb(34, 43, 54), panel_strong: egui::Color32::from_rgb(34, 43, 54),
panel_deep: egui::Color32::from_rgb(15, 20, 27), panel_deep: egui::Color32::from_rgb(15, 20, 27),
@@ -61,6 +61,7 @@ pub const METRICS: DesignMetrics = DesignMetrics {
pub mod layout { pub mod layout {
pub const TITLE_BAR_HEIGHT: f32 = 36.0; pub const TITLE_BAR_HEIGHT: f32 = 36.0;
pub const CONFIG_BAR_HEIGHT: f32 = 48.0;
pub const CENTER_PANEL_TOP: f32 = 48.0; pub const CENTER_PANEL_TOP: f32 = 48.0;
pub const LEFT_X: f32 = 24.0; pub const LEFT_X: f32 = 24.0;
pub const RIGHT_X: f32 = 1328.0; pub const RIGHT_X: f32 = 1328.0;
@@ -76,7 +77,7 @@ pub fn apply_theme(ctx: &egui::Context, theme: &AppTheme) {
visuals.override_text_color = Some(theme.text); visuals.override_text_color = Some(theme.text);
visuals.panel_fill = theme.bg; visuals.panel_fill = theme.bg;
visuals.window_fill = theme.panel; visuals.window_fill = theme.panel;
visuals.window_stroke = egui::Stroke::new(1.0, theme.border); visuals.window_stroke = egui::Stroke::new(1.0_f32, theme.border);
visuals.extreme_bg_color = theme.panel_deep; visuals.extreme_bg_color = theme.panel_deep;
visuals.faint_bg_color = theme.panel_strong; visuals.faint_bg_color = theme.panel_strong;
visuals.code_bg_color = theme.panel_deep; visuals.code_bg_color = theme.panel_deep;
@@ -84,23 +85,23 @@ pub fn apply_theme(ctx: &egui::Context, theme: &AppTheme) {
visuals.error_fg_color = ACCENT_RED; visuals.error_fg_color = ACCENT_RED;
visuals.widgets.noninteractive.bg_fill = theme.panel_strong; visuals.widgets.noninteractive.bg_fill = theme.panel_strong;
visuals.widgets.noninteractive.bg_stroke = egui::Stroke::new(1.0, theme.border_soft); visuals.widgets.noninteractive.bg_stroke = egui::Stroke::new(1.0_f32, theme.border_soft);
visuals.widgets.noninteractive.fg_stroke = egui::Stroke::new(1.0, theme.text); visuals.widgets.noninteractive.fg_stroke = egui::Stroke::new(1.0_f32, theme.text);
visuals.widgets.inactive.bg_fill = theme.panel_strong; visuals.widgets.inactive.bg_fill = theme.panel_strong;
visuals.widgets.inactive.bg_stroke = egui::Stroke::new(1.0, theme.border_soft); visuals.widgets.inactive.bg_stroke = egui::Stroke::new(1.0_f32, theme.border_soft);
visuals.widgets.inactive.fg_stroke = egui::Stroke::new(1.0, theme.text); visuals.widgets.inactive.fg_stroke = egui::Stroke::new(1.0_f32, theme.text);
visuals.widgets.hovered.bg_fill = egui::Color32::from_rgb(44, 57, 70); visuals.widgets.hovered.bg_fill = egui::Color32::from_rgb(44, 57, 70);
visuals.widgets.hovered.bg_stroke = egui::Stroke::new(1.0, theme.accent); visuals.widgets.hovered.bg_stroke = egui::Stroke::new(1.0_f32, theme.accent);
visuals.widgets.hovered.fg_stroke = egui::Stroke::new(1.0, egui::Color32::WHITE); visuals.widgets.hovered.fg_stroke = egui::Stroke::new(1.0_f32, egui::Color32::WHITE);
visuals.widgets.active.bg_fill = theme.accent; visuals.widgets.active.bg_fill = theme.accent;
visuals.widgets.active.bg_stroke = egui::Stroke::new(1.0, theme.accent_hot); visuals.widgets.active.bg_stroke = egui::Stroke::new(1.0_f32, theme.accent_hot);
visuals.widgets.active.fg_stroke = egui::Stroke::new(1.0, egui::Color32::WHITE); visuals.widgets.active.fg_stroke = egui::Stroke::new(1.0_f32, egui::Color32::WHITE);
visuals.widgets.open.bg_fill = egui::Color32::from_rgb(39, 50, 62); visuals.widgets.open.bg_fill = egui::Color32::from_rgb(39, 50, 62);
visuals.widgets.open.bg_stroke = egui::Stroke::new(1.0, theme.accent); visuals.widgets.open.bg_stroke = egui::Stroke::new(1.0_f32, theme.accent);
visuals.widgets.open.fg_stroke = egui::Stroke::new(1.0, theme.text); visuals.widgets.open.fg_stroke = egui::Stroke::new(1.0_f32, theme.text);
visuals.selection.bg_fill = egui::Color32::from_rgb(35, 123, 140); visuals.selection.bg_fill = egui::Color32::from_rgb(35, 123, 140);
visuals.selection.stroke = egui::Stroke::new(1.0, egui::Color32::WHITE); visuals.selection.stroke = egui::Stroke::new(1.0_f32, egui::Color32::WHITE);
visuals.hyperlink_color = theme.accent_hot; visuals.hyperlink_color = theme.accent_hot;
ctx.set_visuals(visuals); ctx.set_visuals(visuals);
@@ -126,8 +127,8 @@ pub fn apply_fonts(ctx: &egui::Context) {
let mut fonts = egui::FontDefinitions::default(); let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert( fonts.font_data.insert(
"Hack-Bold".to_owned(), "MapleMono-NF-CN-Bold".to_owned(),
egui::FontData::from_static(include_bytes!("../static/Hack-Bold.ttf")).into(), egui::FontData::from_static(include_bytes!("../static/MapleMono-NF-CN-Bold.ttf")).into(),
); );
let has_yahei = std::fs::read(r"C:\Windows\Fonts\msyh.ttc") let has_yahei = std::fs::read(r"C:\Windows\Fonts\msyh.ttc")
@@ -144,7 +145,7 @@ pub fn apply_fonts(ctx: &egui::Context) {
.families .families
.entry(egui::FontFamily::Proportional) .entry(egui::FontFamily::Proportional)
.or_default() .or_default()
.insert(0, "Hack-Bold".to_owned()); .insert(0, "MapleMono-NF-CN-Bold".to_owned());
if has_yahei { if has_yahei {
fonts fonts
.families .families
@@ -157,7 +158,7 @@ pub fn apply_fonts(ctx: &egui::Context) {
.families .families
.entry(egui::FontFamily::Monospace) .entry(egui::FontFamily::Monospace)
.or_default() .or_default()
.insert(0, "Hack-Bold".to_owned()); .insert(0, "MapleMono-NF-CN-Bold".to_owned());
if has_yahei { if has_yahei {
fonts fonts
.families .families
@@ -173,7 +174,7 @@ pub fn panel_frame(ctx: &egui::Context) -> egui::Frame {
let style = ctx.global_style(); let style = ctx.global_style();
egui::Frame::window(&style) egui::Frame::window(&style)
.fill(ONE_DARK_PRO.panel) .fill(ONE_DARK_PRO.panel)
.stroke(egui::Stroke::new(1.0, ONE_DARK_PRO.border)) .stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border))
.corner_radius(egui::CornerRadius::same(ONE_DARK_PRO.radius)) .corner_radius(egui::CornerRadius::same(ONE_DARK_PRO.radius))
.inner_margin(egui::Margin::same(METRICS.panel_padding)) .inner_margin(egui::Margin::same(METRICS.panel_padding))
.shadow(egui::epaint::Shadow { .shadow(egui::epaint::Shadow {
@@ -197,7 +198,7 @@ pub fn center_panel_frame() -> egui::Frame {
pub fn group_frame() -> egui::Frame { pub fn group_frame() -> egui::Frame {
egui::Frame::new() egui::Frame::new()
.fill(ONE_DARK_PRO.panel_deep) .fill(ONE_DARK_PRO.panel_deep)
.stroke(egui::Stroke::new(1.0, ONE_DARK_PRO.border_soft)) .stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border_soft))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.inner_margin(egui::Margin::symmetric( .inner_margin(egui::Margin::symmetric(
METRICS.group_padding_x, METRICS.group_padding_x,
@@ -208,7 +209,20 @@ pub fn group_frame() -> egui::Frame {
pub fn tag_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> { pub fn tag_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> {
egui::Button::new(label) egui::Button::new(label)
.fill(ONE_DARK_PRO.panel_strong) .fill(ONE_DARK_PRO.panel_strong)
.stroke(egui::Stroke::new(1.0, ONE_DARK_PRO.border)) .stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border))
.corner_radius(egui::CornerRadius::same(4))
.min_size(egui::vec2(0.0, METRICS.button_height))
}
pub fn rich_tag_button(
label: impl Into<String>,
color: impl Into<Color32>,
) -> egui::Button<'static> {
let text = egui::RichText::new(label.into()).color(color);
egui::Button::new(text)
.fill(ONE_DARK_PRO.panel_strong)
.stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.min_size(egui::vec2(0.0, METRICS.button_height)) .min_size(egui::vec2(0.0, METRICS.button_height))
} }
@@ -216,7 +230,7 @@ pub fn tag_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> {
pub fn primary_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> { pub fn primary_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> {
egui::Button::new(label) egui::Button::new(label)
.fill(ONE_DARK_PRO.accent) .fill(ONE_DARK_PRO.accent)
.stroke(egui::Stroke::new(1.0, ONE_DARK_PRO.accent_hot)) .stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.accent_hot))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.min_size(egui::vec2(112.0, METRICS.button_height)) .min_size(egui::vec2(112.0, METRICS.button_height))
} }
@@ -225,7 +239,7 @@ pub fn danger_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static
egui::Button::new(label) egui::Button::new(label)
.fill(ACCENT_RED) .fill(ACCENT_RED)
.stroke(egui::Stroke::new( .stroke(egui::Stroke::new(
1.0, 1.0_f32,
egui::Color32::from_rgb(255, 138, 126), egui::Color32::from_rgb(255, 138, 126),
)) ))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
@@ -238,7 +252,7 @@ pub fn accent_button(
) -> egui::Button<'static> { ) -> egui::Button<'static> {
egui::Button::new(label) egui::Button::new(label)
.fill(fill) .fill(fill)
.stroke(egui::Stroke::new(1.0, ONE_DARK_PRO.border)) .stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.min_size(egui::vec2(0.0, METRICS.button_height)) .min_size(egui::vec2(0.0, METRICS.button_height))
} }
@@ -257,7 +271,7 @@ pub fn mode_button(label: &'static str, selected: bool) -> egui::Button<'static>
egui::Button::new(egui::RichText::new(label).color(egui::Color32::WHITE)) egui::Button::new(egui::RichText::new(label).color(egui::Color32::WHITE))
.fill(fill) .fill(fill)
.stroke(egui::Stroke::new(1.0, stroke)) .stroke(egui::Stroke::new(1.0_f32, stroke))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.min_size(egui::vec2(96.0, METRICS.button_height)) .min_size(egui::vec2(96.0, METRICS.button_height))
} }
@@ -265,7 +279,7 @@ pub fn mode_button(label: &'static str, selected: bool) -> egui::Button<'static>
pub fn icon_button<'a>(icon: impl Into<egui::WidgetText>, size: egui::Vec2) -> egui::Button<'a> { pub fn icon_button<'a>(icon: impl Into<egui::WidgetText>, size: egui::Vec2) -> egui::Button<'a> {
egui::Button::new(icon) egui::Button::new(icon)
.fill(ONE_DARK_PRO.panel_strong) .fill(ONE_DARK_PRO.panel_strong)
.stroke(egui::Stroke::new(1.0, ONE_DARK_PRO.border)) .stroke(egui::Stroke::new(1.0_f32, ONE_DARK_PRO.border))
.corner_radius(egui::CornerRadius::same(4)) .corner_radius(egui::CornerRadius::same(4))
.min_size(size) .min_size(size)
} }

View File

@@ -187,6 +187,7 @@ impl Texture {
) )
} }
#[allow(clippy::too_many_arguments)]
pub fn from_rgba8_with_sampler( pub fn from_rgba8_with_sampler(
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,

View File

@@ -83,53 +83,6 @@ pub fn apply_theme(ctx: &egui::Context, theme: &AppTheme) {
ctx.set_global_style(style); ctx.set_global_style(style);
} }
pub fn apply_fonts(ctx: &egui::Context) {
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
"Hack-Bold".to_owned(),
egui::FontData::from_static(include_bytes!("../static/Hack-Bold.ttf")).into(),
);
let has_yahei = std::fs::read(r"C:\Windows\Fonts\msyh.ttc")
.or_else(|_| std::fs::read(r"C:\Windows\Fonts\msyhbd.ttc"))
.map(|font_data| {
fonts.font_data.insert(
"Microsoft-YaHei".to_owned(),
egui::FontData::from_owned(font_data).into(),
);
})
.is_ok();
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.insert(0, "Hack-Bold".to_owned());
if has_yahei {
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.push("Microsoft-YaHei".to_owned());
}
fonts
.families
.entry(egui::FontFamily::Monospace)
.or_default()
.insert(0, "Hack-Bold".to_owned());
if has_yahei {
fonts
.families
.entry(egui::FontFamily::Monospace)
.or_default()
.push("Microsoft-YaHei".to_owned());
}
ctx.set_fonts(fonts);
}
pub fn panel_frame(ctx: &egui::Context) -> egui::Frame { pub fn panel_frame(ctx: &egui::Context) -> egui::Frame {
let style = ctx.global_style(); let style = ctx.global_style();
egui::Frame::window(&style) egui::Frame::window(&style)

767
src/ui.rs

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,3 @@
use anyhow;
use serialport::available_ports; use serialport::available_ports;
pub fn serial_enum() -> anyhow::Result<Vec<String>> { pub fn serial_enum() -> anyhow::Result<Vec<String>> {

Binary file not shown.

Binary file not shown.

View File

@@ -126,15 +126,7 @@ fn vs_background(@builtin(vertex_index) vertex_index: u32) -> BackgroundVertexOu
@fragment @fragment
fn fs_background(@builtin(position) frag_coord: vec4f) -> @location(0) vec4f { fn fs_background(@builtin(position) frag_coord: vec4f) -> @location(0) vec4f {
let pixel = frag_coord.xy; return output_color(vec3f(0.0, 0.0, 0.0), 1.0);
let viewport = u.viewport.xy;
let uv = pixel / max(viewport, vec2f(1.0, 1.0));
var color = mix(vec3f(0.018, 0.019, 0.022), vec3f(0.038, 0.040, 0.046), 1.0 - uv.y);
let vignette = smoothstep(0.18, 0.92, length((uv - vec2f(0.52, 0.48)) * vec2f(viewport.x / viewport.y, 1.0)));
color *= 1.0 - vignette * 0.22;
color += vec3f(0.010, 0.010, 0.012) * (1.0 - smoothstep(0.0, 0.85, abs(uv.y - 0.50)));
return output_color(color, 1.0);
} }
@@ -357,10 +349,8 @@ fn circle_alpha(local: vec2f, radius: f32, softness: f32) -> f32 {
return 1.0 - smoothstep(radius, radius + softness, dist); return 1.0 - smoothstep(radius, radius + softness, dist);
} }
// Convert a point authored in hand.png UV space into clip space. // Convert a point authored in sensor-canvas UV space into aspect-fitted clip space.
// This mirrors fs_hand_image's aspect-fit math, so fingertip dots stay attached fn sensor_canvas_uv_to_clip(image_uv: vec2f) -> vec2f {
// to the same image pixels when the app window changes shape.
fn hand_image_uv_to_clip(image_uv: vec2f) -> vec2f {
let viewport_aspect = u.viewport.x / max(u.viewport.y, 1.0); let viewport_aspect = u.viewport.x / max(u.viewport.y, 1.0);
let image_aspect = u.image.x / max(u.image.y, 1.0); let image_aspect = u.image.x / max(u.image.y, 1.0);
@@ -428,7 +418,7 @@ fn vs_hand_membrane(vertex: DotVertexInput, instance: DotInstanceInput) -> HandM
let image_uv = (center_px + rotate_2d(local_px, angle)) / max(u.image.xy, vec2f(1.0, 1.0)); let image_uv = (center_px + rotate_2d(local_px, angle)) / max(u.image.xy, vec2f(1.0, 1.0));
var out: HandMembraneVertexOutput; var out: HandMembraneVertexOutput;
out.clip_position = vec4f(hand_image_uv_to_clip(image_uv), 0.0, 1.0); out.clip_position = vec4f(sensor_canvas_uv_to_clip(image_uv), 0.0, 1.0);
out.local = vertex.local; out.local = vertex.local;
return out; return out;
} }
@@ -478,7 +468,7 @@ fn vs_hand_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexO
let shaped = smoothstep(0.0, 1.0, intensity); let shaped = smoothstep(0.0, 1.0, intensity);
// Hand instances store hand.png UV in world_position.xy instead of 3D world space. // Hand instances store hand.png UV in world_position.xy instead of 3D world space.
let center = hand_image_uv_to_clip(instance.world_position.xy); let center = sensor_canvas_uv_to_clip(instance.world_position.xy);
// Hand fingertip matrices are much smaller than the full Finger view. // Hand fingertip matrices are much smaller than the full Finger view.
// Keep each bead below the local cell spacing so the 12x7 matrix remains visibly separated. // Keep each bead below the local cell spacing so the 12x7 matrix remains visibly separated.
let pixel_size = u.glyph.x * mix(0.22, 0.34, shaped); let pixel_size = u.glyph.x * mix(0.22, 0.34, shaped);
@@ -497,86 +487,25 @@ fn fs_hand_dot(in: DotVertexOutput) -> @location(0) vec4f {
// Use a compact bead so the response feels like it lives on the membrane mesh. // Use a compact bead so the response feels like it lives on the membrane mesh.
let core = circle_alpha(in.local, 0.48, 0.07); let core = circle_alpha(in.local, 0.48, 0.07);
let halo = circle_alpha(in.local, 0.74, 0.14) * 0.12; let halo = circle_alpha(in.local, 0.74, 0.14) * (0.10 + intensity * 0.26);
// Keep the fingertip matrix visually close to JE-Skin's cyan model dots, // Match Finger mode's pressure gradient so each hand region remains readable.
// while still letting pressure brighten the bead a little. let idle = vec3f(0.060, 0.250, 0.320);
let cyan = vec3f(0.34, 0.86, 1.0); let gradient = sample_range_color(intensity);
let hot = sample_range_color(intensity); let color = mix(idle, gradient, smoothstep(0.0, 0.18, intensity))
let color = mix(cyan, hot, 0.22) * mix(0.82, 1.16, intensity); * mix(0.78, 1.18, intensity);
return output_color(color, max(core, halo)); return output_color(color, max(core, halo));
} }
fn chip_pixel_alpha(local: vec2f, half_size: f32, softness: f32) -> f32 {
let q = abs(local) - vec2f(half_size, half_size);
let dist = length(max(q, vec2f(0.0, 0.0))) + min(max(q.x, q.y), 0.0);
return 1.0 - smoothstep(0.0, softness, dist);
}
struct HandPalmChipVertexOutput {
@builtin(position) clip_position: vec4f,
@location(0) local: vec2f,
@location(1) grid: vec2f,
}
@vertex
fn vs_hand_palm_chip(vertex: DotVertexInput, instance: DotInstanceInput) -> HandPalmChipVertexOutput {
let center_px = instance.world_position.xy * u.image.xy;
let size_px = instance.style.yz;
let angle = instance.style.x;
let packed_shape = instance.style.w;
let shape_rows = floor(packed_shape / 100.0);
let shape_cols = max(packed_shape - shape_rows * 100.0, 1.0);
let local_px = vertex.local * size_px * 0.5;
let image_uv = (center_px + rotate_2d(local_px, angle)) / max(u.image.xy, vec2f(1.0, 1.0));
var out: HandPalmChipVertexOutput;
out.clip_position = vec4f(hand_image_uv_to_clip(image_uv), 0.0, 1.0);
out.local = vertex.local;
out.grid = vec2f(shape_cols, max(shape_rows, 1.0));
return out;
}
@fragment
fn fs_hand_palm_chip(in: HandPalmChipVertexOutput) -> @location(0) vec4f {
// Dark rounded tile: this is the inset chip body sitting inside the palm surface.
let panel = rounded_rect_alpha(in.local, 0.10, 0.040);
let inset = rounded_rect_alpha(in.local * vec2f(1.10, 1.08), 0.08, 0.052);
let rim = clamp(panel - inset * 0.72, 0.0, 1.0);
// Inactive chip pixels use the chip's real hand layout:
// horizontal 14 columns x 5 rows, or vertical 4 columns x 11 rows.
let uv = clamp(in.local * 0.5 + vec2f(0.5, 0.5), vec2f(0.0, 0.0), vec2f(1.0, 1.0));
let cell = abs(fract(uv * in.grid) - vec2f(0.5, 0.5));
let micro_pixel = 1.0 - smoothstep(0.105, 0.178, length(cell * vec2f(1.04, 0.94)));
let top_bevel = smoothstep(-0.96, -0.18, -in.local.y) * 0.16;
let lower_shadow = smoothstep(0.20, 0.92, in.local.y) * 0.22;
let side_bevel = smoothstep(0.58, 0.96, abs(in.local.x)) * 0.12;
let scan = (0.5 + 0.5 * sin((uv.y * 36.0 + uv.x * 7.0) * 6.28318)) * 0.026;
let base = vec3f(0.004, 0.012, 0.018);
let glass = vec3f(0.012, 0.048, 0.064);
let pixel_color = vec3f(0.075, 0.300, 0.360);
let rim_color = vec3f(0.060, 0.560, 0.670);
let color = base * (0.92 - lower_shadow)
+ glass * (0.52 + top_bevel + side_bevel + scan)
+ pixel_color * micro_pixel * 0.70
+ rim_color * rim * 0.60;
let alpha = panel * (0.64 + micro_pixel * 0.18 + rim * 0.20);
return output_color(color, alpha);
}
@vertex @vertex
fn vs_hand_palm_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexOutput { fn vs_hand_palm_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexOutput {
let intensity = saturate(instance.style.x); let intensity = saturate(instance.style.x);
let shaped = smoothstep(0.0, 1.0, intensity); let shaped = smoothstep(0.0, 1.0, intensity);
// Palm chip pixels are deliberately smaller than fingertip beads so they read as a chip matrix. // Use the same on-screen point size as Finger mode.
let center = hand_image_uv_to_clip(instance.world_position.xy); let center = sensor_canvas_uv_to_clip(instance.world_position.xy);
let pixel_size = u.glyph.x * mix(0.13, 0.25, shaped); let pixel_size = u.glyph.x * mix(1.07, 2.23, shaped);
let ndc_offset = vertex.local * vec2f(pixel_size / u.viewport.x, pixel_size / u.viewport.y) * 2.0; let ndc_offset = vertex.local * vec2f(pixel_size / u.viewport.x, pixel_size / u.viewport.y) * 2.0;
var out: DotVertexOutput; var out: DotVertexOutput;
@@ -589,16 +518,11 @@ fn vs_hand_palm_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVe
@fragment @fragment
fn fs_hand_palm_dot(in: DotVertexOutput) -> @location(0) vec4f { fn fs_hand_palm_dot(in: DotVertexOutput) -> @location(0) vec4f {
let intensity = saturate(in.intensity); let intensity = saturate(in.intensity);
let pixel = chip_pixel_alpha(in.local, 0.52, 0.070); let base_color = sample_range_color(intensity);
let glow = circle_alpha(in.local, 0.95, 0.22) * intensity * 0.30;
let cold = vec3f(0.070, 0.340, 0.360); let alpha = circle_alpha(in.local, 0.46, 0.045);
let active_color = mix(vec3f(0.110, 0.620, 0.420), vec3f(0.460, 1.000, 0.210), smoothstep(0.08, 1.0, intensity)); let color = base_color * mix(0.86, 1.06, intensity);
let color = mix(cold, active_color, smoothstep(0.02, 0.72, intensity))
* (0.54 + intensity * 1.10)
+ vec3f(0.28, 1.0, 0.36) * glow * 0.90;
let alpha = max(pixel * (0.20 + intensity * 0.76), glow);
return output_color(color, alpha); return output_color(color, alpha);
} }

254
wix/main.wxs Normal file
View File

@@ -0,0 +1,254 @@
<?xml version='1.0' encoding='windows-1252'?>
<!--
Copyright (C) 2017 Christopher R. Field.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
The "cargo wix" subcommand provides a variety of predefined variables available
for customization of this template. The values for each variable are set at
installer creation time. The following variables are available:
TargetTriple = The rustc target triple name.
TargetEnv = The rustc target environment. This is typically either
"msvc" or "gnu" depending on the toolchain downloaded and
installed.
TargetVendor = The rustc target vendor. This is typically "pc", but Rust
does support other vendors, like "uwp".
CargoTargetBinDir = The complete path to the directory containing the
binaries (exes) to include. The default would be
"target\release\". If an explicit rustc target triple is
used, i.e. cross-compiling, then the default path would
be "target\<CARGO_TARGET>\<CARGO_PROFILE>",
where "<CARGO_TARGET>" is replaced with the "CargoTarget"
variable value and "<CARGO_PROFILE>" is replaced with the
value from the "CargoProfile" variable. This can also
be overridden manually with the "target-bin-dir" flag.
CargoTargetDir = The path to the directory for the build artifacts, i.e.
"target".
CargoProfile = The cargo profile used to build the binaries
(usually "debug" or "release").
Version = The version for the installer. The default is the
"Major.Minor.Fix" semantic versioning number of the Rust
package.
-->
<!--
Please do not remove these pre-processor If-Else blocks. These are used with
the `cargo wix` subcommand to automatically determine the installation
destination for 32-bit versus 64-bit installers. Removal of these lines will
cause installation errors.
-->
<?if $(sys.BUILDARCH) = x64 or $(sys.BUILDARCH) = arm64 ?>
<?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
<?else ?>
<?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
<?endif ?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
<Product
Id='*'
Name='eskin-model-player'
UpgradeCode='7CEF316B-BE23-4533-B4B2-87D06D2230B5'
Manufacturer='JOYSONQUIN'
Language='1033'
Codepage='1252'
Version='$(var.Version)'>
<Package Id='*'
Keywords='Installer'
Description='Desktop pressure sensor visualization and playback application.'
Manufacturer='JOYSONQUIN'
InstallerVersion='450'
Languages='1033'
Compressed='yes'
InstallScope='perUser'
InstallPrivileges='limited'
SummaryCodepage='1252'
/>
<MajorUpgrade
Schedule='afterInstallInitialize'
DowngradeErrorMessage='A newer version of [ProductName] is already installed. Setup will now exit.'/>
<Media Id='1' Cabinet='media1.cab' EmbedCab='yes' DiskPrompt='CD-ROM #1'/>
<Property Id='DiskPrompt' Value='eskin-model-player Installation'/>
<Directory Id='TARGETDIR' Name='SourceDir'>
<Directory Id='LocalAppDataFolder'>
<Directory Id='APPLICATIONFOLDER' Name='eskin-model-player'>
<!--
Enabling the license sidecar file in the installer is a four step process:
1. Uncomment the `Component` tag and its contents.
2. Change the value for the `Source` attribute in the `File` tag to a path
to the file that should be included as the license sidecar file. The path
can, and probably should be, relative to this file.
3. Change the value for the `Name` attribute in the `File` tag to the
desired name for the file when it is installed alongside the `bin` folder
in the installation directory. This can be omitted if the desired name is
the same as the file name.
4. Uncomment the `ComponentRef` tag with the Id attribute value of "License"
further down in this file.
-->
<!--
<Component Id='License' Guid='*'>
<File Id='LicenseFile' Name='ChangeMe' DiskId='1' Source='C:\Path\To\File' KeyPath='yes'/>
</Component>
-->
<Directory Id='Bin' Name='bin'>
<Component Id='Path' Guid='FD230ABD-75B9-47D4-870B-CF2EA72B76BE'>
<RegistryValue
Root='HKCU'
Key='Software\JOYSONQUIN\eskin-model-player'
Name='PathComponent'
Type='integer'
Value='1'
KeyPath='yes'/>
<RemoveFolder Id='RemoveBinFolder' Directory='Bin' On='uninstall'/>
<RemoveFolder Id='RemoveApplicationFolder' Directory='APPLICATIONFOLDER' On='uninstall'/>
<Environment
Id='PATH'
Name='PATH'
Value='[Bin]'
Permanent='no'
Part='last'
Action='set'
System='no'/>
</Component>
<Component Id='binary0' Guid='302CAFB5-6951-426B-BC5A-988C351A2CF2'>
<RegistryValue
Root='HKCU'
Key='Software\JOYSONQUIN\eskin-model-player'
Name='BinaryComponent'
Type='integer'
Value='1'
KeyPath='yes'/>
<File
Id='exe0'
Name='ESkinPlayer.exe'
DiskId='1'
Source='$(var.CargoTargetBinDir)\ESkinPlayer.exe'/>
</Component>
</Directory>
</Directory>
</Directory>
</Directory>
<Feature
Id='Binaries'
Title='Application'
Description='Installs all binaries and the license.'
Level='1'
ConfigurableDirectory='APPLICATIONFOLDER'
AllowAdvertise='no'
Display='expand'
Absent='disallow'>
<!--
Uncomment the following `ComponentRef` tag to add the license
sidecar file to the installer.
-->
<!--<ComponentRef Id='License'/>-->
<ComponentRef Id='binary0'/>
<Feature
Id='Environment'
Title='PATH Environment Variable'
Description='Add the install location of the [ProductName] executable to the PATH system environment variable. This allows the [ProductName] executable to be called from any location.'
Level='1'
Absent='allow'>
<ComponentRef Id='Path'/>
</Feature>
</Feature>
<SetProperty Id='ARPINSTALLLOCATION' Value='[APPLICATIONFOLDER]' After='CostFinalize'/>
<!--
Uncomment the following `Icon` and `Property` tags to change the product icon.
The product icon is the graphic that appears in the Add/Remove
Programs control panel for the application.
-->
<!--<Icon Id='ProductICO' SourceFile='wix\Product.ico'/>-->
<!--<Property Id='ARPPRODUCTICON' Value='ProductICO' />-->
<!--
Adding a URL to Add/Remove Programs control panel listing for the
application is a two step process:
1. Uncomment the following `Property` tag with the "ARPHELPLINK" Id
attribute value.
2. Change the value for `Value` attribute of the following
`Property` tag to a valid URL.
-->
<!--<Property Id='ARPHELPLINK' Value='ChangeMe'/>-->
<UI>
<UIRef Id='WixUI_FeatureTree'/>
<!--
Enabling the EULA dialog in the installer is a three step process:
1. Comment out or remove the two `Publish` tags that follow the
`WixVariable` tag.
2. Uncomment the `<WixVariable Id='WixUILicenseRtf' Value='Path\to\Eula.rft'>` tag further down
3. Replace the `Value` attribute of the `WixVariable` tag with
the path to a RTF file that will be used as the EULA and
displayed in the license agreement dialog.
-->
<Publish Dialog='WelcomeDlg' Control='Next' Event='NewDialog' Value='CustomizeDlg' Order='99'>1</Publish>
<Publish Dialog='CustomizeDlg' Control='Back' Event='NewDialog' Value='WelcomeDlg' Order='99'>1</Publish>
</UI>
<!--
Enabling the EULA dialog in the installer requires uncommenting
the following `WixUILicenseRTF` tag and changing the `Value`
attribute.
-->
<!-- <WixVariable Id='WixUILicenseRtf' Value='Relative\Path\to\Eula.rtf'/> -->
<!--
Uncomment the next `WixVariable` tag to customize the installer's
Graphical User Interface (GUI) and add a custom banner image across
the top of each screen. See the WiX Toolset documentation for details
about customization.
The banner BMP dimensions are 493 x 58 pixels.
-->
<!--<WixVariable Id='WixUIBannerBmp' Value='wix\Banner.bmp'/>-->
<!--
Uncomment the next `WixVariable` tag to customize the installer's
Graphical User Interface (GUI) and add a custom image to the first
dialog, or screen. See the WiX Toolset documentation for details about
customization.
The dialog BMP dimensions are 493 x 312 pixels.
-->
<!--<WixVariable Id='WixUIDialogBmp' Value='wix\Dialog.bmp'/>-->
</Product>
</Wix>