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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Display driver

use crate::{command::*, consts::*};
use display_interface::{DisplayError, WriteOnlyDataCommand};

/// ST7565S display driver
///
///  Works in two modes:
///  - Direct Write Mode (by default): This mode allows you to write directly to the display memory by calling the [`draw`] method.
///  - Buffered Mode: This mode allows you to modify an internal buffer by using methods like [`set_pixel`], [`clear`], or by using the [`embedded-graphics`] crate. Once you have made your changes, you can call the [`flush`] method to write the buffer to the display.
///
/// [`embedded-graphics`]: https://docs.rs/embedded-graphics
/// [`set_pixel`]: crate::display::ST7567S#method.set_pixel
/// [`clear`]: crate::display::ST7567S#method.clear
/// [`flush`]: crate::display::ST7567S#method.flush
/// [`draw`]: crate::display::ST7567S#method.draw
///
pub struct ST7567S<DI, MODE> {
    pub(crate) mode: MODE,
    pub(crate) display_interface: DI,
}

/// Basic mode allowing only direct write to the screen controller memory
pub struct DirectWriteMode;

/// Buffered mode allowing to collect changes and then flush them
pub struct BufferedMode {
    buffer: [u8; BUFFER_SIZE],
}

impl BufferedMode {
    pub(crate) fn new() -> Self {
        BufferedMode {
            buffer: [0; BUFFER_SIZE],
        }
    }
}

impl<DI: WriteOnlyDataCommand> ST7567S<DI, DirectWriteMode> {
    /// Create new instance of ST7565S driver in DirectWriteMode
    ///
    /// # Arguments
    /// * `display_interface` - The interface abstraction from `display_interface` crate
    pub fn new(display_interface: DI) -> Self {
        ST7567S {
            mode: DirectWriteMode,
            display_interface,
        }
    }

    /// Move driver to buffered mode
    pub fn into_buffered_graphics_mode(self) -> ST7567S<DI, BufferedMode> {
        ST7567S {
            mode: BufferedMode::new(),
            display_interface: self.display_interface,
        }
    }
}

impl<DI: WriteOnlyDataCommand> ST7567S<DI, BufferedMode> {
    /// Clear internal buffer
    pub fn clear(&mut self) {
        self.mode.buffer = [0; BUFFER_SIZE];
    }

    /// Set pixel in internal buffer
    ///
    /// Pixel coordinates starts from top left corner and goes to bottom right corner
    pub fn set_pixel(&mut self, x: u8, y: u8, value: bool) -> Result<(), DisplayError> {
        if x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT {
            return Err(DisplayError::OutOfBoundsError);
        }

        let column: usize = x as usize;
        let page: usize = (y / 8) as usize;
        let page_bit = y % 8;

        let byte_idx = page * (DISPLAY_WIDTH as usize) + column;
        let byte = self.mode.buffer[byte_idx];
        let bit_value: u8 = value.into();
        let byte = byte & !(1 << page_bit) | (bit_value << page_bit);

        self.mode.buffer[byte_idx] = byte;

        Ok(())
    }

    /// Send internal buffer to the display
    pub fn flush(&mut self) -> Result<(), DisplayError> {
        Self::flush_buffer_chunks(
            &mut self.display_interface,
            self.mode.buffer.as_slice(),
            (0, 0),
            (DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1),
        )
    }
}

impl<DI: WriteOnlyDataCommand, MODE> ST7567S<DI, MODE> {
    /// Send init commands to the display and turn it on
    pub fn init(&mut self) -> Result<(), DisplayError> {
        SetBiasCommand::Bias1_9.write(&mut self.display_interface)?;
        SetSEGDirectionCommand::Normal.write(&mut self.display_interface)?;
        SetCOMDirectionCommand::Reverse.write(&mut self.display_interface)?;
        SetRegulationResistorRatioCommand::Ratio5_0.write(&mut self.display_interface)?;
        SetElectronicVolumeCommand::new(40)
            .unwrap()
            .write(&mut self.display_interface)?;
        SetPowerControlCommand::BoosterOn.write(&mut self.display_interface)?;
        SetPowerControlCommand::VoltageRegulatorOn.write(&mut self.display_interface)?;
        SetPowerControlCommand::VoltageFollowerOn.write(&mut self.display_interface)?;
        SetStartLineCommand::new(0)
            .unwrap()
            .write(&mut self.display_interface)?;

        self.draw([0; BUFFER_SIZE].as_slice())?;

        DisplayOnCommand::On.write(&mut self.display_interface)?;

        Ok(())
    }

    /// Reset some display parameters to default values: Start Line, Column Address, Page Address and COM Direction.
    /// Usually doesn't need to be called
    pub fn reset(&mut self) -> Result<(), DisplayError> {
        ResetCommand.write(&mut self.display_interface)
    }

    /// Send buffer to the display
    ///
    /// Buffer represents by 8 pages of 128 columns where 1 byte represents 8 vertical pixels
    pub fn draw(&mut self, buffer: &[u8]) -> Result<(), DisplayError> {
        if buffer.len() != BUFFER_SIZE {
            return Err(DisplayError::OutOfBoundsError);
        }

        Self::flush_buffer_chunks(
            &mut self.display_interface,
            buffer,
            (0, 0),
            (DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1),
        )
    }

    /// Send part of the buffer to the display.
    /// Buffer represents by 8 pages of 128 columns where 1 byte represents 8 vertical pixels
    ///
    /// # Arguments
    /// * `buffer` - the entire buffer from which the required part will be sent
    /// * `top_left` and `bottom_right` are coordinates of the top left and bottom right corners of the area to be drawn
    pub fn bounded_draw(
        &mut self,
        buffer: &[u8],
        top_left: (u8, u8),
        bottom_right: (u8, u8),
    ) -> Result<(), DisplayError> {
        Self::flush_buffer_chunks(&mut self.display_interface, buffer, top_left, bottom_right)
    }

    pub(crate) fn flush_buffer_chunks(
        display_interface: &mut DI,
        buffer: &[u8],
        top_left: (u8, u8),
        bottom_right: (u8, u8),
    ) -> Result<(), DisplayError> {
        if top_left.0 >= DISPLAY_WIDTH || top_left.1 >= DISPLAY_HEIGHT {
            return Err(DisplayError::OutOfBoundsError);
        }
        if bottom_right.0 >= DISPLAY_WIDTH || bottom_right.1 >= DISPLAY_HEIGHT {
            return Err(DisplayError::OutOfBoundsError);
        }
        if top_left.0 > bottom_right.0 || top_left.1 > bottom_right.1 {
            return Err(DisplayError::OutOfBoundsError);
        }

        let first_page: usize = (top_left.1 / 8) as usize;
        let first_column: usize = top_left.0 as usize;

        let last_page: usize = (bottom_right.1 / 8) as usize;
        let last_column: usize = bottom_right.0 as usize;

        buffer
            .chunks(DISPLAY_WIDTH as usize)
            .skip(first_page)
            .take(last_page - first_page + 1)
            .map(|page| &page[first_column..=last_column])
            .enumerate()
            .try_for_each(|(page_idx, page)| {
                SetPageAddressCommand::new(first_page as u8 + page_idx as u8)
                    .unwrap()
                    .write(display_interface)?;
                SetColumnAddressLSNibbleCommand::new(first_column as u8)
                    .unwrap()
                    .write(display_interface)?;
                SetColumnAddressMSNibbleCommand::new(first_column as u8)
                    .unwrap()
                    .write(display_interface)?;

                display_interface.send_data(display_interface::DataFormat::U8(page))
            })
    }
}