Skip to main content

m5unified/
config.rs

1//! Startup configuration for [`crate::M5Unified`].
2//!
3//! These types mirror the practical subset of M5Unified's board, display,
4//! speaker, RTC, IMU, and peripheral configuration knobs used by examples.
5
6use crate::Board;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct M5UnifiedConfig {
10    pub serial_baudrate: u32,
11    pub external_speaker: ExternalSpeakerConfig,
12    pub external_display: ExternalDisplayConfig,
13    pub clear_display: bool,
14    pub output_power: bool,
15    pub pmic_button: bool,
16    pub internal_imu: bool,
17    pub internal_rtc: bool,
18    pub internal_mic: bool,
19    pub internal_spk: bool,
20    pub external_imu: bool,
21    pub external_rtc: bool,
22    pub disable_rtc_irq: bool,
23    pub led_brightness: u8,
24    pub fallback_board: Option<Board>,
25}
26
27impl M5UnifiedConfig {
28    pub(crate) fn to_raw(&self) -> m5unified_sys::m5u_config_t {
29        m5unified_sys::m5u_config_t {
30            serial_baudrate: self.serial_baudrate,
31            external_speaker_value: self.external_speaker.bits(),
32            external_display_value: self.external_display.bits(),
33            clear_display: u8::from(self.clear_display),
34            output_power: u8::from(self.output_power),
35            pmic_button: u8::from(self.pmic_button),
36            internal_imu: u8::from(self.internal_imu),
37            internal_rtc: u8::from(self.internal_rtc),
38            internal_mic: u8::from(self.internal_mic),
39            internal_spk: u8::from(self.internal_spk),
40            external_imu: u8::from(self.external_imu),
41            external_rtc: u8::from(self.external_rtc),
42            disable_rtc_irq: u8::from(self.disable_rtc_irq),
43            led_brightness: self.led_brightness,
44            fallback_board: self.fallback_board.map(Board::raw).unwrap_or(-1),
45        }
46    }
47}
48
49impl Default for M5UnifiedConfig {
50    fn default() -> Self {
51        Self {
52            serial_baudrate: 0,
53            external_speaker: ExternalSpeakerConfig::default(),
54            external_display: ExternalDisplayConfig::default(),
55            clear_display: true,
56            output_power: true,
57            pmic_button: true,
58            internal_imu: true,
59            internal_rtc: true,
60            internal_mic: true,
61            internal_spk: true,
62            external_imu: false,
63            external_rtc: false,
64            disable_rtc_irq: true,
65            led_brightness: 0,
66            fallback_board: None,
67        }
68    }
69}
70
71#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
72pub struct ExternalSpeakerConfig {
73    pub module_display: bool,
74    pub module_rca: bool,
75    pub hat_spk: bool,
76    pub atomic_spk: bool,
77    pub hat_spk2: bool,
78    pub atomic_echo: bool,
79}
80
81impl ExternalSpeakerConfig {
82    fn bits(self) -> u8 {
83        u8::from(self.module_display)
84            | (u8::from(self.module_rca) << 1)
85            | (u8::from(self.hat_spk) << 2)
86            | (u8::from(self.atomic_spk) << 3)
87            | (u8::from(self.hat_spk2) << 4)
88            | (u8::from(self.atomic_echo) << 5)
89    }
90}
91
92#[derive(Debug, Copy, Clone, PartialEq, Eq)]
93pub struct ExternalDisplayConfig {
94    pub module_display: bool,
95    pub atom_display: bool,
96    pub unit_oled: bool,
97    pub unit_mini_oled: bool,
98    pub unit_lcd: bool,
99    pub unit_glass: bool,
100    pub unit_glass2: bool,
101    pub unit_rca: bool,
102    pub module_rca: bool,
103}
104
105impl ExternalDisplayConfig {
106    fn bits(self) -> u16 {
107        u16::from(self.module_display)
108            | (u16::from(self.atom_display) << 1)
109            | (u16::from(self.unit_oled) << 2)
110            | (u16::from(self.unit_mini_oled) << 3)
111            | (u16::from(self.unit_lcd) << 4)
112            | (u16::from(self.unit_glass) << 5)
113            | (u16::from(self.unit_glass2) << 6)
114            | (u16::from(self.unit_rca) << 7)
115            | (u16::from(self.module_rca) << 8)
116            | 0xFE00
117    }
118}
119
120impl Default for ExternalDisplayConfig {
121    fn default() -> Self {
122        Self {
123            module_display: true,
124            atom_display: true,
125            unit_oled: true,
126            unit_mini_oled: true,
127            unit_lcd: true,
128            unit_glass: true,
129            unit_glass2: true,
130            unit_rca: true,
131            module_rca: true,
132        }
133    }
134}