Skip to main content

dmx_interface/hardware/
efuse.rs

1//! MAC address helper using ESP32-S2 eFuse.
2//!
3//! Reads the factory-programmed MAC address from eFuse
4//! and formats it for use as `WiFi` SSID suffix, etc.
5
6use core::fmt::Write;
7use heapless::String;
8
9/// Read the `WiFi` station MAC address from eFuse.
10///
11/// Returns a formatted string like `"AA:BB:CC:DD:EE:FF"`.
12pub fn mac_address() -> String<17> {
13    let bytes = mac_bytes();
14    let mut s = String::new();
15    for (i, byte) in bytes.iter().enumerate() {
16        if i > 0 {
17            s.push(':').ok();
18        }
19        push_hex_byte(&mut s, *byte);
20    }
21    s
22}
23
24/// Read the `WiFi` station MAC address as raw bytes.
25pub fn mac_bytes() -> [u8; 6] {
26    let mac = esp_hal::efuse::interface_mac_address(esp_hal::efuse::InterfaceMacAddress::Station);
27    let slice = mac.as_bytes();
28    let mut bytes = [0u8; 6];
29    bytes.copy_from_slice(slice);
30    bytes
31}
32
33/// Append a two-character uppercase hex byte to a string.
34fn push_hex_byte<const N: usize>(s: &mut String<N>, byte: u8) {
35    let _ = write!(s, "{byte:02X}");
36}
37
38/// Generate a default AP SSID from the MAC address.
39///
40/// Returns `"ChaosDMX-"` followed by the last two MAC bytes as
41/// uppercase hex digits.
42pub fn default_ap_ssid() -> String<32> {
43    let mac = mac_bytes();
44    let mut s: String<32> = String::try_from("ChaosDMX-").expect("SSID prefix fits");
45    push_hex_byte(&mut s, mac[4]);
46    push_hex_byte(&mut s, mac[5]);
47    s
48}