Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
282bcd163c
|
||
|
|
92c20d61cb |
@@ -1,4 +1,6 @@
|
||||
use crate::storage::cardstore::CardStore;
|
||||
use embedded_hal_bus::i2c::AtomicDevice;
|
||||
use esp_hal::rtc_cntl::Rtc;
|
||||
use esp_hal::{Blocking, i2c::master::I2c, time::Instant};
|
||||
|
||||
use crate::{
|
||||
@@ -14,6 +16,8 @@ use crate::{
|
||||
)]
|
||||
pub async fn nfc_scanner(
|
||||
mut nfc_driver: NfcPn532Driver<AtomicDevice<'static, I2c<'static, Blocking>>>,
|
||||
mut card_store: CardStore,
|
||||
rtc: Rtc<'static>,
|
||||
) {
|
||||
loop {
|
||||
while CARD_DATA.lock().await.is_none() {
|
||||
@@ -24,10 +28,22 @@ pub async fn nfc_scanner(
|
||||
log::error!("Failed to split NFC card data");
|
||||
continue;
|
||||
};
|
||||
CARD_DATA
|
||||
.lock()
|
||||
.await
|
||||
.replace(Card::try_from(split_data).unwrap());
|
||||
let card = match Card::try_from(split_data) {
|
||||
Ok(card) => card,
|
||||
Err(err) => {
|
||||
log::error!("Invalid card data: {err}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
CARD_DATA.lock().await.replace(card);
|
||||
|
||||
match card_store.save_raw_card(split_data, rtc.current_time_us()) {
|
||||
Ok(()) => {}
|
||||
Err(error) => {
|
||||
log::error!("Error storing card in flash: {:?}", error)
|
||||
}
|
||||
}
|
||||
|
||||
let elapsed_ms = start.elapsed().as_millis();
|
||||
log::info!(
|
||||
"NFC read duration: {} ms, card data length: {}",
|
||||
@@ -35,8 +51,8 @@ pub async fn nfc_scanner(
|
||||
card_data.len()
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("NFC read failed: {e}");
|
||||
Err(_) => {
|
||||
log::info!("No NFC card found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+36
-13
@@ -18,11 +18,9 @@ use creaturedex::drivers::tertiary_lcd::{
|
||||
use creaturedex::navigation::inputs::Inputs;
|
||||
use creaturedex::navigation::navigation::{self};
|
||||
use creaturedex::navigation::outputs::Outputs;
|
||||
use creaturedex::network::wifi::setup_wifi;
|
||||
use creaturedex::storage::cardstore::CardStore;
|
||||
use creaturedex::storage::store::Store;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::Instant;
|
||||
use embedded_graphics::pixelcolor::BinaryColor;
|
||||
use embedded_graphics::{
|
||||
mono_font::{MonoTextStyle, ascii::FONT_6X10},
|
||||
@@ -30,13 +28,13 @@ use embedded_graphics::{
|
||||
text::Text,
|
||||
};
|
||||
use embedded_hal_bus::i2c::AtomicDevice;
|
||||
use embedded_hal_bus::util::AtomicCell;
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::gpio::{Input, InputConfig, Pull};
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use log::error;
|
||||
|
||||
use esp_hal::rtc_cntl::Rtc;
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
|
||||
error!("{}", panic_info);
|
||||
@@ -69,7 +67,9 @@ async fn main(spawner: Spawner) {
|
||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
|
||||
|
||||
let (primary, mut secondaries) = spi_bus::DisplayPinConfiguration {
|
||||
let (primary, mut secondaries) = retry(
|
||||
5,
|
||||
spi_bus::DisplayPinConfiguration {
|
||||
spi_peripheral: peripherals.SPI2,
|
||||
sck: peripherals.GPIO36,
|
||||
mosi: peripherals.GPIO35,
|
||||
@@ -81,20 +81,26 @@ async fn main(spawner: Spawner) {
|
||||
reset_primary: peripherals.GPIO10,
|
||||
reset_secondary: peripherals.GPIO47,
|
||||
dc_pin: peripherals.GPIO12,
|
||||
}
|
||||
.build();
|
||||
},
|
||||
spi_bus::DisplayPinConfiguration::build,
|
||||
);
|
||||
|
||||
// Initialize the shared I2C bus using the refactored i2c_bus module.
|
||||
let shared_i2c = {
|
||||
let config = I2cBusPinConfiguration {
|
||||
use alloc::boxed::Box;
|
||||
let bus = retry(5, I2cBusPinConfiguration {
|
||||
i2c_peripheral: peripherals.I2C0,
|
||||
scl: peripherals.GPIO18,
|
||||
sda: peripherals.GPIO17,
|
||||
};
|
||||
config.build()
|
||||
}, I2cBusPinConfiguration::build);
|
||||
Box::leak(Box::new(bus))
|
||||
};
|
||||
|
||||
let mut lcd: TertiaryDisplay = TertiaryDisplayPinConfiguration { i2c: shared_i2c }.build();
|
||||
let mut lcd: TertiaryDisplay = retry(
|
||||
5,
|
||||
TertiaryDisplayPinConfiguration { i2c: shared_i2c },
|
||||
TertiaryDisplayPinConfiguration::build,
|
||||
);
|
||||
lcd.load_charset(EXAMPLE).unwrap();
|
||||
let h = EXAMPLE[HEART];
|
||||
let e = EXAMPLE[EMPTY];
|
||||
@@ -108,7 +114,7 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
|
||||
let store = Store::new(peripherals.FLASH).expect("Could not initialize Flash Store");
|
||||
let card_store = CardStore::new(store).expect("Could not initialize Card Store");
|
||||
let cardstore = CardStore::new(store).expect("Could not initialize Card Store");
|
||||
|
||||
let rtc = Rtc::new(peripherals.LPWR);
|
||||
//setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await;
|
||||
@@ -148,7 +154,10 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
log::info!("Setup complete, entering main loop");
|
||||
spawner.spawn(navigation::run(inputs, outputs).expect("run task failed"));
|
||||
spawner.spawn(background_tasks::nfc_scanner(nfc_driver).expect("nfc scanner task failed"));
|
||||
spawner.spawn(
|
||||
background_tasks::nfc_scanner(nfc_driver, cardstore, rtc).expect("nfc scanner task failed"),
|
||||
);
|
||||
core::future::pending::<()>().await
|
||||
}
|
||||
|
||||
fn display_shit(oled: &mut SecondaryDisplay<'static>, text: &str) {
|
||||
@@ -160,3 +169,17 @@ fn display_shit(oled: &mut SecondaryDisplay<'static>, text: &str) {
|
||||
.unwrap();
|
||||
oled.flush().unwrap();
|
||||
}
|
||||
|
||||
fn retry<T, U, F: FnMut(T) -> Result<U, T>>(repetitions: u32, input: T, mut action: F) -> U {
|
||||
let mut initial_state = input;
|
||||
for iteration in 0..repetitions {
|
||||
log::debug!("Trying to initialize. Retries: {iteration}");
|
||||
|
||||
match action(initial_state) {
|
||||
Ok(success) => return success,
|
||||
Err(recovered_input) => initial_state = recovered_input,
|
||||
}
|
||||
}
|
||||
|
||||
panic!("Init failed after {repetitions} retries.")
|
||||
}
|
||||
|
||||
+11
-11
@@ -1,4 +1,3 @@
|
||||
use alloc::boxed::Box;
|
||||
use embedded_hal_bus::i2c::AtomicDevice;
|
||||
use embedded_hal_bus::util::AtomicCell;
|
||||
use esp_hal::gpio::interconnect::PeripheralInput;
|
||||
@@ -21,18 +20,19 @@ where
|
||||
SCL: PeripheralOutput<'a> + PeripheralInput<'a>,
|
||||
SDA: PeripheralOutput<'a> + PeripheralInput<'a>,
|
||||
{
|
||||
pub fn build(self) -> I2cBus<'a> {
|
||||
pub fn build(self) -> Result<AtomicCell<InnerI2cBus<'a>>, Self> {
|
||||
let config = Config::default().with_frequency(esp_hal::time::Rate::from_khz(400));
|
||||
let i2c = match I2c::new(self.i2c_peripheral, config) {
|
||||
Ok(bus) => bus.with_sda(self.sda).with_scl(self.scl),
|
||||
match I2c::new(self.i2c_peripheral, config) {
|
||||
Ok(bus) => {
|
||||
let i2c = bus.with_sda(self.sda).with_scl(self.scl);
|
||||
log::info!("Shared I2C bus ready on GPIO17 (SDA) and GPIO18 (SCL)");
|
||||
Ok(AtomicCell::new(i2c))
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Shared I2C bus initialization failed for GPIO17/GPIO18: {e:?}");
|
||||
panic!("Shared I2C bus initialization failed");
|
||||
}
|
||||
};
|
||||
let shared_i2c = Box::leak(Box::new(AtomicCell::new(i2c)));
|
||||
log::info!("Shared I2C bus ready on GPIO17 (SDA) and GPIO18 (SCL)");
|
||||
|
||||
shared_i2c
|
||||
unimplemented!("Partial move makes it impossible to retry.");
|
||||
// Err(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ where
|
||||
RES2: OutputPin + 'static,
|
||||
DC: OutputPin + 'static,
|
||||
{
|
||||
pub fn build(self) -> (PrimaryDisplay<'static>, [SecondaryDisplay<'static>; 3]) {
|
||||
pub fn build(self) -> Result<(PrimaryDisplay<'static>, [SecondaryDisplay<'static>; 3]), Self> {
|
||||
let spi = Spi::new(
|
||||
self.spi_peripheral,
|
||||
SpiConfig::default()
|
||||
@@ -178,7 +178,7 @@ where
|
||||
}
|
||||
.build();
|
||||
|
||||
(primary, secondaries)
|
||||
Ok((primary, secondaries))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,19 +15,20 @@ pub struct TertiaryDisplayPinConfiguration<'a> {
|
||||
}
|
||||
|
||||
impl<'a> TertiaryDisplayPinConfiguration<'a> {
|
||||
pub fn build(self) -> TertiaryDisplay<'a> {
|
||||
pub fn build(self) -> Result<TertiaryDisplay<'a>, Self> {
|
||||
let device = AtomicDevice::new(self.i2c);
|
||||
let mut lcd = CharacterDisplayPCF8574T::new(device, LcdDisplayType::Lcd16x2, Delay::new());
|
||||
match lcd.init() {
|
||||
Ok(()) => {
|
||||
log::info!("I2C tertiary LCD initialized on PCF8574T at address 0x27");
|
||||
TertiaryDisplay {
|
||||
Ok(TertiaryDisplay {
|
||||
display: lcd,
|
||||
charset: create_custom_char_set!(),
|
||||
}
|
||||
})
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("I2C tertiary LCD init failed on PCF8574T at address 0x27: {e}");
|
||||
log::error!("I2C tertiary LCD init failed on PCF8574T at address 0x27: {e}");
|
||||
Err(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -407,4 +408,3 @@ pub const BOX: CharMap = [
|
||||
];
|
||||
|
||||
pub const EXAMPLE: CustomCharset = create_custom_char_set!(HEART, BOX, EMPTY);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod card_view;
|
||||
pub mod flash_info_view;
|
||||
pub mod journal_view;
|
||||
pub mod main_menu;
|
||||
pub mod menu_item;
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
use crate::navigation::inputs::ButtonAction;
|
||||
use crate::navigation::navigation::Action;
|
||||
use crate::navigation::navigation::Navigable;
|
||||
use crate::navigation::navigation::NewState;
|
||||
use crate::navigation::outputs::Outputs;
|
||||
use crate::views::view::View;
|
||||
use embedded_graphics::mono_font::MonoTextStyle;
|
||||
use embedded_graphics::mono_font::ascii::FONT_10X20;
|
||||
use embedded_graphics::pixelcolor::Rgb565;
|
||||
use embedded_graphics::prelude::DrawTarget;
|
||||
use embedded_graphics::prelude::RgbColor;
|
||||
use embedded_graphics::text::Text;
|
||||
|
||||
use embedded_graphics::prelude::Point;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FlashInfoView {}
|
||||
|
||||
impl Navigable for FlashInfoView {
|
||||
fn display(&self, outputs: &mut Outputs) -> impl core::future::Future<Output = ()> + Send {
|
||||
let style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
|
||||
Text::new("Flash Initialized: {:?}", Point::new(40, 40), style);
|
||||
|
||||
core::future::ready(())
|
||||
}
|
||||
|
||||
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = NewState> + Send {
|
||||
async move {
|
||||
let Action::Button(input) = input else {
|
||||
// Skip timer inputs
|
||||
return NewState {
|
||||
view: View::FlashInfo(self.clone()),
|
||||
replace_view: true,
|
||||
redraw: false,
|
||||
};
|
||||
};
|
||||
|
||||
let new_menu = match input {
|
||||
_ => View::FlashInfo(self.clone()),
|
||||
};
|
||||
|
||||
NewState {
|
||||
replace_view: matches!(new_menu, View::FlashInfo(_)),
|
||||
view: new_menu,
|
||||
redraw: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
use crate::navigation::navigation::{Action, Navigable, NewState};
|
||||
use crate::navigation::outputs::Outputs;
|
||||
use crate::views::{menu_item, view::View};
|
||||
use crate::views::{flash_info_view::FlashInfoView, menu_item, view::View};
|
||||
|
||||
use crate::navigation::inputs::ButtonAction;
|
||||
|
||||
use embedded_graphics::pixelcolor::Rgb565;
|
||||
use embedded_graphics::prelude::RgbColor;
|
||||
use embedded_graphics_core::draw_target::DrawTarget;
|
||||
pub const MAX_SELECTED: i8 = 2;
|
||||
|
||||
pub const MAX_SELECTED: i32 = 2;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SettingsMenu {
|
||||
@@ -18,16 +21,50 @@ impl Navigable for SettingsMenu {
|
||||
display.clear(Rgb565::BLACK).unwrap();
|
||||
menu_item::show(display, "System Inforation", 0, self.selected);
|
||||
menu_item::show(display, "Wifi Information", 1, self.selected);
|
||||
menu_item::show(display, "Flash Information", 2, self.selected);
|
||||
|
||||
core::future::ready(())
|
||||
}
|
||||
|
||||
fn handle_input(&self, input: Action) -> impl core::future::Future<Output = NewState> + Send {
|
||||
async move {
|
||||
NewState {
|
||||
view: View::Settings(SettingsMenu { selected: 0 }),
|
||||
let Action::Button(input) = input else {
|
||||
// Skip timer inputs
|
||||
return NewState {
|
||||
view: View::Settings(self.clone()),
|
||||
replace_view: true,
|
||||
redraw: !matches!(input, Action::Timer),
|
||||
redraw: false,
|
||||
};
|
||||
};
|
||||
|
||||
let new_menu = match input {
|
||||
ButtonAction::Up => {
|
||||
let new_selected = if (self.selected - 1) < 0 {
|
||||
MAX_SELECTED - 1
|
||||
} else {
|
||||
self.selected - 1
|
||||
};
|
||||
View::Settings(SettingsMenu {
|
||||
selected: new_selected,
|
||||
})
|
||||
}
|
||||
ButtonAction::Down => {
|
||||
let new_selected = self.selected.wrapping_add(1) % MAX_SELECTED;
|
||||
View::Settings(SettingsMenu {
|
||||
selected: new_selected,
|
||||
})
|
||||
}
|
||||
ButtonAction::Ok => match self.selected {
|
||||
2 => View::FlashInfo(FlashInfoView {}),
|
||||
_ => View::Settings(self.clone()),
|
||||
},
|
||||
_ => View::Settings(self.clone()),
|
||||
};
|
||||
|
||||
NewState {
|
||||
replace_view: matches!(new_menu, View::Settings(_)),
|
||||
view: new_menu,
|
||||
redraw: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-2
@@ -1,8 +1,8 @@
|
||||
use crate::navigation::outputs::Outputs;
|
||||
use crate::views::card_view::CardView;
|
||||
use crate::views::{
|
||||
journal_view::JournalView, main_menu::MainMenu, scan_menu::ScanMenu,
|
||||
settings_menu::SettingsMenu,
|
||||
flash_info_view::FlashInfoView, journal_view::JournalView, main_menu::MainMenu,
|
||||
scan_menu::ScanMenu, settings_menu::SettingsMenu,
|
||||
};
|
||||
|
||||
use crate::navigation::navigation::{Navigable, NewState};
|
||||
@@ -14,6 +14,7 @@ pub enum View {
|
||||
Settings(SettingsMenu),
|
||||
Journal(JournalView),
|
||||
Card(CardView),
|
||||
FlashInfo(FlashInfoView),
|
||||
}
|
||||
|
||||
impl Navigable for View {
|
||||
@@ -25,6 +26,7 @@ impl Navigable for View {
|
||||
View::Settings(settings_menu) => settings_menu.display(outputs).await,
|
||||
View::Journal(journal_view) => journal_view.display(outputs).await,
|
||||
View::Card(card_view) => card_view.display(outputs).await,
|
||||
View::FlashInfo(flash_info_view) => flash_info_view.display(outputs).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +42,7 @@ impl Navigable for View {
|
||||
View::Settings(settings_menu) => settings_menu.handle_input(input).await,
|
||||
View::Journal(journal_view) => journal_view.handle_input(input).await,
|
||||
View::Card(card_view) => card_view.handle_input(input).await,
|
||||
View::FlashInfo(flash_info_view) => flash_info_view.handle_input(input).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user