Skip to content

Commit c809c08

Browse files
blakesmitttyShadowApex
authored andcommitted
chore(normalization): move repeated normalization functions to src/input/event/value.rs
1 parent e867a8e commit c809c08

17 files changed

+177
-322
lines changed

src/input/event/value.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,73 @@ pub enum InputValue {
6060
},
6161
}
6262

63+
/// Returns a value between -1.0 and 1.0 based on the given value with its
64+
/// minimum and maximum values.
65+
pub fn normalize_signed_value(raw_value: f64, min: f64, max: f64) -> f64 {
66+
let mid = (max + min) / 2.0;
67+
let event_value = raw_value - mid;
68+
69+
// Normalize the value
70+
if event_value >= 0.0 {
71+
let maximum = max - mid;
72+
event_value / maximum
73+
} else {
74+
let minimum = min - mid;
75+
let value = event_value / minimum;
76+
-value
77+
}
78+
}
79+
80+
/// Convert the given normalized signed value to the real value based on the given
81+
/// minimum and maximum axis range.
82+
pub fn denormalize_signed_value_i16(normal_value: f64, min: f64, max: f64) -> i16 {
83+
let mid = (max + min) / 2.0;
84+
let normal_value_abs = normal_value.abs();
85+
if normal_value >= 0.0 {
86+
let maximum = max - mid;
87+
let value = normal_value * maximum + mid;
88+
value as i16
89+
} else {
90+
let minimum = min - mid;
91+
let value = normal_value_abs * minimum + mid;
92+
value as i16
93+
}
94+
}
95+
96+
/// Convert the given normalized signed value to the real value based on the given
97+
/// minimum and maximum axis range.
98+
pub fn denormalize_signed_value_u8(normal_value: f64, min: f64, max: f64) -> u8 {
99+
let mid = (max + min) / 2.0;
100+
let normal_value_abs = normal_value.abs();
101+
if normal_value >= 0.0 {
102+
let maximum = max - mid;
103+
let value = normal_value * maximum + mid;
104+
value as u8
105+
} else {
106+
let minimum = min - mid;
107+
let value = normal_value_abs * minimum + mid;
108+
value as u8
109+
}
110+
}
111+
112+
// Returns a value between 0.0 and 1.0 based on the given value with its
113+
// maximum.
114+
pub fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
115+
raw_value / max
116+
}
117+
118+
/// De-normalizes the given value from 0.0 - 1.0 into a real value based on
119+
/// the maximum axis range.
120+
pub fn denormalize_unsigned_value_u16(normal_value: f64, max: f64) -> u16 {
121+
(normal_value * max).round() as u16
122+
}
123+
124+
/// De-normalizes the given value from 0.0 - 1.0 into a real value based on
125+
/// the maximum axis range.
126+
pub fn denormalize_unsigned_value_u8(normal_value: f64, max: f64) -> u8 {
127+
(normal_value * max).round() as u8
128+
}
129+
63130
impl InputValue {
64131
/// Returns whether or not the value is "pressed"
65132
pub fn pressed(&self) -> bool {

src/input/source/hidraw/dualsense.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ use crate::{
1414
Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger, Touch, TouchButton,
1515
Touchpad,
1616
},
17-
event::{native::NativeEvent, value::InputValue},
17+
event::{
18+
native::NativeEvent,
19+
value::InputValue,
20+
value::{normalize_signed_value, normalize_unsigned_value},
21+
},
1822
output_event::OutputEvent,
1923
source::{InputError, OutputError, SourceInputDevice, SourceOutputDevice},
2024
},
@@ -368,29 +372,6 @@ fn translate_event(event: dualsense::event::Event) -> NativeEvent {
368372
}
369373
}
370374

371-
/// Returns a value between -1.0 and 1.0 based on the given value with its
372-
/// minimum and maximum values.
373-
fn normalize_signed_value(raw_value: f64, min: f64, max: f64) -> f64 {
374-
let mid = (max + min) / 2.0;
375-
let event_value = raw_value - mid;
376-
377-
// Normalize the value
378-
if event_value >= 0.0 {
379-
let maximum = max - mid;
380-
event_value / maximum
381-
} else {
382-
let minimum = min - mid;
383-
let value = event_value / minimum;
384-
-value
385-
}
386-
}
387-
388-
// Returns a value between 0.0 and 1.0 based on the given value with its
389-
// maximum.
390-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
391-
raw_value / max
392-
}
393-
394375
/// Normalize the value to something between -1.0 and 1.0 based on the DualSense's
395376
/// minimum and maximum axis ranges.
396377
fn normalize_axis_value(event: &dualsense::event::AxisEvent) -> InputValue {

src/input/source/hidraw/flydigi_vader_4_pro.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use crate::{
77
},
88
input::{
99
capability::{Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger},
10-
event::{native::NativeEvent, value::InputValue},
10+
event::{
11+
native::NativeEvent,
12+
value::InputValue,
13+
value::{normalize_signed_value, normalize_unsigned_value},
14+
},
1115
source::{InputError, SourceInputDevice, SourceOutputDevice},
1216
},
1317
udev::device::UdevDevice,
@@ -55,29 +59,6 @@ impl Debug for Vader4Pro {
5559
}
5660
}
5761

58-
/// Returns a value between -1.0 and 1.0 based on the given value with its
59-
/// minimum and maximum values.
60-
fn normalize_signed_value(raw_value: f64, min: f64, max: f64) -> f64 {
61-
let mid = (max + min) / 2.0;
62-
let event_value = raw_value - mid;
63-
64-
// Normalize the value
65-
if event_value >= 0.0 {
66-
let maximum = max - mid;
67-
event_value / maximum
68-
} else {
69-
let minimum = min - mid;
70-
let value = event_value / minimum;
71-
-value
72-
}
73-
}
74-
75-
// Returns a value between 0.0 and 1.0 based on the given value with its
76-
// maximum.
77-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
78-
raw_value / max
79-
}
80-
8162
/// Normalize the value to something between -1.0 and 1.0 based on the
8263
/// minimum and maximum axis ranges.
8364
fn normalize_axis_value(event: event::JoystickEvent) -> InputValue {

src/input/source/hidraw/fts3528.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
},
1010
input::{
1111
capability::{Capability, Touch},
12-
event::{native::NativeEvent, value::InputValue},
12+
event::{native::NativeEvent, value::normalize_unsigned_value, value::InputValue},
1313
source::{InputError, SourceInputDevice, SourceOutputDevice},
1414
},
1515
udev::device::UdevDevice,
@@ -51,20 +51,14 @@ impl Debug for Fts3528Touchscreen {
5151
}
5252
}
5353

54-
// Returns a value between 0.0 and 1.0 based on the given value with its
55-
// maximum.
56-
fn normalize_unsigned_value(raw_value: u16, max: u16) -> f64 {
57-
raw_value as f64 / max as f64
58-
}
59-
6054
/// Normalizes the given input into an input value
6155
fn normalize_axis_value(touch: TouchAxisInput) -> InputValue {
6256
// Normalize the x, y values if touching
6357
let (x, y) = match touch.is_touching {
6458
true => {
6559
// NOTE: X and Y are flipped due to panel rotation.
66-
let x = normalize_unsigned_value(touch.y, TOUCHSCREEN_Y_MAX);
67-
let y = 1.0 - normalize_unsigned_value(touch.x, TOUCHSCREEN_X_MAX);
60+
let x = normalize_unsigned_value(touch.y as f64, TOUCHSCREEN_Y_MAX as f64);
61+
let y = 1.0 - normalize_unsigned_value(touch.x as f64, TOUCHSCREEN_X_MAX as f64);
6862
(Some(x), Some(y))
6963
}
7064
false => (None, None),

src/input/source/hidraw/horipad_steam.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ use crate::{
77
},
88
input::{
99
capability::{Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger},
10-
event::{native::NativeEvent, value::InputValue},
10+
event::{
11+
native::NativeEvent,
12+
value::InputValue,
13+
value::{normalize_signed_value, normalize_unsigned_value},
14+
},
1115
source::{InputError, SourceInputDevice, SourceOutputDevice},
1216
},
1317
udev::device::UdevDevice,
@@ -49,29 +53,6 @@ impl Debug for HoripadSteam {
4953
}
5054
}
5155

52-
/// Returns a value between -1.0 and 1.0 based on the given value with its
53-
/// minimum and maximum values.
54-
fn normalize_signed_value(raw_value: f64, min: f64, max: f64) -> f64 {
55-
let mid = (max + min) / 2.0;
56-
let event_value = raw_value - mid;
57-
58-
// Normalize the value
59-
if event_value >= 0.0 {
60-
let maximum = max - mid;
61-
event_value / maximum
62-
} else {
63-
let minimum = min - mid;
64-
let value = event_value / minimum;
65-
-value
66-
}
67-
}
68-
69-
// Returns a value between 0.0 and 1.0 based on the given value with its
70-
// maximum.
71-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
72-
raw_value / max
73-
}
74-
7556
/// Normalize the value to something between -1.0 and 1.0 based on the
7657
/// minimum and maximum axis ranges.
7758
fn normalize_axis_value(event: event::JoystickEvent) -> InputValue {

src/input/source/hidraw/legion_go.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ use crate::{
1313
Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger, Mouse, MouseButton,
1414
Source, Touch, TouchButton, Touchpad,
1515
},
16-
event::{native::NativeEvent, value::InputValue},
16+
event::{
17+
native::NativeEvent,
18+
value::InputValue,
19+
value::{normalize_signed_value, normalize_unsigned_value},
20+
},
1721
source::{InputError, SourceInputDevice, SourceOutputDevice},
1822
},
1923
udev::device::UdevDevice,
@@ -262,29 +266,6 @@ impl Debug for LegionGoController {
262266
}
263267
}
264268

265-
/// Returns a value between -1.0 and 1.0 based on the given value with its
266-
/// minimum and maximum values.
267-
fn normalize_signed_value(raw_value: f64, min: f64, max: f64) -> f64 {
268-
let mid = (max + min) / 2.0;
269-
let event_value = raw_value - mid;
270-
271-
// Normalize the value
272-
if event_value >= 0.0 {
273-
let maximum = max - mid;
274-
event_value / maximum
275-
} else {
276-
let minimum = min - mid;
277-
let value = event_value / minimum;
278-
-value
279-
}
280-
}
281-
282-
// Returns a value between 0.0 and 1.0 based on the given value with its
283-
// maximum.
284-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
285-
raw_value / max
286-
}
287-
288269
/// Normalize the value to something between -1.0 and 1.0 based on the
289270
/// minimum and maximum axis ranges.
290271
fn normalize_axis_value(event: AxisEvent) -> InputValue {

src/input/source/hidraw/legos_touchpad.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
drivers::legos::{event, touchpad_driver::TouchpadDriver, PAD_FORCE_MAX, PAD_MOTION_MAX},
55
input::{
66
capability::{Capability, Gamepad, GamepadTrigger, Touch, TouchButton, Touchpad},
7-
event::{native::NativeEvent, value::InputValue},
7+
event::{native::NativeEvent, value::normalize_unsigned_value, value::InputValue},
88
output_event::OutputEvent,
99
source::{InputError, OutputError, SourceInputDevice, SourceOutputDevice},
1010
},
@@ -54,12 +54,6 @@ impl Debug for LegionSTouchpadController {
5454
}
5555
}
5656

57-
// Returns a value between 0.0 and 1.0 based on the given value with its
58-
// maximum.
59-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
60-
raw_value / max
61-
}
62-
6357
/// Normalize the value to something between -1.0 and 1.0 based on the
6458
/// minimum and maximum axis ranges.
6559
fn normalize_axis_value(event: event::AxisEvent) -> InputValue {

src/input/source/hidraw/legos_xinput.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ use crate::{
1414
},
1515
input::{
1616
capability::{Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger},
17-
event::{native::NativeEvent, value::InputValue},
17+
event::{
18+
native::NativeEvent,
19+
value::InputValue,
20+
value::{normalize_signed_value, normalize_unsigned_value},
21+
},
1822
output_event::OutputEvent,
1923
source::{InputError, OutputError, SourceInputDevice, SourceOutputDevice},
2024
},
@@ -193,29 +197,6 @@ impl Debug for LegionSXInputController {
193197
}
194198
}
195199

196-
/// Returns a value between -1.0 and 1.0 based on the given value with its
197-
/// minimum and maximum values.
198-
fn normalize_signed_value(raw_value: f64, min: f64, max: f64) -> f64 {
199-
let mid = (max + min) / 2.0;
200-
let event_value = raw_value - mid;
201-
202-
// Normalize the value
203-
if event_value >= 0.0 {
204-
let maximum = max - mid;
205-
event_value / maximum
206-
} else {
207-
let minimum = min - mid;
208-
let value = event_value / minimum;
209-
-value
210-
}
211-
}
212-
213-
// Returns a value between 0.0 and 1.0 based on the given value with its
214-
// maximum.
215-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
216-
raw_value / max
217-
}
218-
219200
/// Normalize the value to something between -1.0 and 1.0 based on the Deck's
220201
/// minimum and maximum axis ranges.
221202
fn normalize_axis_value(event: event::AxisEvent) -> InputValue {

src/input/source/hidraw/opineo.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
},
88
input::{
99
capability::{Capability, Gamepad, GamepadTrigger, Touch, TouchButton, Touchpad},
10-
event::{native::NativeEvent, value::InputValue},
10+
event::{native::NativeEvent, value::normalize_unsigned_value, value::InputValue},
1111
output_capability::OutputCapability,
1212
source::{InputError, OutputError, SourceInputDevice, SourceOutputDevice},
1313
},
@@ -85,12 +85,6 @@ impl Debug for OrangePiNeoTouchpad {
8585
}
8686
}
8787

88-
// Returns a value between 0.0 and 1.0 based on the given value with its
89-
// maximum.
90-
fn normalize_unsigned_value(raw_value: f64, max: f64) -> f64 {
91-
raw_value / max
92-
}
93-
9488
/// Normalize the value to something between -1.0 and 1.0 based on the Deck's
9589
/// minimum and maximum axis ranges.
9690
fn normalize_axis_value(event: event::TouchAxisEvent) -> InputValue {

0 commit comments

Comments
 (0)