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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//! # ST7920
//!
//! This is a Rust driver library for LCD displays using the [ST7920] controller.
//!
//! It supports graphics mode of the controller, 128x64 in 1bpp. SPI connection to MCU is supported.
//!
//! The controller supports 1 bit-per-pixel displays, so an off-screen buffer has to be used to provide random access to pixels.
//!
//! Size of the buffer is 1024 bytes.
//!
//! The buffer has to be flushed to update the display after a group of draw calls has been completed.
//! The flush is not part of embedded-graphics API.

#![no_std]
use num_derive::ToPrimitive;
use num_traits::ToPrimitive;

use embedded_hal::blocking::delay::DelayUs;
use embedded_hal::blocking::spi;
use embedded_hal::digital::v2::OutputPin;

#[derive(Debug)]
pub enum Error<CommError, PinError> {
    Comm(CommError),
    Pin(PinError),
}

/// ST7920 instructions.
#[derive(ToPrimitive)]
enum Instruction {
    BasicFunction = 0x30,
    ExtendedFunction = 0x34,
    ClearScreen = 0x01,
    EntryMode = 0x06,
    DisplayOnCursorOff = 0x0C,
    GraphicsOn = 0x36,
    SetGraphicsAddress = 0x80,
}

pub const WIDTH: u32 = 128;
pub const HEIGHT: u32 = 64;
const ROW_SIZE: usize = (WIDTH / 8) as usize;
const BUFFER_SIZE: usize = ROW_SIZE * HEIGHT as usize;
const X_ADDR_DIV: u8 = 16;

pub struct ST7920<SPI, RST, CS>
where
    SPI: spi::Write<u8>,
    RST: OutputPin,
    CS: OutputPin,
{
    /// SPI pin
    spi: SPI,

    /// Reset pin.
    rst: RST,

    /// CS pin
    cs: Option<CS>,

    buffer: [u8; BUFFER_SIZE],

    flip: bool,
}

impl<SPI, RST, CS, PinError, SPIError> ST7920<SPI, RST, CS>
where
    SPI: spi::Write<u8, Error = SPIError>,
    RST: OutputPin<Error = PinError>,
    CS: OutputPin<Error = PinError>,
{
    /// Create a new driver instance that uses SPI connection.
    pub fn new(spi: SPI, rst: RST, cs: Option<CS>, flip: bool) -> Self {
        let buffer = [0; BUFFER_SIZE];

        ST7920 {
            spi,
            rst,
            cs,
            buffer,
            flip,
        }
    }

    fn enable_cs(&mut self, delay: &mut dyn DelayUs<u32>) -> Result<(), Error<SPIError, PinError>> {
        if let Some(cs) = self.cs.as_mut() {
            cs.set_high().map_err(Error::Pin)?;
            delay.delay_us(1);
        }
        Ok(())
    }

    fn disable_cs(
        &mut self,
        delay: &mut dyn DelayUs<u32>,
    ) -> Result<(), Error<SPIError, PinError>> {
        if let Some(cs) = self.cs.as_mut() {
            delay.delay_us(1);
            cs.set_high().map_err(Error::Pin)?;
        }
        Ok(())
    }

    /// Initialize the display controller
    pub fn init(&mut self, delay: &mut dyn DelayUs<u32>) -> Result<(), Error<SPIError, PinError>> {
        self.enable_cs(delay)?;
        self.hard_reset(delay)?;
        self.write_command(Instruction::BasicFunction)?;
        delay.delay_us(200);
        self.write_command(Instruction::DisplayOnCursorOff)?;
        delay.delay_us(100);
        self.write_command(Instruction::ClearScreen)?;
        delay.delay_us(10 * 1000);
        self.write_command(Instruction::EntryMode)?;
        delay.delay_us(100);
        self.write_command(Instruction::ExtendedFunction)?;
        delay.delay_us(10 * 1000);
        self.write_command(Instruction::GraphicsOn)?;
        delay.delay_us(100 * 1000);

        self.disable_cs(delay)?;
        Ok(())
    }

    fn hard_reset(
        &mut self,
        delay: &mut dyn DelayUs<u32>,
    ) -> Result<(), Error<SPIError, PinError>> {
        self.rst.set_low().map_err(Error::Pin)?;
        delay.delay_us(40 * 1000);
        self.rst.set_high().map_err(Error::Pin)?;
        delay.delay_us(40 * 1000);
        Ok(())
    }

    fn write_command(&mut self, command: Instruction) -> Result<(), Error<SPIError, PinError>> {
        self.write_command_param(command, 0)
    }

    fn write_command_param(
        &mut self,
        command: Instruction,
        param: u8,
    ) -> Result<(), Error<SPIError, PinError>> {
        let command_param = command.to_u8().unwrap() | param;
        let cmd: u8 = 0xF8;

        self.spi
            .write(&[cmd, command_param & 0xF0, (command_param << 4) & 0xF0])
            .map_err(Error::Comm)?;

        Ok(())
    }

    fn write_data(&mut self, data: u8) -> Result<(), Error<SPIError, PinError>> {
        self.spi
            .write(&[0xFA, data & 0xF0, (data << 4) & 0xF0])
            .map_err(Error::Comm)?;
        Ok(())
    }

    fn set_address(&mut self, x: u8, y: u8) -> Result<(), Error<SPIError, PinError>> {
        const HALF_HEIGHT: u8 = HEIGHT as u8 / 2;

        self.write_command_param(
            Instruction::SetGraphicsAddress,
            if y < HALF_HEIGHT { y } else { y - HALF_HEIGHT },
        )?;
        self.write_command_param(
            Instruction::SetGraphicsAddress,
            if y < HALF_HEIGHT {
                x / X_ADDR_DIV
            } else {
                x / X_ADDR_DIV + (WIDTH as u8 / X_ADDR_DIV)
            },
        )?;

        Ok(())
    }

    /// Clear whole display area
    pub fn clear(&mut self, delay: &mut dyn DelayUs<u32>) -> Result<(), Error<SPIError, PinError>> {
        self.enable_cs(delay)?;

        for y in 0..HEIGHT as u8 / 2 {
            self.set_address(0, y)?;

            for _x in 0..ROW_SIZE {
                self.write_data(0)?;
                self.write_data(0)?;
            }
        }

        self.disable_cs(delay)?;
        Ok(())
    }

    /// Draw pixel
    pub fn set_pixel(&mut self, mut x: u8, mut y: u8, val: u8) {
        if self.flip {
            y = (HEIGHT - 1) as u8 - y;
            x = (WIDTH - 1) as u8 - x;
        }

        let x_mask = 0x80 >> (x % 8);
        if val != 0 {
            self.buffer[y as usize * ROW_SIZE + x as usize / 8] |= x_mask;
        } else {
            self.buffer[y as usize * ROW_SIZE + x as usize / 8] &= !x_mask;
        }
    }

    /// Flush buffer to update entire display
    pub fn flush(&mut self, delay: &mut dyn DelayUs<u32>) -> Result<(), Error<SPIError, PinError>> {
        self.enable_cs(delay)?;

        for y in 0..HEIGHT as u8 / 2 {
            self.set_address(0, y)?;

            let mut row_start = y as usize * ROW_SIZE;
            for x in 0..ROW_SIZE {
                self.write_data(self.buffer[row_start + x])?;
            }
            row_start += (HEIGHT as usize / 2) * ROW_SIZE;
            for x in 0..ROW_SIZE {
                self.write_data(self.buffer[row_start + x])?;
            }
        }

        self.disable_cs(delay)?;
        Ok(())
    }

    /// Flush buffer to update region of the display
    pub fn flush_region(
        &mut self,
        x: u8,
        mut y: u8,
        w: u8,
        h: u8,
        delay: &mut dyn DelayUs<u32>,
    ) -> Result<(), Error<SPIError, PinError>> {
        self.enable_cs(delay)?;

        let mut adj_x = x;
        if self.flip {
            y = HEIGHT as u8 - (y + h);
            adj_x = WIDTH as u8 - (x + w);
        }

        let left = (adj_x / X_ADDR_DIV) * X_ADDR_DIV;
        let mut right = ((adj_x + w) / X_ADDR_DIV) * X_ADDR_DIV;
        if right < adj_x + w {
            right += X_ADDR_DIV; //make sure rightmost pixels are covered
        }

        let mut row_start = y as usize * ROW_SIZE;
        for y in y..y + h {
            self.set_address(adj_x, y)?;

            for x in left / 8..right / 8 {
                self.write_data(self.buffer[row_start + x as usize])?;
                //TODO send in a single SPI transaction
            }

            row_start += ROW_SIZE;
        }

        self.disable_cs(delay)?;
        Ok(())
    }
}

#[cfg(feature = "graphics")]
use embedded_graphics;

#[cfg(feature = "graphics")]
use self::embedded_graphics::{
    geometry::Point,
    drawable::Pixel,
    pixelcolor::BinaryColor,
    prelude::*,
    DrawTarget,
};

#[cfg(feature = "graphics")]
impl<SPI, CS, RST, PinError, SPIError> DrawTarget<BinaryColor> for ST7920<SPI, CS, RST>
where
    SPI: spi::Write<u8, Error = SPIError>,
    RST: OutputPin<Error = PinError>,
    CS: OutputPin<Error = PinError>,
{
    type Error = core::convert::Infallible;

    /// Draw a `Pixel` that has a color defined as `Gray8`.
    fn draw_pixel(&mut self, pixel: Pixel<BinaryColor>) -> Result<(), Self::Error> {
        let Pixel(coord, color) = pixel;
        let x = coord.x as u8;
        let y = coord.y as u8;
        let c = match color { BinaryColor::Off => 0 , BinaryColor::On => 1 };
        self.set_pixel(x, y, c);
        Ok(())
    }

    fn size(&self) -> Size {
        if self.flip {
            Size::new(HEIGHT, WIDTH)
        } else {
            Size::new(WIDTH, HEIGHT)
        }
    }
}


impl<SPI, RST, CS, PinError, SPIError> ST7920<SPI, RST, CS>
where
    SPI: spi::Write<u8, Error = SPIError>,
    RST: OutputPin<Error = PinError>,
    CS: OutputPin<Error = PinError>,
{
    pub fn flush_region_graphics(
        &mut self,
        region: (Point, Size),
        delay: &mut dyn DelayUs<u32>,
    ) -> Result<(), Error<SPIError, PinError>> {
        self.flush_region(
            region.0.x as u8,
            region.0.y as u8,
            region.1.width as u8,
            region.1.height as u8,
            delay,
        )
    }
}