embedded_counters/
display_interface.rs

1use std::fmt::Display;
2use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand};
3use serde::Serialize;
4
5use super::Counter;
6
7#[derive(Debug, Default, Serialize)]
8pub struct DIC {
9    pub cmd: Counter,
10    pub data: Counter,
11}
12
13impl WriteOnlyDataCommand for DIC {
14    fn send_commands(&mut self, cmd: DataFormat<'_>) -> Result<(), DisplayError> {
15        self.cmd.iterations += 1;
16        self.cmd.bytes += data_format_bytes(cmd);
17
18        Ok(())
19    }
20
21    fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError> {
22        self.data.iterations += 1;
23        self.data.bytes += data_format_bytes(buf);
24
25        Ok(())
26    }
27}
28
29impl Display for DIC {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(f, "CMD[{}] DATA[{}]", self.cmd, self.data)
32    }
33}
34
35fn data_format_bytes(input: DataFormat<'_>) -> usize {
36    match input {
37        DataFormat::U8(buf) => buf.len(),
38        DataFormat::U16(buf) => buf.len() * 2,
39        DataFormat::U16BE(buf) => buf.len() * 2,
40        DataFormat::U16LE(buf) => buf.len() * 2,
41        DataFormat::U8Iter(iter) => iter.count(),
42        DataFormat::U16BEIter(iter) => iter.count() * 2,
43        DataFormat::U16LEIter(iter) => iter.count() * 2,
44        _ => panic!("Unable to determine data format"),
45    }
46}