Cast variable size data from SPI to array of structs

⚓ Rust    📅 2026-06-10    👤 surdeus    👁️ 2      

surdeus

I am trying to write an embedded-hal-async SPI driver.(Pixy2 camera)

There should be a function approximately:

pub async get_blocks(&mut self) -> Result<&[Block]>

The amount of blocks(length of data) is known only when the packet header is read.

Block is a struct.

pub struct Block {
    pub signature: u16,
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
    pub angle: i16,
    pub index: u8,
    pub age: u8,
}

How do I efficienctly(no alloc) and idiomatically implement the reading of the data from SPI and getting this array from the data?

Btw I am referencing this C++ code:

I tried something very broken with bytemuck, zerocopy and hacky and it does not work, better not including that here.

Should I put a buffer into the Pixy2 driver sturct field?

How such things are usually solved?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed