rudelblinken_sdk/
lib.rs

1//! # Rudelblinken SDK
2//!
3//! This is the SDK for the Rudelblinken platform. It provides a set of functions to interact with the connected hardware.
4#![feature(split_array)]
5
6mod rudel;
7pub use rudel::{
8    export, exports,
9    exports::rudel::base::ble_guest::{Advertisement, Guest as BleGuest},
10    exports::rudel::base::run::Guest,
11    rudel::base::base::{get_base_version, log, sleep, time, yield_now, LogLevel, SemanticVersion},
12    rudel::base::ble::{
13        configure_advertisement, get_ble_version, set_advertisement_data, AdvertisementData,
14        AdvertisementSettings,
15    },
16    rudel::base::hardware::{
17        get_ambient_light, get_ambient_light_type, get_hardware_version, get_led_info,
18        get_vibration, get_vibration_sensor_type, led_count, set_leds, set_rgb, AmbientLightType,
19        LedColor, LedInfo, VibrationSensorType,
20    },
21};
22
23pub fn get_name() -> String {
24    let tuple = rudel::rudel::base::base::get_name();
25    let array: [u8; 16] = [
26        tuple.0, tuple.1, tuple.2, tuple.3, tuple.4, tuple.5, tuple.6, tuple.7, tuple.8, tuple.9,
27        tuple.10, tuple.11, tuple.12, tuple.13, tuple.14, tuple.15,
28    ];
29    let length = array
30        .iter()
31        .enumerate()
32        .find(|(_, x)| **x == 0)
33        .map(|(index, _)| index)
34        .unwrap_or(0);
35    let array = &array[0..length];
36    String::from_utf8_lossy(array).to_string()
37}
38
39impl exports::rudel::base::ble_guest::Advertisement {
40    /// Get the manufacturer data as a byte array.
41    ///
42    /// Only the first self.data_length bytes are valid. You should probably use `get_data` instead.
43    pub unsafe fn get_data_array(&self) -> &[u8; 32] {
44        // SAFETY: Does the same as the safe function below, but without copying
45        return unsafe { std::mem::transmute::<_, &[u8; 32]>(&self.data) };
46    }
47    /// Get the manufacturer data as a byte array.
48    ///
49    /// Only the first self.data_length bytes are valid. You should probably use `get_data_mut` instead.
50    pub unsafe fn get_data_array_mut(&mut self) -> &mut [u8; 32] {
51        // SAFETY: Does the same as the safe function below, but without copying
52        return unsafe { std::mem::transmute::<_, &mut [u8; 32]>(&mut self.data) };
53    }
54    // // The same as the above
55    // pub fn get_data_array_safe(&self) -> [u8; 32] {
56    //     let mut array = [0u8; 32];
57    //     array[0..4].copy_from_slice(&self.data.0.to_le_bytes());
58    //     array[4..8].copy_from_slice(&self.data.1.to_le_bytes());
59    //     array[8..12].copy_from_slice(&self.data.2.to_le_bytes());
60    //     array[12..16].copy_from_slice(&self.data.3.to_le_bytes());
61    //     array[16..20].copy_from_slice(&self.data.4.to_le_bytes());
62    //     array[20..24].copy_from_slice(&self.data.5.to_le_bytes());
63    //     array[24..28].copy_from_slice(&self.data.6.to_le_bytes());
64    //     array[28..32].copy_from_slice(&self.data.7.to_le_bytes());
65    //     return array;
66    // }
67    /// Get the manufacturer data as a slice
68    pub fn get_data(&self) -> &[u8] {
69        let length = std::cmp::min(self.data_length as usize, 32);
70        let array = unsafe { self.get_data_array() };
71        return &array[..length];
72    }
73    /// Get the manufacturer data as a slice
74    pub fn get_data_mut(&mut self) -> &mut [u8] {
75        let length = std::cmp::min(self.data_length as usize, 32);
76        let array = unsafe { self.get_data_array_mut() };
77        return &mut array[..length];
78    }
79    /// Get the sender address
80    pub fn get_address(&self) -> &[u8; 6] {
81        let (start, _) =
82            unsafe { std::mem::transmute::<&u64, &[u8; 8]>(&self.address) }.split_array_ref::<6>();
83        return start;
84    }
85    /// Get the sender address
86    pub fn get_address_mut(&mut self) -> &mut [u8; 6] {
87        let (start, _) =
88            unsafe { std::mem::transmute::<&mut u64, &mut [u8; 8]>(&mut self.address) }
89                .split_array_mut::<6>();
90        return start;
91    }
92}