1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::fmt::Display;
use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand};
use serde::Serialize;

use super::Counter;

#[derive(Debug, Default, Serialize)]
pub struct DIC {
    pub cmd: Counter,
    pub data: Counter,
}

impl WriteOnlyDataCommand for DIC {
    fn send_commands(&mut self, cmd: DataFormat<'_>) -> Result<(), DisplayError> {
        self.cmd.iterations += 1;
        self.cmd.bytes += data_format_bytes(cmd);

        Ok(())
    }

    fn send_data(&mut self, buf: DataFormat<'_>) -> Result<(), DisplayError> {
        self.data.iterations += 1;
        self.data.bytes += data_format_bytes(buf);

        Ok(())
    }
}

impl Display for DIC {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CMD[{}] DATA[{}]", self.cmd, self.data)
    }
}

fn data_format_bytes(input: DataFormat<'_>) -> usize {
    match input {
        DataFormat::U8(buf) => buf.len(),
        DataFormat::U16(buf) => buf.len() * 2,
        DataFormat::U16BE(buf) => buf.len() * 2,
        DataFormat::U16LE(buf) => buf.len() * 2,
        DataFormat::U8Iter(iter) => iter.count(),
        DataFormat::U16BEIter(iter) => iter.count() * 2,
        DataFormat::U16LEIter(iter) => iter.count() * 2,
        _ => panic!("Unable to determine data format"),
    }
}