14 Commits

Author SHA1 Message Date
lenn
9fb4c0edf4 对五个手指分别进行查表插值 2026-07-07 21:13:37 +08:00
lenn
1e3cfdcf21 shader: collapse palm chip background pass and refine dot rendering 2026-07-07 16:35:19 +08:00
lenn
a04b903e96 Integrate hand fingertip force overlays 2026-07-07 09:43:25 +08:00
lenn
c4bccc1747 refactor: 配置面板改造为顶部固定栏 + 压力归一化优化 2026-07-06 14:07:23 +08:00
lenn
5264f0c247 Tune hand-tip and palm chip layout coordinates
Adjust HandTipMatrix / HandPalmChip center, angle, and size in render.rs and propagate the spacing tweak through shader.wgsl.
2026-07-03 15:30:25 +08:00
lenn
a999d9c064 Refactor to hand-mode UI and reorganize WGSL shader
Replace spatial force / stats panels with unified hand force panels; simplify app.rs wiring and reorganize shader.wgsl for the hand rendering layout.
2026-07-03 13:41:02 +08:00
lenn
71d314ac44 Add finger mode UI and refine breakout/serial integration 2026-07-02 15:22:13 +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
lennlouisgeek
f30ebcf20b Add pressure visual modes and breakout game 2026-06-29 03:13:57 +08:00
lennlouisgeek
eab24fc2bf update 2026-06-28 23:29:13 +08:00
27 changed files with 5614 additions and 487 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:*"
]
}
}

43
Cargo.lock generated
View File

@@ -1304,7 +1304,7 @@ checksum = "dea2df4cf52843e0452895c455a1a2cfbb842a1e7329671acf418fdc53ed4c59"
[[package]]
name = "eskin-model-player"
version = "0.5.0"
version = "5.0.0"
dependencies = [
"anyhow",
"bytemuck",
@@ -1320,6 +1320,7 @@ dependencies = [
"log",
"serialport",
"tobj",
"winresource",
]
[[package]]
@@ -3645,6 +3646,15 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_spanned"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26"
dependencies = [
"serde_core",
]
[[package]]
name = "serialport"
version = "4.9.0"
@@ -3989,6 +3999,21 @@ dependencies = [
"ahash",
]
[[package]]
name = "toml"
version = "1.1.2+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81f3d15e84cbcd896376e6730314d59fb5a87f31e4b038454184435cd57defee"
dependencies = [
"indexmap",
"serde_core",
"serde_spanned",
"toml_datetime",
"toml_parser",
"toml_writer",
"winnow",
]
[[package]]
name = "toml_datetime"
version = "1.1.1+spec-1.1.0"
@@ -4019,6 +4044,12 @@ dependencies = [
"winnow",
]
[[package]]
name = "toml_writer"
version = "1.1.1+spec-1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "756daf9b1013ebe47a8776667b466417e2d4c5679d441c26230efd9ef78692db"
[[package]]
name = "tracing"
version = "0.1.44"
@@ -5017,6 +5048,16 @@ dependencies = [
"memchr",
]
[[package]]
name = "winresource"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0986a8b1d586b7d3e4fe3d9ea39fb451ae22869dcea4aa109d287a374d866087"
dependencies = [
"toml",
"version_check",
]
[[package]]
name = "wit-bindgen"
version = "0.51.0"

View File

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

View File

@@ -27,6 +27,14 @@ fn main() -> Result<()> {
copy_items(&items, &resource_dir, &copy_options)?;
}
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
let mut resource = winresource::WindowsResource::new();
resource.set_icon("res/icon.ico");
resource.compile().expect("compile windows icon failed");
}
println!("cargo:rustc-env=RESOURCE_DIR={}", resource_dir.display());
Ok(())

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

File diff suppressed because it is too large Load Diff

BIN
res/128x128@2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
res/hand.glb Normal file

Binary file not shown.

BIN
res/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

View File

@@ -1,4 +1,6 @@
use crate::connection::ConnectionManager;
use crate::breakout::{BreakoutGame, control_from_matrix};
use crate::connection::{ConnectionManager, ConnectionState};
use crate::force::{ForceEstimatorState, HAND_FINGERTIP_COUNT, HudSpatialForce};
use crate::recording::Recorder;
use crate::render::{ActiveMode, FingerMode, HandGatewayMode};
use crate::style::{ONE_DARK_PRO, apply_fonts, apply_theme, layout};
@@ -6,25 +8,40 @@ use crate::ui::SerialMode;
use crate::{
matrix::{MATRIX_COLS, MATRIX_ROWS},
render::{
BackgroundRenderResources, PRESSURE_CELL_COUNT, PressureFrame, WgpuBackgroundCallback,
BackgroundRenderResources, PRESSURE_CELL_COUNT, PressureFrame, PressureSamples,
WgpuBackgroundCallback,
},
ui::{
ConfigPanelState, ConnectPanelState, FloatingPanelState, MatrixConfigState,
draw_config_panel, draw_export_panel, draw_matrix_config_panel, draw_stats_panel,
panel_restore_item,
draw_config_panel, draw_hand_force_panels, panel_restore_item,
},
};
use eframe::{egui, egui_wgpu};
use std::sync::Arc;
use std::{
sync::Arc,
time::{Duration, Instant},
};
const DATA_LOG_EVERY_FRAMES: u64 = 30;
const SUMMARY_POINTS_PER_SERIES: usize = 42;
const HAND_FORCE_PANEL_COUNT: usize = 7;
const HAND_FORCE_SEGMENT_COUNTS: [usize; HAND_FORCE_PANEL_COUNT] = [84, 84, 84, 84, 84, 70, 44];
const HAND_IMAGE_SIZE_PX: [f32; 2] = [971.0, 1117.0];
const HAND_FINGERTIP_FORCE_ANCHORS_PX: [[f32; 2]; HAND_FINGERTIP_COUNT] = [
[265.0, 495.0],
[359.0, 260.0],
[482.0, 228.0],
[596.0, 265.0],
[693.0, 370.0],
];
const RATE_UPDATE_INTERVAL: Duration = Duration::from_millis(500);
const DATA_IDLE_PANEL_EXIT_DELAY: Duration = Duration::from_secs(3);
pub struct EskinDesktopApp {
connect_panel: FloatingPanelState,
connect_state: ConnectPanelState,
connection: Arc<ConnectionManager>,
pressure_matrix: PressureFrame,
data_log_frame: u64,
hand_pressure: PressureSamples,
scene_panel: FloatingPanelState,
config_panel: FloatingPanelState,
config_state: ConfigPanelState,
@@ -36,9 +53,66 @@ pub struct EskinDesktopApp {
matrix_config_panel: FloatingPanelState,
matrix_config: MatrixConfigState,
signal_history: Vec<f32>,
hand_signal_histories: [Vec<f32>; HAND_FORCE_PANEL_COUNT],
force_estimator: ForceEstimatorState,
latest_spatial_force: Option<HudSpatialForce>,
latest_hand_spatial_forces: [Option<HudSpatialForce>; HAND_FINGERTIP_COUNT],
spatial_force_panel_force: Option<HudSpatialForce>,
latest_raw_matrix: Vec<u32>,
latest_matrix_rows: u32,
latest_matrix_cols: u32,
last_sample_at: Option<Instant>,
breakout_visible: bool,
breakout_game: BreakoutGame,
live_rates: LiveRateMeters,
context_menu_open: bool,
context_menu_pos: egui::Pos2,
active_mode: ActiveMode,
}
struct LiveRateMeters {
window_started_at: Instant,
window_rx_frames: u64,
window_render_frames: u32,
sample_rate_hz: f32,
render_rate_hz: f32,
}
impl LiveRateMeters {
fn new() -> Self {
Self {
window_started_at: Instant::now(),
window_rx_frames: 0,
window_render_frames: 0,
sample_rate_hz: 0.0,
render_rate_hz: 0.0,
}
}
fn tick_render(&mut self, total_rx_frames: u64) {
if total_rx_frames < self.window_rx_frames {
self.window_rx_frames = total_rx_frames;
self.sample_rate_hz = 0.0;
}
self.window_render_frames = self.window_render_frames.saturating_add(1);
let now = Instant::now();
let elapsed = now.duration_since(self.window_started_at);
if elapsed < RATE_UPDATE_INTERVAL {
return;
}
let seconds = elapsed.as_secs_f32().max(0.001);
let rx_delta = total_rx_frames.saturating_sub(self.window_rx_frames);
self.sample_rate_hz = rx_delta as f32 / seconds;
self.render_rate_hz = self.window_render_frames as f32 / seconds;
self.window_started_at = now;
self.window_rx_frames = total_rx_frames;
self.window_render_frames = 0;
}
}
impl EskinDesktopApp {
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
egui_extras::install_image_loaders(&cc.egui_ctx);
@@ -66,7 +140,7 @@ impl EskinDesktopApp {
connect_state: ConnectPanelState::default(),
connection: Arc::new(ConnectionManager::new()),
pressure_matrix: [[0.0, 0.0]; PRESSURE_CELL_COUNT],
data_log_frame: 0,
hand_pressure: Vec::new(),
scene_panel: FloatingPanelState::new(
[layout::LEFT_X, layout::TOP_Y],
[layout::LEFT_TAG_X, layout::TOP_Y],
@@ -92,19 +166,25 @@ impl EskinDesktopApp {
),
matrix_config: MatrixConfigState::default(),
signal_history: Vec::with_capacity(128),
active_mode: ActiveMode::Finger(FingerMode {
rows: 12,
cols: 7,
range: 0..7000,
dot: true,
}),
hand_signal_histories: std::array::from_fn(|_| Vec::with_capacity(128)),
force_estimator: ForceEstimatorState::new(),
latest_spatial_force: None,
latest_hand_spatial_forces: [None; HAND_FINGERTIP_COUNT],
spatial_force_panel_force: None,
latest_raw_matrix: Vec::new(),
latest_matrix_rows: MATRIX_ROWS,
latest_matrix_cols: MATRIX_COLS,
last_sample_at: None,
breakout_visible: false,
breakout_game: BreakoutGame::default(),
live_rates: LiveRateMeters::new(),
context_menu_open: false,
context_menu_pos: egui::pos2(24.0, 48.0),
active_mode: ActiveMode::Hand(HandGatewayMode { range: 0..7000 }),
}
}
fn draw_wgpu_background(&mut self, ui: &mut egui::Ui) {
self.update_pressure_matrix();
let rect = ui.max_rect();
fn draw_wgpu_background_rect(&self, ui: &mut egui::Ui, rect: egui::Rect) {
let width = rect.width().max(1.0);
let height = rect.height().max(1.0);
@@ -114,34 +194,168 @@ impl EskinDesktopApp {
width,
height,
pressure: self.pressure_matrix,
hand_pressure: self.hand_pressure.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) {
if !self.recent_sample_is_fresh() {
return;
}
if matches!(self.active_mode, ActiveMode::Hand(_)) {
self.paint_hand_spatial_force_overlay(ui, rect);
return;
}
let Some(force) = self.latest_spatial_force else {
return;
};
let center = rect.center();
paint_spatial_force_arrow(ui.painter(), center, force, 34.0, 54.0);
}
fn paint_hand_spatial_force_overlay(&self, ui: &egui::Ui, rect: egui::Rect) {
for (force, anchor_px) in self
.latest_hand_spatial_forces
.iter()
.zip(HAND_FINGERTIP_FORCE_ANCHORS_PX)
{
let Some(force) = force else {
continue;
};
let center = hand_image_pixel_to_screen(rect, anchor_px);
paint_spatial_force_arrow(ui.painter(), center, *force, 20.0, 34.0);
}
}
fn strongest_hand_spatial_force(&self) -> Option<HudSpatialForce> {
self.latest_hand_spatial_forces
.iter()
.flatten()
.copied()
.max_by(|a, b| a.magnitude.total_cmp(&b.magnitude))
}
fn analyze_spatial_force(&mut self, values: &[u32]) {
if matches!(self.config_state.mode, SerialMode::Hand) {
self.latest_hand_spatial_forces = self.force_estimator.analyze_fingertips(values);
self.latest_spatial_force = self.strongest_hand_spatial_force();
} else {
self.latest_spatial_force = self.force_estimator.analyze(values);
self.latest_hand_spatial_forces = [None; HAND_FINGERTIP_COUNT];
}
}
fn connection_has_live_data(&self) -> bool {
matches!(
self.connection.state(),
ConnectionState::Connected | ConnectionState::Streaming
)
}
fn recent_sample_is_fresh(&self) -> bool {
self.last_sample_at
.is_some_and(|sample_at| sample_at.elapsed() <= DATA_IDLE_PANEL_EXIT_DELAY)
}
fn hand_force_panels_visible(&self) -> bool {
self.connection_has_live_data() && self.recent_sample_is_fresh()
}
fn sync_disconnected_live_state(&mut self) {
if self.connection_has_live_data() {
return;
}
self.last_sample_at = None;
self.latest_spatial_force = None;
self.latest_hand_spatial_forces = [None; HAND_FINGERTIP_COUNT];
self.spatial_force_panel_force = None;
}
fn draw_workspace(&mut self, ui: &mut egui::Ui) {
let screen = ui.max_rect();
let workspace = egui::Rect::from_min_max(
screen.left_top()
+ egui::vec2(0.0, layout::TITLE_BAR_HEIGHT + layout::CONFIG_BAR_HEIGHT),
screen.right_bottom(),
);
self.draw_wgpu_background_rect(ui, workspace);
}
fn draw_split_workspace(&mut self, ui: &mut egui::Ui) {
let screen = ui.max_rect();
let content = egui::Rect::from_min_max(
screen.left_top() + egui::vec2(0.0, layout::TITLE_BAR_HEIGHT),
screen.right_bottom(),
);
let gutter = 10.0;
let half_width = (content.width() - gutter).max(1.0) * 0.5;
let left = egui::Rect::from_min_size(content.min, egui::vec2(half_width, content.height()));
let right = egui::Rect::from_min_max(
egui::pos2(left.right() + gutter, content.top()),
content.right_bottom(),
);
ui.painter()
.rect_filled(screen, egui::CornerRadius::ZERO, ONE_DARK_PRO.bg);
paint_split_viewport_shell(ui, left, "DOTMATRIX", "压力矩阵 / WGPU");
paint_split_viewport_shell(ui, right, "BREAKOUT", "压力控制打砖块");
let left_body = split_viewport_body(left);
let right_body = split_viewport_body(right);
self.draw_wgpu_background_rect(ui, left_body);
let breakout_control = control_from_matrix(
&self.latest_raw_matrix,
self.latest_matrix_rows,
self.latest_matrix_cols,
);
self.breakout_game.draw_viewport(
ui.ctx(),
right_body,
&mut self.breakout_visible,
breakout_control,
);
}
fn update_pressure_matrix(&mut self) {
if let Some(sample) = self.connection.take_latest_sample() {
self.last_sample_at = Some(Instant::now());
normalize_pressure_sample(
&sample.matrix,
sample.rows,
sample.cols,
&mut self.pressure_matrix,
);
self.hand_pressure = normalize_pressure_samples(&sample.matrix);
self.latest_raw_matrix.clear();
self.latest_raw_matrix.extend_from_slice(&sample.matrix);
self.latest_matrix_rows = sample.rows;
self.latest_matrix_cols = sample.cols;
// Feed data to recorder
self.recorder.add_frame(&sample.matrix);
self.analyze_spatial_force(&sample.matrix);
// Update signal history (sum of all pressure values)
let total: f32 = sample.matrix.iter().map(|v| *v as f32).sum();
self.signal_history.push(total);
if self.signal_history.len() > 128 {
// Keep JE-Skin's summary path separate from the optional spatial-force vector.
let raw_total = sample
.matrix
.iter()
.fold(0_u64, |sum, value| sum + *value as u64)
.min(u32::MAX as u64) as u32;
let force = raw_to_g1(raw_total).min(25.6);
let force = if force <= 0.1 { 0.0 } else { force };
self.signal_history.push(force);
if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES {
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);
}
update_hand_signal_histories(&mut self.hand_signal_histories, &sample.matrix);
}
}
@@ -256,57 +470,131 @@ impl EskinDesktopApp {
}
}
fn draw_floating_panels(&mut self, ctx: &egui::Context) {
// draw_scene_panel(ctx, &mut self.scene_panel);
if let Some(next_mode) =
draw_config_panel(ctx, &mut self.config_panel, &mut self.config_state)
{
fn draw_floating_panels(
&mut self,
ctx: &egui::Context,
stats: crate::serial_core::serial::SerialIoStats,
) {
self.scene_panel.visible = false;
self.connect_panel.visible = false;
self.export_panel.visible = false;
self.matrix_config_panel.visible = false;
self.context_menu_open = false;
if let Some(next_mode) = draw_config_panel(
ctx,
&mut self.config_panel,
&mut self.config_state,
&self.connection,
&self.recorder,
stats,
self.live_rates.sample_rate_hz,
self.live_rates.render_rate_hz,
) {
self.switch_mode(next_mode);
}
draw_stats_panel(ctx, &mut self.stats_panel);
draw_export_panel(
self.stats_panel.visible = false;
draw_hand_force_panels(
ctx,
&mut self.export_panel,
&self.recorder,
&mut self.export_path,
self.hand_force_panels_visible(),
&self.hand_signal_histories,
);
draw_matrix_config_panel(ctx, &mut self.matrix_config_panel, &mut self.matrix_config);
}
fn draw_panel_context_menu(&mut self, ui: &mut egui::Ui) {
let response = ui.interact(
ui.max_rect(),
egui::Id::new("workspace_panel_context_menu"),
egui::Sense::click(),
);
fn draw_panel_context_menu(&mut self, ctx: &egui::Context) {
let (secondary_clicked, pointer_pos, escape_pressed) = ctx.input(|input| {
(
input.pointer.secondary_clicked(),
input.pointer.interact_pos(),
input.key_pressed(egui::Key::Escape),
)
});
response.context_menu(|ui| {
if secondary_clicked {
self.context_menu_open = true;
self.context_menu_pos = pointer_pos.unwrap_or(self.context_menu_pos);
}
if escape_pressed {
self.context_menu_open = false;
}
if !self.context_menu_open {
return;
}
let mut close_menu = false;
let response = egui::Area::new(egui::Id::new("workspace_context_menu_area"))
.fixed_pos(self.context_menu_pos)
.order(egui::Order::Foreground)
.show(ctx, |ui| {
egui::Frame::popup(ui.style()).show(ui, |ui| {
ui.set_min_width(132.0);
ui.label("显示面板");
ui.separator();
panel_restore_item(ui, "配置", &mut self.config_panel);
panel_restore_item(ui, "录制", &mut self.export_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);
}
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.export_panel.visible = true;
self.matrix_config_panel.visible = true;
self.stats_panel.visible = true;
ui.close();
close_menu = true;
}
});
});
let clicked_outside = ctx.input(|input| {
input.pointer.primary_clicked()
&& input
.pointer
.interact_pos()
.is_some_and(|pos| !response.response.rect.contains(pos))
});
if close_menu || clicked_outside {
self.context_menu_open = false;
}
}
fn switch_mode(&mut self, next: SerialMode) {
self.connect_state.mode = next;
self.config_state.mode = next;
self.config_state.baud_rate = next.baud_rate();
self.connection.disconnect();
self.pressure_matrix.fill([0.0, 0.0]);
self.data_log_frame = 0;
self.hand_pressure.clear();
self.last_sample_at = None;
self.force_estimator.reset();
self.latest_spatial_force = None;
self.latest_hand_spatial_forces = [None; HAND_FINGERTIP_COUNT];
self.spatial_force_panel_force = None;
self.signal_history.clear();
self.hand_signal_histories
.iter_mut()
.for_each(|history| history.clear());
self.active_mode = match next {
SerialMode::Finger => ActiveMode::Finger(FingerMode {
@@ -320,27 +608,351 @@ impl EskinDesktopApp {
}
}
fn log_pressure_sample(raw: &[u32], rows: u32, cols: u32) {
let max = raw.iter().copied().max().unwrap_or(0);
let sum: u64 = raw.iter().map(|value| *value as u64).sum();
let non_zero = raw.iter().filter(|value| **value != 0).count();
let preview = raw
.iter()
.take(12)
.map(u32::to_string)
.collect::<Vec<_>>()
.join(", ");
fn hand_image_pixel_to_screen(rect: egui::Rect, point_px: [f32; 2]) -> egui::Pos2 {
let image_uv = egui::vec2(
point_px[0] / HAND_IMAGE_SIZE_PX[0].max(1.0),
point_px[1] / HAND_IMAGE_SIZE_PX[1].max(1.0),
);
let viewport_aspect = rect.width() / rect.height().max(1.0);
let image_aspect = HAND_IMAGE_SIZE_PX[0] / HAND_IMAGE_SIZE_PX[1].max(1.0);
let mut screen_uv = image_uv;
println!(
"[pressure] {rows}x{cols} cells={} non_zero={non_zero} max={max} sum={sum} first=[{preview}]",
raw.len()
if viewport_aspect > image_aspect {
let image_width = image_aspect / viewport_aspect;
screen_uv.x = image_uv.x * image_width + (1.0 - image_width) * 0.5;
} else {
let image_height = viewport_aspect / image_aspect;
screen_uv.y = image_uv.y * image_height + (1.0 - image_height) * 0.5;
}
egui::pos2(
rect.left() + screen_uv.x * rect.width(),
rect.top() + screen_uv.y * rect.height(),
)
}
fn paint_spatial_force_arrow(
painter: &egui::Painter,
center: egui::Pos2,
force: HudSpatialForce,
base_length: f32,
magnitude_scale: f32,
) {
let magnitude = force.magnitude.clamp(0.0, 1.8);
let length = base_length + magnitude * magnitude_scale;
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 head_length = (length * 0.24).clamp(8.0, 14.0);
let head_width = head_length * 0.5;
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(8.0, glow));
painter.line_segment([center, end], egui::Stroke::new(2.4, color));
painter.line_segment(
[end, end - direction * head_length + side * head_width],
egui::Stroke::new(2.4, color),
);
painter.line_segment(
[end, end - direction * head_length - side * head_width],
egui::Stroke::new(2.4, color),
);
painter.circle_filled(center, 3.8, color);
}
fn update_hand_signal_histories(
histories: &mut [Vec<f32>; HAND_FORCE_PANEL_COUNT],
raw_values: &[u32],
) {
let mut offset = 0usize;
for (index, (history, sample_count)) in histories.iter_mut().zip(HAND_FORCE_SEGMENT_COUNTS).enumerate() {
let raw_total = raw_values
.get(offset..offset + sample_count)
.unwrap_or(&[])
.iter()
.fold(0_u64, |sum, value| sum + *value as u64)
.min(u32::MAX as u64) as u32;
let force = match index {
0 => raw_to_g1(raw_total).min(20.6),
1 => raw_to_g2(raw_total).min(20.6),
2 => raw_to_g3(raw_total).min(20.6),
3 => raw_to_g4(raw_total).min(20.6),
4 => raw_to_g5(raw_total).min(20.6),
_ => raw_to_gd(raw_total).min(20.6),
};
// let force = raw_to_g1(raw_total).min(25.6);
let force = if force <= 0.1 { 0.0 } else { force };
history.push(force);
if history.len() > SUMMARY_POINTS_PER_SERIES {
history.remove(0);
}
offset += sample_count;
}
}
fn raw_to_gd(raw: u32) -> f32 {
const RAW: [u32; 12] = [
0, 21382, 108507, 123183, 147405, 171105, 192395, 250443, 231423, 350560, 396616, 429444,
];
const FORCE_CENTI_N: [f32; 12] = [
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0, 2557.0,
];
if raw <= RAW[0] {
return FORCE_CENTI_N[0] / 100.0;
}
if raw >= RAW[RAW.len() - 1] {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
}
let mut left = 0usize;
let mut right = RAW.len() - 1;
while left + 1 < right {
let mid = (left + right) / 2;
if RAW[mid] <= raw {
left = mid;
} else {
right = mid;
}
}
let raw_left = RAW[left] as f32;
let raw_right = RAW[right] as f32;
let span = (raw_right - raw_left).max(1.0);
let ratio = (raw as f32 - raw_left) / span;
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
}
fn raw_to_g1(raw: u32) -> f32 {
const RAW: [u32; 11] = [
0, 20239, 121454, 171532, 223968, 241467, 293500, 322744, 436294, 523275, 633083,
];
const FORCE_CENTI_N: [f32; 11] = [
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
];
if raw <= RAW[0] {
return FORCE_CENTI_N[0] / 100.0;
}
if raw >= RAW[RAW.len() - 1] {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
}
let mut left = 0usize;
let mut right = RAW.len() - 1;
while left + 1 < right {
let mid = (left + right) / 2;
if RAW[mid] <= raw {
left = mid;
} else {
right = mid;
}
}
let raw_left = RAW[left] as f32;
let raw_right = RAW[right] as f32;
let span = (raw_right - raw_left).max(1.0);
let ratio = (raw as f32 - raw_left) / span;
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
}
fn raw_to_g2(raw: u32) -> f32 {
const RAW: [u32; 11] = [
0, 5398, 52896, 86945, 122546, 145001, 165933, 220575, 241578, 350285, 449530,
];
const FORCE_CENTI_N: [f32; 11] = [
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
];
if raw <= RAW[0] {
return FORCE_CENTI_N[0] / 100.0;
}
if raw >= RAW[RAW.len() - 1] {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
}
let mut left = 0usize;
let mut right = RAW.len() - 1;
while left + 1 < right {
let mid = (left + right) / 2;
if RAW[mid] <= raw {
left = mid;
} else {
right = mid;
}
}
let raw_left = RAW[left] as f32;
let raw_right = RAW[right] as f32;
let span = (raw_right - raw_left).max(1.0);
let ratio = (raw as f32 - raw_left) / span;
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
}
fn raw_to_g3(raw: u32) -> f32 {
const RAW: [u32; 11] = [
0, 4920, 35945, 46499, 65566, 69613, 89971, 122321, 146384, 203426, 269474,
];
const FORCE_CENTI_N: [f32; 11] = [
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
];
if raw <= RAW[0] {
return FORCE_CENTI_N[0] / 100.0;
}
if raw >= RAW[RAW.len() - 1] {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
}
let mut left = 0usize;
let mut right = RAW.len() - 1;
while left + 1 < right {
let mid = (left + right) / 2;
if RAW[mid] <= raw {
left = mid;
} else {
right = mid;
}
}
let raw_left = RAW[left] as f32;
let raw_right = RAW[right] as f32;
let span = (raw_right - raw_left).max(1.0);
let ratio = (raw as f32 - raw_left) / span;
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
}
fn raw_to_g4(raw: u32) -> f32 {
const RAW: [u32; 11] = [
0, 11346, 92229, 139822, 182282, 220004, 259898, 307303, 377922, 460755, 587834,
];
const FORCE_CENTI_N: [f32; 11] = [
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
];
if raw <= RAW[0] {
return FORCE_CENTI_N[0] / 100.0;
}
if raw >= RAW[RAW.len() - 1] {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
}
let mut left = 0usize;
let mut right = RAW.len() - 1;
while left + 1 < right {
let mid = (left + right) / 2;
if RAW[mid] <= raw {
left = mid;
} else {
right = mid;
}
}
let raw_left = RAW[left] as f32;
let raw_right = RAW[right] as f32;
let span = (raw_right - raw_left).max(1.0);
let ratio = (raw as f32 - raw_left) / span;
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
}
fn raw_to_g5(raw: u32) -> f32 {
const RAW: [u32; 11] = [
0, 10081, 40910, 49944, 64291, 68299, 79282, 97767, 113567, 140520, 167132,
];
const FORCE_CENTI_N: [f32; 11] = [
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
];
if raw <= RAW[0] {
return FORCE_CENTI_N[0] / 100.0;
}
if raw >= RAW[RAW.len() - 1] {
return FORCE_CENTI_N[FORCE_CENTI_N.len() - 1] / 100.0;
}
let mut left = 0usize;
let mut right = RAW.len() - 1;
while left + 1 < right {
let mid = (left + right) / 2;
if RAW[mid] <= raw {
left = mid;
} else {
right = mid;
}
}
let raw_left = RAW[left] as f32;
let raw_right = RAW[right] as f32;
let span = (raw_right - raw_left).max(1.0);
let ratio = (raw as f32 - raw_left) / span;
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
}
fn paint_split_viewport_shell(
ui: &egui::Ui,
rect: egui::Rect,
title: &'static str,
subtitle: &'static str,
) {
let painter = ui.painter();
painter.rect_filled(
rect,
egui::CornerRadius::same(8),
egui::Color32::from_rgb(11, 17, 23),
);
painter.rect_stroke(
rect,
egui::CornerRadius::same(8),
egui::Stroke::new(1.0, ONE_DARK_PRO.border_soft),
egui::StrokeKind::Outside,
);
painter.line_segment(
[
rect.left_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),
);
painter.text(
rect.left_top() + egui::vec2(14.0, 16.0),
egui::Align2::LEFT_CENTER,
title,
egui::FontId::monospace(13.0),
ONE_DARK_PRO.accent,
);
painter.text(
rect.left_top() + egui::vec2(122.0, 16.0),
egui::Align2::LEFT_CENTER,
subtitle,
egui::FontId::proportional(12.0),
ONE_DARK_PRO.text_dim,
);
}
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;
fn split_viewport_body(rect: egui::Rect) -> egui::Rect {
egui::Rect::from_min_max(
rect.left_top() + egui::vec2(10.0, 44.0),
rect.right_bottom() - egui::vec2(10.0, 10.0),
)
}
fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut PressureFrame) {
normalized.fill([0.0, 0.0]);
let src_cols = cols.max(1);
@@ -352,27 +964,71 @@ fn normalize_pressure_sample(raw: &[u32], rows: u32, cols: u32, normalized: &mut
let src_index = (row * src_cols + col) as usize;
let dst_index = (row * MATRIX_COLS + col) as usize;
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;
const DISPLAY_GAMMA: f32 = 0.45;
let raw_value = value as f32;
let mapped = ((raw_value - RANGE_MIN) / (RANGE_MAX - RANGE_MIN)).clamp(0.0, 1.0);
let intensity = if raw_value <= RANGE_MIN + 4.0 {
0.0
} else {
mapped.powf(DISPLAY_GAMMA)
};
let display_value = if raw_value <= RANGE_MIN + 4.0 {
0.0
} else {
raw_value.round().min(9999.0)
};
normalized[dst_index] = [mapped, display_value];
}
}
[intensity, display_value]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pressure_normalization_makes_low_hand_values_visible() {
let [zero_intensity, zero_label] = normalize_pressure_value(4);
assert_eq!(zero_intensity, 0.0);
assert_eq!(zero_label, 0.0);
let [low_intensity, low_label] = normalize_pressure_value(100);
assert!(
low_intensity > 0.10,
"low hand-gateway values should be visible in the wgpu dot matrix"
);
assert_eq!(low_label, 100.0);
let [max_intensity, max_label] = normalize_pressure_value(7000);
assert_eq!(max_intensity, 1.0);
assert_eq!(max_label, 7000.0);
}
}
impl eframe::App for EskinDesktopApp {
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
let ctx = ui.ctx().clone();
let stats = self.connection.stats();
self.live_rates.tick_render(stats.rx_frames);
self.draw_wgpu_background(ui);
self.draw_panel_context_menu(ui);
self.update_pressure_matrix();
self.sync_disconnected_live_state();
self.draw_workspace(ui);
self.draw_title_bar(ui, frame);
self.draw_floating_panels(&ctx);
self.draw_floating_panels(&ctx, stats);
// Keep repainting while the wgpu background is a realtime viewport.
ctx.request_repaint();

655
src/breakout.rs Normal file
View File

@@ -0,0 +1,655 @@
use eframe::egui;
use crate::style::{self, ACCENT_BLUE, ACCENT_GREEN, ACCENT_ORANGE, ACCENT_RED, ONE_DARK_PRO};
const BRICK_ROWS: usize = 7;
const BRICK_COLS: usize = 12;
const PADDLE_W: f32 = 0.24;
const PADDLE_H: f32 = 0.030;
const PADDLE_Y: f32 = 0.88;
const PADDLE_SPEED: f32 = 0.92;
const BALL_RADIUS: f32 = 0.010;
const BALL_SPEED: f32 = 0.52;
const MAX_BALL_SPEED: f32 = 0.86;
const PRESSURE_RANGE_MIN: f32 = 0.0;
const PRESSURE_RANGE_MAX: f32 = 7000.0;
const PAUSE_COOLDOWN: f64 = 0.64;
#[derive(Clone, Copy, PartialEq, Eq)]
enum BreakoutPhase {
Idle,
Running,
Paused,
Won,
Over,
}
struct Brick {
rect: egui::Rect,
alive: bool,
flash: f32,
}
#[derive(Default, Clone, Copy)]
pub struct BreakoutControl {
pub axis: f32,
pub top_force: f32,
pub left_force: f32,
pub right_force: f32,
}
pub struct BreakoutGame {
phase: BreakoutPhase,
bricks: Vec<Brick>,
paddle_x: f32,
ball_pos: egui::Pos2,
ball_vel: egui::Vec2,
score: u32,
combo: u32,
lives: u32,
last_time: Option<f64>,
previous_pause_gesture: bool,
pause_locked_until: f64,
}
impl Default for BreakoutGame {
fn default() -> Self {
let mut game = Self {
phase: BreakoutPhase::Idle,
bricks: Vec::new(),
paddle_x: 0.5,
ball_pos: egui::pos2(0.5, PADDLE_Y - PADDLE_H - BALL_RADIUS),
ball_vel: egui::vec2(0.0, 0.0),
score: 0,
combo: 0,
lives: 3,
last_time: None,
previous_pause_gesture: false,
pause_locked_until: 0.0,
};
game.rebuild_bricks();
game
}
}
impl BreakoutGame {
pub fn draw_viewport(
&mut self,
ctx: &egui::Context,
rect: egui::Rect,
visible: &mut bool,
control: BreakoutControl,
) {
if !*visible {
self.last_time = None;
return;
}
ctx.request_repaint();
let now = ctx.input(|input| input.time);
let dt = self
.last_time
.map(|last| (now - last) as f32)
.unwrap_or(1.0 / 60.0)
.clamp(0.0, 1.0 / 30.0);
self.last_time = Some(now);
let mut close_requested = false;
egui::Area::new(egui::Id::new("breakout_game_viewport"))
.fixed_pos(rect.min)
.order(egui::Order::Middle)
.show(ctx, |ui| {
ui.set_min_size(rect.size());
ui.set_max_size(rect.size());
style::group_frame().show(ui, |ui| {
ui.set_min_size(rect.size() - egui::vec2(20.0, 16.0));
if self.draw_contents(ui, control, dt, now, true) {
close_requested = true;
}
});
});
if close_requested {
*visible = false;
}
}
fn draw_contents(
&mut self,
ui: &mut egui::Ui,
control: BreakoutControl,
dt: f32,
now: f64,
embedded: bool,
) -> bool {
let mut close_requested = false;
ui.horizontal(|ui| {
ui.colored_label(
ACCENT_BLUE,
egui::RichText::new("NEON BREAKOUT").monospace().size(13.0),
);
ui.label(style::subtle_text("压力矩阵控制 / 左右分区驱动"));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if embedded && ui.add(style::tag_button("退出分屏")).clicked() {
close_requested = true;
}
if ui.add(style::tag_button("重开")).clicked() {
self.reset();
self.start();
}
if ui.add(style::tag_button("暂停")).clicked() {
self.toggle_pause();
}
});
});
ui.add_space(5.0);
ui.horizontal(|ui| {
status_chip(ui, "状态", self.status_text(), status_color(self.phase));
status_chip(ui, "分数", self.score.to_string(), ACCENT_GREEN);
status_chip(ui, "连击", self.combo.to_string(), ACCENT_ORANGE);
status_chip(ui, "生命", self.lives.to_string(), ACCENT_RED);
});
ui.add_space(7.0);
let available = ui.available_size();
let game_size = egui::vec2(available.x.max(500.0), (available.y - 6.0).max(330.0));
let (rect, response) = ui.allocate_exact_size(game_size, egui::Sense::click());
if response.clicked() && self.phase != BreakoutPhase::Running {
self.start();
}
let keyboard_axis = ui.input(|input| {
(input.key_down(egui::Key::ArrowRight) as i32
- input.key_down(egui::Key::ArrowLeft) as i32) as f32
});
if ui.input(|input| input.key_pressed(egui::Key::Space)) {
self.start();
}
if ui.input(|input| input.key_pressed(egui::Key::P)) {
self.toggle_pause();
}
let axis = if keyboard_axis.abs() > 0.0 {
keyboard_axis
} else {
control.axis
};
self.handle_pressure_gesture(control.top_force, now);
self.update(dt, axis);
self.paint(ui, rect, control);
close_requested
}
fn reset(&mut self) {
self.phase = BreakoutPhase::Idle;
self.paddle_x = 0.5;
self.ball_pos = egui::pos2(0.5, PADDLE_Y - PADDLE_H - BALL_RADIUS);
self.ball_vel = egui::Vec2::ZERO;
self.score = 0;
self.combo = 0;
self.lives = 3;
self.rebuild_bricks();
}
fn start(&mut self) {
if self.phase == BreakoutPhase::Over || self.phase == BreakoutPhase::Won {
self.reset();
}
if self.phase != BreakoutPhase::Running {
self.phase = BreakoutPhase::Running;
if self.ball_vel == egui::Vec2::ZERO {
self.launch_ball();
}
}
}
fn toggle_pause(&mut self) {
self.phase = match self.phase {
BreakoutPhase::Running => BreakoutPhase::Paused,
BreakoutPhase::Paused => BreakoutPhase::Running,
other => other,
};
}
fn handle_pressure_gesture(&mut self, top_force: f32, now: f64) {
let threshold = pressure_pause_threshold();
let active = top_force >= threshold;
if active && !self.previous_pause_gesture && now >= self.pause_locked_until {
if matches!(
self.phase,
BreakoutPhase::Idle | BreakoutPhase::Over | BreakoutPhase::Won
) {
self.start();
} else {
self.toggle_pause();
}
self.pause_locked_until = now + PAUSE_COOLDOWN;
}
self.previous_pause_gesture = active;
}
fn update(&mut self, dt: f32, axis: f32) {
for brick in &mut self.bricks {
brick.flash = (brick.flash - dt * 2.4).max(0.0);
}
if self.phase != BreakoutPhase::Running {
self.ball_pos = egui::pos2(self.paddle_x, PADDLE_Y - PADDLE_H - BALL_RADIUS);
return;
}
self.paddle_x = (self.paddle_x + axis.clamp(-1.0, 1.0) * PADDLE_SPEED * dt)
.clamp(PADDLE_W * 0.5, 1.0 - PADDLE_W * 0.5);
self.ball_pos += self.ball_vel * dt;
self.resolve_wall_collision();
self.resolve_paddle_collision();
self.resolve_brick_collision();
}
fn launch_ball(&mut self) {
let direction = 0.24;
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;
}
fn resolve_wall_collision(&mut self) {
if self.ball_pos.x - BALL_RADIUS <= 0.0 {
self.ball_pos.x = BALL_RADIUS;
self.ball_vel.x = self.ball_vel.x.abs();
} else if self.ball_pos.x + BALL_RADIUS >= 1.0 {
self.ball_pos.x = 1.0 - BALL_RADIUS;
self.ball_vel.x = -self.ball_vel.x.abs();
}
if self.ball_pos.y - BALL_RADIUS <= 0.0 {
self.ball_pos.y = BALL_RADIUS;
self.ball_vel.y = self.ball_vel.y.abs();
}
if self.ball_pos.y - BALL_RADIUS > 1.0 {
self.lives = self.lives.saturating_sub(1);
self.combo = 0;
if self.lives == 0 {
self.phase = BreakoutPhase::Over;
self.ball_vel = egui::Vec2::ZERO;
} else {
self.ball_pos = egui::pos2(self.paddle_x, PADDLE_Y - PADDLE_H - BALL_RADIUS);
self.launch_ball();
}
}
}
fn resolve_paddle_collision(&mut self) {
if self.ball_vel.y <= 0.0 {
return;
}
let paddle = paddle_rect(self.paddle_x);
if !circle_hits_rect(self.ball_pos, BALL_RADIUS, paddle) {
return;
}
self.ball_pos.y = paddle.top() - BALL_RADIUS;
let offset = ((self.ball_pos.x - self.paddle_x) / (PADDLE_W * 0.5)).clamp(-1.0, 1.0);
let speed = self.ball_vel.length().clamp(BALL_SPEED, MAX_BALL_SPEED);
self.ball_vel = egui::vec2(offset * 0.82, -1.0).normalized() * speed;
self.combo = 0;
}
fn resolve_brick_collision(&mut self) {
for brick in &mut self.bricks {
if !brick.alive || !circle_hits_rect(self.ball_pos, BALL_RADIUS, brick.rect) {
continue;
}
let overlap_x = (self.ball_pos.x + BALL_RADIUS - brick.rect.left())
.min(brick.rect.right() - (self.ball_pos.x - BALL_RADIUS));
let overlap_y = (self.ball_pos.y + BALL_RADIUS - brick.rect.top())
.min(brick.rect.bottom() - (self.ball_pos.y - BALL_RADIUS));
if overlap_x < overlap_y {
self.ball_vel.x *= -1.0;
} else {
self.ball_vel.y *= -1.0;
}
brick.alive = false;
brick.flash = 1.0;
self.combo += 1;
self.score += 40 + self.combo * 5;
let speed = (self.ball_vel.length() * 1.014).min(MAX_BALL_SPEED);
self.ball_vel = self.ball_vel.normalized() * speed;
break;
}
if self.bricks.iter().all(|brick| !brick.alive) {
self.phase = BreakoutPhase::Won;
self.ball_vel = egui::Vec2::ZERO;
}
}
fn rebuild_bricks(&mut self) {
self.bricks.clear();
let gap_x = 0.012;
let gap_y = 0.017;
let left = 0.075;
let top = 0.10;
let total_w = 0.85;
let brick_w = (total_w - gap_x * (BRICK_COLS - 1) as f32) / BRICK_COLS as f32;
let brick_h = 0.040;
for row in 0..BRICK_ROWS {
for col in 0..BRICK_COLS {
let x = left + col as f32 * (brick_w + gap_x);
let y = top + row as f32 * (brick_h + gap_y);
self.bricks.push(Brick {
rect: egui::Rect::from_min_size(egui::pos2(x, y), egui::vec2(brick_w, brick_h)),
alive: true,
flash: 0.0,
});
}
}
}
fn status_text(&self) -> &'static str {
match self.phase {
BreakoutPhase::Idle => "待机",
BreakoutPhase::Running => "运行",
BreakoutPhase::Paused => "暂停",
BreakoutPhase::Won => "过关",
BreakoutPhase::Over => "结束",
}
}
fn paint(&self, ui: &egui::Ui, rect: egui::Rect, control: BreakoutControl) {
let painter = ui.painter_at(rect);
painter.rect_filled(
rect,
egui::CornerRadius::same(6),
egui::Color32::from_rgb(8, 14, 19),
);
painter.rect_stroke(
rect,
egui::CornerRadius::same(6),
egui::Stroke::new(1.0, color_alpha(ONE_DARK_PRO.accent, 110)),
egui::StrokeKind::Outside,
);
paint_arena_grid(&painter, rect);
for (index, brick) in self.bricks.iter().enumerate() {
paint_brick(&painter, rect, brick, index);
}
paint_paddle(&painter, rect, self.paddle_x);
paint_ball(&painter, rect, self.ball_pos);
paint_control_meter(&painter, rect, control);
if self.phase == BreakoutPhase::Idle
|| self.phase == BreakoutPhase::Paused
|| self.phase == BreakoutPhase::Won
|| self.phase == BreakoutPhase::Over
{
paint_center_overlay(&painter, rect, self.phase);
}
}
}
pub fn control_from_matrix(raw: &[u32], rows: u32, cols: u32) -> BreakoutControl {
if raw.is_empty() {
return BreakoutControl::default();
}
let rows = rows.max(1) as usize;
let cols = cols.max(1) as usize;
let sample_rows = rows.min(2);
let sample_cols = cols.min(2);
let top_gesture_rows = rows.min(1);
let top_gesture_cols = (cols / 3).clamp(1, cols);
let top_gesture_col_start = (cols - top_gesture_cols) / 2;
let top_gesture_col_end = top_gesture_col_start + top_gesture_cols;
let avg = |row_start: usize, row_end: usize, col_start: usize, col_end: usize| -> f32 {
let mut sum = 0.0;
let mut count = 0.0;
for row in row_start..row_end {
for col in col_start..col_end {
let index = row * cols + col;
if let Some(value) = raw.get(index) {
sum += *value as f32;
count += 1.0;
}
}
}
if count > 0.0 { sum / count } else { 0.0 }
};
let tl = avg(0, sample_rows, 0, sample_cols);
let tr = avg(0, sample_rows, cols.saturating_sub(sample_cols), cols);
let bl = avg(rows.saturating_sub(sample_rows), rows, 0, sample_cols);
let br = avg(
rows.saturating_sub(sample_rows),
rows,
cols.saturating_sub(sample_cols),
cols,
);
let left_force = tl + bl;
let right_force = tr + br;
let top_force = avg(
0,
top_gesture_rows,
top_gesture_col_start,
top_gesture_col_end,
);
let span = 1200.0_f32.max((PRESSURE_RANGE_MAX - PRESSURE_RANGE_MIN) * 0.22);
let raw_axis = ((right_force - left_force) / span).clamp(-1.0, 1.0);
let axis = if raw_axis.abs() < 0.045 {
0.0
} else {
raw_axis
};
BreakoutControl {
axis,
top_force,
left_force,
right_force,
}
}
fn pressure_pause_threshold() -> f32 {
420.0_f32.max(((PRESSURE_RANGE_MAX - PRESSURE_RANGE_MIN).max(1000.0) * 0.07).round())
}
fn paddle_rect(paddle_x: f32) -> egui::Rect {
egui::Rect::from_center_size(
egui::pos2(paddle_x, PADDLE_Y),
egui::vec2(PADDLE_W, PADDLE_H),
)
}
fn circle_hits_rect(center: egui::Pos2, radius: f32, rect: egui::Rect) -> bool {
let closest_x = center.x.clamp(rect.left(), rect.right());
let closest_y = center.y.clamp(rect.top(), rect.bottom());
let dx = center.x - closest_x;
let dy = center.y - closest_y;
dx * dx + dy * dy <= radius * radius
}
fn status_chip(ui: &mut egui::Ui, label: &'static str, value: impl ToString, color: egui::Color32) {
egui::Frame::new()
.fill(color_alpha(ONE_DARK_PRO.panel_deep, 190))
.stroke(egui::Stroke::new(1.0, color_alpha(color, 92)))
.corner_radius(egui::CornerRadius::same(4))
.inner_margin(egui::Margin::symmetric(8, 4))
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.colored_label(ONE_DARK_PRO.text_subtle, label);
ui.colored_label(color, value.to_string());
});
});
}
fn status_color(phase: BreakoutPhase) -> egui::Color32 {
match phase {
BreakoutPhase::Idle => ONE_DARK_PRO.text_dim,
BreakoutPhase::Running => ACCENT_GREEN,
BreakoutPhase::Paused => ACCENT_ORANGE,
BreakoutPhase::Won => ACCENT_GREEN,
BreakoutPhase::Over => ACCENT_RED,
}
}
fn to_screen(rect: egui::Rect, point: egui::Pos2) -> egui::Pos2 {
egui::pos2(
rect.left() + point.x * rect.width(),
rect.top() + point.y * rect.height(),
)
}
fn to_screen_rect(rect: egui::Rect, local: egui::Rect) -> egui::Rect {
egui::Rect::from_min_max(
rect.left_top() + local.min.to_vec2() * rect.size(),
rect.left_top() + local.max.to_vec2() * rect.size(),
)
}
fn paint_arena_grid(painter: &egui::Painter, rect: egui::Rect) {
// 游戏背景保持纯净,避免网格和外框干扰砖块本身的色块节奏。
painter.rect_filled(
rect,
egui::CornerRadius::same(6),
egui::Color32::from_rgb(2, 8, 10),
);
painter.rect_stroke(
rect,
egui::CornerRadius::same(6),
egui::Stroke::new(1.0, color_alpha(ONE_DARK_PRO.accent, 70)),
egui::StrokeKind::Inside,
);
}
fn paint_brick(painter: &egui::Painter, arena: egui::Rect, brick: &Brick, index: usize) {
let rect = to_screen_rect(arena, brick.rect);
if brick.alive {
let row = index / BRICK_COLS;
let col = index % BRICK_COLS;
// 参考示例图:纯色矩形砖块,黑色间隔由砖块之间的空隙自然露出。
painter.rect_filled(rect, egui::CornerRadius::ZERO, brick_color(row, col));
} else if brick.flash > 0.0 {
let alpha = (brick.flash * 150.0) as u8;
painter.rect_stroke(
rect.expand(4.0 * brick.flash),
egui::CornerRadius::same(4),
egui::Stroke::new(1.4, color_alpha(ONE_DARK_PRO.accent_hot, alpha)),
egui::StrokeKind::Outside,
);
}
}
fn paint_paddle(painter: &egui::Painter, arena: egui::Rect, paddle_x: f32) {
let rect = to_screen_rect(arena, paddle_rect(paddle_x));
// 托盘也改成纯色块,和砖块保持同一种视觉语言。
painter.rect_filled(rect, egui::CornerRadius::ZERO, ACCENT_BLUE);
}
fn paint_ball(painter: &egui::Painter, arena: egui::Rect, ball_pos: egui::Pos2) {
let center = to_screen(arena, ball_pos);
let radius = BALL_RADIUS * arena.width().min(arena.height());
// 球保留为一个普通实心圆,避免额外高光和光晕。
painter.circle_filled(center, radius, egui::Color32::from_rgb(241, 207, 70));
}
fn paint_control_meter(painter: &egui::Painter, arena: egui::Rect, control: BreakoutControl) {
let meter_width = arena.width().mul_add(0.30, 0.0).clamp(180.0, 240.0);
let meter = egui::Rect::from_min_size(
arena.left_bottom() + egui::vec2(18.0, -34.0),
egui::vec2(meter_width, 18.0),
);
painter.rect_filled(
meter,
egui::CornerRadius::same(4),
color_alpha(ONE_DARK_PRO.panel_deep, 220),
);
painter.rect_stroke(
meter,
egui::CornerRadius::same(4),
egui::Stroke::new(1.0, color_alpha(ONE_DARK_PRO.border, 120)),
egui::StrokeKind::Outside,
);
let center_x = meter.center().x;
painter.line_segment(
[
egui::pos2(center_x, meter.top()),
egui::pos2(center_x, meter.bottom()),
],
egui::Stroke::new(1.0, ONE_DARK_PRO.border_soft),
);
let marker_x = center_x + control.axis.clamp(-1.0, 1.0) * meter.width() * 0.45;
painter.circle_filled(
egui::pos2(marker_x, meter.center().y),
5.0,
color_alpha(ACCENT_GREEN, 210),
);
painter.circle_filled(
egui::pos2(marker_x, meter.center().y),
2.3,
color_alpha(egui::Color32::WHITE, 150),
);
painter.text(
meter.right_center() + egui::vec2(10.0, 0.0),
egui::Align2::LEFT_CENTER,
format!(
"L {:>4.0} R {:>4.0} T {:>4.0}",
control.left_force, control.right_force, control.top_force
),
egui::FontId::monospace(10.0),
ONE_DARK_PRO.text_dim,
);
}
fn paint_center_overlay(painter: &egui::Painter, rect: egui::Rect, phase: BreakoutPhase) {
painter.rect_filled(
rect,
egui::CornerRadius::same(6),
color_alpha(egui::Color32::BLACK, 82),
);
let (title, detail) = match phase {
BreakoutPhase::Idle => ("按压顶部或点击开始", "左右分区压力控制挡板"),
BreakoutPhase::Paused => ("已暂停", "再次按压顶部、P 或点击暂停继续"),
BreakoutPhase::Won => ("恭喜过关", "按压顶部、点击或空格重新开始"),
BreakoutPhase::Over => ("游戏结束", "点击或空格重开"),
BreakoutPhase::Running => ("", ""),
};
painter.text(
rect.center() - egui::vec2(0.0, 10.0),
egui::Align2::CENTER_CENTER,
title,
egui::FontId::proportional(24.0),
ONE_DARK_PRO.text,
);
painter.text(
rect.center() + egui::vec2(0.0, 20.0),
egui::Align2::CENTER_CENTER,
detail,
egui::FontId::proportional(13.0),
ONE_DARK_PRO.text_dim,
);
}
fn brick_color(row: usize, col: usize) -> egui::Color32 {
const PALETTE: [egui::Color32; 5] = [
egui::Color32::from_rgb(46, 178, 104),
egui::Color32::from_rgb(73, 188, 220),
egui::Color32::from_rgb(234, 199, 73),
egui::Color32::from_rgb(232, 125, 64),
egui::Color32::from_rgb(237, 68, 62),
];
PALETTE[(col + row * 2) % PALETTE.len()]
}
fn color_alpha(color: egui::Color32, alpha: u8) -> egui::Color32 {
egui::Color32::from_rgba_premultiplied(color.r(), color.g(), color.b(), alpha)
}

View File

@@ -4,7 +4,10 @@ use std::time::Duration;
use crossbeam_channel::{self, Receiver, Sender, TryRecvError};
use crate::serial_core::serial::{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.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -27,6 +30,9 @@ struct Session {
cancel_tx: Sender<()>,
handle: JoinHandle<()>,
sample_rx: Receiver<Vec<i32>>,
stats_rx: Receiver<SerialIoStats>,
rows: u32,
cols: u32,
}
/// Thread-safe connection manager that the UI and renderer can share.
@@ -34,6 +40,7 @@ pub struct ConnectionManager {
state: Arc<Mutex<ConnectionState>>,
session: Arc<Mutex<Option<Session>>>,
latest_sample: Arc<Mutex<Option<PressureSample>>>,
stats: Arc<Mutex<SerialIoStats>>,
}
impl ConnectionManager {
@@ -42,6 +49,7 @@ impl ConnectionManager {
state: Arc::new(Mutex::new(ConnectionState::Disconnected)),
session: Arc::new(Mutex::new(None)),
latest_sample: Arc::new(Mutex::new(None)),
stats: Arc::new(Mutex::new(SerialIoStats::default())),
}
}
@@ -53,8 +61,31 @@ impl ConnectionManager {
*self.state.lock().unwrap() = new_state;
}
pub fn stats(&self) -> SerialIoStats {
let session = self.session.lock().unwrap();
if let Some(ref session) = *session {
loop {
match session.stats_rx.try_recv() {
Ok(stats) => *self.stats.lock().unwrap() = stats,
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => break,
}
}
}
*self.stats.lock().unwrap()
}
/// 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.set_state(ConnectionState::Connecting);
@@ -64,16 +95,22 @@ impl ConnectionManager {
let latest_sample = Arc::clone(&self.latest_sample);
let (cancel_tx, cancel_rx) = crossbeam_channel::bounded::<()>(1);
let (sample_tx, sample_rx) = crossbeam_channel::bounded::<Vec<i32>>(16);
let (stats_tx, stats_rx) = crossbeam_channel::bounded::<SerialIoStats>(16);
*self.stats.lock().unwrap() = SerialIoStats::default();
let handle = thread::spawn(move || {
let result = run_device_loop(
&port,
rows,
cols,
baud_rate,
protocol,
&state,
&cancel_rx,
&sample_tx,
&stats_tx,
&latest_sample,
recorder,
);
if let Err(e) = result {
eprintln!("[connection] device loop error: {e}");
@@ -85,6 +122,9 @@ impl ConnectionManager {
cancel_tx,
handle,
sample_rx,
stats_rx,
rows,
cols,
});
}
@@ -102,6 +142,7 @@ impl ConnectionManager {
self.set_state(ConnectionState::Disconnected);
*self.latest_sample.lock().unwrap() = None;
*self.stats.lock().unwrap() = SerialIoStats::default();
}
/// Drain pending samples (non-blocking) and return the last one.
@@ -112,10 +153,12 @@ impl ConnectionManager {
loop {
match session.sample_rx.try_recv() {
Ok(vals) => {
let rows = 12u32;
let cols = 7u32;
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::Disconnected) => break,
@@ -140,13 +183,17 @@ fn run_device_loop(
port_name: &str,
rows: u32,
cols: u32,
baud_rate: u32,
protocol: SerialProtocol,
state: &Arc<Mutex<ConnectionState>>,
cancel_rx: &Receiver<()>,
sample_tx: &Sender<Vec<i32>>,
stats_tx: &Sender<SerialIoStats>,
latest_sample: &Arc<Mutex<Option<PressureSample>>>,
recorder: Recorder,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let port = serialport::new(port_name, 921_600)
.timeout(Duration::from_millis(100))
let port = serialport::new(port_name, baud_rate)
.timeout(Duration::from_millis(1))
.open()?;
*state.lock().unwrap() = ConnectionState::Connected;
@@ -154,36 +201,19 @@ fn run_device_loop(
let mut rw = SerialPortReadWrite::new(port);
*state.lock().unwrap() = ConnectionState::Streaming;
// We need to also forward samples to latest_sample
let (inner_tx, inner_rx) = crossbeam_channel::bounded::<Vec<i32>>(16);
let latest = Arc::clone(latest_sample);
let outer_tx = sample_tx.clone();
run_serial_loop(
&mut rw,
rows as usize,
cols as usize,
protocol,
cancel_rx,
sample_tx,
Some(stats_tx),
Some(&recorder),
);
// Bridge thread: reads from inner channel, forwards to both sample_tx and latest_sample
let bridge_cancel = cancel_rx.clone();
let bridge_handle = thread::spawn(move || {
loop {
if bridge_cancel.try_recv().is_ok() {
break;
if let Ok(mut latest) = latest_sample.lock() {
*latest = None;
}
match inner_rx.try_recv() {
Ok(vals) => {
// Store latest
let matrix = vals.iter().map(|v| (*v).max(0) as u32).collect();
*latest.lock().unwrap() = Some(PressureSample { matrix, rows, cols });
// Forward
let _ = outer_tx.try_send(vals);
}
Err(TryRecvError::Empty) => {
std::thread::sleep(Duration::from_millis(1));
}
Err(TryRecvError::Disconnected) => break,
}
}
});
run_serial_loop(&mut rw, rows as usize, cols as usize, cancel_rx, &inner_tx);
let _ = bridge_handle.join();
Ok(())
}

86
src/force.rs Normal file
View File

@@ -0,0 +1,86 @@
use crate::serial_core::multi_dim_force::PztProcessor;
pub const FINGER_SAMPLE_COUNT: usize = 84;
pub const HAND_FINGERTIP_COUNT: usize = 5;
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,
fingertip_processors: [PztProcessor; HAND_FINGERTIP_COUNT],
}
impl ForceEstimatorState {
pub fn new() -> Self {
Self {
pzt_processor: PztProcessor::new(),
fingertip_processors: std::array::from_fn(|_| PztProcessor::new()),
}
}
pub fn reset(&mut self) {
self.pzt_processor.reset_baseline();
self.fingertip_processors
.iter_mut()
.for_each(PztProcessor::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,
})
}
pub fn analyze_fingertips(
&mut self,
values: &[u32],
) -> [Option<HudSpatialForce>; HAND_FINGERTIP_COUNT] {
let mut forces = [None; HAND_FINGERTIP_COUNT];
for (tip_index, processor) in self.fingertip_processors.iter_mut().enumerate() {
let start = tip_index * FINGER_SAMPLE_COUNT;
let end = start + FINGER_SAMPLE_COUNT;
let Some(segment) = values.get(start..end) else {
break;
};
let pzt_values = segment
.iter()
.map(|value| *value as f32)
.collect::<Vec<_>>();
forces[tip_index] = 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,
});
}
forces
}
}
impl Default for ForceEstimatorState {
fn default() -> Self {
Self::new()
}
}

View File

@@ -1,5 +1,9 @@
#![allow(dead_code)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod app;
mod breakout;
mod connection;
mod force;
mod matrix;
mod model;
mod recording;
@@ -16,12 +20,15 @@ use eframe::egui;
fn main() -> eframe::Result<()> {
env_logger::init();
let window_icon = eframe::icon_data::from_png_bytes(include_bytes!("../res/128x128@2x.png"))
.expect("load window icon failed");
let options = eframe::NativeOptions {
renderer: eframe::Renderer::Wgpu,
viewport: egui::ViewportBuilder::default()
.with_inner_size([1920.0, 1080.0])
.with_min_inner_size([1280.0, 720.0])
.with_decorations(false),
.with_decorations(false)
.with_icon(window_icon),
..Default::default()
};

View File

@@ -13,11 +13,13 @@ use std::ops::Range;
pub const PRESSURE_CELL_COUNT: usize =
(crate::matrix::MATRIX_ROWS * crate::matrix::MATRIX_COLS) as usize;
pub type PressureFrame = [[f32; 2]; PRESSURE_CELL_COUNT];
pub type PressureSamples = Vec<[f32; 2]>;
pub struct WgpuBackgroundCallback {
pub width: f32,
pub height: f32,
pub pressure: PressureFrame,
pub hand_pressure: PressureSamples,
pub active_mode: ActiveMode,
}
@@ -42,32 +44,54 @@ pub struct HandGatewayMode {
const HAND_TIP_MATRICES: [HandTipMatrix; 5] = [
HandTipMatrix {
center_px: [260.0, 490.0],
center_px: [265.0, 495.0],
size_px: [70.0, 120.0],
angle_rad: -0.45,
angle_rad: -0.40,
},
HandTipMatrix {
center_px: [360.0, 255.0],
center_px: [359.0, 260.0],
size_px: [70.0, 120.0],
angle_rad: -0.05,
angle_rad: -0.17,
},
HandTipMatrix {
center_px: [485.0, 225.0],
center_px: [482.0, 228.0],
size_px: [70.0, 120.0],
angle_rad: 0.0,
angle_rad: -0.03,
},
HandTipMatrix {
center_px: [595.0, 260.0],
center_px: [596.0, 265.0],
size_px: [70.0, 120.0],
angle_rad: 0.12,
angle_rad: 0.10,
},
HandTipMatrix {
center_px: [705.0, 385.0],
center_px: [693.0, 370.0],
size_px: [70.0, 120.0],
angle_rad: 0.28,
angle_rad: 0.22,
},
];
const HAND_PALM_CHIPS: [HandPalmChip; 2] = [
HandPalmChip {
center_px: [530.0, 593.0],
size_px: [258.0, 88.0],
angle_rad: 0.12,
rows: 5,
cols: 14,
},
HandPalmChip {
center_px: [610.0, 778.0],
size_px: [82.0, 228.0],
angle_rad: 0.05,
rows: 11,
cols: 4,
},
];
const HAND_FINGER_SENSOR_CELLS: usize = 12 * 7;
const HAND_PALM_HORIZONTAL_OFFSET: usize = HAND_FINGER_SENSOR_CELLS * 5;
const HAND_PALM_VERTICAL_OFFSET: usize = HAND_PALM_HORIZONTAL_OFFSET + 5 * 14;
const HAND_TIP_DOT_LOCAL_Y_OFFSET_PX: f32 = 14.0;
// 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.
// size_px keeps the same 7:12 aspect as the Finger-mode 7 columns x 12 rows matrix.
@@ -77,6 +101,16 @@ struct HandTipMatrix {
angle_rad: f32,
}
// Palm chips follow the hand layout: one horizontal 5x14 matrix and one vertical
// 11x4 matrix, rendered as dark inset chip tiles on the palm.
struct HandPalmChip {
center_px: [f32; 2],
size_px: [f32; 2],
angle_rad: f32,
rows: u32,
cols: u32,
}
impl egui_wgpu::CallbackTrait for WgpuBackgroundCallback {
fn prepare(
&self,
@@ -87,7 +121,13 @@ impl egui_wgpu::CallbackTrait for WgpuBackgroundCallback {
resources: &mut egui_wgpu::CallbackResources,
) -> Vec<wgpu::CommandBuffer> {
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()
}
@@ -115,15 +155,24 @@ pub struct BackgroundRenderResources {
hand_image_pipeline: wgpu::RenderPipeline,
glyph_pipeline: wgpu::RenderPipeline,
dot_pipeline: wgpu::RenderPipeline,
hand_membrane_pipeline: wgpu::RenderPipeline,
hand_dot_pipeline: wgpu::RenderPipeline,
hand_palm_chip_pipeline: wgpu::RenderPipeline,
hand_palm_dot_pipeline: wgpu::RenderPipeline,
hand_image_bind_group: wgpu::BindGroup,
hand_image_texture: texture::Texture,
glyph_vertex_buffer: wgpu::Buffer,
glyph_instance_buffer: wgpu::Buffer,
glyph_instances: Vec<GlyphInstance>,
hand_membrane_instance_buffer: wgpu::Buffer,
hand_membrane_instances: Vec<GlyphInstance>,
hand_dot_instance_buffer: wgpu::Buffer,
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_instances: Vec<GlyphInstance>,
render_options: RenderOptions,
}
@@ -445,8 +494,14 @@ impl BackgroundRenderResources {
let glyph_pipeline =
create_glyph_pipeline(device, target_format, &shader, &pipeline_layout);
let dot_pipeline = create_dot_pipeline(device, target_format, &shader, &pipeline_layout);
let hand_membrane_pipeline =
create_hand_membrane_pipeline(device, target_format, &shader, &pipeline_layout);
let hand_dot_pipeline =
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 =
create_hand_palm_dot_pipeline(device, target_format, &shader, &pipeline_layout);
let _model_pipelines =
create_model_pipelines(device, target_format, &shader, &model_pipeline_layout);
@@ -474,6 +529,16 @@ impl BackgroundRenderResources {
contents: bytemuck::cast_slice(&glyph_instances),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
});
let hand_membrane_instances = build_hand_membrane_instances(
hand_image_texture.width as f32,
hand_image_texture.height as f32,
);
let hand_membrane_instance_buffer =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Hand Fingertip Membrane Instance Buffer"),
contents: bytemuck::cast_slice(&hand_membrane_instances),
usage: wgpu::BufferUsages::VERTEX,
});
let hand_dot_instances = build_hand_dot_instances(
rows,
cols,
@@ -487,6 +552,29 @@ impl BackgroundRenderResources {
contents: bytemuck::cast_slice(&hand_dot_instances),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
});
let hand_palm_chip_instances = build_hand_palm_chip_instances(
hand_image_texture.width as f32,
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 =
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Hand Palm Chip Dot Instance Buffer"),
contents: bytemuck::cast_slice(&hand_palm_dot_instances),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
});
Self {
layout,
@@ -500,19 +588,35 @@ impl BackgroundRenderResources {
hand_image_pipeline,
glyph_pipeline,
dot_pipeline,
hand_membrane_pipeline,
hand_dot_pipeline,
hand_palm_chip_pipeline,
hand_palm_dot_pipeline,
hand_image_bind_group,
hand_image_texture,
glyph_vertex_buffer,
glyph_instance_buffer,
glyph_instances,
hand_membrane_instance_buffer,
hand_membrane_instances,
hand_dot_instance_buffer,
hand_dot_instances,
hand_palm_chip_instance_buffer,
hand_palm_chip_instances,
hand_palm_dot_instance_buffer,
hand_palm_dot_instances,
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);
self.uniform = MatrixUniform::new(
width,
@@ -544,20 +648,41 @@ impl BackgroundRenderResources {
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() {
pressure.as_slice()
} else {
hand_pressure
};
// Hand mode uses UV-anchored fingertip matrices over hand.png.
// Rebuild their instance positions here so pressure colors update every frame.
self.hand_dot_instances = build_hand_dot_instances(
self.rows,
self.cols,
self.hand_image_texture.width as f32,
self.hand_image_texture.height as f32,
pressure,
hand_pressure,
);
queue.write_buffer(
&self.hand_dot_instance_buffer,
0,
bytemuck::cast_slice(&self.hand_dot_instances),
);
// Palm chips reuse the same live 12x7 pressure frame, but draw it as
// embedded micro-pixels inside dark chip tiles.
self.hand_palm_dot_instances = build_hand_palm_dot_instances(
self.rows,
self.cols,
self.hand_image_texture.width as f32,
self.hand_image_texture.height as f32,
hand_pressure,
);
queue.write_buffer(
&self.hand_palm_dot_instance_buffer,
0,
bytemuck::cast_slice(&self.hand_palm_dot_instances),
);
}
fn paint(&self, render_pass: &mut wgpu::RenderPass<'_>, active_mode: &ActiveMode) {
@@ -595,11 +720,26 @@ impl BackgroundRenderResources {
fn paint_hand(&self, render_pass: &mut wgpu::RenderPass<'_>, mode: &HandGatewayMode) {
let _range = mode.range.clone();
// Draw the five fingertip dot matrices after hand.png, so they sit on top of the texture.
// First draw the translucent sensor membranes, then draw live pressure beads on their grid.
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_vertex_buffer(0, self.glyph_vertex_buffer.slice(..));
render_pass.set_vertex_buffer(1, self.hand_palm_dot_instance_buffer.slice(..));
render_pass.draw(0..6, 0..self.hand_palm_dot_instances.len() as u32);
}
fn visible_instance_count(&self, rows: u32, cols: u32) -> u32 {
@@ -867,6 +1007,39 @@ fn create_dot_pipeline(
})
}
fn create_hand_membrane_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 Fingertip Sensor Membrane Pipeline"),
layout: Some(layout),
vertex: wgpu::VertexState {
module: shader,
entry_point: Some("vs_hand_membrane"),
compilation_options: Default::default(),
buffers: &[GlyphVertex::desc(), GlyphInstance::desc()],
},
fragment: Some(wgpu::FragmentState {
module: shader,
entry_point: Some("fs_hand_membrane"),
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_dot_pipeline(
device: &wgpu::Device,
target_format: &wgpu::TextureFormat,
@@ -900,28 +1073,138 @@ 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(
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 Dot Pipeline"),
layout: Some(layout),
vertex: wgpu::VertexState {
module: shader,
entry_point: Some("vs_hand_palm_dot"),
compilation_options: Default::default(),
buffers: &[GlyphVertex::desc(), GlyphInstance::desc()],
},
fragment: Some(wgpu::FragmentState {
module: shader,
entry_point: Some("fs_hand_palm_dot"),
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 build_hand_membrane_instances(image_width: f32, image_height: f32) -> Vec<GlyphInstance> {
HAND_TIP_MATRICES
.iter()
.map(|tip| {
let uv_x = tip.center_px[0] / image_width.max(1.0);
let uv_y = tip.center_px[1] / image_height.max(1.0);
let membrane_size = [tip.size_px[0] * 1.44, tip.size_px[1] * 1.36];
GlyphInstance {
// Membrane shaders read xy as hand.png UV.
world_position: [uv_x, uv_y, 0.0, 1.0],
// Store angle and an oversized source-image pixel size for the floating sensor film.
style: [tip.angle_rad, membrane_size[0], membrane_size[1], 0.0],
}
})
.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(
rows: u32,
cols: u32,
image_width: f32,
image_height: f32,
pressure: &PressureFrame,
pressure: &[[f32; 2]],
) -> Vec<GlyphInstance> {
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 sin = tip.angle_rad.sin();
for row in 0..rows {
for col in 0..cols {
let index = (row * cols + col) as usize;
let [normalized, display_value] =
pressure.get(index).copied().unwrap_or([0.0, 0.0]);
let [normalized, display_value] = sample_pressure_at(
pressure,
tip_index * HAND_FINGER_SENSOR_CELLS + index,
index,
);
// 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_y = (row as f32 - rows as f32 / 2.0 + 0.5) / rows as f32 * tip.size_px[1];
let local_y = (row as f32 - rows as f32 / 2.0 + 0.5) / rows as f32 * tip.size_px[1]
+ HAND_TIP_DOT_LOCAL_Y_OFFSET_PX;
// Rotate the local matrix so it follows the direction of the finger.
let x = tip.center_px[0] + local_x * cos - local_y * sin;
@@ -941,6 +1224,75 @@ fn build_hand_dot_instances(
instances
}
fn build_hand_palm_dot_instances(
_rows: u32,
_cols: u32,
image_width: f32,
image_height: f32,
pressure: &[[f32; 2]],
) -> Vec<GlyphInstance> {
let chip_dot_count: usize = HAND_PALM_CHIPS
.iter()
.map(|chip| (chip.rows * chip.cols) as usize)
.sum();
let mut instances = Vec::with_capacity(chip_dot_count);
for (chip_index, chip) in HAND_PALM_CHIPS.into_iter().enumerate() {
let cos = chip.angle_rad.cos();
let sin = chip.angle_rad.sin();
// Palm films can float slightly beyond the hand artwork, similar to fingertip membranes.
let active_size = [chip.size_px[0] * 0.86, chip.size_px[1] * 0.86];
for row in 0..chip.rows {
for col in 0..chip.cols {
let offset = match chip_index {
0 => HAND_PALM_HORIZONTAL_OFFSET,
_ => HAND_PALM_VERTICAL_OFFSET,
};
let index = hand_palm_pressure_index(chip_index, row, col, chip.rows, chip.cols);
let [normalized, display_value] =
sample_pressure_at(pressure, offset + index, index);
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 {
world_position: [
x / image_width.max(1.0),
y / image_height.max(1.0),
0.0,
1.0,
],
style: [normalized, display_value, 0.0, 0.0],
});
}
}
}
instances
}
fn hand_palm_pressure_index(chip_index: usize, row: u32, col: u32, rows: u32, cols: u32) -> usize {
if chip_index == 0 {
(col * rows + (rows - 1 - row)) as usize
} else {
(row * cols + col) as usize
}
}
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])
}
fn build_glyph_instances(
rows: u32,
cols: u32,
@@ -1064,3 +1416,24 @@ impl GlyphInstance {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hand_palm_horizontal_uses_column_major_pressure_order() {
assert_eq!(hand_palm_pressure_index(0, 0, 0, 5, 14), 4);
assert_eq!(hand_palm_pressure_index(0, 1, 0, 5, 14), 3);
assert_eq!(hand_palm_pressure_index(0, 0, 1, 5, 14), 9);
assert_eq!(hand_palm_pressure_index(0, 4, 13, 5, 14), 65);
}
#[test]
fn hand_palm_vertical_keeps_row_major_pressure_order() {
assert_eq!(hand_palm_pressure_index(1, 0, 0, 11, 4), 0);
assert_eq!(hand_palm_pressure_index(1, 0, 1, 11, 4), 1);
assert_eq!(hand_palm_pressure_index(1, 1, 0, 11, 4), 4);
assert_eq!(hand_palm_pressure_index(1, 10, 3, 11, 4), 43);
}
}

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() % 2 != 0 {
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;

View File

@@ -13,6 +13,15 @@ pub struct TactileACodec {
expected_data_len: usize,
}
impl From<u8> for TactileAFrameStatusCode {
fn from(value: u8) -> Self {
match value {
0 => TactileAFrameStatusCode::Success,
_ => TactileAFrameStatusCode::Failure,
}
}
}
impl TactileACodec {
pub fn new(cols: usize, rows: usize) -> TactileACodec {
Self {
@@ -37,7 +46,7 @@ impl TactileACodec {
Ok(vals)
}
pub fn build_req_frame(cols: usize, rows: usize) -> TactileAFrame {
pub fn build_req_frame(cols: usize, rows: usize) -> anyhow::Result<TactileAFrame> {
let header = [0x55, 0xAA];
let payload_len: usize = 9;
let device_addr: u8 = 0x34;
@@ -46,7 +55,7 @@ impl TactileACodec {
let start_addr: u32 = 7168;
let except_data_len: usize = cols * rows * 2;
let checksum: u8 = 0;
TactileAFrame::Req(TactileAReqFrame {
Ok(TactileAFrame::Req(TactileAReqFrame {
meta: TactileAFrameMetaData {
header,
payload_len,
@@ -57,7 +66,7 @@ impl TactileACodec {
except_data_len,
checksum,
},
})
}))
}
}
@@ -102,10 +111,7 @@ impl Codec<TactileAFrame> for TactileACodec {
self.buffer[10],
]);
let except_data_len = u16::from_le_bytes([self.buffer[11], self.buffer[12]]) as usize;
let status = match self.buffer[13] {
0 => TactileAFrameStatusCode::Success,
_ => TactileAFrameStatusCode::Failure,
};
let status = TactileAFrameStatusCode::from(self.buffer[13]);
if except_data_len != self.expected_data_len {
log::debug!(

View File

@@ -9,6 +9,8 @@ pub enum SerialError {
AlreadyConnected,
StateError,
NoRecordedData,
ExportError,
ImportError,
}
impl fmt::Display for SerialError {
@@ -21,6 +23,8 @@ impl fmt::Display for SerialError {
SerialError::AlreadyConnected => write!(f, "Already Connected"),
SerialError::StateError => write!(f, "State Error"),
SerialError::NoRecordedData => write!(f, "No Recorded Data"),
SerialError::ExportError => write!(f, "Export Error"),
SerialError::ImportError => write!(f, "Import Error"),
}
}
}

View File

@@ -1,3 +1,13 @@
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestFrame {
pub header: [u8; 2],
pub cmd: u8,
pub length: usize,
pub payload: Vec<u8>,
pub checksum: u8,
pub dts_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TactileAFrameMetaData {
pub header: [u8; 2],

View File

@@ -2,5 +2,6 @@ pub mod codec;
pub mod codecs;
pub mod error;
pub mod frame;
pub mod multi_dim_force;
pub mod serial;
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 % 2 == 0 {
(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,12 +1,27 @@
use crate::recording::Recorder;
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::frame::TactileAFrame;
use crate::serial_core::utils::elapsed_millis;
use crossbeam_channel::{Receiver, Sender, TryRecvError};
use crossbeam_channel::{Receiver, Sender};
use std::io::{Read, Write};
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)]
pub struct SerialIoStats {
pub rx_bytes: u64,
pub tx_bytes: u64,
pub rx_frames: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SerialProtocol {
TactileA,
HandGateway,
}
/// Runs the serial polling loop on the calling (background) thread.
/// Sends decoded pressure matrix data (Vec<i32>) to the output channel.
@@ -14,14 +29,43 @@ 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,
rows: usize,
cols: usize,
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 = TactileACodec::new(cols, rows);
let req_frame = TactileACodec::build_req_frame(cols, rows);
let req_frame = match TactileACodec::build_req_frame(cols, rows) {
Ok(frame) => frame,
Err(err) => {
eprintln!("[serial] request frame build error: {err}");
return;
}
};
let mut buffer = [0u8; 1024];
let mut poll_interval = Duration::from_millis(POLL_INTERVAL_MS);
let poll_interval = Duration::from_millis(POLL_INTERVAL_MS);
let mut io_stats = SerialIoStats::default();
loop {
// Check cancel
@@ -31,7 +75,10 @@ pub fn run_serial_loop(
// Send poll request
if let Ok(req_bytes) = codec.encode(&req_frame) {
let _ = port.write_all(&req_bytes);
if port.write_all(&req_bytes).is_ok() {
io_stats.tx_bytes += req_bytes.len() as u64;
publish_stats(stats_tx, io_stats);
}
}
// Read response with poll interval
@@ -43,11 +90,20 @@ pub fn run_serial_loop(
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 {
if let TactileAFrame::Rep(rep) = frame {
if let Ok(vals) = TactileACodec::parse_data_frame(&rep.payload) {
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);
io_stats.rx_frames += 1;
publish_stats(stats_tx, io_stats);
}
}
}
@@ -68,6 +124,111 @@ 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);
io_stats.rx_frames += 1;
publish_stats(stats_tx, io_stats);
}
}
}
}
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) {
if let Some(tx) = stats_tx {
let _ = tx.try_send(stats);
}
}
/// Trait abstracting read+write for the serial port
pub trait ReadWrite: Send {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize>;

View File

@@ -1,10 +1,64 @@
use std::time::Instant;
pub fn usize_to_u16_be_bytes(n: usize) -> [u8; 2] {
(n as u16).to_be_bytes()
}
pub fn usize_to_u16_le_bytes(n: usize) -> [u8; 2] {
(n as u16).to_be_bytes()
}
pub fn u16_to_hex_be_bytes(n: u16) -> [u8; 2] {
n.to_be_bytes()
}
pub fn u16_to_hex_le_bytes(n: u16) -> [u8; 2] {
n.to_le_bytes()
}
pub fn calc_crc8_smbus(c: &[u8]) -> u8 {
let crc8_smbus = crc::Crc::<u8>::new(&crc::CRC_8_SMBUS);
crc8_smbus.checksum(c)
}
pub fn calc_crc8_itu(c: &[u8]) -> u8 {
let crc8_itu_alg = crc::Crc::<u8>::new(&crc::CRC_8_I_432_1);
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 {
start_at.elapsed().as_millis() as u64
}
#[cfg(test)]
mod test {
use crate::serial_core::utils::{calc_crc8_itu, calc_crc8_itu_xor55, calc_crc8_smbus};
#[test]
fn test_crc8_itu() {
let req_vec = vec![
0x55, 0xAA, 0x09, 0x00, 0x34, 0x00, 0xFB, 0x00, 0x1C, 0x00, 0x00, 0x18, 0x00,
];
let checksum = calc_crc8_itu(req_vec.as_slice());
assert_eq!(checksum, 0x7A);
}
#[test]
fn test_crc8_smbus() {
let req_vec = vec![
0x55, 0xAA, 0x09, 0x00, 0x34, 0x00, 0xFB, 0x00, 0x1C, 0x00, 0x00, 0x18, 0x00,
];
let checksum = calc_crc8_smbus(req_vec.as_slice());
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)]
pub struct AppTheme {
@@ -29,7 +29,7 @@ pub struct DesignMetrics {
}
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_strong: egui::Color32::from_rgb(34, 43, 54),
panel_deep: egui::Color32::from_rgb(15, 20, 27),
@@ -61,6 +61,7 @@ pub const METRICS: DesignMetrics = DesignMetrics {
pub mod layout {
pub const TITLE_BAR_HEIGHT: f32 = 36.0;
pub const CONFIG_BAR_HEIGHT: f32 = 38.0;
pub const CENTER_PANEL_TOP: f32 = 48.0;
pub const LEFT_X: f32 = 24.0;
pub const RIGHT_X: f32 = 1328.0;
@@ -213,6 +214,19 @@ pub fn tag_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> {
.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, ONE_DARK_PRO.border))
.corner_radius(egui::CornerRadius::same(4))
.min_size(egui::vec2(0.0, METRICS.button_height))
}
pub fn primary_button(label: impl Into<egui::WidgetText>) -> egui::Button<'static> {
egui::Button::new(label)
.fill(ONE_DARK_PRO.accent)

1194
src/ui.rs

File diff suppressed because it is too large Load Diff

View File

@@ -63,7 +63,7 @@ fn linear_to_srgb(linear: vec3f) -> vec3f {
fn output_color(linear_rgb: vec3f, alpha: f32) -> vec4f {
let clamped = clamp(linear_rgb, vec3f(0.0), vec3f(1.0));
if (u.color.w > 0.5) {
if u.color.w > 0.5 {
return vec4f(clamped, alpha);
}
@@ -91,12 +91,12 @@ fn range_stop_color(index: u32) -> vec3f {
fn sample_range_color(value: f32) -> vec3f {
let t = saturate(value);
if (t <= 0.33) {
if t <= 0.33 {
let local = smoothstep(0.0, 0.33, t);
return mix(range_stop_color(0u), range_stop_color(1u), local);
}
if (t <= 0.66) {
if t <= 0.66 {
let local = smoothstep(0.33, 0.66, t);
return mix(range_stop_color(1u), range_stop_color(2u), local);
}
@@ -105,7 +105,6 @@ fn sample_range_color(value: f32) -> vec3f {
return mix(range_stop_color(2u), range_stop_color(3u), local);
}
// background
struct BackgroundVertexOutput {
@builtin(position) clip_position: vec4f,
@@ -126,18 +125,9 @@ fn vs_background(@builtin(vertex_index) vertex_index: u32) -> BackgroundVertexOu
@fragment
fn fs_background(@builtin(position) frag_coord: vec4f) -> @location(0) vec4f {
let pixel = frag_coord.xy;
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);
return output_color(vec3f(0.0, 0.0, 0.0), 1.0);
}
// hand image background
struct HandImageVertexOutput {
@builtin(position) clip_position: vec4f,
@@ -175,7 +165,7 @@ fn fs_hand_image(in: HandImageVertexOutput) -> @location(0) vec4f {
let image_aspect = u.image.x / max(u.image.y, 1.0);
var uv = in.screen_uv;
if (viewport_aspect > image_aspect) {
if viewport_aspect > image_aspect {
let image_width = image_aspect / viewport_aspect;
uv.x = (uv.x - (1.0 - image_width) * 0.5) / image_width;
} else {
@@ -183,7 +173,7 @@ fn fs_hand_image(in: HandImageVertexOutput) -> @location(0) vec4f {
uv.y = (uv.y - (1.0 - image_height) * 0.5) / image_height;
}
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
if uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 {
discard;
}
@@ -191,7 +181,6 @@ fn fs_hand_image(in: HandImageVertexOutput) -> @location(0) vec4f {
return output_color(color.rgb, color.a);
}
// glyph
struct GlyphVertexInput {
@location(0) local: vec2f,
@@ -234,45 +223,45 @@ fn digit_segment_on(digit: u32, segment: u32) -> bool {
fn seven_segment_digit_alpha(local: vec2f, digit: u32) -> f32 {
var alpha = 0.0;
if (digit_segment_on(digit, 0u)) {
if digit_segment_on(digit, 0u) {
alpha = max(alpha, rect_alpha(local, vec2f(0.0, 0.70), vec2f(0.38, 0.078)));
}
if (digit_segment_on(digit, 1u)) {
if digit_segment_on(digit, 1u) {
alpha = max(alpha, rect_alpha(local, vec2f(0.39, 0.36), vec2f(0.078, 0.335)));
}
if (digit_segment_on(digit, 2u)) {
if digit_segment_on(digit, 2u) {
alpha = max(alpha, rect_alpha(local, vec2f(0.39, -0.36), vec2f(0.078, 0.335)));
}
if (digit_segment_on(digit, 3u)) {
if digit_segment_on(digit, 3u) {
alpha = max(alpha, rect_alpha(local, vec2f(0.0, -0.70), vec2f(0.38, 0.078)));
}
if (digit_segment_on(digit, 4u)) {
if digit_segment_on(digit, 4u) {
alpha = max(alpha, rect_alpha(local, vec2f(-0.39, -0.36), vec2f(0.078, 0.335)));
}
if (digit_segment_on(digit, 5u)) {
if digit_segment_on(digit, 5u) {
alpha = max(alpha, rect_alpha(local, vec2f(-0.39, 0.36), vec2f(0.078, 0.335)));
}
if (digit_segment_on(digit, 6u)) {
if digit_segment_on(digit, 6u) {
alpha = max(alpha, rect_alpha(local, vec2f(0.0, 0.0), vec2f(0.35, 0.075)));
}
return alpha;
}
fn digit_count(value: u32) -> u32 {
if (value >= 1000u) {
if value >= 1000u {
return 4u;
}
if (value >= 100u) {
if value >= 100u {
return 3u;
}
if (value >= 10u) {
if value >= 10u {
return 2u;
}
return 1u;
}
fn digit_at(value: u32, slot: u32, count: u32) -> u32 {
if (count == 4u) {
if count == 4u {
switch slot {
case 0u: { return (value / 1000u) % 10u; }
case 1u: { return (value / 100u) % 10u; }
@@ -280,14 +269,14 @@ fn digit_at(value: u32, slot: u32, count: u32) -> u32 {
default: { return value % 10u; }
}
}
if (count == 3u) {
if count == 3u {
switch slot {
case 0u: { return (value / 100u) % 10u; }
case 1u: { return (value / 10u) % 10u; }
default: { return value % 10u; }
}
}
if (count == 2u) {
if count == 2u {
return select(value % 10u, (value / 10u) % 10u, slot == 0u);
}
return value % 10u;
@@ -302,7 +291,7 @@ fn number_alpha(local: vec2f, display_value: f32) -> f32 {
var alpha = 0.0;
for (var slot = 0u; slot < 4u; slot = slot + 1u) {
if (slot < count) {
if slot < count {
let center_x = start_x + f32(slot) * slot_width;
let digit_local = vec2f((local.x - center_x) / (slot_width * 0.78), local.y / 0.92);
let digit = digit_at(value, slot, count);
@@ -343,8 +332,7 @@ struct DotVertexInput {
struct DotInstanceInput {
@location(1) world_position: vec4f,
@location(2) style: vec4f
}
@location(2) style: vec4f}
struct DotVertexOutput {
@builtin(position) clip_position: vec4f,
@@ -366,7 +354,7 @@ fn hand_image_uv_to_clip(image_uv: vec2f) -> vec2f {
var screen_uv = image_uv;
if (viewport_aspect > image_aspect) {
if viewport_aspect > image_aspect {
let image_width = image_aspect / viewport_aspect;
screen_uv.x = image_uv.x * image_width + (1.0 - image_width) * 0.5;
} else {
@@ -377,6 +365,154 @@ fn hand_image_uv_to_clip(image_uv: vec2f) -> vec2f {
return vec2f(screen_uv.x * 2.0 - 1.0, 1.0 - screen_uv.y * 2.0);
}
struct HandMembraneVertexOutput {
@builtin(position) clip_position: vec4f,
@location(0) local: vec2f,
}
fn rotate_2d(point: vec2f, angle: f32) -> vec2f {
let c = cos(angle);
let s = sin(angle);
return vec2f(point.x * c - point.y * s, point.x * s + point.y * c);
}
fn rounded_rect_alpha(local: vec2f, radius: f32, softness: f32) -> f32 {
let q = abs(local) - vec2f(1.0 - radius, 1.0 - radius);
let dist = length(max(q, vec2f(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - radius;
return 1.0 - smoothstep(0.0, softness, dist);
}
fn dot_matrix(uv: vec2f, grid: vec2f, dot_radius: f32, dot_softness: f32) -> f32 {
let cell = fract(uv * grid) - vec2f(0.5, 0.5);
return 1.0 - smoothstep(dot_radius, dot_radius + dot_softness, length(cell));
}
fn fingertip_film_alpha(
local: vec2f,
half_width: f32,
cap_center_y: f32,
softness: f32,
) -> f32 {
// Top/front of the film is a closed round fingertip cap.
let cap_dist = length(vec2f(local.x, local.y - cap_center_y));
let cap = (1.0 - smoothstep(half_width, half_width + softness, cap_dist))
* (1.0 - smoothstep(cap_center_y - softness, cap_center_y + softness, local.y));
// The rear remains open; the carrier quad clips the membrane at its end.
let side = 1.0 - smoothstep(half_width, half_width + softness, abs(local.x));
let body_gate = smoothstep(cap_center_y - softness, cap_center_y + softness, local.y);
let body = side * body_gate;
return max(cap, body);
}
@vertex
fn vs_hand_membrane(vertex: DotVertexInput, instance: DotInstanceInput) -> HandMembraneVertexOutput {
let center_px = instance.world_position.xy * u.image.xy;
let size_px = instance.style.yz;
let angle = instance.style.x;
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: HandMembraneVertexOutput;
out.clip_position = vec4f(hand_image_uv_to_clip(image_uv), 0.0, 1.0);
out.local = vertex.local;
return out;
}
@fragment
fn fs_hand_membrane(in: HandMembraneVertexOutput) -> @location(0) vec4f {
let p = in.local;
let halo_shape = fingertip_film_alpha(p, 0.72, -0.20, 0.055);
let panel = fingertip_film_alpha(p, 0.66, -0.22, 0.035);
let inner = fingertip_film_alpha(p, 0.58, -0.25, 0.045);
let clear_inner = fingertip_film_alpha(p, 0.48, -0.24, 0.160);
let halo = clamp(halo_shape - panel, 0.0, 1.0);
let rim = clamp(panel - inner, 0.0, 1.0);
let edge_fade = pow(clamp(1.0 - clear_inner, 0.0, 1.0), 1.20) * panel;
// Strong pseudo extrusion / bevel.
let depth_offset = vec2f(0.045, 0.055);
let back_shape_1 = fingertip_film_alpha(p - depth_offset * 0.55, 0.66, -0.22, 0.045);
let back_shape_2 = fingertip_film_alpha(p - depth_offset, 0.66, -0.22, 0.060);
let extrusion = clamp(max(back_shape_1, back_shape_2) - panel, 0.0, 1.0);
let bevel_offset = vec2f(0.028, 0.034);
let shifted_down_right = fingertip_film_alpha(p - bevel_offset, 0.66, -0.22, 0.035);
let bevel_light = clamp(panel - shifted_down_right, 0.0, 1.0);
let shifted_up_left = fingertip_film_alpha(p + bevel_offset, 0.66, -0.22, 0.035);
let bevel_dark = clamp(panel - shifted_up_left, 0.0, 1.0);
let broad_bevel = edge_fade * clamp(0.58 - p.x * 0.20 - p.y * 0.18, 0.0, 1.0);
// Micro lattice.
let uv = clamp(
p * vec2f(0.52, 0.46) + vec2f(0.5, 0.46),
vec2f(0.0, 0.0),
vec2f(1.0, 1.0),
);
let grid = vec2f(18.0, 34.0);
let cell_uv = fract(uv * grid) - vec2f(0.5, 0.5);
let dot_dist = length(cell_uv * vec2f(1.0, 1.06));
let dot_aa = max(fwidth(dot_dist) * 1.35, 0.006);
let dots = (1.0 - smoothstep(0.105 - dot_aa, 0.105 + dot_aa, dot_dist)) * inner;
let sheen_axis = p.x * 0.88 + p.y * 0.22 + 0.16;
let sheen = (1.0 - smoothstep(0.018, 0.105, abs(sheen_axis))) * panel;
let shell_sheen = sheen * (0.26 + edge_fade * 0.74);
// Temporary synthetic pressure hotspot. Real hand pressure dots are drawn above this pass.
let pressure_center = vec2f(-0.08, -0.12);
let pressure_dist = length((p - pressure_center) * vec2f(1.0, 0.74));
let pressure = (1.0 - smoothstep(0.05, 0.36, pressure_dist)) * inner;
let pressure_hot = (1.0 - smoothstep(0.02, 0.13, pressure_dist)) * inner;
let glass_base = vec3f(0.006, 0.055, 0.105);
let glass_cyan = vec3f(0.025, 0.42, 0.58);
let rim_color = vec3f(0.10, 0.88, 1.00);
let extrusion_color = vec3f(0.004, 0.080, 0.110);
let extrusion_edge_color = vec3f(0.015, 0.30, 0.38);
let bevel_highlight = vec3f(0.48, 1.00, 1.00);
let bevel_dark_color = vec3f(0.004, 0.035, 0.060);
let dot_color = vec3f(0.08, 0.48, 0.58);
let pressure_color = dot_color;
let pressure_hot_color = dot_color;
let color = extrusion_color * extrusion * 0.90
+ extrusion_edge_color * extrusion * halo_shape * 0.32
+ glass_base * panel * 0.26
+ glass_cyan * edge_fade * 0.46
+ glass_cyan * broad_bevel * 0.24
+ rim_color * rim * 0.88
+ bevel_highlight * bevel_light * 1.35
+ bevel_dark_color * bevel_dark * 0.95
+ bevel_highlight * shell_sheen * 0.28
+ rim_color * halo * 0.16
+ dot_color * dots * 0.68
+ pressure_color * dots * pressure * 0.82
+ pressure_hot_color * dots * pressure_hot * 0.90;
let alpha = clamp(
extrusion * 0.44
+ panel * 0.055
+ edge_fade * 0.30
+ rim * 0.34
+ bevel_light * 0.34
+ bevel_dark * 0.20
+ shell_sheen * 0.09
+ halo * 0.045
+ dots * 0.09
+ dots * pressure * 0.10,
0.0,
0.90,
);
return output_color(color, alpha);
}
@vertex
fn vs_hand_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexOutput {
let intensity = saturate(instance.style.x);
@@ -387,7 +523,7 @@ fn vs_hand_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexO
let center = hand_image_uv_to_clip(instance.world_position.xy);
// 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.
let pixel_size = u.glyph.x * mix(0.38, 0.56, shaped);
let pixel_size = u.glyph.x * mix(0.22, 0.34, shaped);
let ndc_offset = vertex.local * vec2f(pixel_size / u.viewport.x, pixel_size / u.viewport.y) * 2.0;
var out: DotVertexOutput;
@@ -401,15 +537,83 @@ fn vs_hand_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexO
fn fs_hand_dot(in: DotVertexOutput) -> @location(0) vec4f {
let intensity = saturate(in.intensity);
// Use a compact round bead instead of the larger Finger-mode glow marker.
let core = circle_alpha(in.local, 0.56, 0.08);
let halo = circle_alpha(in.local, 0.78, 0.14) * 0.16;
// 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 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,
// while still letting pressure brighten the bead a little.
let cyan = vec3f(0.34, 0.86, 1.0);
let hot = sample_range_color(intensity);
let color = mix(cyan, hot, 0.22) * mix(0.88, 1.18, intensity);
// Match Finger mode's pressure gradient so each hand region remains readable.
let idle = vec3f(0.060, 0.250, 0.320);
let gradient = sample_range_color(intensity);
let color = mix(idle, gradient, smoothstep(0.0, 0.18, intensity))
* mix(0.78, 1.18, intensity);
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 {
// The live palm-dot pass draws every chip cell, including the idle dots.
// Keeping this background pass transparent prevents a second offset dot grid.
return output_color(vec3f(0.0, 0.0, 0.0), 0.0);
}
@vertex
fn vs_hand_palm_dot(vertex: DotVertexInput, instance: DotInstanceInput) -> DotVertexOutput {
let intensity = saturate(instance.style.x);
let shaped = smoothstep(0.0, 1.0, intensity);
let center = hand_image_uv_to_clip(instance.world_position.xy);
let pixel_size = u.glyph.x * mix(0.22, 0.34, shaped);
let ndc_offset = vertex.local * vec2f(pixel_size / u.viewport.x, pixel_size / u.viewport.y) * 2.0;
var out: DotVertexOutput;
out.clip_position = vec4f(center + ndc_offset, 0.0, 1.0);
out.local = vertex.local;
out.intensity = intensity;
return out;
}
@fragment
fn fs_hand_palm_dot(in: DotVertexOutput) -> @location(0) vec4f {
let intensity = saturate(in.intensity);
let core = circle_alpha(in.local, 0.48, 0.07);
let halo = circle_alpha(in.local, 0.74, 0.14) * (0.10 + intensity * 0.26);
let idle = vec3f(0.060, 0.250, 0.320);
let gradient = sample_range_color(intensity);
let color = mix(idle, gradient, smoothstep(0.0, 0.18, intensity))
* mix(0.78, 1.18, intensity);
return output_color(color, max(core, halo));
}
@@ -435,24 +639,12 @@ fn fs_dot(in: DotVertexOutput) -> @location(0) vec4f {
let intensity = saturate(in.intensity);
let base_color = sample_range_color(intensity);
let core = circle_alpha(in.local, 0.42, 0.055);
let glow = circle_alpha(in.local, 0.92, 0.20) * intensity * 0.32;
let highlight_pos = in.local - vec2f(-0.18, 0.20);
let highlight = circle_alpha(highlight_pos, 0.16, 0.08) * 0.45;
let brightness = mix(0.82, 1.22, intensity);
let color = base_color * brightness + vec3f(1.0, 1.0, 1.0) * highlight;
let alpha = max(core, glow);
let alpha = circle_alpha(in.local, 0.46, 0.045);
let color = base_color * mix(0.86, 1.06, intensity);
return output_color(color, alpha);
}
// model
struct ModelVertexInput {
@location(0) position: vec3f,
@@ -571,11 +763,11 @@ fn aces_tonemap(x: vec3f) -> vec3f {
fn material_normal(in: ModelVertexOutput, front_facing: bool) -> vec3f {
var n = normalize(in.world_normal);
if (!front_facing && material.flags.w > 0.5) {
if !front_facing && material.flags.w > 0.5 {
n = -n;
}
if (material.flags.z < 0.5) {
if material.flags.z < 0.5 {
return n;
}
@@ -595,17 +787,17 @@ fn fs_model(in: ModelVertexOutput, @builtin(front_facing) front_facing: bool) ->
let base_color = base_sample * material.base_color * in.color;
let alpha_mode = material.emissive_alpha.w;
var alpha = base_color.a;
if (alpha_mode < 0.5) {
if alpha_mode < 0.5 {
alpha = 1.0;
} else if (alpha_mode < 1.5) {
if (alpha < material.flags.x) {
} else if alpha_mode < 1.5 {
if alpha < material.flags.x {
discard;
}
alpha = 1.0;
}
let debug_mode = u32(u.render_options.x + 0.5);
if (debug_mode == 1u) {
if debug_mode == 1u {
return output_color(base_color.rgb, alpha);
}
@@ -671,7 +863,7 @@ fn fs_model(in: ModelVertexOutput, @builtin(front_facing) front_facing: bool) ->
let ambient_strength = 0.08;
// Temporary neutral linear ambient term until a real IBL/HDR environment is added.
var ambient = vec3f(0.0);
if (u.render_options.y > 0.5) {
if u.render_options.y > 0.5 {
let ambient_diffuse = albedo * (1.0 - metallic) * ambient_color * ambient_strength;
let ambient_specular = f_ambient * ambient_color * ambient_strength * (1.0 - roughness * 0.55);
ambient = (ambient_diffuse + ambient_specular) * ao;
@@ -679,7 +871,7 @@ fn fs_model(in: ModelVertexOutput, @builtin(front_facing) front_facing: bool) ->
let exposed_color = (ambient + key + fill + emissive) * max(u.render_options.w, 0.0);
var color = exposed_color;
if (u.render_options.z > 0.5) {
if u.render_options.z > 0.5 {
color = aces_tonemap(exposed_color);
}

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>