From ca92459649bbb1234f0060dba9d097102b8da55b Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Sun, 2 Sep 2018 21:04:33 +0200 Subject: [PATCH] Added some helper functions to St7735IO --- src/st7735.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src/st7735.rs b/src/st7735.rs index 6ed80e1..51b6b2e 100644 --- a/src/st7735.rs +++ b/src/st7735.rs @@ -332,18 +332,40 @@ impl St7735 { -> St7735IO where SPIAddr: Location, GPIOAddr: Location { - St7735IO { - st7735: self, - spi : spi, - gpio : gpio - } + St7735IO::start(self, spi, gpio) } } + impl St7735IO where SPIAddr: Location, GPIOAddr: Location { + pub fn start(st7735 : St7735, mut spi : PeripheralRef, gpio : PeripheralRef) + -> St7735IO + where SPIAddr: Location, + GPIOAddr: Location { + + spi.configure(|s| { + s.set_enabled(true) + .set_master_mode(true) + .set_software_slave_select(true) + .set_clock_divider(128) + // required for master mode, even if ss is done manually + .set_slave_select_output_enabled(true) + }); + + let mut io = St7735IO { + st7735: st7735, + spi : spi, + gpio : gpio + }; + + io.set_cs(); + + io + } + fn set_rs(&mut self) { self.gpio.set_bit(self.st7735.rs_pin); } @@ -386,11 +408,39 @@ impl St7735IO delay_ms(500); } + fn write_byte(&mut self, byte : u8) { + self.spi.set_data(byte as u32); + while self.spi.busy() {} + } + + fn write_data_byte(&mut self, data : u8) { + self.set_rs(); + + self.reset_cs(); + self.write_byte(data); + self.set_cs(); + } + + fn write_command_byte(&mut self, cmd : u8) { + self.set_rs(); + + self.reset_cs(); + self.write_byte(cmd); + self.set_cs(); + } + + pub fn write_command(&mut self, cmd : Command) { + let cmd_byte = command_to_byte(cmd); + self.write_command_byte(cmd_byte); + } + pub fn init(&mut self) { } - pub fn done(self) -> (St7735, PeripheralRef, PeripheralRef) { + pub fn done(mut self) -> (St7735, PeripheralRef, PeripheralRef) { + self.set_cs(); + self.spi.set_enabled(false); (self.st7735, self.spi, self.gpio) } }