mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-04-30 18:49:09 +00:00
Compare commits
2 Commits
d96b3ed11a
...
b551f4521f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b551f4521f | ||
|
|
adcbe87bd7 |
@@ -1,18 +1,18 @@
|
||||
use embassy_time::{Delay, Duration, Timer};
|
||||
use esp_hal::{delay, gpio::Output, peripherals, rmt::ConstChannelAccess};
|
||||
use esp_hal_smartled::SmartLedsAdapterAsync;
|
||||
use log::{debug, error, info};
|
||||
use init::hardware;
|
||||
use log::{debug, error, info};
|
||||
use smart_leds::SmartLedsWriteAsync;
|
||||
use smart_leds::colors::{BLACK, GREEN, RED, YELLOW};
|
||||
use smart_leds::{brightness, colors::BLUE};
|
||||
use smart_leds::SmartLedsWriteAsync;
|
||||
|
||||
use crate::{FEEDBACK_STATE, init};
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum FeedbackState {
|
||||
Ack,
|
||||
Nak,
|
||||
Nack,
|
||||
Error,
|
||||
Startup,
|
||||
WIFI,
|
||||
@@ -24,21 +24,37 @@ const LED_LEVEL: u8 = 255;
|
||||
//TODO ERROR STATE: 1 Blink = unknows error, 3 Blink = no sd card
|
||||
|
||||
#[embassy_executor::task]
|
||||
pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, { init::hardware::LED_BUFFER_SIZE }>, buzzer: peripherals::GPIO21<'static>) {
|
||||
pub async fn feedback_task(
|
||||
mut led: SmartLedsAdapterAsync<
|
||||
ConstChannelAccess<esp_hal::rmt::Tx, 0>,
|
||||
{ init::hardware::LED_BUFFER_SIZE },
|
||||
>,
|
||||
buzzer: peripherals::GPIO21<'static>,
|
||||
) {
|
||||
debug!("Starting feedback task");
|
||||
let mut buzzer = init::hardware::setup_buzzer(buzzer);
|
||||
loop {
|
||||
let feedback_state = FEEDBACK_STATE.wait().await;
|
||||
match feedback_state {
|
||||
FeedbackState::Ack => {
|
||||
led.write(brightness([GREEN; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
led.write(brightness(
|
||||
[GREEN; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
buzzer.set_high();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
buzzer.set_low();
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
}
|
||||
FeedbackState::Nak => {
|
||||
led.write(brightness([YELLOW; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
FeedbackState::Nack => {
|
||||
led.write(brightness(
|
||||
[YELLOW; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
buzzer.set_high();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
buzzer.set_low();
|
||||
@@ -46,10 +62,20 @@ pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp
|
||||
buzzer.set_high();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
buzzer.set_low();
|
||||
led.write(brightness([BLACK; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
led.write(brightness(
|
||||
[BLACK; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
FeedbackState::Error => {
|
||||
led.write(brightness([RED; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
led.write(brightness(
|
||||
[RED; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
buzzer.set_high();
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
buzzer.set_low();
|
||||
@@ -59,7 +85,12 @@ pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp
|
||||
buzzer.set_low();
|
||||
}
|
||||
FeedbackState::Startup => {
|
||||
led.write(brightness([GREEN; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
led.write(brightness(
|
||||
[GREEN; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
buzzer.set_high();
|
||||
Timer::after(Duration::from_millis(10)).await;
|
||||
buzzer.set_low();
|
||||
@@ -71,20 +102,34 @@ pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp
|
||||
buzzer.set_high();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
buzzer.set_low();
|
||||
led.write(brightness([BLACK; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
led.write(brightness(
|
||||
[BLACK; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
FeedbackState::WIFI => {
|
||||
led.write(brightness([BLUE; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
|
||||
led.write(brightness(
|
||||
[BLUE; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
FeedbackState::Idle => {
|
||||
// Do nothing
|
||||
led.write(brightness(
|
||||
[GREEN; init::hardware::NUM_LEDS].into_iter(),
|
||||
LED_LEVEL,
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
debug!("Feedback state: {:?}", feedback_state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// async fn beep_ack() {
|
||||
// buzzer.set_high();
|
||||
// buzzer.set_low();
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
use core::cell::RefCell;
|
||||
|
||||
use bleps::att::Att;
|
||||
use critical_section::Mutex;
|
||||
use ds3231::InterruptControl;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::Stack;
|
||||
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_hal::gpio::{Input, InputConfig};
|
||||
use esp_hal::i2c::master::Config;
|
||||
use esp_hal::peripherals::{
|
||||
GPIO0, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2,
|
||||
@@ -27,6 +32,7 @@ use esp_println::dbg;
|
||||
use esp_println::logger::init_logger;
|
||||
use log::{debug, error, info};
|
||||
|
||||
use crate::FEEDBACK_STATE;
|
||||
use crate::init::network;
|
||||
use crate::init::sd_card::setup_sdcard;
|
||||
use crate::init::wifi;
|
||||
@@ -53,6 +59,8 @@ use crate::store::persistence::Persistence;
|
||||
pub const NUM_LEDS: usize = 66;
|
||||
pub const LED_BUFFER_SIZE: usize = NUM_LEDS * 25;
|
||||
|
||||
static SD_DET: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
loop {
|
||||
@@ -70,6 +78,7 @@ pub async fn hardware_init(
|
||||
I2c<'static, Async>,
|
||||
SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE>,
|
||||
GPIO21<'static>,
|
||||
GPIO0<'static>,
|
||||
) {
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
@@ -90,12 +99,13 @@ pub async fn hardware_init(
|
||||
let stack = network::setup_network(network_seed, interfaces.ap, spawner);
|
||||
|
||||
Timer::after(Duration::from_millis(1)).await;
|
||||
init_lvl_shifter(peripherals.GPIO0);
|
||||
|
||||
let uart_device = setup_uart(peripherals.UART1, peripherals.GPIO16, peripherals.GPIO17);
|
||||
|
||||
let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23);
|
||||
|
||||
let mut sd_det_gpio = peripherals.GPIO0;
|
||||
|
||||
let spi_bus = setup_spi(
|
||||
peripherals.SPI2,
|
||||
peripherals.GPIO19,
|
||||
@@ -119,19 +129,14 @@ pub async fn hardware_init(
|
||||
|
||||
debug!("hardware init done");
|
||||
|
||||
(uart_device, stack, i2c_device, led, buzzer_gpio)
|
||||
}
|
||||
|
||||
// Initialize the level shifter for the NFC reader and LED (output-enable (OE) input is low, all outputs are placed in the high-impedance (Hi-Z) state)
|
||||
fn init_lvl_shifter(oe_pin: GPIO0<'static>) {
|
||||
let mut oe_lvl_shifter = Output::new(
|
||||
oe_pin,
|
||||
esp_hal::gpio::Level::Low,
|
||||
OutputConfig::default()
|
||||
.with_drive_mode(esp_hal::gpio::DriveMode::PushPull)
|
||||
.with_drive_strength(esp_hal::gpio::DriveStrength::_10mA),
|
||||
);
|
||||
oe_lvl_shifter.set_high();
|
||||
(
|
||||
uart_device,
|
||||
stack,
|
||||
i2c_device,
|
||||
led,
|
||||
buzzer_gpio,
|
||||
sd_det_gpio,
|
||||
)
|
||||
}
|
||||
|
||||
fn setup_uart(
|
||||
|
||||
39
src/main.rs
39
src/main.rs
@@ -6,16 +6,21 @@
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::Stack;
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}, channel::Channel, pubsub::{
|
||||
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
|
||||
channel::Channel,
|
||||
pubsub::{
|
||||
PubSubChannel, Publisher,
|
||||
WaitResult::{Lagged, Message},
|
||||
}, signal::Signal
|
||||
},
|
||||
signal::Signal,
|
||||
};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use esp_hal::gpio::Input;
|
||||
use esp_hal::{gpio::InputConfig, peripherals};
|
||||
use log::{debug, info};
|
||||
use static_cell::make_static;
|
||||
|
||||
use crate::{store::TallyID};
|
||||
use crate::store::TallyID;
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
@@ -32,7 +37,7 @@ type TallyPublisher = Publisher<'static, NoopRawMutex, TallyID, 8, 2, 1>;
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(mut spawner: Spawner) {
|
||||
let (uart_device, stack, _i2c, _led, buzzer_gpio) =
|
||||
let (uart_device, stack, _i2c, _led, buzzer_gpio, sd_det_gpio) =
|
||||
init::hardware::hardware_init(&mut spawner).await;
|
||||
|
||||
wait_for_stack_up(stack).await;
|
||||
@@ -56,6 +61,9 @@ async fn main(mut spawner: Spawner) {
|
||||
|
||||
debug!("spawing feedback task..");
|
||||
spawner.must_spawn(feedback::feedback_task(_led, buzzer_gpio));
|
||||
|
||||
debug!("spawn sd detect task");
|
||||
spawner.must_spawn(sd_detect_task(sd_det_gpio));
|
||||
/******************************************************************************/
|
||||
|
||||
let mut sub = chan.subscriber().unwrap();
|
||||
@@ -76,6 +84,29 @@ async fn main(mut spawner: Spawner) {
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn sd_detect_task(sd_det_gpio: peripherals::GPIO0<'static>) {
|
||||
let mut sd_det = Input::new(sd_det_gpio, InputConfig::default());
|
||||
sd_det.wait_for(esp_hal::gpio::Event::AnyEdge);
|
||||
|
||||
loop {
|
||||
sd_det.wait_for_any_edge().await;
|
||||
{
|
||||
if sd_det.is_high() {
|
||||
FEEDBACK_STATE.signal(feedback::FeedbackState::Ack);
|
||||
debug!("card insert");
|
||||
}
|
||||
//card is not insert on low
|
||||
else {
|
||||
FEEDBACK_STATE.signal(feedback::FeedbackState::Nack);
|
||||
debug!("card removed");
|
||||
}
|
||||
}
|
||||
//debounce time
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_stack_up(stack: Stack<'static>) {
|
||||
loop {
|
||||
if stack.is_link_up() {
|
||||
|
||||
Reference in New Issue
Block a user