hd44780_controller/command/
cursor_or_display_shift.rs1use crate::device::*;
2
3use super::*;
4
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub struct CursorOrDisplayShift {
7 pub mode: Mode,
8 pub direction: Direction,
9}
10
11#[derive(Copy, Clone, Debug, Eq, PartialEq)]
12pub enum Mode {
13 Cursor,
14 Display,
15}
16
17#[derive(Copy, Clone, Debug, Eq, PartialEq)]
18pub enum Direction {
19 Left,
20 Right,
21}
22
23impl CursorOrDisplayShift {
24 fn code(&self) -> u8 {
25 let mut code = 0x10;
26
27 if self.mode == Mode::Display {
28 code |= 0x1 << 3;
29 }
30 if self.direction == Direction::Right {
31 code |= 0x1 << 2;
32 }
33
34 code
35 }
36}
37
38impl SyncCommand for CursorOrDisplayShift {
39 type Ret = ();
40
41 type Err = super::Error;
42
43 fn execute<D: SyncDevice + ?Sized>(&self, dev: &mut D) -> Result<Self::Ret, Self::Err> {
44 dev.write_byte(RegisterSelectMode::Command, self.code())
45 .map_err(|_| super::Error::DeviceError)?;
46 dev.delay_us(50);
47
48 Ok(())
49 }
50}
51
52#[cfg(feature = "async")]
53#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
54impl AsyncCommand for CursorOrDisplayShift {
55 type Ret = ();
56
57 type Err = super::Error;
58
59 async fn execute_async<D: AsyncDevice + ?Sized>(
60 &self,
61 dev: &mut D,
62 ) -> Result<Self::Ret, Self::Err> {
63 dev.write_byte_async(RegisterSelectMode::Command, self.code())
64 .await
65 .map_err(|_| super::Error::DeviceError)?;
66 dev.delay_us_async(50).await;
67
68 Ok(())
69 }
70}