/ firmware / disabled / esp32s3_lcd147b_minimal.rs
esp32s3_lcd147b_minimal.rs
  1  #![no_std]
  2  #![no_main]
  3  
  4  extern crate alloc;
  5  
  6  use alloc::boxed::Box;
  7  use esp_hal::{
  8      clock::CpuClock,
  9      delay::Delay,
 10      gpio::{Level, Output, OutputConfig},
 11      main,
 12      spi::{
 13          Mode,
 14          master::{Config as SpiConfig, Spi},
 15      },
 16      time::Rate,
 17  };
 18  use mipidsi::{
 19      Builder,
 20      interface::SpiInterface,
 21      models::ST7789,
 22      options::{ColorOrder, Orientation, Rotation},
 23  };
 24  use mousefood::{
 25      embedded_graphics::{
 26          draw_target::DrawTarget,
 27          pixelcolor::{Rgb565, RgbColor},
 28      },
 29      prelude::*,
 30  };
 31  use panic_rtt_target as _;
 32  use ratatui::{
 33      Frame, Terminal,
 34      style::*,
 35      widgets::{Block, Paragraph, Wrap},
 36  };
 37  
 38  const LCD_WIDTH: u16 = 172;
 39  const LCD_HEIGHT: u16 = 320;
 40  const LCD_X_OFFSET: u16 = 34;
 41  
 42  esp_bootloader_esp_idf::esp_app_desc!();
 43  
 44  #[main]
 45  fn main() -> ! {
 46      rtt_target::rtt_init_defmt!();
 47  
 48      let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
 49      let peripherals = esp_hal::init(config);
 50  
 51      esp_alloc::heap_allocator!(size: 128 * 1024);
 52  
 53      let delay = Delay::new();
 54  
 55      let _backlight = Output::new(peripherals.GPIO46, Level::High, OutputConfig::default());
 56      let rst = Output::new(peripherals.GPIO39, Level::High, OutputConfig::default());
 57  
 58      let spi = Spi::new(
 59          peripherals.SPI2,
 60          SpiConfig::default()
 61              .with_frequency(Rate::from_mhz(40))
 62              .with_mode(Mode::_0),
 63      )
 64      .unwrap()
 65      .with_sck(peripherals.GPIO40)
 66      .with_mosi(peripherals.GPIO45);
 67  
 68      let cs = Output::new(peripherals.GPIO42, Level::High, OutputConfig::default());
 69      let spi_device = embedded_hal_bus::spi::ExclusiveDevice::new(spi, cs, delay).unwrap();
 70  
 71      let dc = Output::new(peripherals.GPIO41, Level::High, OutputConfig::default());
 72      let buffer = Box::leak(Box::new([0_u8; 4096]));
 73      let spi_interface = SpiInterface::new(spi_device, dc, buffer);
 74  
 75      let mut delay = Delay::new();
 76      let mut display = Builder::new(ST7789, spi_interface)
 77          .display_size(LCD_WIDTH, LCD_HEIGHT)
 78          .display_offset(LCD_X_OFFSET, 0)
 79          .orientation(Orientation::default().rotate(Rotation::Deg270))
 80          .color_order(ColorOrder::Bgr)
 81          .reset_pin(rst)
 82          .init(&mut delay)
 83          .expect("Failed to init ST7789 display");
 84  
 85      let _ = display.clear(Rgb565::BLACK);
 86  
 87      let backend = EmbeddedBackend::new(&mut display, Default::default());
 88      let mut terminal = Terminal::new(backend).unwrap();
 89  
 90      loop {
 91          let _ = terminal.draw(draw);
 92          delay.delay_millis(33);
 93      }
 94  }
 95  
 96  fn draw(frame: &mut Frame) {
 97      let text = "Ratatui on embedded devices!";
 98      let paragraph = Paragraph::new(text.dark_gray()).wrap(Wrap { trim: true });
 99      let bordered_block = Block::bordered()
100          .border_style(Style::new().yellow())
101          .title("Mousefood");
102      frame.render_widget(paragraph.block(bordered_block), frame.area());
103  }