mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-03 23:24:10 +00:00 
			
		
		
		
	moved LED and feedback to new lib
This commit is contained in:
		
							parent
							
								
									009f6cbb2e
								
							
						
					
					
						commit
						a0ed04a560
					
				
							
								
								
									
										188
									
								
								src/feedback.rs
									
									
									
									
									
								
							
							
						
						
									
										188
									
								
								src/feedback.rs
									
									
									
									
									
								
							@ -6,6 +6,7 @@ use smart_leds::SmartLedsWriteAsync;
 | 
			
		||||
use smart_leds::colors::{BLACK, GREEN, RED, YELLOW};
 | 
			
		||||
use smart_leds::{brightness, colors::BLUE};
 | 
			
		||||
 | 
			
		||||
use crate::init::hardware;
 | 
			
		||||
use crate::{FEEDBACK_STATE, init};
 | 
			
		||||
 | 
			
		||||
#[derive(Copy, Clone, Debug)]
 | 
			
		||||
@ -24,107 +25,104 @@ const LED_LEVEL: u8 = 255;
 | 
			
		||||
 | 
			
		||||
#[embassy_executor::task]
 | 
			
		||||
pub async fn feedback_task(
 | 
			
		||||
    // mut led: SmartLedsAdapterAsync<
 | 
			
		||||
    //     ConstChannelAccess<esp_hal::rmt::Tx, 0>,
 | 
			
		||||
    //     { init::hardware::LED_BUFFER_SIZE },
 | 
			
		||||
    // >,
 | 
			
		||||
    mut led: SmartLedsAdapterAsync<'static, { 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();
 | 
			
		||||
        //         buzzer.set_high();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(100)).await;
 | 
			
		||||
        //         buzzer.set_low();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(50)).await;
 | 
			
		||||
        //     }
 | 
			
		||||
        //     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();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(100)).await;
 | 
			
		||||
        //         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();
 | 
			
		||||
        //     }
 | 
			
		||||
        //     FeedbackState::Error => {
 | 
			
		||||
        //         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();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(500)).await;
 | 
			
		||||
        //         buzzer.set_high();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(500)).await;
 | 
			
		||||
        //         buzzer.set_low();
 | 
			
		||||
        //     }
 | 
			
		||||
        //     FeedbackState::Startup => {
 | 
			
		||||
        //         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();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(10)).await;
 | 
			
		||||
        //         buzzer.set_high();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(10)).await;
 | 
			
		||||
        //         buzzer.set_low();
 | 
			
		||||
        //         Timer::after(Duration::from_millis(50)).await;
 | 
			
		||||
        //         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();
 | 
			
		||||
        //     }
 | 
			
		||||
        //     FeedbackState::WIFI => {
 | 
			
		||||
        //         led.write(brightness(
 | 
			
		||||
        //             [BLUE; init::hardware::NUM_LEDS].into_iter(),
 | 
			
		||||
        //             LED_LEVEL,
 | 
			
		||||
        //         ))
 | 
			
		||||
        //         .await
 | 
			
		||||
        //         .unwrap();
 | 
			
		||||
        //     }
 | 
			
		||||
        //     FeedbackState::Idle => {
 | 
			
		||||
        //         led.write(brightness(
 | 
			
		||||
        //             [GREEN; init::hardware::NUM_LEDS].into_iter(),
 | 
			
		||||
        //             LED_LEVEL,
 | 
			
		||||
        //         ))
 | 
			
		||||
        //         .await
 | 
			
		||||
        //         .unwrap();
 | 
			
		||||
        //     }
 | 
			
		||||
        // };
 | 
			
		||||
        match feedback_state {
 | 
			
		||||
            FeedbackState::Ack => {
 | 
			
		||||
                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::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();
 | 
			
		||||
                Timer::after(Duration::from_millis(100)).await;
 | 
			
		||||
                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();
 | 
			
		||||
            }
 | 
			
		||||
            FeedbackState::Error => {
 | 
			
		||||
                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();
 | 
			
		||||
                Timer::after(Duration::from_millis(500)).await;
 | 
			
		||||
                buzzer.set_high();
 | 
			
		||||
                Timer::after(Duration::from_millis(500)).await;
 | 
			
		||||
                buzzer.set_low();
 | 
			
		||||
            }
 | 
			
		||||
            FeedbackState::Startup => {
 | 
			
		||||
                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();
 | 
			
		||||
                Timer::after(Duration::from_millis(10)).await;
 | 
			
		||||
                buzzer.set_high();
 | 
			
		||||
                Timer::after(Duration::from_millis(10)).await;
 | 
			
		||||
                buzzer.set_low();
 | 
			
		||||
                Timer::after(Duration::from_millis(50)).await;
 | 
			
		||||
                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();
 | 
			
		||||
            }
 | 
			
		||||
            FeedbackState::WIFI => {
 | 
			
		||||
                led.write(brightness(
 | 
			
		||||
                    [BLUE; init::hardware::NUM_LEDS].into_iter(),
 | 
			
		||||
                    LED_LEVEL,
 | 
			
		||||
                ))
 | 
			
		||||
                .await
 | 
			
		||||
                .unwrap();
 | 
			
		||||
            }
 | 
			
		||||
            FeedbackState::Idle => {
 | 
			
		||||
                led.write(brightness(
 | 
			
		||||
                    [GREEN; init::hardware::NUM_LEDS].into_iter(),
 | 
			
		||||
                    LED_LEVEL,
 | 
			
		||||
                ))
 | 
			
		||||
                .await
 | 
			
		||||
                .unwrap();
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
        debug!("Feedback state: {:?}", feedback_state);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,7 @@ use esp_hal::peripherals::{
 | 
			
		||||
    GPIO0, GPIO1, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, I2C0, RMT, SPI2,
 | 
			
		||||
    UART1,
 | 
			
		||||
};
 | 
			
		||||
use esp_hal::rmt::Rmt;
 | 
			
		||||
use esp_hal::spi::master::{Config as Spi_config, Spi};
 | 
			
		||||
use esp_hal::system::software_reset;
 | 
			
		||||
use esp_hal::time::Rate;
 | 
			
		||||
@ -23,6 +24,7 @@ use esp_hal::{
 | 
			
		||||
    timer::systimer::SystemTimer,
 | 
			
		||||
    uart::Uart,
 | 
			
		||||
};
 | 
			
		||||
use esp_hal_smartled::{SmartLedsAdapterAsync, buffer_size_async};
 | 
			
		||||
use esp_println::logger::init_logger;
 | 
			
		||||
use log::{debug, error};
 | 
			
		||||
 | 
			
		||||
@ -47,8 +49,8 @@ use crate::init::wifi;
 | 
			
		||||
 *
 | 
			
		||||
 *************************************************/
 | 
			
		||||
 | 
			
		||||
pub const NUM_LEDS: usize = 66;
 | 
			
		||||
pub const LED_BUFFER_SIZE: usize = NUM_LEDS * 25;
 | 
			
		||||
pub const NUM_LEDS: usize = 1;
 | 
			
		||||
pub const LED_BUFFER_SIZE: usize = buffer_size_async(NUM_LEDS);
 | 
			
		||||
 | 
			
		||||
static SD_DET: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
 | 
			
		||||
 | 
			
		||||
@ -70,6 +72,7 @@ pub async fn hardware_init(
 | 
			
		||||
    I2c<'static, Async>,
 | 
			
		||||
    GPIO21<'static>,
 | 
			
		||||
    GPIO0<'static>,
 | 
			
		||||
    SmartLedsAdapterAsync<'static, LED_BUFFER_SIZE>,
 | 
			
		||||
    SDCardPersistence,
 | 
			
		||||
) {
 | 
			
		||||
    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
 | 
			
		||||
@ -116,6 +119,8 @@ pub async fn hardware_init(
 | 
			
		||||
 | 
			
		||||
    let buzzer_gpio = peripherals.GPIO21;
 | 
			
		||||
 | 
			
		||||
    let led = setup_led(peripherals.RMT, peripherals.GPIO1);
 | 
			
		||||
 | 
			
		||||
    Timer::after(Duration::from_millis(500)).await;
 | 
			
		||||
 | 
			
		||||
    debug!("hardware init done");
 | 
			
		||||
@ -126,6 +131,7 @@ pub async fn hardware_init(
 | 
			
		||||
        i2c_device,
 | 
			
		||||
        buzzer_gpio,
 | 
			
		||||
        sd_det_gpio,
 | 
			
		||||
        led,
 | 
			
		||||
        vol_mgr,
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
@ -184,23 +190,19 @@ pub fn setup_buzzer(buzzer_gpio: GPIO21<'static>) -> Output<'static> {
 | 
			
		||||
    buzzer
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// fn setup_led(
 | 
			
		||||
//     rmt: RMT<'static>,
 | 
			
		||||
//     led_gpio: GPIO1<'static>,
 | 
			
		||||
// ) -> SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE> {
 | 
			
		||||
//     debug!("setup led");
 | 
			
		||||
//     let rmt: Rmt<'_, esp_hal::Async> = {
 | 
			
		||||
//         let frequency: Rate = Rate::from_mhz(80);
 | 
			
		||||
//         Rmt::new(rmt, frequency)
 | 
			
		||||
//     }
 | 
			
		||||
//     .expect("Failed to initialize RMT")
 | 
			
		||||
//     .into_async();
 | 
			
		||||
//
 | 
			
		||||
//     let rmt_channel = rmt.channel0;
 | 
			
		||||
//     let rmt_buffer = [0_u32; buffer_size_async(NUM_LEDS)];
 | 
			
		||||
//
 | 
			
		||||
//     let led: SmartLedsAdapterAsync<_, LED_BUFFER_SIZE> =
 | 
			
		||||
//         SmartLedsAdapterAsync::new(rmt_channel, led_gpio, rmt_buffer);
 | 
			
		||||
//
 | 
			
		||||
//     led
 | 
			
		||||
// }
 | 
			
		||||
fn setup_led<'a>(
 | 
			
		||||
    rmt: RMT<'a>,
 | 
			
		||||
    led_gpio: GPIO1<'a>,
 | 
			
		||||
) -> esp_hal_smartled::SmartLedsAdapterAsync<'a, LED_BUFFER_SIZE> {
 | 
			
		||||
    let rmt: Rmt<'_, esp_hal::Async> = {
 | 
			
		||||
        let frequency: Rate = Rate::from_mhz(80);
 | 
			
		||||
        Rmt::new(rmt, frequency)
 | 
			
		||||
    }
 | 
			
		||||
    .expect("Failed to initialize RMT")
 | 
			
		||||
    .into_async();
 | 
			
		||||
 | 
			
		||||
    let rmt_channel = rmt.channel0;
 | 
			
		||||
    let rmt_buffer = [esp_hal::rmt::PulseCode::default(); LED_BUFFER_SIZE];
 | 
			
		||||
 | 
			
		||||
    SmartLedsAdapterAsync::new(rmt_channel, led_gpio, rmt_buffer)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -47,7 +47,7 @@ static CHAN: StaticCell<TallyChannel> = StaticCell::new();
 | 
			
		||||
 | 
			
		||||
#[esp_rtos::main]
 | 
			
		||||
async fn main(spawner: Spawner) -> ! {
 | 
			
		||||
    let (uart_device, stack, i2c, buzzer_gpio, sd_det_gpio, persistence_layer) =
 | 
			
		||||
    let (uart_device, stack, i2c, buzzer_gpio, sd_det_gpio, led, persistence_layer) =
 | 
			
		||||
        init::hardware::hardware_init(spawner).await;
 | 
			
		||||
 | 
			
		||||
    info!("Starting up...");
 | 
			
		||||
@ -72,8 +72,8 @@ async fn main(spawner: Spawner) -> ! {
 | 
			
		||||
        publisher,
 | 
			
		||||
    ));
 | 
			
		||||
 | 
			
		||||
    // debug!("spawing feedback task..");
 | 
			
		||||
    // spawner.must_spawn(feedback::feedback_task(led, buzzer_gpio));
 | 
			
		||||
    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));
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user