0
0
Fork 0

spat: Add screen

This commit is contained in:
Jannik Beyerstedt 2026-06-15 23:08:53 +02:00
commit b2ec98da2e
7 changed files with 463 additions and 30 deletions

View file

@ -18,6 +18,10 @@ use embassy_gps::gps::l76k;
use esp_backtrace as _;
use esp_hal::clock::CpuClock;
use esp_hal::timer::timg::TimerGroup;
#[cfg(feature = "screen")]
use esp_hal::{delay, gpio, spi, time};
#[cfg(all(target_arch = "riscv32", feature = "spat"))]
use esp_println::println;
use esp_radio::wifi;
use log::{debug, error, info, warn};
@ -28,6 +32,8 @@ mod applogic;
mod cache;
mod geo_alg;
mod radio;
#[cfg(feature = "screen")]
mod screen;
const WIFI_CHANNEL: radio::Channel = 180;
@ -106,8 +112,53 @@ async fn main(spawner: Spawner) -> ! {
)
.expect("Failed to spawn GNSS task"),
);
#[cfg(feature = "gnss")]
info!("GNSS task started, waiting for GNSS fix...");
// ST7789 172*320px screen
// SCL: D8/ GPIO8 (SPI_SCK)
// SDA: D10/ GPIO10 (SPI_MOSI)
// RES: D3/ GPIO7
// DC: D4/ GPIO23
// CS: D5/ GPIO24
// BL: TODO (hard-wire to high? or D1/ GPIO0)
#[cfg(feature = "screen")]
let mut spi_buffer = [0_u8; 512];
#[cfg(feature = "screen")]
let mut display = {
let (spi, cs) = setup_st7789_spi(
peripherals.GPIO8,
peripherals.GPIO10,
peripherals.GPIO24,
peripherals.GPIO9,
peripherals.SPI2,
)
.expect("Failed to build SPI config");
let output_config = gpio::OutputConfig::default();
let dc_output = gpio::Output::new(peripherals.GPIO23, gpio::Level::Low, output_config);
let rst_output = gpio::Output::new(peripherals.GPIO7, gpio::Level::High, output_config);
let _ = gpio::Output::new(peripherals.GPIO0, gpio::Level::High, output_config); // backlight
let mut display_delay = delay::Delay::new();
let spi_device = embedded_hal_bus::spi::ExclusiveDevice::new_no_delay(spi, cs)
.expect("Failed to initialize SPI device");
mipidsi::Builder::new(
mipidsi::models::ST7789,
mipidsi::interface::SpiInterface::new(spi_device, dc_output, &mut spi_buffer),
)
.display_size(screen::HEIGHT + 34, screen::WIDTH) // somehow this display has 34 px of invisible space on the left (in native orientation)
.reset_pin(rst_output)
.invert_colors(mipidsi::options::ColorInversion::Inverted)
.orientation(mipidsi::options::Orientation::new().rotate(mipidsi::options::Rotation::Deg90))
.init(&mut display_delay)
.expect("Failed to initialize display")
};
#[cfg(feature = "screen")]
let _ = screen::draw_slash_screen(&mut display).inspect_err(log_screen_error);
let mut state = applogic::State::new();
loop {
embassy_time::Timer::after(embassy_time::Duration::from_millis(5)).await;
@ -124,7 +175,18 @@ async fn main(spawner: Spawner) -> ! {
// run GLOSA algorithm
#[cfg(feature = "spat")]
state.run_glosa();
let signal_groups = state.run_glosa();
for sig in &signal_groups {
println!(
"GLOSA: {} {} for {} s",
sig.maneuver, sig.phase, sig.min_end_sec
);
}
#[cfg(feature = "screen")]
let _ = screen::draw_glosa(&mut display, &signal_groups)
.inspect_err(log_screen_error);
}
}
});
@ -284,3 +346,41 @@ async fn gnss_task(
}
}
}
/// Builds the SPI interface for the ST7789 display
///
/// Returns (spi master, chip-select) tuple or SPI config error
#[cfg(feature = "screen")]
fn setup_st7789_spi(
scl: esp_hal::peripherals::GPIO8<'static>,
sda: esp_hal::peripherals::GPIO10<'static>,
cs: esp_hal::peripherals::GPIO24<'static>,
miso: esp_hal::peripherals::GPIO9<'static>,
spi: esp_hal::peripherals::SPI2<'static>,
) -> Result<
(
spi::master::Spi<'static, esp_hal::Blocking>,
gpio::Output<'static>,
),
spi::master::ConfigError,
> {
let spi_config = spi::master::Config::default()
.with_frequency(time::Rate::from_mhz(10))
.with_mode(spi::Mode::_0);
let spi: spi::master::Spi<'_, esp_hal::Blocking> = spi::master::Spi::new(spi, spi_config)?
.with_sck(scl)
.with_mosi(sda)
.with_miso(miso);
let cs_output = gpio::Output::new(cs, gpio::Level::High, gpio::OutputConfig::default());
Ok((spi, cs_output))
}
#[cfg(feature = "screen")]
fn log_screen_error<E>(error: &E)
where
E: core::fmt::Debug,
{
error!("TODO: {error:?}");
}