Crates.io | serial-sensors-proto |
lib.rs | serial-sensors-proto |
version | 0.4.0 |
source | src |
created_at | 2024-07-03 00:54:59.071273 |
updated_at | 2024-07-06 16:27:57.929161 |
description | A simple wire format for transmitting MEMS sensor data and friends |
homepage | https://github.com/sunsided/serial-sensors-proto |
repository | https://github.com/sunsided/serial-sensors-proto |
max_upload_size | |
id | 1290060 |
size | 58,322 |
A simple wire format for transmitting MEMS sensor data and friends.
The approach is twofold:
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:
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);
}