mirror of
https://github.com/kittywitch/esp32-c3-meepy.git
synced 2026-02-09 07:59:18 -08:00
feat: less just, lol
This commit is contained in:
parent
8830e92597
commit
6841142729
10 changed files with 549 additions and 56 deletions
212
src/main.rs
212
src/main.rs
|
|
@ -1,15 +1,30 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use {
|
||||
core::ptr::addr_of_mut,
|
||||
display_interface_spi::{SPIInterface, *},
|
||||
embedded_graphics::{
|
||||
mono_font::{ascii::FONT_8X13, MonoFont, MonoTextStyle},
|
||||
mono_font::{ascii::{FONT_8X13, FONT_6X10}, MonoFont, MonoTextStyle},
|
||||
pixelcolor::{Rgb565, Bgr565, RgbColor},
|
||||
prelude::*,
|
||||
primitives::{Line, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, Triangle},
|
||||
primitives::{Styled, Line, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, Triangle},
|
||||
text::{Alignment, Text},
|
||||
},
|
||||
embedded_text::{
|
||||
TextBox,
|
||||
style::{
|
||||
TextBoxStyleBuilder,
|
||||
HeightMode,
|
||||
},
|
||||
alignment::HorizontalAlignment as TextHorizontalAlignment,
|
||||
},
|
||||
embedded_layout::{
|
||||
layout::linear::LinearLayout,
|
||||
prelude::*,
|
||||
},
|
||||
embedded_hal_bus::spi::{ExclusiveDevice, NoDelay},
|
||||
esp_backtrace as _,
|
||||
esp_hal::{
|
||||
|
|
@ -29,8 +44,13 @@ use {
|
|||
},
|
||||
esp_println::println,
|
||||
ili9341::{DisplaySize240x320, Ili9341, Orientation},
|
||||
embassy_executor::Spawner,
|
||||
embassy_time::{Duration, Timer},
|
||||
};
|
||||
|
||||
const SSID: &str = env!("WIFI_SSID");
|
||||
const PASSWORD: &str = env!("WIFI_PASSWORD");
|
||||
|
||||
esp_bootloader_esp_idf::esp_app_desc!();
|
||||
|
||||
type TFTSpiDevice<'spi> = ExclusiveDevice<Spi<'spi, Blocking>, Output<'spi>, NoDelay>;
|
||||
|
|
@ -111,8 +131,21 @@ impl<'a> Dimensions for DrawFlipper<'a, '_> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,Copy)]
|
||||
enum StyleVariant {
|
||||
Header,
|
||||
Regular
|
||||
}
|
||||
|
||||
impl<'spi> TFT<'spi> {
|
||||
const H_BG: Bgr565 = Bgr565::CSS_DARK_SLATE_GRAY;
|
||||
const H_S: Bgr565 = Bgr565::CYAN;
|
||||
const H_FG: Bgr565 = Bgr565::WHITE;
|
||||
|
||||
const R_BG: Bgr565 = Bgr565::CSS_DARK_SLATE_GRAY;
|
||||
const R_S: Bgr565 = Bgr565::BLUE;
|
||||
const R_FG: Bgr565 = Bgr565::WHITE;
|
||||
|
||||
pub fn new(
|
||||
spi2: SPI2<'spi>,
|
||||
sclk: impl OutputPin + 'spi,
|
||||
|
|
@ -163,43 +196,178 @@ impl<'spi> TFT<'spi> {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn containing_recty(&mut self, x: i32, y: i32, w: u32, h: u32) {
|
||||
pub fn containing_recty(&mut self, x: i32, y: i32, w: u32, h: u32, style: StyleVariant) -> Styled<Rectangle, PrimitiveStyle<Bgr565>> {
|
||||
let bg = match style {
|
||||
StyleVariant::Header => Self::H_BG,
|
||||
StyleVariant::Regular => Self::R_BG,
|
||||
};
|
||||
let s = match style {
|
||||
StyleVariant::Header => Self::H_S,
|
||||
StyleVariant::Regular => Self::R_S,
|
||||
};
|
||||
let style = PrimitiveStyleBuilder::new()
|
||||
.fill_color(bg)
|
||||
.stroke_color(s)
|
||||
.stroke_width(1)
|
||||
.build();
|
||||
Rectangle::new(Point::new(x, y), Size::new(w, h))
|
||||
.into_styled(PrimitiveStyle::with_stroke(Bgr565::CYAN, 1))
|
||||
.draw(&mut self.draw_target())
|
||||
.unwrap();
|
||||
.clone()
|
||||
.into_styled(style)
|
||||
}
|
||||
|
||||
pub fn container(&mut self, margin: i32, y: i32, h: u32) {
|
||||
pub fn container(&mut self, margin: i32, y: i32, h: u32, style: StyleVariant) -> Styled<Rectangle, PrimitiveStyle<Bgr565>> {
|
||||
let width = self.display.bounding_box().size.width;
|
||||
self.containing_recty(margin, y, width - margin as u32, h);
|
||||
self.containing_recty(margin, y, width - margin as u32, h, style)
|
||||
}
|
||||
|
||||
pub fn println(&mut self, text: &str, x: i32, y: i32) {
|
||||
let style = MonoTextStyle::new(&FONT_8X13, Bgr565::WHITE);
|
||||
let style = MonoTextStyle::new(&FONT_6X10, Bgr565::WHITE);
|
||||
Text::with_alignment(text, Point::new(x, y), style, Alignment::Center)
|
||||
.draw(&mut self.draw_target())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[main]
|
||||
fn main() -> ! {
|
||||
macro_rules! mk_static {
|
||||
($t:ty,$val:expr) => {{
|
||||
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
||||
#[deny(unused_attributes)]
|
||||
let x = STATIC_CELL.uninit().write(($val));
|
||||
x
|
||||
}};
|
||||
}
|
||||
/*
|
||||
#[embassy_executor::task]
|
||||
async fn connection(mut controller: WifiController<'static>) {
|
||||
println!("start connection task");
|
||||
println!("Device capabilities: {:?}", controller.capabilities());
|
||||
loop {
|
||||
if esp_wifi::wifi::wifi_state() == WifiState::StaConnected {
|
||||
// wait until we're no longer connected
|
||||
controller.wait_for_event(WifiEvent::StaDisconnected).await;
|
||||
Timer::after(Duration::from_millis(5000)).await
|
||||
}
|
||||
if !matches!(controller.is_started(), Ok(true)) {
|
||||
let client_config = Configuration::Client(ClientConfiguration {
|
||||
ssid: SSID.into(),
|
||||
password: PASSWORD.into(),
|
||||
..Default::default()
|
||||
});
|
||||
controller.set_configuration(&client_config).unwrap();
|
||||
println!("Starting wifi");
|
||||
controller.start_async().await.unwrap();
|
||||
println!("Wifi started!");
|
||||
}
|
||||
println!("About to connect...");
|
||||
|
||||
match controller.connect_async().await {
|
||||
Ok(_) => println!("Wifi connected!"),
|
||||
Err(e) => {
|
||||
println!("Failed to connect to wifi: {e:?}");
|
||||
Timer::after(Duration::from_millis(5000)).await
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) {
|
||||
runner.run().await
|
||||
}*/
|
||||
|
||||
|
||||
fn init_heap() {
|
||||
const HEAP_SIZE: usize = 64 * 1024;
|
||||
static mut HEAP: core::mem::MaybeUninit<[u8; HEAP_SIZE]> = core::mem::MaybeUninit::uninit();
|
||||
|
||||
unsafe {
|
||||
esp_alloc::HEAP.add_region(esp_alloc::HeapRegion::new(
|
||||
addr_of_mut!(HEAP) as *mut u8,
|
||||
HEAP_SIZE,
|
||||
esp_alloc::MemoryCapability::Internal.into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
struct Controller<'tft> {
|
||||
pub tft: TFT<'tft>,
|
||||
}
|
||||
|
||||
impl Controller<'_> {
|
||||
async fn init(peripherals: Peripherals) -> Self {
|
||||
let tft = Self::init_screen(peripherals).await;
|
||||
let controller = Self {
|
||||
tft,
|
||||
};
|
||||
controller
|
||||
}
|
||||
|
||||
async fn init_screen<'tft>(peripherals: Peripherals) -> TFT<'tft> {
|
||||
let dc = peripherals.GPIO9;
|
||||
let mosi = peripherals.GPIO6;
|
||||
let sclk = peripherals.GPIO4;
|
||||
let miso = peripherals.GPIO5;
|
||||
let cs = peripherals.GPIO7;
|
||||
let rst = peripherals.GPIO0;
|
||||
|
||||
let mut tft = TFT::new(peripherals.SPI2, sclk, miso, mosi, cs, rst, dc);
|
||||
let _ = tft.draw_target().clear(Bgr565::BLACK);
|
||||
tft
|
||||
}
|
||||
}
|
||||
|
||||
#[esp_rtos::main]
|
||||
async fn main(spawner: Spawner) {
|
||||
init_heap();
|
||||
#[cfg(feature = "log")]
|
||||
{
|
||||
// The default log level can be specified here.
|
||||
// You can see the esp-println documentation: https://docs.rs/esp-println
|
||||
esp_println::logger::init_logger(log::LevelFilter::Info);
|
||||
}
|
||||
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals: Peripherals = init(config);
|
||||
esp_alloc::heap_allocator!(size: 72*1024);
|
||||
/* let timer1 = TimerGroup::new(peripherals.TIMG1);
|
||||
let rng = Rng::new();
|
||||
let esp_wifi_ctrl = &*mk_static!(
|
||||
EspWifiController<'static>,
|
||||
init(timer1.timer0, rng).unwrap()
|
||||
);
|
||||
let (controller, interfaces) = esp_wifi::wifi::new(esp_wifi_ctrl, peripherals.WIFI).unwrap();
|
||||
let wifi_interface = interfaces.sta;
|
||||
let seed = rng.random();
|
||||
let wifi_config = Config::dhcpv4(Default::default());
|
||||
let (stack, runner) = embassy_net::new(
|
||||
wifi_interface,
|
||||
wifi_config,
|
||||
mk_static!(StackResources<8>, StackResources::<8>::new()),
|
||||
seed,
|
||||
);*/
|
||||
|
||||
let dc = peripherals.GPIO9;
|
||||
let mosi = peripherals.GPIO6; // sdo -> MOSI
|
||||
let sclk = peripherals.GPIO4;
|
||||
let miso = peripherals.GPIO5; // sdi -> MISO
|
||||
let cs = peripherals.GPIO7;
|
||||
let rst = peripherals.GPIO0;
|
||||
let mut controller = Controller::init(peripherals).await;
|
||||
|
||||
let mut tft = TFT::new(peripherals.SPI2, sclk, miso, mosi, cs, rst, dc);
|
||||
tft.draw_target().clear(Bgr565::BLACK);
|
||||
tft.container(2, 2, 16);
|
||||
tft.println("nya~! -w-", 100, 40);
|
||||
let display_area = controller.tft.display.bounding_box();
|
||||
let text_style = MonoTextStyle::new(&FONT_6X10, Bgr565::WHITE);
|
||||
let margin = 2;
|
||||
let display_width = display_area.size.width;
|
||||
let display_height = display_area.size.height;
|
||||
|
||||
LinearLayout::vertical(
|
||||
Chain::new(
|
||||
LinearLayout::horizontal(
|
||||
Chain::new(
|
||||
Text::new("Initializing~!", Point::new(margin, margin), text_style)
|
||||
).append(
|
||||
controller.tft.container(margin, -10, 20, StyleVariant::Header)
|
||||
)
|
||||
)
|
||||
)
|
||||
).with_alignment(horizontal::Center)
|
||||
.arrange()
|
||||
.align_to(&display_area, horizontal::Center, vertical::Center)
|
||||
.draw(&mut controller.tft.draw_target())
|
||||
.unwrap();
|
||||
|
||||
loop {
|
||||
// your business logic
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue