diff --git a/src/main.rs b/src/main.rs index fb0dec4..d0f9753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,11 +121,12 @@ fn run(mut scs: scs::Instance, mut p: hcl::platform::Instance) { let mut st7735io = st7735.start(spi, gpio); loop { st7735io.init(); - st7735io.fill_rect(0, 0, 128, 160, 0x00, 0x00); - st7735io.fill_rect(4, 4, 60, 76, 0x00, 0x1F); - st7735io.fill_rect(4, 80, 60, 76, 0xF8, 0x00); - st7735io.fill_rect(64, 4, 60, 76, 0x07, 0xE0); - st7735io.fill_rect(64, 80, 60, 76, 0xFF, 0xE0); + st7735io.set_orientation(st7735::DisplayOrientation::Landscape); + st7735io.fill_rect(0, 0, 160, 128, st7735::COLOR_BLACK); + st7735io.fill_rect(4, 4, 76, 60, st7735::COLOR_RED); + st7735io.fill_rect(4, 64, 76, 60, st7735::COLOR_GREEN); + st7735io.fill_rect(80, 4, 76, 60, st7735::COLOR_BLUE); + st7735io.fill_rect(80, 64, 76, 60, st7735::COLOR_YELLOW); systick::delay_ms(2000); } let (st7735, mut spi, mut gpio) = st7735io.done(); diff --git a/src/st7735/mod.rs b/src/st7735/mod.rs index e334f9b..c2855b2 100644 --- a/src/st7735/mod.rs +++ b/src/st7735/mod.rs @@ -24,6 +24,31 @@ pub enum DisplayType { RED144_GREENTAB } +pub enum DisplayOrientation { + Portrait, + InvertedPortrait, + Landscape, + InvertedLandscape +} + +const MADCTL_MY : u8 = 0x80; // Mirror Y +const MADCTL_MX : u8 = 0x40; // Mirrror x +const MADCTL_MV : u8 = 0x20; // Swap XY +const MADCTL_ML : u8 = 0x10; // Scan address order +const MADCTL_RGB : u8 = 0x00; +const MADCTL_BGR : u8 = 0x08; +const MADCTL_MH : u8 = 0x04; // Horizontal scan oder + + +pub const COLOR_BLACK : u16 = 0x0000; +pub const COLOR_BLUE : u16 = 0x001F; +pub const COLOR_RED : u16 = 0xF800; +pub const COLOR_GREEN : u16 = 0x07E0; +pub const COLOR_CYAN : u16 = 0x07FF; +pub const COLOR_MAGENTA : u16 = 0xF81F; +pub const COLOR_YELLOW : u16 = 0xFFE0; +pub const COLOR_WHITE : u16 = 0xFFFF; + pub struct St7735 { @@ -195,7 +220,7 @@ impl St7735IO while self.spi.busy() {} } - pub fn write_data_byte(&mut self, data : u8) { + fn write_data_byte(&mut self, data : u8) { self.set_rs(); self.reset_cs(); @@ -203,13 +228,11 @@ impl St7735IO self.set_cs(); } - pub fn write_color_bytes(&mut self, data0 : u8, data1 : u8) { - self.set_rs(); - - self.reset_cs(); + fn write_color_bytes(&mut self, color: u16) { + let data0 = (color >> 8) as u8; + let data1 = (color & 0xFF) as u8; self.write_byte(data0); self.write_byte(data1); - self.set_cs(); } fn write_command_byte(&mut self, cmd : u8) { @@ -333,7 +356,67 @@ impl St7735IO self.write_command(&Command::RAMWR); // write to RAM } - pub fn fill_rect(&mut self, x : u8, y : u8, w : u8, h : u8, c1 : u8, c2 : u8) { + + pub fn set_orientation(&mut self, orientation : DisplayOrientation) { + let param = match orientation { + DisplayOrientation::Portrait => { + self.st7735.width = DEFAULT_WIDTH; + self.st7735.height = match self.st7735.display_type { + DisplayType::RED144_GREENTAB => DEFAULT_HEIGHT_144, + _ => DEFAULT_HEIGHT_18, + }; + + match self.st7735.display_type { + DisplayType::RED_18_BLACKTAB => MADCTL_MX | MADCTL_MY | MADCTL_RGB, + _ => MADCTL_MX | MADCTL_MY | MADCTL_BGR + } + }, + + DisplayOrientation::Landscape => { + self.st7735.height = DEFAULT_WIDTH; + self.st7735.width = match self.st7735.display_type { + DisplayType::RED144_GREENTAB => DEFAULT_HEIGHT_144, + _ => DEFAULT_HEIGHT_18, + }; + + match self.st7735.display_type { + DisplayType::RED_18_BLACKTAB => MADCTL_MY | MADCTL_MV | MADCTL_RGB, + _ => MADCTL_MY | MADCTL_MV | MADCTL_BGR + } + }, + + DisplayOrientation::InvertedPortrait => { + self.st7735.width = DEFAULT_WIDTH; + self.st7735.height = match self.st7735.display_type { + DisplayType::RED144_GREENTAB => DEFAULT_HEIGHT_144, + _ => DEFAULT_HEIGHT_18, + }; + + match self.st7735.display_type { + DisplayType::RED_18_BLACKTAB => MADCTL_RGB, + _ => MADCTL_BGR + } + }, + + DisplayOrientation::InvertedLandscape => { + self.st7735.height = DEFAULT_WIDTH; + self.st7735.width = match self.st7735.display_type { + DisplayType::RED144_GREENTAB => DEFAULT_HEIGHT_144, + _ => DEFAULT_HEIGHT_18, + }; + + match self.st7735.display_type { + DisplayType::RED_18_BLACKTAB => MADCTL_MX | MADCTL_MV | MADCTL_RGB, + _ => MADCTL_MX | MADCTL_MV | MADCTL_BGR + } + }, + }; + + self.write_command(&Command::MADCTL(param)); + } + + + pub fn fill_rect(&mut self, x : u8, y : u8, w : u8, h : u8, color: u16) { self.set_addr_win(x, y, x + w - 1, y + h - 1); @@ -342,15 +425,13 @@ impl St7735IO for i in 0..h { for j in 0..w { - self.write_byte(c1); - self.write_byte(c2); + self.write_color_bytes(color); } } self.set_cs(); } - pub fn done(mut self) -> (St7735, PeripheralRef, PeripheralRef) { self.set_cs(); self.spi.set_enabled(false);