code/src/main.rs

96 lines
2.4 KiB
Rust
Raw Normal View History

2023-07-06 21:55:56 +00:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
2023-07-14 13:21:19 +00:00
use embassy_executor::Executor;
use embassy_rp::gpio::{Level, Output};
use embassy_rp::multicore::{spawn_core1, Stack};
use embassy_rp::peripherals::PIN_25;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use static_cell::StaticCell;
2023-07-06 21:55:56 +00:00
use {defmt_rtt as _, panic_probe as _};
2023-07-14 14:30:54 +00:00
const BUF_SIZE: usize = 64;
2023-07-14 13:21:19 +00:00
static mut CORE1_STACK: Stack<4096> = Stack::new();
static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
2023-07-14 14:30:54 +00:00
static CHANNEL: Channel<CriticalSectionRawMutex, Buffer, 1> = Channel::new();
2023-07-06 21:55:56 +00:00
2023-07-14 16:10:21 +00:00
enum State {
Happy,
Sad,
Relaxed,
Surprised,
Unknown,
}
2023-07-14 14:30:54 +00:00
#[derive(Clone)]
struct Buffer([f32; BUF_SIZE]);
impl Default for Buffer {
fn default() -> Self {
Buffer([0.0; BUF_SIZE])
}
2023-07-14 13:21:19 +00:00
}
2023-07-06 21:55:56 +00:00
2023-07-14 13:21:19 +00:00
#[cortex_m_rt::entry]
fn main() -> ! {
let p = embassy_rp::init(Default::default());
let led = Output::new(p.PIN_25, Level::Low);
2023-07-06 21:55:56 +00:00
2023-07-14 13:21:19 +00:00
spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(led))));
});
2023-07-06 21:55:56 +00:00
2023-07-14 13:21:19 +00:00
let executor0 = EXECUTOR0.init(Executor::new());
executor0.run(|spawner| unwrap!(spawner.spawn(core0_task())));
}
2023-07-06 21:55:56 +00:00
2023-07-14 13:21:19 +00:00
#[embassy_executor::task]
async fn core0_task() {
2023-07-14 14:30:54 +00:00
use nanorand::{Rng, WyRand};
let mut rng = WyRand::new();
let mut buf = Buffer([0.0; BUF_SIZE]);
let mut count = 0;
2023-07-14 13:21:19 +00:00
info!("Hello from core 0");
2023-07-14 16:10:21 +00:00
// Sample EEG data, then append it to buffer
// When full, send buffer to be added to the queue
2023-07-14 13:21:19 +00:00
loop {
2023-07-14 14:30:54 +00:00
if count >= BUF_SIZE {
CHANNEL.send(buf.clone()).await;
count = 0;
} else {
buf.0[count] = rng.generate::<f32>();
count += 1;
}
2023-07-14 13:21:19 +00:00
}
}
#[embassy_executor::task]
async fn core1_task(mut led: Output<'static, PIN_25>) {
2023-07-14 14:30:54 +00:00
use tinyvec::ArrayVec;
let mut queue = ArrayVec::<[Buffer; 64]>::new();
2023-07-14 13:21:19 +00:00
info!("Hello from core 1");
loop {
2023-07-14 16:10:21 +00:00
// Shouldn't block on waiting for message?
// Need to test
2023-07-14 14:39:10 +00:00
queue.push(CHANNEL.recv().await);
process_data(&queue[0]).await;
2023-07-14 16:10:21 +00:00
queue.remove(0);
2023-07-06 21:55:56 +00:00
}
}
2023-07-14 14:39:10 +00:00
2023-07-14 16:10:21 +00:00
async fn process_data(buf: &Buffer) -> State {
2023-07-14 14:39:10 +00:00
// Low-pass filter
// FFT (w/ hanning window)
2023-07-14 16:10:21 +00:00
info!("Running FFT...");
return State::Unknown;
2023-07-14 14:39:10 +00:00
}