From ce288566537bf61cf82db3a80f5243e37fc72998 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sat, 18 May 2019 19:51:24 +0000 Subject: [PATCH] rust: LCD x/y y/x orientation --- rust/accelerometer/src/main.rs | 2 +- rust/game-of-life/src/main.rs | 2 +- rust/k210-console/src/main.rs | 2 +- rust/k210-shared/src/board/lcd.rs | 37 +++++++++++++++---------------- rust/mandelbrot/src/main.rs | 2 +- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/rust/accelerometer/src/main.rs b/rust/accelerometer/src/main.rs index 61e97e6..1bc8cf2 100644 --- a/rust/accelerometer/src/main.rs +++ b/rust/accelerometer/src/main.rs @@ -77,7 +77,7 @@ fn main() -> ! { io_set_power(); let spi = p.SPI0.constrain(); - let lcd = LCD::new(spi); + let mut lcd = LCD::new(spi); lcd.init(); lcd.set_direction(lcd::direction::YX_LRUD); lcd.clear(lcd_colors::PURPLE); diff --git a/rust/game-of-life/src/main.rs b/rust/game-of-life/src/main.rs index e0e18a2..dc8adcc 100644 --- a/rust/game-of-life/src/main.rs +++ b/rust/game-of-life/src/main.rs @@ -155,7 +155,7 @@ fn main() -> ! { io_set_power(); let spi = p.SPI0.constrain(); - let lcd = LCD::new(spi); + let mut lcd = LCD::new(spi); lcd.init(); lcd.set_direction(lcd::direction::YX_LRUD); lcd.clear(lcd_colors::PURPLE); diff --git a/rust/k210-console/src/main.rs b/rust/k210-console/src/main.rs index b76f53b..6bc3613 100644 --- a/rust/k210-console/src/main.rs +++ b/rust/k210-console/src/main.rs @@ -116,7 +116,7 @@ fn main() -> ! { /* LCD init */ let spi = p.SPI0.constrain(); - let lcd = LCD::new(spi); + let mut lcd = LCD::new(spi); lcd.init(); lcd.set_direction(lcd::direction::YX_RLDU); lcd.clear(lcd_colors::PURPLE); diff --git a/rust/k210-shared/src/board/lcd.rs b/rust/k210-shared/src/board/lcd.rs index 8b37c47..9551105 100644 --- a/rust/k210-shared/src/board/lcd.rs +++ b/rust/k210-shared/src/board/lcd.rs @@ -12,8 +12,8 @@ pub const SPI_SLAVE_SELECT: u32 = 3; pub const DCX_GPIONUM: u8 = 2; pub const RST_GPIONUM: u8 = 3; -pub const LCD_X_MAX: u16 = 320; -pub const LCD_Y_MAX: u16 = 240; +pub const LCD_X_MAX: u16 = 240; +pub const LCD_Y_MAX: u16 = 320; #[repr(u8)] #[derive(Copy, Clone)] @@ -113,11 +113,17 @@ pub const DIR_MASK: u8 = 0xE0; pub struct LCD { spi: SPI, + pub width: u16, + pub height: u16, } impl LCD { pub fn new(spi: X) -> Self { - Self { spi } + Self { + spi, + width: 0, + height: 0, + } } /* Low-level functions */ @@ -245,7 +251,7 @@ impl LCD { /* High-level functions */ - pub fn init(&self) { + pub fn init(&mut self) { self.hard_init(); /*soft reset*/ self.write_command(command::SOFTWARE_RESET); @@ -262,20 +268,14 @@ impl LCD { self.write_command(command::DISPLAY_ON); } - pub fn set_direction(&self, dir: direction) { - /* No support for YX orientations right now -- - lcd_ctl.dir = dir; - if (dir & DIR_XY_MASK) - { - lcd_ctl.width = LCD_Y_MAX - 1; - lcd_ctl.height = LCD_X_MAX - 1; + pub fn set_direction(&mut self, dir: direction) { + if ((dir as u8) & DIR_XY_MASK) != 0 { + self.width = LCD_Y_MAX; + self.height = LCD_X_MAX; + } else { + self.width = LCD_X_MAX; + self.height = LCD_Y_MAX; } - else - { - lcd_ctl.width = LCD_X_MAX - 1; - lcd_ctl.height = LCD_Y_MAX - 1; - } - */ self.write_command(command::MEMORY_ACCESS_CTL); self.write_byte(&[dir as u8]); @@ -304,8 +304,7 @@ impl LCD { pub fn clear(&self, color: u16) { let data = ((color as u32) << 16) | (color as u32); - //set_area(0, 0, lcd_ctl.width, lcd_ctl.height); - self.set_area(0, 0, LCD_X_MAX - 1, LCD_Y_MAX - 1); + self.set_area(0, 0, self.width - 1, self.height - 1); self.fill_data(data, (LCD_X_MAX as usize) * (LCD_Y_MAX as usize) / 2); } diff --git a/rust/mandelbrot/src/main.rs b/rust/mandelbrot/src/main.rs index ef6980c..239b51d 100644 --- a/rust/mandelbrot/src/main.rs +++ b/rust/mandelbrot/src/main.rs @@ -81,7 +81,7 @@ fn main() -> ! { io_set_power(); let spi = p.SPI0.constrain(); - let lcd = LCD::new(spi); + let mut lcd = LCD::new(spi); lcd.init(); lcd.set_direction(lcd::direction::YX_RLDU); lcd.clear(lcd_colors::PURPLE);