hd44780_controller/command/
function_set.rs

1use crate::device::*;
2
3use super::*;
4
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub struct FunctionSet {
7    pub data_length: DataLength,
8    pub number_of_lines: NumberOfLines,
9    pub character_font: CharacterFont,
10}
11
12impl Default for FunctionSet {
13    fn default() -> Self {
14        Self {
15            data_length: DataLength::FourBit,
16            number_of_lines: NumberOfLines::Two,
17            character_font: CharacterFont::FiveByEight,
18        }
19    }
20}
21
22#[derive(Copy, Clone, Debug, Eq, PartialEq)]
23pub enum DataLength {
24    FourBit,
25    EightBit,
26}
27
28#[derive(Copy, Clone, Debug, Eq, PartialEq)]
29pub enum NumberOfLines {
30    One,
31    Two,
32}
33
34#[derive(Copy, Clone, Debug, Eq, PartialEq)]
35pub enum CharacterFont {
36    FiveByEight,
37    FiveByTen,
38}
39
40impl FunctionSet {
41    fn code(&self) -> u8 {
42        let mut code = 0x20;
43
44        if self.data_length == DataLength::EightBit {
45            code |= 0x1 << 4;
46        }
47        if self.number_of_lines == NumberOfLines::Two {
48            code |= 0x1 << 3;
49        }
50        if self.character_font == CharacterFont::FiveByTen {
51            code |= 0x1 << 2;
52        }
53
54        code
55    }
56}
57
58impl SyncCommand for FunctionSet {
59    type Ret = ();
60
61    type Err = super::Error;
62
63    fn execute<D: SyncDevice + ?Sized>(&self, dev: &mut D) -> Result<Self::Ret, Self::Err> {
64        dev.write_byte(RegisterSelectMode::Command, self.code())
65            .map_err(|_| super::Error::DeviceError)?;
66        dev.delay_us(50);
67
68        Ok(())
69    }
70}
71
72#[cfg(feature = "async")]
73#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
74impl AsyncCommand for FunctionSet {
75    type Ret = ();
76
77    type Err = super::Error;
78
79    async fn execute_async<D: AsyncDevice + ?Sized>(
80        &self,
81        dev: &mut D,
82    ) -> Result<Self::Ret, Self::Err> {
83        dev.write_byte_async(RegisterSelectMode::Command, self.code())
84            .await
85            .map_err(|_| super::Error::DeviceError)?;
86        dev.delay_us_async(50).await;
87
88        Ok(())
89    }
90}