Fix madctl

This commit is contained in:
Sofia 2026-05-13 17:37:19 +03:00
parent 4e9ac8f10d
commit 1cdd390440
2 changed files with 65 additions and 21 deletions

View File

@ -3,13 +3,6 @@ use core::ops::Mul;
use embedded_hal::{delay::DelayNs, spi::SpiBus}; use embedded_hal::{delay::DelayNs, spi::SpiBus};
use esp_hal::{DriverMode, gpio::Output, spi::master::Spi}; 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)] #[derive(Clone, Copy)]
pub struct Rgb565(pub u8, pub u8, pub u8); pub struct Rgb565(pub u8, pub u8, pub u8);
@ -163,11 +156,63 @@ pub enum Writeable<'d> {
Data(&'d [u8]), Data(&'d [u8]),
} }
#[derive(Default)]
pub enum Rotation { pub enum Rotation {
Portrait = 0x00, #[default]
Landscape = 0x60, Portrait,
InvertedPortrait = 0xc0, Landscape,
InvertedLandscape = 0xa0, 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> { 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.hard_reset();
self.delay.delay_ms(150); self.delay.delay_ms(150);
// self.soft_reset(); // self.soft_reset();
self.set_sleep(false); self.set_sleep(false);
self.delay.delay_ms(50); self.delay.delay_ms(50);
// TODO fix madctl self.write_madctl(set_address_mode);
self.write(Writeable::Command(Command::MADCTL));
self.write(Writeable::Data(&[0b0000_1000]));
self.delay.delay_ms(150); self.delay.delay_ms(150);
self.set_color_mode((ColorMode::ColorMode65K as u8) | (ColorMode::ColorMode16BIT as u8)); 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::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) { 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) { pub fn set_window(&mut self, pos0: Position, pos1: Position) {
self.set_columns(pos0.x, pos1.x); self.set_columns(pos0.x, pos1.x);
self.delay.delay_ms(1);
self.set_rows(pos0.y, pos1.y); self.set_rows(pos0.y, pos1.y);
self.delay.delay_ms(1);
self.write(Writeable::Command(Command::RamWR)); self.write(Writeable::Command(Command::RamWR));
} }

View File

@ -21,7 +21,7 @@ use esp_hal::{
use esp_backtrace as _; use esp_backtrace as _;
// use mipidsi::{Builder, interface::SpiInterface, models::ST7789}; // 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}; // use crate::display::{Color, Display, Position, Rgb565, Rotation};
@ -90,7 +90,10 @@ fn main() -> ! {
delay: Delay::new(), delay: Delay::new(),
}; };
display.init(); display.init(SetAddressMode {
color_order: display::ColorOrder::Bgr,
..Default::default()
});
display.draw_rect( display.draw_rect(
Position::new(0, 0), Position::new(0, 0),
Position::new(32, 32), Position::new(32, 32),