From 1cdd390440c758141537cdaae552c06ea536d545 Mon Sep 17 00:00:00 2001 From: Sofia Date: Wed, 13 May 2026 17:37:19 +0300 Subject: [PATCH] Fix madctl --- src/display.rs | 79 ++++++++++++++++++++++++++++++++++++++------------ src/main.rs | 7 +++-- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/display.rs b/src/display.rs index 8894395..9cf4e02 100644 --- a/src/display.rs +++ b/src/display.rs @@ -3,13 +3,6 @@ use core::ops::Mul; use embedded_hal::{delay::DelayNs, spi::SpiBus}; use esp_hal::{DriverMode, gpio::Output, spi::master::Spi}; -// use atmega_hal::{ -// Spi, -// port::{self, Pin, PinOps, mode}, -// spi::ChipSelectPin, -// }; -// use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::SpiBus}; - #[derive(Clone, Copy)] pub struct Rgb565(pub u8, pub u8, pub u8); @@ -163,11 +156,63 @@ pub enum Writeable<'d> { Data(&'d [u8]), } +#[derive(Default)] pub enum Rotation { - Portrait = 0x00, - Landscape = 0x60, - InvertedPortrait = 0xc0, - InvertedLandscape = 0xa0, + #[default] + Portrait, + Landscape, + InvertedPortrait, + InvertedLandscape, +} + +#[derive(Default)] +pub enum ColorOrder { + #[default] + Rgb, + Bgr, +} + +#[derive(Default)] +pub enum RefreshOrder { + #[default] + TopBottomLeftRight, + TopBottomRightLeft, + BottomTopLeftRight, + BottomTopRightLeft, +} + +#[derive(Default)] +pub struct SetAddressMode { + pub rotation: Rotation, + pub color_order: ColorOrder, + pub refresh_order: RefreshOrder, +} + +impl SetAddressMode { + pub fn into_madctl(&self) -> u8 { + let mut result = 0; + + result = match self.rotation { + Rotation::Portrait => result, + Rotation::Landscape => result | 0b0110_0000, + Rotation::InvertedPortrait => result | 0b1100_0000, + Rotation::InvertedLandscape => result | 0b1010_0000, + }; + + result = match self.color_order { + ColorOrder::Rgb => result, + ColorOrder::Bgr => result | 0b0000_1000, + }; + + result = match self.refresh_order { + RefreshOrder::TopBottomLeftRight => result, + RefreshOrder::TopBottomRightLeft => result | 0b0000_0100, + RefreshOrder::BottomTopLeftRight => result | 0b0001_0000, + RefreshOrder::BottomTopRightLeft => result | 0b0001_0100, + }; + + result + } } impl<'d, DM: DriverMode, T: DelayNs> Display<'d, DM, T> { @@ -184,16 +229,14 @@ impl<'d, DM: DriverMode, T: DelayNs> Display<'d, DM, T> { } } - pub fn init(&mut self) { + pub fn init(&mut self, set_address_mode: SetAddressMode) { self.hard_reset(); self.delay.delay_ms(150); // self.soft_reset(); self.set_sleep(false); self.delay.delay_ms(50); - // TODO fix madctl - self.write(Writeable::Command(Command::MADCTL)); - self.write(Writeable::Data(&[0b0000_1000])); + self.write_madctl(set_address_mode); self.delay.delay_ms(150); self.set_color_mode((ColorMode::ColorMode65K as u8) | (ColorMode::ColorMode16BIT as u8)); @@ -243,9 +286,9 @@ impl<'d, DM: DriverMode, T: DelayNs> Display<'d, DM, T> { } } - pub fn set_rotation(&mut self, rotation: Rotation) { + pub fn write_madctl(&mut self, set_address_mode: SetAddressMode) { self.write(Writeable::Command(Command::MADCTL)); - self.write(Writeable::Data(&[rotation as u8])); + self.write(Writeable::Data(&[set_address_mode.into_madctl()])); } pub fn set_color_mode(&mut self, mode: u8) { @@ -269,9 +312,7 @@ impl<'d, DM: DriverMode, T: DelayNs> Display<'d, DM, T> { pub fn set_window(&mut self, pos0: Position, pos1: Position) { self.set_columns(pos0.x, pos1.x); - self.delay.delay_ms(1); self.set_rows(pos0.y, pos1.y); - self.delay.delay_ms(1); self.write(Writeable::Command(Command::RamWR)); } diff --git a/src/main.rs b/src/main.rs index 46848b4..a7a116c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ use esp_hal::{ use esp_backtrace as _; // use mipidsi::{Builder, interface::SpiInterface, models::ST7789}; -use crate::display::{Color, Display, Position, Rgb565}; +use crate::display::{Color, Display, Position, Rgb565, SetAddressMode}; // use crate::display::{Color, Display, Position, Rgb565, Rotation}; @@ -90,7 +90,10 @@ fn main() -> ! { delay: Delay::new(), }; - display.init(); + display.init(SetAddressMode { + color_order: display::ColorOrder::Bgr, + ..Default::default() + }); display.draw_rect( Position::new(0, 0), Position::new(32, 32),