m5unified 0.3.4

Safe Rust wrapper for M5Unified.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! IMU data, calibration, and axis-order helpers.
//!
//! This module wraps M5Unified's IMU class with typed vectors, sensor masks,
//! sensor kind detection, and safe accessors for combined IMU data.

use core::ffi::c_int;

use crate::system::Board;

#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct Vec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ImuAxis {
    XPos,
    XNeg,
    YPos,
    YNeg,
    ZPos,
    ZNeg,
    Raw(i32),
}

impl ImuAxis {
    pub const fn raw(self) -> i32 {
        match self {
            Self::XPos => 0,
            Self::XNeg => 1,
            Self::YPos => 2,
            Self::YNeg => 3,
            Self::ZPos => 4,
            Self::ZNeg => 5,
            Self::Raw(raw) => raw,
        }
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct ImuSensorMask(u8);

impl ImuSensorMask {
    pub const NONE: Self = Self(0);
    pub const ACCEL: Self = Self(1 << 0);
    pub const GYRO: Self = Self(1 << 1);
    pub const MAG: Self = Self(1 << 2);

    pub const fn from_raw(raw: u8) -> Self {
        Self(raw)
    }

    pub const fn raw(self) -> u8 {
        self.0
    }

    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }
}

#[derive(Debug)]
pub struct Imu;

impl Imu {
    pub fn begin(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_imu_begin() }
    }

    pub fn begin_for_board(&mut self, board: Board) -> bool {
        unsafe { m5unified_sys::m5u_imu_begin_for_board(board.raw()) }
    }

    pub fn init(&mut self) -> bool {
        self.begin()
    }

    pub fn init_for_board(&mut self, board: Board) -> bool {
        self.begin_for_board(board)
    }

    pub fn ak8963(&self) -> ImuDevice {
        ImuDevice::new(ImuDeviceKind::Ak8963)
    }

    pub fn bmm150(&self) -> ImuDevice {
        ImuDevice::new(ImuDeviceKind::Bmm150)
    }

    pub fn bmi270(&self) -> ImuDevice {
        ImuDevice::new(ImuDeviceKind::Bmi270)
    }

    pub fn mpu6886(&self) -> ImuDevice {
        ImuDevice::new(ImuDeviceKind::Mpu6886)
    }

    pub fn sh200q(&self) -> ImuDevice {
        ImuDevice::new(ImuDeviceKind::Sh200q)
    }

    pub fn accel(&self) -> Option<Vec3> {
        let (mut x, mut y, mut z) = (0.0, 0.0, 0.0);
        let ok = unsafe { m5unified_sys::m5u_imu_get_accel(&mut x, &mut y, &mut z) };
        ok.then_some(Vec3 { x, y, z })
    }

    pub fn accel_data(&self) -> Option<Vec3> {
        self.accel()
    }

    pub fn gyro(&self) -> Option<Vec3> {
        let (mut x, mut y, mut z) = (0.0, 0.0, 0.0);
        let ok = unsafe { m5unified_sys::m5u_imu_get_gyro(&mut x, &mut y, &mut z) };
        ok.then_some(Vec3 { x, y, z })
    }

    pub fn gyro_data(&self) -> Option<Vec3> {
        self.gyro()
    }

    pub fn mag(&self) -> Option<Vec3> {
        let (mut x, mut y, mut z) = (0.0, 0.0, 0.0);
        let ok = unsafe { m5unified_sys::m5u_imu_get_mag(&mut x, &mut y, &mut z) };
        ok.then_some(Vec3 { x, y, z })
    }

    pub fn gyro_mag(&self) -> Option<Vec3> {
        self.mag()
    }

    pub fn temperature_c(&self) -> Option<f32> {
        let mut temp = 0.0;
        let ok = unsafe { m5unified_sys::m5u_imu_get_temp_c(&mut temp) };
        ok.then_some(temp)
    }

    pub fn is_enabled(&self) -> bool {
        unsafe { m5unified_sys::m5u_imu_is_enabled() }
    }

    pub fn kind(&self) -> ImuKind {
        ImuKind::from_raw(unsafe { m5unified_sys::m5u_imu_get_type() as i32 })
    }

    pub fn update(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_imu_update() }
    }

    pub fn update_mask(&mut self) -> ImuSensorMask {
        let raw = unsafe { m5unified_sys::m5u_imu_update_mask() };
        ImuSensorMask::from_raw(raw.max(0) as u8)
    }

    pub fn data(&self) -> Option<ImuData> {
        let mut raw = m5unified_sys::m5u_imu_data_t::default();
        let ok = unsafe { m5unified_sys::m5u_imu_get_data(&mut raw) };
        ok.then(|| ImuData {
            usec: raw.usec,
            accel: Vec3 {
                x: raw.accel_x,
                y: raw.accel_y,
                z: raw.accel_z,
            },
            gyro: Vec3 {
                x: raw.gyro_x,
                y: raw.gyro_y,
                z: raw.gyro_z,
            },
            mag: Vec3 {
                x: raw.mag_x,
                y: raw.mag_y,
                z: raw.mag_z,
            },
            temperature_c: self.temperature_c(),
        })
    }

    pub fn load_offset_from_nvs(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_imu_load_offset_from_nvs() }
    }

    pub fn save_offset_to_nvs(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_imu_save_offset_to_nvs() }
    }

    pub fn offset_data(&self, index: i32) -> f32 {
        unsafe { m5unified_sys::m5u_imu_get_offset_data(index) }
    }

    pub fn set_calibration(&mut self, x: f32, y: f32, z: f32) {
        unsafe { m5unified_sys::m5u_imu_set_calibration(x, y, z) }
    }

    pub fn sleep(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_imu_sleep() }
    }

    pub fn set_clock_hz(&mut self, freq: u32) {
        unsafe { m5unified_sys::m5u_imu_set_clock(freq) }
    }

    pub fn set_axis_order(&mut self, axis0: ImuAxis, axis1: ImuAxis, axis2: ImuAxis) -> bool {
        unsafe { m5unified_sys::m5u_imu_set_axis_order(axis0.raw(), axis1.raw(), axis2.raw()) }
    }

    pub fn set_axis_order_right_handed(&mut self, axis0: ImuAxis, axis1: ImuAxis) -> bool {
        unsafe { m5unified_sys::m5u_imu_set_axis_order_right_handed(axis0.raw(), axis1.raw()) }
    }

    pub fn set_axis_order_left_handed(&mut self, axis0: ImuAxis, axis1: ImuAxis) -> bool {
        unsafe { m5unified_sys::m5u_imu_set_axis_order_left_handed(axis0.raw(), axis1.raw()) }
    }

    pub fn set_int_pin_active_logic(&mut self, level: bool) -> bool {
        unsafe { m5unified_sys::m5u_imu_set_int_pin_active_logic(level) }
    }

    pub fn set_calibration_strength(&mut self, accel: u8, gyro: u8, mag: u8) {
        unsafe { m5unified_sys::m5u_imu_set_calibration_strength(accel, gyro, mag) }
    }

    pub fn clear_offset_data(&mut self) {
        unsafe { m5unified_sys::m5u_imu_clear_offset_data() }
    }

    pub fn set_offset_data(&mut self, index: usize, value: i32) {
        unsafe { m5unified_sys::m5u_imu_set_offset_data(index, value) }
    }

    pub fn offset_data_i32(&self, index: usize) -> i32 {
        unsafe { m5unified_sys::m5u_imu_get_offset_data_i32(index) }
    }

    pub fn raw_data(&self, index: usize) -> i16 {
        unsafe { m5unified_sys::m5u_imu_get_raw_data(index) }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ImuKind {
    None,
    Sh200q,
    Mpu6050,
    Mpu6886,
    Mpu9250,
    Bmi270,
    Unknown(i32),
}

impl ImuKind {
    pub const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::None,
            2 => Self::Sh200q,
            3 => Self::Mpu6050,
            4 => Self::Mpu6886,
            5 => Self::Mpu9250,
            6 => Self::Bmi270,
            raw => Self::Unknown(raw),
        }
    }

    pub const fn raw(self) -> i32 {
        match self {
            Self::None => 0,
            Self::Unknown(raw) => raw,
            Self::Sh200q => 2,
            Self::Mpu6050 => 3,
            Self::Mpu6886 => 4,
            Self::Mpu9250 => 5,
            Self::Bmi270 => 6,
        }
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq)]
pub struct ImuData {
    pub usec: u32,
    pub accel: Vec3,
    pub gyro: Vec3,
    pub mag: Vec3,
    pub temperature_c: Option<f32>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ImuDeviceKind {
    Ak8963,
    Bmm150,
    Bmi270,
    Mpu6886,
    Sh200q,
}

impl ImuDeviceKind {
    const fn raw(self) -> c_int {
        match self {
            Self::Ak8963 => 0,
            Self::Bmm150 => 1,
            Self::Bmi270 => 2,
            Self::Mpu6886 => 3,
            Self::Sh200q => 4,
        }
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct RawVec3 {
    pub x: i16,
    pub y: i16,
    pub z: i16,
}

impl RawVec3 {
    pub const fn new(x: i16, y: i16, z: i16) -> Self {
        Self { x, y, z }
    }

    pub fn scaled(self, resolution: f32) -> Vec3 {
        Vec3 {
            x: self.x as f32 * resolution,
            y: self.y as f32 * resolution,
            z: self.z as f32 * resolution,
        }
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct ImuRawData {
    pub sensor_mask: ImuSensorMask,
    pub accel: RawVec3,
    pub gyro: RawVec3,
    pub mag: RawVec3,
    pub temp_adc: i16,
}

impl ImuRawData {
    fn from_raw(raw: m5unified_sys::m5u_imu_raw_data_t) -> Self {
        Self {
            sensor_mask: ImuSensorMask::from_raw(raw.sensor_mask),
            accel: RawVec3::new(raw.accel_x, raw.accel_y, raw.accel_z),
            gyro: RawVec3::new(raw.gyro_x, raw.gyro_y, raw.gyro_z),
            mag: RawVec3::new(raw.mag_x, raw.mag_y, raw.mag_z),
            temp_adc: raw.temp,
        }
    }

    pub const fn has_accel(self) -> bool {
        self.sensor_mask.contains(ImuSensorMask::ACCEL)
    }

    pub const fn has_gyro(self) -> bool {
        self.sensor_mask.contains(ImuSensorMask::GYRO)
    }

    pub const fn has_mag(self) -> bool {
        self.sensor_mask.contains(ImuSensorMask::MAG)
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ImuConvertParams {
    pub accel_res: f32,
    pub gyro_res: f32,
    pub mag_res: f32,
    pub temp_res: f32,
    pub temp_offset: f32,
}

impl ImuConvertParams {
    fn from_raw(raw: m5unified_sys::m5u_imu_convert_param_t) -> Self {
        Self {
            accel_res: raw.accel_res,
            gyro_res: raw.gyro_res,
            mag_res: raw.mag_res,
            temp_res: raw.temp_res,
            temp_offset: raw.temp_offset,
        }
    }

    pub fn temperature_c(self, adc: i16) -> f32 {
        adc as f32 * self.temp_res + self.temp_offset
    }
}

impl Default for ImuConvertParams {
    fn default() -> Self {
        Self::from_raw(m5unified_sys::m5u_imu_convert_param_t::default())
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ImuDevice {
    kind: ImuDeviceKind,
}

impl ImuDevice {
    const fn new(kind: ImuDeviceKind) -> Self {
        Self { kind }
    }

    pub const fn kind(&self) -> ImuDeviceKind {
        self.kind
    }

    pub fn begin(&mut self) -> ImuSensorMask {
        let raw = unsafe { m5unified_sys::m5u_imu_device_begin(self.kind.raw()) };
        ImuSensorMask::from_raw(raw.max(0) as u8)
    }

    pub fn init(&mut self) -> ImuSensorMask {
        self.begin()
    }

    pub fn raw_data(&self) -> Option<ImuRawData> {
        let mut raw = m5unified_sys::m5u_imu_raw_data_t::default();
        unsafe { m5unified_sys::m5u_imu_device_get_raw_data(self.kind.raw(), &mut raw) }
            .then_some(ImuRawData::from_raw(raw))
    }

    pub fn convert_params(&self) -> Option<ImuConvertParams> {
        let mut raw = m5unified_sys::m5u_imu_convert_param_t::default();
        unsafe { m5unified_sys::m5u_imu_device_get_convert_param(self.kind.raw(), &mut raw) }
            .then_some(ImuConvertParams::from_raw(raw))
    }

    pub fn data(&self) -> Option<ImuData> {
        let raw = self.raw_data()?;
        let params = self.convert_params().unwrap_or_default();
        Some(ImuData {
            usec: 0,
            accel: raw.accel.scaled(params.accel_res),
            gyro: raw.gyro.scaled(params.gyro_res),
            mag: raw.mag.scaled(params.mag_res),
            temperature_c: self.temperature_adc().map(|adc| params.temperature_c(adc)),
        })
    }

    pub fn temperature_adc(&self) -> Option<i16> {
        let mut adc = 0;
        unsafe { m5unified_sys::m5u_imu_device_get_temp_adc(self.kind.raw(), &mut adc) }
            .then_some(adc)
    }

    pub fn temperature_c(&self) -> Option<f32> {
        let adc = self.temperature_adc()?;
        self.convert_params()
            .map(|params| params.temperature_c(adc))
    }

    pub fn sleep(&mut self) -> bool {
        unsafe { m5unified_sys::m5u_imu_device_sleep(self.kind.raw()) }
    }

    pub fn set_int_pin_active_logic(&mut self, level: bool) -> bool {
        unsafe { m5unified_sys::m5u_imu_device_set_int_pin_active_logic(self.kind.raw(), level) }
    }

    pub fn who_am_i(&self) -> Option<u8> {
        let raw = unsafe { m5unified_sys::m5u_imu_device_who_am_i(self.kind.raw()) };
        (raw >= 0).then_some(raw as u8)
    }
}