#sensor #serialization #data #mems #protocols #cobs #format

no-std serial-sensors-proto

A simple wire format for transmitting MEMS sensor data and friends

5 releases (3 breaking)

0.4.0 Jul 6, 2024
0.3.0 Jul 5, 2024
0.2.3 Jul 4, 2024
0.1.0 Jul 3, 2024

#491 in Embedded development

Download history 51/week @ 2024-07-25 50/week @ 2024-08-01 23/week @ 2024-08-08 37/week @ 2024-08-15 23/week @ 2024-08-22 8/week @ 2024-08-29 11/week @ 2024-09-05 26/week @ 2024-09-12 7/week @ 2024-09-19 26/week @ 2024-09-26 16/week @ 2024-10-03 1/week @ 2024-10-10 1/week @ 2024-10-24 4/week @ 2024-10-31 47/week @ 2024-11-07

52 downloads per month

EUPL-1.2

60KB
1.5K SLoC

serial-sensors-proto

A simple wire format for transmitting MEMS sensor data and friends.

Crates.io Docs Build Status Safety Dance codecov MSRV EUPL 1.2 licensed

The approach is twofold:

  • The protocol is a little bit extensible in sensor and data types and supports 1-, 3- and 4-dimensional readings.
  • Data packets are serialized using bincode first, then byte-stuffed using corncobs (i.e. using Consistent Overhead Byte Stuffing, COBS).

On the receiving end, the entire process runs in reverse.

Apart from supporting some generic sensor types such as accelerometers, magnetometers, gyroscopes etc., the format has some capabilities for self-description. If implemented, the sensor maker and name, as well as linear normalization factors can be sent over the wire once or periodically, allowing for automatic and sensor-agnostic conversion on the host.


See stm32f3disco-rust and serial-sensors for an example. Here's an example screenshot from that project, showing sensor data as it streams in:

An example output from serial-sensors TUI

The device-side code can be as simple as this:

fn example() {
    let value = AccelerometerI16::new(Vector3Data { x: 1, y: -2, z: 3 });
    let frame = Version1DataFrame::new(u32::MAX, 12, 0, value);

    // Serialize into a transmit buffer.
    let mut buffer = [0_u8; 48];
    let buffer = serialize(frame, &mut buffer).unwrap();
    assert_eq!(buffer.len(), 21);

    // ... send the buffer over the wire ...

    // Deserialization the received buffer.
    let data = deserialize(buffer).unwrap();
    assert_eq!(data.version, Version1);
    assert_eq!(data.data.global_sequence, u32::MAX);
    assert_eq!(data.data.sensor_sequence, 12);
    assert_eq!(data.data.sensor_tag, 0);

    let data: AccelerometerI16 = data.try_into().unwrap();
    assert_eq!(data.x, 1);
    assert_eq!(data.y, -2);
    assert_eq!(data.z, 3);
}

Dependencies

~1–1.7MB
~37K SLoC