hd44780_controller/command/
entry_mode_set.rs

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