feat:add slave
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
use std::time::Instant;
|
||||
use crate::serial_core::error::CodecError;
|
||||
use crate::serial_core::frame::{FrameHandler, TactileAFrameMetaData, TactileARepFrame, TestFrame};
|
||||
use crate::serial_core::utils::elapsed_millis;
|
||||
use crate::serial_core::{codec::Codec, codecs::test::TestCodec, frame::{TactileAFrame, TactileAFrameStatusCode}};
|
||||
|
||||
use crate::serial_core::frame::{
|
||||
FrameHandler, TactileAFrameMetaData, TactileARepFrame, TactileAReqFrame, TestFrame,
|
||||
};
|
||||
use crate::serial_core::record::Recording;
|
||||
use crate::serial_core::record::{self, CsvExporter};
|
||||
use crate::serial_core::utils::{calc_crc8_itu, elapsed_millis, usize_to_u16_le_bytes};
|
||||
use crate::serial_core::{
|
||||
codec::Codec,
|
||||
frame::{TactileAFrame, TactileAFrameStatusCode},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use std::time::Instant;
|
||||
const FRAME_BUFFER_MIN_LENGTH: usize = 15;
|
||||
|
||||
pub struct TactileACodec {
|
||||
@@ -11,10 +18,14 @@ pub struct TactileACodec {
|
||||
frame_nb: u64,
|
||||
}
|
||||
|
||||
pub struct TactileACsvExporter;
|
||||
pub struct TactileACsvExporter {
|
||||
channels: usize,
|
||||
}
|
||||
|
||||
pub struct TactileACsvImporter {
|
||||
|
||||
channels: usize,
|
||||
data_row: usize,
|
||||
packets: Vec<TactileADataPacket>,
|
||||
}
|
||||
|
||||
pub struct TactileAHandler;
|
||||
@@ -29,32 +40,74 @@ impl From<u8> for TactileAFrameStatusCode {
|
||||
fn from(value: u8) -> Self {
|
||||
match value {
|
||||
0 => TactileAFrameStatusCode::Success,
|
||||
_ => TactileAFrameStatusCode::Failure
|
||||
_ => TactileAFrameStatusCode::Failure,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&TactileARepFrame> for TactileADataPacket {
|
||||
type Error = CodecError;
|
||||
fn try_from(value: &TactileARepFrame) -> Result<TactileADataPacket, Self::Error> {
|
||||
let data = TactileACodec::parse_data_frame(&value.payload)?;
|
||||
let dts_ms = value.dts_ms;
|
||||
Ok(TactileADataPacket {
|
||||
data: data,
|
||||
dts_ms: dts_ms,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TactileACodec {
|
||||
pub fn new() -> TactileACodec {
|
||||
pub fn new(cols: usize, rows: usize) -> TactileACodec {
|
||||
Self {
|
||||
buffer: Vec::new(),
|
||||
frame_nb: 0
|
||||
frame_nb: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_data_frame(data: &[u8]) -> Result<Vec<i32>> {
|
||||
if data.len() % 2 == 0 {
|
||||
pub fn parse_data_frame(data: &[u8]) -> Result<Vec<i32>, CodecError> {
|
||||
if data.len() % 2 != 0 {
|
||||
return Err(CodecError::InvalidLength);
|
||||
}
|
||||
|
||||
let vals: Vec<i32> = data
|
||||
.chunks_exact(2)
|
||||
.map(|chunk| u16::from_be_bytes(bytes))
|
||||
}
|
||||
.chunks_exact(2)
|
||||
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]) as i32)
|
||||
.collect::<Vec<i32>>();
|
||||
|
||||
Ok(vals)
|
||||
}
|
||||
|
||||
pub fn build_req_frame(cols: usize, rows: usize) -> anyhow::Result<TactileAFrame> {
|
||||
let header = [0x55, 0xAA];
|
||||
let payload_len: usize = 9;
|
||||
let device_addr: u8 = 0x34;
|
||||
let extend_code: u8 = 0x00;
|
||||
let func_code: u8 = 0xFB;
|
||||
let start_addr: u32 = 7168;
|
||||
let except_data_len: usize = cols * rows * 2;
|
||||
let checksum: u8 = 0;
|
||||
Ok(TactileAFrame::Req(TactileAReqFrame {
|
||||
meta: TactileAFrameMetaData {
|
||||
header,
|
||||
payload_len,
|
||||
device_addr,
|
||||
extend_code,
|
||||
func_code,
|
||||
start_addr,
|
||||
except_data_len,
|
||||
checksum,
|
||||
},
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl Codec<TactileAFrame> for TactileACodec {
|
||||
fn decode(&mut self, input: &[u8], session_started_at: std::time::Instant) -> Result<Vec<TactileAFrame>, CodecError> {
|
||||
fn decode(
|
||||
&mut self,
|
||||
input: &[u8],
|
||||
session_started_at: std::time::Instant,
|
||||
) -> Result<Vec<TactileAFrame>, CodecError> {
|
||||
self.buffer.extend_from_slice(input);
|
||||
let mut frames: Vec<TactileAFrame> = Vec::new();
|
||||
|
||||
@@ -63,9 +116,7 @@ impl Codec<TactileAFrame> for TactileACodec {
|
||||
break;
|
||||
}
|
||||
|
||||
let header_pos = self.buffer
|
||||
.windows(2)
|
||||
.position(|w| w == [0xAA, 0x55]);
|
||||
let header_pos = self.buffer.windows(2).position(|w| w == [0xAA, 0x55]);
|
||||
|
||||
let Some(pos) = header_pos else {
|
||||
self.buffer.clear();
|
||||
@@ -84,10 +135,15 @@ impl Codec<TactileAFrame> for TactileACodec {
|
||||
let device_addr = self.buffer[4];
|
||||
let extend_code = self.buffer[5];
|
||||
let func_code = self.buffer[6];
|
||||
let start_addr = u32::from_le_bytes([self.buffer[7],self.buffer[8],self.buffer[9],self.buffer[10]]);
|
||||
let except_data_len = u16::from_le_bytes([self.buffer[11], self.buffer[12]]) as usize;
|
||||
let start_addr = u32::from_le_bytes([
|
||||
self.buffer[7],
|
||||
self.buffer[8],
|
||||
self.buffer[9],
|
||||
self.buffer[10],
|
||||
]);
|
||||
let except_data_len = u16::from_le_bytes([self.buffer[11], self.buffer[12]]) as usize;
|
||||
let status = TactileAFrameStatusCode::from(self.buffer[13]);
|
||||
let payload = self.buffer[14..14+except_data_len].to_vec();
|
||||
let payload = self.buffer[14..14 + except_data_len].to_vec();
|
||||
let crc8_itu_alg = crc::Crc::<u8>::new(&crc::CRC_8_I_432_1);
|
||||
let checksum = crc8_itu_alg.checksum(&payload.as_slice());
|
||||
let frame_length = except_data_len + FRAME_BUFFER_MIN_LENGTH;
|
||||
@@ -111,7 +167,7 @@ impl Codec<TactileAFrame> for TactileACodec {
|
||||
meta,
|
||||
status,
|
||||
payload,
|
||||
dts_ms
|
||||
dts_ms,
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -119,11 +175,29 @@ impl Codec<TactileAFrame> for TactileACodec {
|
||||
}
|
||||
|
||||
Ok(frames)
|
||||
|
||||
}
|
||||
|
||||
fn encode(&self, frame: &TactileAFrame) -> Result<Vec<u8>, crate::serial_core::error::CodecError> {
|
||||
todo!()
|
||||
fn encode(
|
||||
&self,
|
||||
frame: &TactileAFrame,
|
||||
) -> Result<Vec<u8>, crate::serial_core::error::CodecError> {
|
||||
match frame {
|
||||
TactileAFrame::Req(f) => {
|
||||
let mut req_bytes: Vec<u8> = Vec::new();
|
||||
req_bytes.extend_from_slice(f.meta.header.as_slice());
|
||||
req_bytes.extend_from_slice(usize_to_u16_le_bytes(f.meta.payload_len).as_slice());
|
||||
req_bytes.push(f.meta.device_addr);
|
||||
req_bytes.push(f.meta.extend_code);
|
||||
req_bytes.push(f.meta.func_code);
|
||||
req_bytes.extend_from_slice(f.meta.start_addr.to_le_bytes().as_slice());
|
||||
let checksum = calc_crc8_itu(req_bytes.as_slice());
|
||||
req_bytes.push(checksum);
|
||||
Ok(req_bytes)
|
||||
}
|
||||
_ => {
|
||||
Err(CodecError::InvalidFrameType)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,12 +205,43 @@ impl Codec<TactileAFrame> for TactileACodec {
|
||||
impl FrameHandler<TactileAFrame, i32> for TactileAHandler {
|
||||
async fn on_frame(&mut self, frame: &TactileAFrame) -> anyhow::Result<Option<Vec<i32>>> {
|
||||
match frame {
|
||||
TactileARepFrame => {
|
||||
|
||||
}
|
||||
_ => {
|
||||
|
||||
TactileAFrame::Rep(rep) => {
|
||||
let vals = TactileACodec::parse_data_frame(&rep.payload)?;
|
||||
Ok(Some(vals))
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TactileACsvExporter {
|
||||
fn new(channels: usize) -> Self {
|
||||
TactileACsvExporter { channels }
|
||||
}
|
||||
}
|
||||
|
||||
impl CsvExporter<TactileARepFrame> for TactileACsvExporter {
|
||||
type Error = CodecError;
|
||||
fn csv_header(&self, recording: &Recording<TactileARepFrame>) -> Vec<String> {
|
||||
let mut header: Vec<String> = Vec::new();
|
||||
for i in 0..self.channels {
|
||||
header.push(format!("channel{}", i + 1));
|
||||
}
|
||||
|
||||
header.push("dts".to_string());
|
||||
header
|
||||
}
|
||||
|
||||
fn csv_row(
|
||||
&self,
|
||||
item: &record::RecordedFrame<TactileARepFrame>,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
let packet = TactileADataPacket::try_from(&item.frame)?;
|
||||
let mut row: Vec<String> = packet.data.iter().map(|x| x.to_string()).collect();
|
||||
row.push(packet.dts_ms.to_string());
|
||||
Ok(row)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use std::io::Read;
|
||||
use std::time::Instant;
|
||||
use crate::serial_core::frame::{FrameHandler};
|
||||
use crate::serial_core::utils::*;
|
||||
use crate::serial_core::{codec::Codec, error::CodecError, frame::TestFrame};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
@@ -9,7 +8,10 @@ use chrono::Local;
|
||||
use csv::StringRecord;
|
||||
use crate::serial_core::record::{write_csv, CsvExporter, CsvImporter, RecordedFrame, Recording};
|
||||
use crc::{Crc, CRC_8_SMBUS};
|
||||
use crate::serial_core::utils::*;
|
||||
use crate::serial_core::utils::{
|
||||
elapsed_millis,
|
||||
usize_to_u16_be_bytes
|
||||
};
|
||||
pub struct TestCodec {
|
||||
buffer: Vec<u8>,
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ pub enum CodecError {
|
||||
InvalidHeader,
|
||||
InvalidTail,
|
||||
InvalidLength,
|
||||
InvalidFrameType,
|
||||
PayloadTooLarge,
|
||||
}
|
||||
|
||||
@@ -44,6 +45,7 @@ impl fmt::Display for CodecError {
|
||||
CodecError::InvalidHeader => write!(f, "Invalid Header"),
|
||||
CodecError::InvalidTail => write!(f, "Invalid Tail"),
|
||||
CodecError::InvalidLength => write!(f, "Invalid Length"),
|
||||
CodecError::InvalidFrameType => write!(f, "Invalid Frame Type"),
|
||||
CodecError::PayloadTooLarge => write!(f, "Payload too large"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,197 @@
|
||||
use crate::serial_core::codec::Codec;
|
||||
use crate::serial_core::frame::{FrameHandler, TestFrame};
|
||||
use crate::serial_core::model::HudChartState;
|
||||
use crate::serial_core::codecs::tactile_a::TactileACodec;
|
||||
use crate::serial_core::frame::{FrameHandler, TactileAFrame, TestFrame};
|
||||
use crate::serial_core::model::{HudChartState, HudPacket};
|
||||
use crate::serial_core::record::Recording;
|
||||
use anyhow::Result;
|
||||
use tauri::{AppHandle, Emitter};
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::time::{self, Duration, MissedTickBehavior};
|
||||
use tokio_serial::SerialStream;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use std::future::pending;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
use log::info;
|
||||
use crate::serial_core::record::{FrameTiming, RecordedFrame};
|
||||
use crate::serial_core::TestRecording;
|
||||
|
||||
pub async fn run_serial<C, H, T>(
|
||||
pub enum PollMode<F> {
|
||||
Disable,
|
||||
Enabled(Box<dyn PollRequester<F>>)
|
||||
}
|
||||
|
||||
pub trait SerialFrame: Clone + Send + 'static {
|
||||
fn dts_ms(&self) -> u64;
|
||||
|
||||
fn to_hud_packet(
|
||||
&self,
|
||||
chart_state: &mut HudChartState,
|
||||
display_values: Option<&[i32]>,
|
||||
) -> Option<HudPacket>;
|
||||
}
|
||||
|
||||
impl SerialFrame for TestFrame {
|
||||
fn dts_ms(&self) -> u64 {
|
||||
self.dts_ms
|
||||
}
|
||||
|
||||
fn to_hud_packet(
|
||||
&self,
|
||||
chart_state: &mut HudChartState,
|
||||
display_values: Option<&[i32]>,
|
||||
) -> Option<HudPacket> {
|
||||
Some(chart_state.apply_frame(self, display_values))
|
||||
}
|
||||
}
|
||||
|
||||
impl SerialFrame for TactileAFrame {
|
||||
fn dts_ms(&self) -> u64 {
|
||||
match self {
|
||||
TactileAFrame::Req(_) => 0,
|
||||
TactileAFrame::Rep(rep) => rep.dts_ms,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_hud_packet(
|
||||
&self,
|
||||
_chart_state: &mut HudChartState,
|
||||
_display_values: Option<&[i32]>,
|
||||
) -> Option<HudPacket> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PollRequester<F>: Send {
|
||||
fn poll_interval(&self) -> Option<Duration> {
|
||||
None
|
||||
}
|
||||
|
||||
fn should_request(&mut self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn next_request(&mut self) -> Result<Option<F>> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn on_rx_frame(&mut self, _frame: &F) {}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct NoopPollRequester;
|
||||
|
||||
impl<F> PollRequester<F> for NoopPollRequester {}
|
||||
|
||||
pub struct TactileAPollRequester {
|
||||
period: Duration,
|
||||
cols: usize,
|
||||
rows: usize,
|
||||
awaiting_reply: bool,
|
||||
last_request_at: Option<Instant>,
|
||||
reply_timeout: Duration,
|
||||
}
|
||||
|
||||
impl TactileAPollRequester {
|
||||
pub fn new(period: Duration, cols: usize, rows: usize, reply_timeout: Duration) -> Self {
|
||||
Self {
|
||||
period,
|
||||
cols,
|
||||
rows,
|
||||
awaiting_reply: false,
|
||||
last_request_at: None,
|
||||
reply_timeout,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PollRequester<TactileAFrame> for TactileAPollRequester {
|
||||
fn poll_interval(&self) -> Option<Duration> {
|
||||
Some(self.period)
|
||||
}
|
||||
|
||||
fn should_request(&mut self) -> bool {
|
||||
if !self.awaiting_reply {
|
||||
return true;
|
||||
}
|
||||
let timed_out = self
|
||||
.last_request_at
|
||||
.map(|t| t.elapsed() >= self.reply_timeout)
|
||||
.unwrap_or(false);
|
||||
|
||||
if timed_out {
|
||||
self.awaiting_reply = false;
|
||||
self.last_request_at = None;
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn next_request(&mut self) -> Result<Option<TactileAFrame>> {
|
||||
let req = TactileACodec::build_req_frame(self.cols, self.rows)?;
|
||||
self.awaiting_reply = true;
|
||||
self.last_request_at = Some(Instant::now());
|
||||
Ok(Some(req))
|
||||
}
|
||||
|
||||
fn on_rx_frame(&mut self, frame: &TactileAFrame) {
|
||||
if matches!(frame, TactileAFrame::Rep(_)) {
|
||||
self.awaiting_reply = false;
|
||||
self.last_request_at = None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_serial<C, H, T, F>(
|
||||
app: AppHandle,
|
||||
mut port: SerialStream,
|
||||
mut codec: C,
|
||||
mut handler: H,
|
||||
session_started_at: Instant,
|
||||
recording: Arc<Mutex<TestRecording>>,
|
||||
recording: Arc<Mutex<Recording<F>>>,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<()>
|
||||
where
|
||||
C: Codec<TestFrame> + Send + 'static,
|
||||
H: FrameHandler<TestFrame, T> + Send + 'static,
|
||||
F: SerialFrame,
|
||||
C: Codec<F> + Send + 'static,
|
||||
H: FrameHandler<F, T> + Send + 'static,
|
||||
T: Into<i32>
|
||||
{
|
||||
run_serial_with_poll(
|
||||
app, port, codec, handler, session_started_at, recording, cancel, PollMode::Disable
|
||||
).await
|
||||
}
|
||||
|
||||
pub async fn run_serial_with_poll<C, H, T, F>(
|
||||
app: AppHandle,
|
||||
mut port: SerialStream,
|
||||
mut codec: C,
|
||||
mut handler: H,
|
||||
session_started_at: Instant,
|
||||
recording: Arc<Mutex<Recording<F>>>,
|
||||
cancel: CancellationToken,
|
||||
poll_mode: PollMode<F>
|
||||
) -> Result<()>
|
||||
where
|
||||
F: SerialFrame,
|
||||
C: Codec<F> + Send + 'static,
|
||||
H: FrameHandler<F, T> + Send + 'static,
|
||||
T: Into<i32>,
|
||||
{
|
||||
let mut requester = match poll_mode {
|
||||
PollMode::Disable => None,
|
||||
PollMode::Enabled(r) => Some(r),
|
||||
};
|
||||
|
||||
let mut poll_interval = requester
|
||||
.as_ref()
|
||||
.and_then(|r| r.poll_interval())
|
||||
.map(|d| {
|
||||
let mut it = time::interval(d);
|
||||
it.set_missed_tick_behavior(MissedTickBehavior::Skip);
|
||||
it
|
||||
});
|
||||
|
||||
let mut chart_state = HudChartState::new();
|
||||
let mut buffer = [0u8; 1024];
|
||||
let mut prune_interval = time::interval(Duration::from_millis(450));
|
||||
@@ -35,6 +200,23 @@ where
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = cancel.cancelled() => break,
|
||||
_ = async {
|
||||
match poll_interval.as_mut() {
|
||||
Some(it) => {
|
||||
it.tick().await;
|
||||
}
|
||||
None => pending::<()>().await,
|
||||
}
|
||||
} => {
|
||||
if let Some(r) = requester.as_mut() {
|
||||
if r.should_request() {
|
||||
if let Some(req) = r.next_request()? {
|
||||
let bytes = codec.encode(&req)?;
|
||||
port.write_all(&bytes).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = prune_interval.tick() => {
|
||||
if let Some(packet) = chart_state.prune_stale() {
|
||||
app.emit("hud_stream", packet)?;
|
||||
@@ -48,6 +230,10 @@ where
|
||||
|
||||
let frames = codec.decode(&buffer[..n], session_started_at)?;
|
||||
for frame in frames {
|
||||
if let Some(r) = requester.as_mut() {
|
||||
r.on_rx_frame(&frame);
|
||||
}
|
||||
|
||||
let decode_res = handler
|
||||
.on_frame(&frame)
|
||||
.await?
|
||||
@@ -55,7 +241,7 @@ where
|
||||
|
||||
let mut record = recording.lock().map_err(|_| anyhow::anyhow!("recording state poisoned"))?;
|
||||
record.push(RecordedFrame{
|
||||
timing: FrameTiming { pts_ms: None, dts_ms: frame.dts_ms },
|
||||
timing: FrameTiming { pts_ms: None, dts_ms: frame.dts_ms() },
|
||||
frame: frame.clone(),
|
||||
});
|
||||
|
||||
@@ -69,12 +255,12 @@ where
|
||||
None
|
||||
};
|
||||
|
||||
let packet = chart_state.apply_frame(&frame, display_values.as_deref());
|
||||
app.emit("hud_stream", packet)?;
|
||||
if let Some(packet) = frame.to_hud_packet(&mut chart_state, display_values.as_deref()) {
|
||||
app.emit("hud_stream", packet)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use anyhow::Ok;
|
||||
use crc::{Crc, CRC_8_I_432_1, CRC_8_SMBUS};
|
||||
use std::time::Instant;
|
||||
|
||||
pub fn usize_to_u16_be_bytes(n: usize) -> [u8; 2] {
|
||||
@@ -16,23 +18,43 @@ pub fn u16_to_hex_le_bytes(n: u16) -> [u8; 2] {
|
||||
(n as u16).to_le_bytes()
|
||||
}
|
||||
|
||||
pub fn crc8(data: &[u8]) -> u8 {
|
||||
let mut crc: u8 = 0x00;
|
||||
pub fn calc_crc8_smbus(c: &[u8]) -> u8 {
|
||||
let crc8_smbus = crc::Crc::<u8>::new(&crc::CRC_8_SMBUS);
|
||||
let checksum = crc8_smbus.checksum(c);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
for &byte in data {
|
||||
crc ^= byte;
|
||||
for _ in 0..8 {
|
||||
if (crc & 0x80) != 0 {
|
||||
crc = (crc << 1) ^ 0x07;
|
||||
} else {
|
||||
crc <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crc
|
||||
pub fn calc_crc8_itu(c: &[u8]) -> u8 {
|
||||
let crc8_itu_alg = crc::Crc::<u8>::new(&crc::CRC_8_I_432_1);
|
||||
let checksum = crc8_itu_alg.checksum(c);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
pub fn elapsed_millis(start_at: Instant) -> u64 {
|
||||
start_at.elapsed().as_millis() as u64
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use anyhow::Ok;
|
||||
|
||||
use crate::serial_core::utils::{calc_crc8_itu, calc_crc8_smbus};
|
||||
|
||||
#[test]
|
||||
fn test_crc8_itu() -> anyhow::Result<()> {
|
||||
let req_vec = vec![0x55, 0xAA, 0x09, 0x00, 0x34, 0x00, 0xFB, 0x00, 0x1C, 0x00, 0x00, 0x18, 0x00];
|
||||
let checksum = calc_crc8_itu(req_vec.as_slice());
|
||||
assert_eq!(checksum, 0x7A);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_crc8_smbus() -> anyhow::Result<()> {
|
||||
let req_vec = vec![0x55, 0xAA, 0x09, 0x00, 0x34, 0x00, 0xFB, 0x00, 0x1C, 0x00, 0x00, 0x18, 0x00];
|
||||
let checksum = calc_crc8_smbus(req_vec.as_slice());
|
||||
assert_eq!(checksum, 0x2F);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user