toml config support
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -1318,8 +1318,10 @@ dependencies = [
|
|||||||
"gltf",
|
"gltf",
|
||||||
"image",
|
"image",
|
||||||
"log",
|
"log",
|
||||||
|
"serde",
|
||||||
"serialport",
|
"serialport",
|
||||||
"tobj",
|
"tobj",
|
||||||
|
"toml",
|
||||||
"winresource",
|
"winresource",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -4001,9 +4003,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "1.1.2+spec-1.1.0"
|
version = "1.1.3+spec-1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "81f3d15e84cbcd896376e6730314d59fb5a87f31e4b038454184435cd57defee"
|
checksum = "53c96ecdfa941c8fc4fcaed14f99ada8ebed502eef533015095a07e3301d4c3c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"serde_core",
|
"serde_core",
|
||||||
@@ -4046,9 +4048,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml_writer"
|
name = "toml_writer"
|
||||||
version = "1.1.1+spec-1.1.0"
|
version = "1.1.2+spec-1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "756daf9b1013ebe47a8776667b466417e2d4c5679d441c26230efd9ef78692db"
|
checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing"
|
name = "tracing"
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ crc = "3.4.0"
|
|||||||
log = "0.4.29"
|
log = "0.4.29"
|
||||||
tobj = "4.0.4"
|
tobj = "4.0.4"
|
||||||
gltf = "1.4.1"
|
gltf = "1.4.1"
|
||||||
|
toml = "1.1.3"
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
|||||||
59
config.toml
Normal file
59
config.toml
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
[thumb]
|
||||||
|
"0.57" = 35694
|
||||||
|
"2.57" = 226640
|
||||||
|
"3.57" = 319868
|
||||||
|
"4.57" = 398021
|
||||||
|
"5.57" = 450448
|
||||||
|
"6.57" = 510506
|
||||||
|
"8.57" = 592075
|
||||||
|
"10.57" = 655760
|
||||||
|
"15.57" = 771671
|
||||||
|
"20.57" = 813967
|
||||||
|
|
||||||
|
[index]
|
||||||
|
"0.57" = 45490
|
||||||
|
"2.57" = 267579
|
||||||
|
"3.57" = 352066
|
||||||
|
"4.57" = 431681
|
||||||
|
"5.57" = 457605
|
||||||
|
"6.57" = 550875
|
||||||
|
"8.57" = 624327
|
||||||
|
"10.57" = 732735
|
||||||
|
"15.57" = 883397
|
||||||
|
"20.57" = 899338
|
||||||
|
|
||||||
|
[middle]
|
||||||
|
"0.57" = 51562
|
||||||
|
"2.57" = 252840
|
||||||
|
"3.57" = 326531
|
||||||
|
"4.57" = 386571
|
||||||
|
"5.57" = 415819
|
||||||
|
"6.57" = 514679
|
||||||
|
"8.57" = 631745
|
||||||
|
"10.57" = 678945
|
||||||
|
"15.57" = 810847
|
||||||
|
"20.57" = 900422
|
||||||
|
|
||||||
|
[ring]
|
||||||
|
"0.57" = 33213
|
||||||
|
"2.57" = 220611
|
||||||
|
"3.57" = 307383
|
||||||
|
"4.57" = 369637
|
||||||
|
"5.57" = 430462
|
||||||
|
"6.57" = 489659
|
||||||
|
"8.57" = 584789
|
||||||
|
"10.57" = 664960
|
||||||
|
"15.57" = 802749
|
||||||
|
"20.57" = 926907
|
||||||
|
|
||||||
|
[little]
|
||||||
|
"0.57" = 44291
|
||||||
|
"2.57" = 216657
|
||||||
|
"3.57" = 301950
|
||||||
|
"4.57" = 366186
|
||||||
|
"5.57" = 384791
|
||||||
|
"6.57" = 496521
|
||||||
|
"8.57" = 566189
|
||||||
|
"10.57" = 701459
|
||||||
|
"15.57" = 845608
|
||||||
|
"20.57" = 940796
|
||||||
254
src/app.rs
254
src/app.rs
@@ -1,4 +1,5 @@
|
|||||||
use crate::breakout::{BreakoutGame, control_from_matrix};
|
use crate::breakout::{BreakoutGame, control_from_matrix};
|
||||||
|
use crate::config::{HandForceConfig, try_load_hand_force_config};
|
||||||
use crate::connection::{ConnectionManager, ConnectionState};
|
use crate::connection::{ConnectionManager, ConnectionState};
|
||||||
use crate::force::{ForceEstimatorState, HAND_FINGERTIP_COUNT, HudSpatialForce};
|
use crate::force::{ForceEstimatorState, HAND_FINGERTIP_COUNT, HudSpatialForce};
|
||||||
use crate::recording::Recorder;
|
use crate::recording::Recorder;
|
||||||
@@ -17,6 +18,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
use eframe::{egui, egui_wgpu};
|
use eframe::{egui, egui_wgpu};
|
||||||
|
use std::collections::BTreeMap;
|
||||||
use std::{
|
use std::{
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
@@ -70,6 +72,7 @@ pub struct EskinDesktopApp {
|
|||||||
context_menu_open: bool,
|
context_menu_open: bool,
|
||||||
context_menu_pos: egui::Pos2,
|
context_menu_pos: egui::Pos2,
|
||||||
active_mode: ActiveMode,
|
active_mode: ActiveMode,
|
||||||
|
hand_force_config: Option<HandForceConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct LiveRateMeters {
|
struct LiveRateMeters {
|
||||||
@@ -137,6 +140,7 @@ impl EskinDesktopApp {
|
|||||||
MATRIX_COLS,
|
MATRIX_COLS,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
let hand_force_config = try_load_hand_force_config();
|
||||||
Self {
|
Self {
|
||||||
connect_panel: FloatingPanelState::new([0.0, 0.0], [layout::RIGHT_TAG_X, 48.0]),
|
connect_panel: FloatingPanelState::new([0.0, 0.0], [layout::RIGHT_TAG_X, 48.0]),
|
||||||
connect_state: ConnectPanelState::default(),
|
connect_state: ConnectPanelState::default(),
|
||||||
@@ -184,6 +188,7 @@ impl EskinDesktopApp {
|
|||||||
context_menu_open: false,
|
context_menu_open: false,
|
||||||
context_menu_pos: egui::pos2(24.0, 48.0),
|
context_menu_pos: egui::pos2(24.0, 48.0),
|
||||||
active_mode: ActiveMode::Hand(HandGatewayMode { range: 0..7000 }),
|
active_mode: ActiveMode::Hand(HandGatewayMode { range: 0..7000 }),
|
||||||
|
hand_force_config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +253,8 @@ impl EskinDesktopApp {
|
|||||||
fn analyze_spatial_force(&mut self, values: &[u32]) {
|
fn analyze_spatial_force(&mut self, values: &[u32]) {
|
||||||
if matches!(self.config_state.mode, SerialMode::Hand) {
|
if matches!(self.config_state.mode, SerialMode::Hand) {
|
||||||
let enabled = std::array::from_fn(|tip_index| {
|
let enabled = std::array::from_fn(|tip_index| {
|
||||||
hand_segment_force(values, tip_index) > HAND_FORCE_DISPLAY_THRESHOLD_N
|
hand_segment_force(values, tip_index, self.hand_force_config.as_ref())
|
||||||
|
> HAND_FORCE_DISPLAY_THRESHOLD_N
|
||||||
});
|
});
|
||||||
self.latest_hand_spatial_forces = self
|
self.latest_hand_spatial_forces = self
|
||||||
.force_estimator
|
.force_estimator
|
||||||
@@ -351,8 +357,11 @@ impl EskinDesktopApp {
|
|||||||
|
|
||||||
self.analyze_spatial_force(&sample.matrix);
|
self.analyze_spatial_force(&sample.matrix);
|
||||||
|
|
||||||
let force =
|
let force = update_hand_signal_histories(
|
||||||
update_hand_signal_histories(&mut self.hand_signal_histories, &sample.matrix)
|
&mut self.hand_signal_histories,
|
||||||
|
&sample.matrix,
|
||||||
|
self.hand_force_config.as_ref(),
|
||||||
|
)
|
||||||
.min(25.6);
|
.min(25.6);
|
||||||
self.signal_history.push(force);
|
self.signal_history.push(force);
|
||||||
if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES {
|
if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES {
|
||||||
@@ -712,10 +721,11 @@ fn paint_spatial_force_arrow(
|
|||||||
fn update_hand_signal_histories(
|
fn update_hand_signal_histories(
|
||||||
histories: &mut [Vec<f32>; HAND_FORCE_PANEL_COUNT],
|
histories: &mut [Vec<f32>; HAND_FORCE_PANEL_COUNT],
|
||||||
raw_values: &[u32],
|
raw_values: &[u32],
|
||||||
|
config: Option<&HandForceConfig>,
|
||||||
) -> f32 {
|
) -> f32 {
|
||||||
let mut total_force = 0.0;
|
let mut total_force = 0.0;
|
||||||
for (index, history) in histories.iter_mut().enumerate() {
|
for (index, history) in histories.iter_mut().enumerate() {
|
||||||
let force = hand_segment_force(raw_values, index);
|
let force = hand_segment_force(raw_values, index, config);
|
||||||
let force = if force <= HAND_FORCE_DISPLAY_THRESHOLD_N {
|
let force = if force <= HAND_FORCE_DISPLAY_THRESHOLD_N {
|
||||||
0.0
|
0.0
|
||||||
} else {
|
} else {
|
||||||
@@ -731,7 +741,7 @@ fn update_hand_signal_histories(
|
|||||||
total_force
|
total_force
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hand_segment_force(raw_values: &[u32], index: usize) -> f32 {
|
fn hand_segment_force(raw_values: &[u32], index: usize, config: Option<&HandForceConfig>) -> f32 {
|
||||||
let offset: usize = HAND_FORCE_SEGMENT_COUNTS[..index.min(HAND_FORCE_PANEL_COUNT)]
|
let offset: usize = HAND_FORCE_SEGMENT_COUNTS[..index.min(HAND_FORCE_PANEL_COUNT)]
|
||||||
.iter()
|
.iter()
|
||||||
.sum();
|
.sum();
|
||||||
@@ -746,22 +756,78 @@ fn hand_segment_force(raw_values: &[u32], index: usize) -> f32 {
|
|||||||
.fold(0_u64, |sum, value| sum + *value as u64)
|
.fold(0_u64, |sum, value| sum + *value as u64)
|
||||||
.min(u32::MAX as u64) as u32;
|
.min(u32::MAX as u64) as u32;
|
||||||
|
|
||||||
raw_to_hand_segment_g(index, raw_total).min(20.6)
|
raw_to_hand_segment_g(index, raw_total, config).min(20.6)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn raw_to_hand_segment_g(index: usize, raw: u32) -> f32 {
|
fn raw_to_hand_segment_g(index: usize, raw: u32, config: Option<&HandForceConfig>) -> f32 {
|
||||||
match index {
|
match index {
|
||||||
0 => raw_to_g1(raw),
|
0 => raw_to_g1(raw, config),
|
||||||
1 => raw_to_g2(raw),
|
1 => raw_to_g2(raw, config),
|
||||||
2 => raw_to_g3(raw),
|
2 => raw_to_g3(raw, config),
|
||||||
3 => raw_to_g4(raw),
|
3 => raw_to_g4(raw, config),
|
||||||
4 => raw_to_g5(raw),
|
4 => raw_to_g5(raw, config),
|
||||||
5 => raw_to_g6(raw),
|
5 => raw_to_g6(raw),
|
||||||
6 => raw_to_g7(raw),
|
6 => raw_to_g7(raw),
|
||||||
_ => raw_to_gd(raw),
|
_ => raw_to_gd(raw),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_force_table(values: &BTreeMap<String, u32>) -> Vec<(u32, f32)> {
|
||||||
|
let mut table = values
|
||||||
|
.iter()
|
||||||
|
.filter_map(|(force_n, raw)| force_n.parse::<f32>().ok().map(|force| (*raw, force)))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
table.sort_by(|a, b| a.1.total_cmp(&b.1));
|
||||||
|
table.insert(0, (0, 0.0));
|
||||||
|
table
|
||||||
|
}
|
||||||
|
|
||||||
|
fn interpolate_force(raw: u32, table: &[(u32, f32)]) -> f32 {
|
||||||
|
let Some(&(first_raw, first_force)) = table.first() else {
|
||||||
|
return 0.0;
|
||||||
|
};
|
||||||
|
let Some(&(last_raw, last_force)) = table.last() else {
|
||||||
|
return 0.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
if raw <= first_raw {
|
||||||
|
return first_force;
|
||||||
|
}
|
||||||
|
if raw >= last_raw {
|
||||||
|
return last_force;
|
||||||
|
}
|
||||||
|
|
||||||
|
let right = table.partition_point(|(raw_point, _)| *raw_point <= raw);
|
||||||
|
let (raw_left, force_left) = table[right - 1];
|
||||||
|
let (raw_right, force_right) = table[right];
|
||||||
|
let ratio = (raw - raw_left) as f32 / (raw_right - raw_left).max(1) as f32;
|
||||||
|
force_left + ratio * (force_right - force_left)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn force_from_config_or_fallback(
|
||||||
|
raw: u32,
|
||||||
|
config_values: Option<&BTreeMap<String, u32>>,
|
||||||
|
fallback_raw: &[u32],
|
||||||
|
fallback_force_centi_n: &[f32],
|
||||||
|
) -> f32 {
|
||||||
|
if let Some(config_values) = config_values {
|
||||||
|
let table = to_force_table(config_values);
|
||||||
|
if table.len() >= 2 && table.windows(2).all(|pair| pair[0].0 <= pair[1].0) {
|
||||||
|
return interpolate_force(raw, &table);
|
||||||
|
}
|
||||||
|
log::warn!(
|
||||||
|
"Hand force config raw values are not monotonically increasing; using fallback table"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let fallback = fallback_raw
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.zip(fallback_force_centi_n.iter().map(|force| force / 100.0))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
interpolate_force(raw, &fallback)
|
||||||
|
}
|
||||||
|
|
||||||
fn raw_to_gd(raw: u32) -> f32 {
|
fn raw_to_gd(raw: u32) -> f32 {
|
||||||
const RAW: [u32; 11] = [
|
const RAW: [u32; 11] = [
|
||||||
0, 486, 5310, 8130, 10005, 12883, 14700, 18141, 19877, 26270, 32648,
|
0, 486, 5310, 8130, 10005, 12883, 14700, 18141, 19877, 26270, 32648,
|
||||||
@@ -797,7 +863,7 @@ fn raw_to_gd(raw: u32) -> f32 {
|
|||||||
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
|
(FORCE_CENTI_N[left] + ratio * (FORCE_CENTI_N[right] - FORCE_CENTI_N[left])) / 100.0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn raw_to_g1(raw: u32) -> f32 {
|
fn raw_to_g1(raw: u32, config: Option<&HandForceConfig>) -> f32 {
|
||||||
const RAW: [u32; 11] = [
|
const RAW: [u32; 11] = [
|
||||||
0, 35694, 226640, 319868, 398021, 450448, 510506, 592075, 655760, 771671, 813967,
|
0, 35694, 226640, 319868, 398021, 450448, 510506, 592075, 655760, 771671, 813967,
|
||||||
];
|
];
|
||||||
@@ -805,34 +871,10 @@ fn raw_to_g1(raw: u32) -> f32 {
|
|||||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
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] {
|
force_from_config_or_fallback(raw, config.map(|cfg| &cfg.thumb), &RAW, &FORCE_CENTI_N)
|
||||||
return FORCE_CENTI_N[0] / 100.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if raw >= RAW[RAW.len() - 1] {
|
fn raw_to_g2(raw: u32, config: Option<&HandForceConfig>) -> f32 {
|
||||||
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] = [
|
const RAW: [u32; 11] = [
|
||||||
0, 45490, 267579, 352066, 431681, 457605, 550875, 624327, 732735, 883397, 899338,
|
0, 45490, 267579, 352066, 431681, 457605, 550875, 624327, 732735, 883397, 899338,
|
||||||
];
|
];
|
||||||
@@ -840,34 +882,10 @@ fn raw_to_g2(raw: u32) -> f32 {
|
|||||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
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] {
|
force_from_config_or_fallback(raw, config.map(|cfg| &cfg.index), &RAW, &FORCE_CENTI_N)
|
||||||
return FORCE_CENTI_N[0] / 100.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if raw >= RAW[RAW.len() - 1] {
|
fn raw_to_g3(raw: u32, config: Option<&HandForceConfig>) -> f32 {
|
||||||
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] = [
|
const RAW: [u32; 11] = [
|
||||||
0, 51562, 252840, 326531, 386571, 415819, 514679, 631745, 678945, 810847, 900422,
|
0, 51562, 252840, 326531, 386571, 415819, 514679, 631745, 678945, 810847, 900422,
|
||||||
];
|
];
|
||||||
@@ -875,34 +893,10 @@ fn raw_to_g3(raw: u32) -> f32 {
|
|||||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
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] {
|
force_from_config_or_fallback(raw, config.map(|cfg| &cfg.middle), &RAW, &FORCE_CENTI_N)
|
||||||
return FORCE_CENTI_N[0] / 100.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if raw >= RAW[RAW.len() - 1] {
|
fn raw_to_g4(raw: u32, config: Option<&HandForceConfig>) -> f32 {
|
||||||
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] = [
|
const RAW: [u32; 11] = [
|
||||||
0, 33213, 220611, 307383, 369637, 430462, 489659, 584789, 664960, 802749, 926907,
|
0, 33213, 220611, 307383, 369637, 430462, 489659, 584789, 664960, 802749, 926907,
|
||||||
];
|
];
|
||||||
@@ -910,34 +904,10 @@ fn raw_to_g4(raw: u32) -> f32 {
|
|||||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
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] {
|
force_from_config_or_fallback(raw, config.map(|cfg| &cfg.ring), &RAW, &FORCE_CENTI_N)
|
||||||
return FORCE_CENTI_N[0] / 100.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if raw >= RAW[RAW.len() - 1] {
|
fn raw_to_g5(raw: u32, config: Option<&HandForceConfig>) -> f32 {
|
||||||
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] = [
|
const RAW: [u32; 11] = [
|
||||||
0, 44291, 216657, 301950, 366186, 384791, 496521, 566189, 701459, 845608, 940796,
|
0, 44291, 216657, 301950, 366186, 384791, 496521, 566189, 701459, 845608, 940796,
|
||||||
];
|
];
|
||||||
@@ -945,31 +915,7 @@ fn raw_to_g5(raw: u32) -> f32 {
|
|||||||
0.0, 57.0, 257.0, 357.0, 457.0, 557.0, 657.0, 857.0, 1057.0, 1557.0, 2057.0,
|
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] {
|
force_from_config_or_fallback(raw, config.map(|cfg| &cfg.little), &RAW, &FORCE_CENTI_N)
|
||||||
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_g6(raw: u32) -> f32 {
|
fn raw_to_g6(raw: u32) -> f32 {
|
||||||
@@ -1219,6 +1165,30 @@ fn normalize_pressure_value(value: u32) -> [f32; 2] {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn raw_to_g1_uses_loaded_thumb_config() {
|
||||||
|
let config: HandForceConfig = toml::from_str(
|
||||||
|
r#"
|
||||||
|
[thumb]
|
||||||
|
"0.57" = 10
|
||||||
|
"2.57" = 20
|
||||||
|
|
||||||
|
[index]
|
||||||
|
"0.57" = 1
|
||||||
|
[middle]
|
||||||
|
"0.57" = 1
|
||||||
|
[ring]
|
||||||
|
"0.57" = 1
|
||||||
|
[little]
|
||||||
|
"0.57" = 1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.expect("test config should parse");
|
||||||
|
|
||||||
|
assert_eq!(raw_to_g1(20, Some(&config)), 2.57);
|
||||||
|
assert_ne!(raw_to_g1(20, None), 2.57);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pressure_normalization_makes_low_hand_values_visible() {
|
fn pressure_normalization_makes_low_hand_values_visible() {
|
||||||
let [zero_intensity, zero_label] = normalize_pressure_value(4);
|
let [zero_intensity, zero_label] = normalize_pressure_value(4);
|
||||||
|
|||||||
70
src/config.rs
Normal file
70
src/config.rs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use std::{
|
||||||
|
collections::BTreeMap,
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct HandForceConfig {
|
||||||
|
pub thumb: BTreeMap<String, u32>,
|
||||||
|
pub index: BTreeMap<String, u32>,
|
||||||
|
pub middle: BTreeMap<String, u32>,
|
||||||
|
pub ring: BTreeMap<String, u32>,
|
||||||
|
pub little: BTreeMap<String, u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_load_hand_force_config() -> Option<HandForceConfig> {
|
||||||
|
let path: PathBuf = std::env::current_exe().ok()?.parent()?.join("config.toml");
|
||||||
|
load_hand_force_config(&path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_hand_force_config(path: &Path) -> Option<HandForceConfig> {
|
||||||
|
let content = fs::read_to_string(path).ok()?;
|
||||||
|
toml::from_str(&content).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loads_hand_force_config_toml() {
|
||||||
|
let path = std::env::temp_dir().join(format!(
|
||||||
|
"eskin-hand-force-config-{}-{}.toml",
|
||||||
|
std::process::id(),
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("system clock should be after Unix epoch")
|
||||||
|
.as_nanos()
|
||||||
|
));
|
||||||
|
fs::write(
|
||||||
|
&path,
|
||||||
|
r#"
|
||||||
|
[thumb]
|
||||||
|
"0.57" = 35694
|
||||||
|
"2.57" = 226640
|
||||||
|
|
||||||
|
[index]
|
||||||
|
"0.57" = 45490
|
||||||
|
|
||||||
|
[middle]
|
||||||
|
"0.57" = 51562
|
||||||
|
|
||||||
|
[ring]
|
||||||
|
"0.57" = 33213
|
||||||
|
|
||||||
|
[little]
|
||||||
|
"0.57" = 44291
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.expect("test config should be writable");
|
||||||
|
|
||||||
|
let config = load_hand_force_config(&path).expect("valid TOML should load");
|
||||||
|
fs::remove_file(&path).expect("test config should be removable");
|
||||||
|
|
||||||
|
assert_eq!(config.thumb["2.57"], 226640);
|
||||||
|
assert_eq!(config.index["0.57"], 45490);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
mod app;
|
mod app;
|
||||||
mod breakout;
|
mod breakout;
|
||||||
|
mod config;
|
||||||
mod connection;
|
mod connection;
|
||||||
mod force;
|
mod force;
|
||||||
mod matrix;
|
mod matrix;
|
||||||
|
|||||||
Reference in New Issue
Block a user