atomic data

This commit is contained in:
Robin Appelman 2024-02-18 18:47:15 +01:00
commit 9b539d3a17

View file

@ -1,6 +1,7 @@
#![no_std]
use bytemuck::{cast, cast_slice};
use core::sync::atomic::{AtomicU32, Ordering};
use embedded_io::{Error, Read, ReadExactError};
use embedded_io_async::Read as AsyncRead;
use num_enum::TryFromPrimitive;
@ -313,9 +314,9 @@ impl<R: AsyncRead> AsyncMessageStream<R> {
#[derive(Default, Debug, Copy, Clone)]
pub struct Data {
respiratory: f32,
distance: f32,
heartbeat: f32,
pub respiratory: f32,
pub distance: f32,
pub heartbeat: f32,
}
impl Data {
@ -335,6 +336,42 @@ impl Data {
}
}
#[derive(Default, Debug)]
pub struct AtomicData {
respiratory: AtomicU32,
distance: AtomicU32,
heartbeat: AtomicU32,
}
impl AtomicData {
pub fn update(&self, message: MessageBody) {
match message {
MessageBody::Respiratory(rate) => {
self.respiratory.store(rate.to_bits(), Ordering::SeqCst);
}
MessageBody::Distance(Some(distance)) => {
self.distance.store(distance.to_bits(), Ordering::SeqCst);
}
MessageBody::Heartbeat(rate) => {
self.heartbeat.store(rate.to_bits(), Ordering::SeqCst);
}
_ => {}
}
}
pub fn respiratory(&self) -> f32 {
f32::from_bits(self.respiratory.load(Ordering::Relaxed))
}
pub fn distance(&self) -> f32 {
f32::from_bits(self.distance.load(Ordering::Relaxed))
}
pub fn heartbeat(&self) -> f32 {
f32::from_bits(self.heartbeat.load(Ordering::Relaxed))
}
}
fn checksum(data: &[u8]) -> u8 {
let mut result = 0;
for byte in data {