Skip to content

Commit

Permalink
Maybe async?
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstalgia committed Nov 26, 2024
1 parent 431425f commit 29f77bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
46 changes: 24 additions & 22 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
ble: BleStuff::build(),
})
}
pub fn doodle(&mut self) -> Result<()> {
async fn doodle(&mut self) -> Result<()> {
const BACK_BUTTON_BOUND: Rectangle =
Rectangle::new(Point::new(290, 0), Size::new_equal(24));
if self.paint_check() {
Expand All @@ -130,7 +130,7 @@ where
kind: TouchKind::Start,
}) => {
if BACK_BUTTON_BOUND.contains(*new_point) {
self.change_view(AppView::MainMenu)?;
self.change_view(AppView::MainMenu).await?;
return Ok(());
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ where

Ok(())
}
fn main_menu(&mut self) -> Result<()> {
async fn main_menu(&mut self) -> Result<()> {
let options_offset = Point::new(20, 50);
if self.paint_check() {
info!(
Expand Down Expand Up @@ -257,7 +257,7 @@ where
point,
kind: TouchKind::Start,
}) if point.y < 20 && point.x < 100 => {
self.change_view(AppView::MainMenu)?;
self.change_view(AppView::MainMenu).await?;
}
Some(TouchEvent {
point,
Expand All @@ -275,12 +275,12 @@ where
{
info!("{choice} at {point}");
match choice {
MainMenu::Start => self.change_view(AppView::BadgeDisplay)?,
MainMenu::NameInput => self.change_view(AppView::NameInput)?,
MainMenu::HrSelect => self.change_view(AppView::HrSelect)?,
MainMenu::Start => self.change_view(AppView::BadgeDisplay).await?,
MainMenu::NameInput => self.change_view(AppView::NameInput).await?,
MainMenu::HrSelect => self.change_view(AppView::HrSelect).await?,
// MainMenu::HrSelect => self.ble_stuff.scan_for_select(),
// ;
MainMenu::Doodle => self.change_view(AppView::Doodle)?,
MainMenu::Doodle => self.change_view(AppView::Doodle).await?,
_ => (),
}
} else {
Expand All @@ -294,7 +294,7 @@ where
}
// awful hardcoded-ness
// TODO fix at some point
fn name_input(&mut self) -> Result<()> {
async fn name_input(&mut self) -> Result<()> {
let offset = Point::new(0, 100);
let mut width: i32 = self.display.bounding_box().size.width as i32
/ self.username_scratch.len().max(1) as i32;
Expand Down Expand Up @@ -537,13 +537,13 @@ where
self.settings.username.clone_from(&self.username_scratch);
self.settings.littlefs_save()?;
}
self.change_view(AppView::MainMenu)?;
self.change_view(AppView::MainMenu).await?;
}
_ => (),
}
Ok(())
}
fn hr_select(&mut self) -> Result<()> {
async fn hr_select(&mut self) -> Result<()> {
let has_hr_saved = self.settings.hr.saved.is_some();
let monitors_discovered = !self.ble.discovered.is_empty();

Expand Down Expand Up @@ -672,7 +672,7 @@ where
point,
kind: TouchKind::Start,
}) if BACK_BUTTON_BOUND.contains(*point) => {
self.change_view(AppView::MainMenu)?;
self.change_view(AppView::MainMenu).await?;
return Ok(());
}
Some(TouchEvent {
Expand Down Expand Up @@ -704,14 +704,14 @@ where
info!("Saving {device}!");
self.settings.hr.saved = Some(device);
self.settings.littlefs_save()?;
self.change_view(AppView::MainMenu)?;
self.change_view(AppView::MainMenu).await?;
return Ok(());
}
Some(TouchEvent {
point,
kind: TouchKind::Start,
}) if RESCAN_BUTTON_BOUND.contains(*point) => {
self.change_view(AppView::HrSelect)?;
self.change_view(AppView::HrSelect).await?;
return Ok(());
}
Some(TouchEvent {
Expand Down Expand Up @@ -746,25 +746,25 @@ where

Ok(())
}
pub fn main_loop(&mut self) -> Result<()> {
pub async fn main_loop(&mut self) -> Result<()> {
match self.view {
AppView::Doodle => {
self.doodle()?;
self.doodle().await?;
}
AppView::MainMenu => {
self.main_menu()?;
self.main_menu().await?;
}
AppView::NameInput => {
self.name_input()?;
self.name_input().await?;
}
AppView::HrSelect => {
self.hr_select()?;
self.hr_select().await?;
}
_ => (),
}
Ok(())
}
fn change_view(&mut self, new_view: AppView) -> Result<()> {
async fn change_view(&mut self, new_view: AppView) -> Result<()> {
self.repaint_full()?;
self.view = new_view;
self.debounce_instant = Instant::now();
Expand All @@ -791,7 +791,8 @@ where
)
.draw(&mut self.display)?;

self.ble.discovered = block_on(async { self.ble.scan_for_select().await })?;
// self.ble.discovered = block_on(async { self.ble.scan_for_select().await })?;
self.ble.discovered = self.ble.scan_for_select().await?;

info!("{:?}", self.ble.discovered);

Expand All @@ -807,7 +808,8 @@ where
}
AppView::BadgeDisplay => {
if let Some(addr) = self.settings.hr.saved.as_ref() {
block_on(async { self.ble.connect_to_monitor(addr.mac).await })?;
// block_on(async { self.ble.connect_to_monitor(addr.mac).await })?;
self.ble.connect_to_monitor(addr.mac).await?;
}
info!("Done.");
}
Expand Down
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
use app::App;
use display_interface_spi::SPIInterface;
use embedded_graphics::{pixelcolor::Rgb565, prelude::*};
use errors::AppError;
use esp_idf_hal::{
delay::{Delay, FreeRtos},
gpio::PinDriver,
prelude::*,
spi::{SpiDeviceDriver, SpiDriver, SpiDriverConfig, SPI2},
task::block_on,
timer::{TimerConfig, TimerDriver},
// units::*,
};
use esp_idf_svc::fs::fatfs::Fatfs;
Expand Down Expand Up @@ -265,10 +268,19 @@ fn main() -> Result<()> {

let mut app = App::build(touch_rx, display)?;

loop {
delay.delay_ms(10);
app.main_loop()?;
}
let mut timer = TimerDriver::new(peripherals.timer00, &TimerConfig::new())?;
let err = block_on(async {
loop {
if let Err(e) = app.main_loop().await {
break e;
}
timer.delay(timer.tick_hz() / 100).await.unwrap();
}
});

error!("{err}");

panic!()

// Ok(())
}

0 comments on commit 29f77bf

Please sign in to comment.