Skip to content

Commit 30c0678

Browse files
jscatena88barafael
authored andcommitted
Const all the things
1 parent a8ad713 commit 30c0678

File tree

6 files changed

+62
-64
lines changed

6 files changed

+62
-64
lines changed

src/accel.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct Accel {
2626
}
2727

2828
impl Accel {
29-
pub fn new(x: i16, y: i16, z: i16) -> Self {
29+
pub const fn new(x: i16, y: i16, z: i16) -> Self {
3030
Self { x, y, z }
3131
}
3232

@@ -36,7 +36,7 @@ impl Accel {
3636
/// - 2 bytes per axis (high byte, low byte)
3737
/// - Big-endian byte order
3838
/// - Signed integers (-32768 to +32767)
39-
pub fn from_bytes(data: [u8; 6]) -> Self {
39+
pub const fn from_bytes(data: [u8; 6]) -> Self {
4040
let x = [data[0], data[1]];
4141
let y = [data[2], data[3]];
4242
let z = [data[4], data[5]];
@@ -47,22 +47,22 @@ impl Accel {
4747
}
4848
}
4949

50-
pub fn to_bytes(&self) -> [u8; 6] {
50+
pub const fn to_bytes(&self) -> [u8; 6] {
5151
let x = self.x.to_be_bytes();
5252
let y = self.y.to_be_bytes();
5353
let z = self.z.to_be_bytes();
5454
[x[0], x[1], y[0], y[1], z[0], z[1]]
5555
}
5656

57-
pub fn x(&self) -> i16 {
57+
pub const fn x(&self) -> i16 {
5858
self.x
5959
}
6060

61-
pub fn y(&self) -> i16 {
61+
pub const fn y(&self) -> i16 {
6262
self.y
6363
}
6464

65-
pub fn z(&self) -> i16 {
65+
pub const fn z(&self) -> i16 {
6666
self.z
6767
}
6868

@@ -72,7 +72,7 @@ impl Accel {
7272
/// 1. Takes raw ADC values (-32768 to +32767)
7373
/// 2. Divides by scale factor based on full-scale range
7474
/// 3. Results in g-force values (e.g., ±2g, ±4g, etc.)
75-
pub fn scaled(&self, scale: AccelFullScale) -> AccelF32 {
75+
pub const fn scaled(&self, scale: AccelFullScale) -> AccelF32 {
7676
AccelF32 {
7777
x: scale.scale_value(self.x),
7878
y: scale.scale_value(self.y),
@@ -118,7 +118,7 @@ impl AccelFullScale {
118118
}
119119
}
120120

121-
pub fn scale_value(self, value: i16) -> f32 {
121+
pub const fn scale_value(self, value: i16) -> f32 {
122122
(value as f32) / self.scale()
123123
}
124124
}
@@ -147,19 +147,19 @@ pub struct AccelF32 {
147147
}
148148

149149
impl AccelF32 {
150-
pub fn new(x: f32, y: f32, z: f32) -> Self {
150+
pub const fn new(x: f32, y: f32, z: f32) -> Self {
151151
Self { x, y, z }
152152
}
153153

154-
pub fn x(&self) -> f32 {
154+
pub const fn x(&self) -> f32 {
155155
self.x
156156
}
157157

158-
pub fn y(&self) -> f32 {
158+
pub const fn y(&self) -> f32 {
159159
self.y
160160
}
161161

162-
pub fn z(&self) -> f32 {
162+
pub const fn z(&self) -> f32 {
163163
self.z
164164
}
165165
}

src/calibration.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ impl CalibrationThreshold {
5050
}
5151

5252
/// Get the threshold value
53-
pub fn value(&self) -> i16 {
53+
pub const fn value(&self) -> i16 {
5454
self.value
5555
}
5656

5757
/// Check if the given value is within the threshold
58-
pub(crate) fn is_value_within(self, value: i16) -> bool {
58+
pub(crate) const fn is_value_within(self, value: i16) -> bool {
5959
value.abs() <= self.value
6060
}
6161

6262
/// Check if the given acceleration vector is within the threshold
63-
pub fn is_accel_within(self, accel: &Accel) -> bool {
63+
pub const fn is_accel_within(self, accel: &Accel) -> bool {
6464
self.is_value_within(accel.x())
6565
&& self.is_value_within(accel.y())
6666
&& self.is_value_within(accel.z())
6767
}
6868

6969
/// Check if the given gyro vector is within the threshold
70-
pub fn is_gyro_within(self, gyro: &Gyro) -> bool {
70+
pub const fn is_gyro_within(self, gyro: &Gyro) -> bool {
7171
self.is_value_within(gyro.x())
7272
&& self.is_value_within(gyro.y())
7373
&& self.is_value_within(gyro.z())
@@ -79,7 +79,7 @@ impl CalibrationThreshold {
7979
/// This is technically the single step of a PID controller where we are using only
8080
/// the `I` part (`D` is not needed because calibration is not time-dependent,
8181
/// and `P` because noise is mitigated by working on averages).
82-
pub fn next_offset(self, current_mean: i16, current_offset: i16) -> i16 {
82+
pub const fn next_offset(self, current_mean: i16, current_offset: i16) -> i16 {
8383
// In this PID controller the "error" is the observed average (when the calibration
8484
// is correct the average is expected to be zero, or anyway within the given threshold).
8585
if self.is_value_within(current_mean) {
@@ -124,7 +124,7 @@ pub enum ReferenceGravity {
124124

125125
impl ReferenceGravity {
126126
/// Actual `g` value at a given scale
127-
fn gravity_value(scale: AccelFullScale) -> i16 {
127+
const fn gravity_value(scale: AccelFullScale) -> i16 {
128128
match scale {
129129
AccelFullScale::G2 => 16384,
130130
AccelFullScale::G4 => 8192,
@@ -134,7 +134,7 @@ impl ReferenceGravity {
134134
}
135135

136136
/// Acceleration vector representing gravity compensation in the given direction
137-
pub fn gravity_compensation(self, scale: AccelFullScale) -> Accel {
137+
pub const fn gravity_compensation(self, scale: AccelFullScale) -> Accel {
138138
match self {
139139
Self::Zero => Accel::new(0, 0, 0),
140140
Self::XN => Accel::new(-Self::gravity_value(scale), 0, 0),
@@ -168,47 +168,47 @@ impl CalibrationActions {
168168
const GYRO_Z: u8 = 1 << 5;
169169

170170
/// Build an empty bit set
171-
pub fn empty() -> Self {
171+
pub const fn empty() -> Self {
172172
Self { flags: 0 }
173173
}
174174

175175
/// Build a full bit set
176-
pub fn all() -> Self {
176+
pub const fn all() -> Self {
177177
Self { flags: 0x3f }
178178
}
179179

180180
/// Check if we have nothing more to calibrate
181-
pub fn is_empty(self) -> bool {
181+
pub const fn is_empty(self) -> bool {
182182
self.flags == 0
183183
}
184184

185185
/// Check if acceleration x axis calibration is required
186-
pub fn accel_x(self) -> bool {
186+
pub const fn accel_x(self) -> bool {
187187
self.flags & Self::ACCEL_X != 0
188188
}
189189
/// Check if acceleration y axis calibration is required
190-
pub fn accel_y(self) -> bool {
190+
pub const fn accel_y(self) -> bool {
191191
self.flags & Self::ACCEL_Y != 0
192192
}
193193
/// Check if acceleration z axis calibration is required
194-
pub fn accel_z(self) -> bool {
194+
pub const fn accel_z(self) -> bool {
195195
self.flags & Self::ACCEL_Z != 0
196196
}
197197
/// Check if gyro x axis calibration is required
198-
pub fn gyro_x(self) -> bool {
198+
pub const fn gyro_x(self) -> bool {
199199
self.flags & Self::GYRO_X != 0
200200
}
201201
/// Check if gyro y axis calibration is required
202-
pub fn gyro_y(self) -> bool {
202+
pub const fn gyro_y(self) -> bool {
203203
self.flags & Self::GYRO_Y != 0
204204
}
205205
/// Check if gyro z axis calibration is required
206-
pub fn gyro_z(self) -> bool {
206+
pub const fn gyro_z(self) -> bool {
207207
self.flags & Self::GYRO_Z != 0
208208
}
209209

210210
/// Set the given flag
211-
fn with_flag(self, value: bool, flag: u8) -> Self {
211+
const fn with_flag(self, value: bool, flag: u8) -> Self {
212212
Self {
213213
flags: if value {
214214
self.flags | flag
@@ -219,27 +219,27 @@ impl CalibrationActions {
219219
}
220220

221221
/// Set acceleration x flag
222-
pub fn with_accel_x(self, value: bool) -> Self {
222+
pub const fn with_accel_x(self, value: bool) -> Self {
223223
self.with_flag(value, Self::ACCEL_X)
224224
}
225225
/// Set acceleration y flag
226-
pub fn with_accel_y(self, value: bool) -> Self {
226+
pub const fn with_accel_y(self, value: bool) -> Self {
227227
self.with_flag(value, Self::ACCEL_Y)
228228
}
229229
/// Set acceleration z flag
230-
pub fn with_accel_z(self, value: bool) -> Self {
230+
pub const fn with_accel_z(self, value: bool) -> Self {
231231
self.with_flag(value, Self::ACCEL_Z)
232232
}
233233
/// Set gyro x flag
234-
pub fn with_gyro_x(self, value: bool) -> Self {
234+
pub const fn with_gyro_x(self, value: bool) -> Self {
235235
self.with_flag(value, Self::GYRO_X)
236236
}
237237
/// Set gyro y flag
238-
pub fn with_gyro_y(self, value: bool) -> Self {
238+
pub const fn with_gyro_y(self, value: bool) -> Self {
239239
self.with_flag(value, Self::GYRO_Y)
240240
}
241241
/// Set gyro z flag
242-
pub fn with_gyro_z(self, value: bool) -> Self {
242+
pub const fn with_gyro_z(self, value: bool) -> Self {
243243
self.with_flag(value, Self::GYRO_Z)
244244
}
245245
}
@@ -274,7 +274,7 @@ pub struct CalibrationParameters {
274274
impl CalibrationParameters {
275275
/// Create calibration parameters given accel and gyro scale and a reference gravity
276276
/// (sensible defaults are used for all other parameters)
277-
pub fn new(
277+
pub const fn new(
278278
accel_scale: AccelFullScale,
279279
gyro_scale: GyroFullScale,
280280
gravity: ReferenceGravity,
@@ -292,7 +292,7 @@ impl CalibrationParameters {
292292

293293
/// Change acceleration threshold
294294
/// (consumes and returns `Self` to be callable in a "builder-like" pattern)
295-
pub fn with_accel_threshold(self, threshold: i16) -> Self {
295+
pub const fn with_accel_threshold(self, threshold: i16) -> Self {
296296
Self {
297297
accel_threshold: CalibrationThreshold { value: threshold },
298298
..self
@@ -301,7 +301,7 @@ impl CalibrationParameters {
301301

302302
/// Change gyro threshold
303303
/// (consumes and returns `Self` to be callable in a "builder-like" pattern)
304-
pub fn with_gyro_threshold(self, threshold: i16) -> Self {
304+
pub const fn with_gyro_threshold(self, threshold: i16) -> Self {
305305
Self {
306306
gyro_threshold: CalibrationThreshold { value: threshold },
307307
..self
@@ -310,7 +310,7 @@ impl CalibrationParameters {
310310

311311
/// Change warmup iterations count
312312
/// (consumes and returns `Self` to be callable in a "builder-like" pattern)
313-
pub fn with_warmup_iterations(self, warmup_iterations: usize) -> Self {
313+
pub const fn with_warmup_iterations(self, warmup_iterations: usize) -> Self {
314314
Self {
315315
warmup_iterations,
316316
..self
@@ -319,7 +319,7 @@ impl CalibrationParameters {
319319

320320
/// Change iterations count
321321
/// (consumes and returns `Self` to be callable in a "builder-like" pattern)
322-
pub fn with_iterations(self, iterations: usize) -> Self {
322+
pub const fn with_iterations(self, iterations: usize) -> Self {
323323
Self { iterations, ..self }
324324
}
325325
}
@@ -345,7 +345,7 @@ pub struct MeanAccumulator {
345345
impl MeanAccumulator {
346346
/// Initializes the means with zero values
347347
/// (and also fixes the reference gravity compensation)
348-
pub fn new(accel_scale: AccelFullScale, gravity: ReferenceGravity) -> Self {
348+
pub const fn new(accel_scale: AccelFullScale, gravity: ReferenceGravity) -> Self {
349349
Self {
350350
ax: 0,
351351
ay: 0,
@@ -358,7 +358,7 @@ impl MeanAccumulator {
358358
}
359359

360360
/// Adds a new sample (subtracting the reference gravity)
361-
pub fn add(&mut self, accel: &Accel, gyro: &Gyro) {
361+
pub const fn add(&mut self, accel: &Accel, gyro: &Gyro) {
362362
self.ax += (accel.x() as i32) - (self.gravity_compensation.x() as i32);
363363
self.ay += (accel.y() as i32) - (self.gravity_compensation.y() as i32);
364364
self.az += (accel.z() as i32) - (self.gravity_compensation.z() as i32);
@@ -368,7 +368,7 @@ impl MeanAccumulator {
368368
}
369369

370370
/// Compute average values (consumes `self` because the computation is done)
371-
pub fn means(mut self) -> (Accel, Gyro) {
371+
pub const fn means(mut self) -> (Accel, Gyro) {
372372
self.ax /= ITERATIONS as i32;
373373
self.ay /= ITERATIONS as i32;
374374
self.az /= ITERATIONS as i32;

src/gyro.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ pub struct Gyro {
1919
}
2020

2121
impl Gyro {
22-
pub fn new(x: i16, y: i16, z: i16) -> Self {
22+
pub const fn new(x: i16, y: i16, z: i16) -> Self {
2323
Self { x, y, z }
2424
}
2525

26-
pub fn from_bytes(data: [u8; 6]) -> Self {
26+
pub const fn from_bytes(data: [u8; 6]) -> Self {
2727
let x = [data[0], data[1]];
2828
let y = [data[2], data[3]];
2929
let z = [data[4], data[5]];
@@ -34,26 +34,26 @@ impl Gyro {
3434
}
3535
}
3636

37-
pub fn to_bytes(&self) -> [u8; 6] {
37+
pub const fn to_bytes(&self) -> [u8; 6] {
3838
let x = self.x.to_be_bytes();
3939
let y = self.y.to_be_bytes();
4040
let z = self.z.to_be_bytes();
4141
[x[0], x[1], y[0], y[1], z[0], z[1]]
4242
}
4343

44-
pub fn x(&self) -> i16 {
44+
pub const fn x(&self) -> i16 {
4545
self.x
4646
}
4747

48-
pub fn y(&self) -> i16 {
48+
pub const fn y(&self) -> i16 {
4949
self.y
5050
}
5151

52-
pub fn z(&self) -> i16 {
52+
pub const fn z(&self) -> i16 {
5353
self.z
5454
}
5555

56-
pub fn scaled(&self, scale: GyroFullScale) -> GyroF32 {
56+
pub const fn scaled(&self, scale: GyroFullScale) -> GyroF32 {
5757
GyroF32 {
5858
x: scale.scale_value(self.x),
5959
y: scale.scale_value(self.y),
@@ -100,7 +100,7 @@ impl GyroFullScale {
100100
}
101101
}
102102

103-
pub fn scale_value(self, value: i16) -> f32 {
103+
pub const fn scale_value(self, value: i16) -> f32 {
104104
(value as f32) / self.scale()
105105
}
106106
}
@@ -124,19 +124,19 @@ pub struct GyroF32 {
124124
}
125125

126126
impl GyroF32 {
127-
pub fn new(x: f32, y: f32, z: f32) -> Self {
127+
pub const fn new(x: f32, y: f32, z: f32) -> Self {
128128
Self { x, y, z }
129129
}
130130

131-
pub fn x(&self) -> f32 {
131+
pub const fn x(&self) -> f32 {
132132
self.x
133133
}
134134

135-
pub fn y(&self) -> f32 {
135+
pub const fn y(&self) -> f32 {
136136
self.y
137137
}
138138

139-
pub fn z(&self) -> f32 {
139+
pub const fn z(&self) -> f32 {
140140
self.z
141141
}
142142
}
@@ -145,4 +145,4 @@ impl From<GyroF32> for [f32; 3] {
145145
fn from(value: GyroF32) -> Self {
146146
[value.x, value.y, value.z]
147147
}
148-
}
148+
}

0 commit comments

Comments
 (0)