mirror of
https://codeberg.org/icewind/hlk_ld6002.git
synced 2026-06-03 16:44:10 +02:00
atomic data
This commit is contained in:
parent
6f80d51daf
commit
9b539d3a17
1 changed files with 40 additions and 3 deletions
43
src/lib.rs
43
src/lib.rs
|
|
@ -1,6 +1,7 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
use bytemuck::{cast, cast_slice};
|
use bytemuck::{cast, cast_slice};
|
||||||
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
use embedded_io::{Error, Read, ReadExactError};
|
use embedded_io::{Error, Read, ReadExactError};
|
||||||
use embedded_io_async::Read as AsyncRead;
|
use embedded_io_async::Read as AsyncRead;
|
||||||
use num_enum::TryFromPrimitive;
|
use num_enum::TryFromPrimitive;
|
||||||
|
|
@ -313,9 +314,9 @@ impl<R: AsyncRead> AsyncMessageStream<R> {
|
||||||
|
|
||||||
#[derive(Default, Debug, Copy, Clone)]
|
#[derive(Default, Debug, Copy, Clone)]
|
||||||
pub struct Data {
|
pub struct Data {
|
||||||
respiratory: f32,
|
pub respiratory: f32,
|
||||||
distance: f32,
|
pub distance: f32,
|
||||||
heartbeat: f32,
|
pub heartbeat: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Data {
|
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 {
|
fn checksum(data: &[u8]) -> u8 {
|
||||||
let mut result = 0;
|
let mut result = 0;
|
||||||
for byte in data {
|
for byte in data {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue