Skip to main content

joydev_rs/
correction.rs

1use std::fmt;
2use std::mem::{transmute, zeroed};
3
4use sys::js_corr;
5
6use crate::CorrectionType;
7
8/// Axis correction
9///
10/// Correction is used for axis calibration.
11#[derive(Clone, Copy)]
12pub struct Correction {
13	pub(crate) corr: js_corr,
14}
15
16impl Correction {
17	/// Returns coefficient values
18	pub const fn coefficient(&self) -> &[i32; 8] {
19		&self.corr.coef
20	}
21
22	/// Returns coefficient values for editing
23	pub fn coefficient_mut(&mut self) -> &mut [i32; 8] {
24		&mut self.corr.coef
25	}
26
27	/// Creates new correction from `coefficient` values, `precision` and `type`
28	pub const fn new(coefficient: &[i32; 8], precision: i16, type_: CorrectionType) -> Self {
29		Self {
30			corr: js_corr {
31				coef: *coefficient,
32				prec: precision,
33				type_: type_ as u16,
34			},
35		}
36	}
37
38	/// Returns precision
39	pub const fn precision(&self) -> i16 {
40		self.corr.prec
41	}
42
43	/// Set precision
44	pub fn set_precision(&mut self, precision: i16) {
45		self.corr.prec = precision;
46	}
47
48	/// Set type
49	pub fn set_type(&mut self, type_: CorrectionType) {
50		self.corr.prec = type_ as i16;
51	}
52
53	/// Returns type
54	pub fn type_(&self) -> CorrectionType {
55		unsafe { transmute(self.corr.type_) }
56	}
57}
58
59impl Default for Correction {
60	fn default() -> Self {
61		Self {
62			corr: unsafe { zeroed() },
63		}
64	}
65}
66
67impl fmt::Debug for Correction {
68	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69		write!(
70			f,
71			"Correction {{ coefficient: {:?}, precision: {:?}, type: {:?} }}",
72			self.coefficient(),
73			self.precision(),
74			self.type_()
75		)
76	}
77}