@@ -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
125125impl 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 {
274274impl 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 {
345345impl 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 ;
0 commit comments