Skip to content

Commit

Permalink
Move by_refdes into pub(crate) module
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Jan 27, 2025
1 parent 9c63d4c commit 83a8cc6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
27 changes: 1 addition & 26 deletions task/host-sp-comms/src/bsp/gimlet_bcde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! SP inventory types and implementation
//!
//! This reduces clutter in the main `ServerImpl` implementation
use super::ServerImpl;
use super::{inventory::by_refdes, ServerImpl};

use drv_i2c_api::I2cDevice;
use drv_i2c_api::ResponseCode;
Expand All @@ -21,31 +21,6 @@ use host_sp_messages::{InventoryData, InventoryDataResult};
userlib::task_slot!(I2C, i2c_driver);
userlib::task_slot!(SPI, spi_driver);

/// `const` function to convert a `&'static str` to a fixed-size byte array
///
/// This must be called a `const` parameter of `s.len()`
const fn byteify<const N: usize>(s: &'static [u8]) -> [u8; N] {
let mut out = [0u8; N];
let mut i = 0;
while i < s.len() {
out[i] = s[i];
i += 1;
}
out
}
macro_rules! by_refdes {
($refdes:ident, $dev:ident) => {
paste::paste! {{
const BYTE_ARRAY: &'static [u8] = stringify!($refdes).as_bytes();
(
byteify::<{ BYTE_ARRAY.len() }>(BYTE_ARRAY),
i2c_config::devices::[<$dev _ $refdes:lower >] as fn(TaskId) -> I2cDevice,
i2c_config::sensors::[<$dev:upper _ $refdes:upper _SENSORS>]
)
}}
};
}

impl ServerImpl {
/// Number of devices in our inventory
pub(crate) const INVENTORY_COUNT: u32 = 72;
Expand Down
31 changes: 31 additions & 0 deletions task/host-sp-comms/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,34 @@
/// Inventory API version (always 0 for now)
pub(crate) const INVENTORY_API_VERSION: u32 = 0;

/// `const` function to convert a `&'static str` to a fixed-size byte array
///
/// This must be called a `const` parameter of `s.len()`
pub(crate) const fn byteify<const N: usize>(s: &'static [u8]) -> [u8; N] {
let mut out = [0u8; N];
let mut i = 0;
while i < s.len() {
out[i] = s[i];
i += 1;
}
out
}
macro_rules! by_refdes {
// Length is found based on refdes
($refdes:ident, $dev:ident) => {
by_refdes!($refdes, $dev, stringify!($refdes).as_bytes().len())
};
// Length is provided, e.g. if it is not consistent between refdes
($refdes:ident, $dev:ident, $n:expr) => {
paste::paste! {{
const BYTE_ARRAY: &'static [u8] = stringify!($refdes).as_bytes();
(
$crate::inventory::byteify::<{ $n }>(BYTE_ARRAY),
i2c_config::devices::[<$dev _ $refdes:lower >] as fn(TaskId) -> I2cDevice,
i2c_config::sensors::[<$dev:upper _ $refdes:upper _SENSORS>]
)
}}
};
}
pub(crate) use by_refdes;

0 comments on commit 83a8cc6

Please sign in to comment.