code/src/main.rs

118 lines
3.1 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;
2023-07-14 16:52:38 +00:00
use embassy_rp::adc::{Adc, Config, InterruptHandler, Pin};
use embassy_rp::bind_interrupts;
use embassy_rp::gpio::Pull;
2023-07-14 13:21:19 +00:00
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 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();
static BUF_CHANNEL: Channel<CriticalSectionRawMutex, Buffer, 256> = Channel::new();
2023-07-14 16:52:38 +00:00
bind_interrupts!(struct Irqs {
ADC_IRQ_FIFO => InterruptHandler;
});
const BUF_SIZE: usize = 256;
2023-07-06 21:55:56 +00:00
2023-07-14 16:52:38 +00:00
#[derive(Format)]
2023-07-14 16:10:21 +00:00
enum State {
Happy,
Sad,
Relaxed,
Surprised,
2023-07-14 16:52:38 +00:00
}
// Position in degrees for each servo
#[derive(Format)]
struct ServoPosition(f32, f32, f32, f32);
impl State {
fn servo_positions(&self) -> ServoPosition {
// This is where we define the positions and pose for each state
match self {
_ => ServoPosition(0.0, 0.0, 0.0, 0.0),
}
}
2023-07-14 16:10:21 +00:00
}
2023-07-14 14:30:54 +00:00
#[derive(Clone)]
2023-07-14 16:52:38 +00:00
struct Buffer([u16; BUF_SIZE]);
2023-07-14 14:30:54 +00:00
impl Default for Buffer {
fn default() -> Self {
2023-07-14 16:52:38 +00:00
Buffer([0; BUF_SIZE])
2023-07-14 14:30:54 +00:00
}
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());
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(process_data())));
2023-07-14 13:21:19 +00:00
});
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(read_eeg())));
2023-07-14 13:21:19 +00:00
}
2023-07-06 21:55:56 +00:00
2023-07-14 13:21:19 +00:00
#[embassy_executor::task]
async fn read_eeg() {
2023-07-14 16:52:38 +00:00
let p = embassy_rp::init(Default::default());
let mut adc = Adc::new(p.ADC, Irqs, Config::default());
let mut eeg = Pin::new(p.PIN_26, Pull::None);
2023-07-14 14:30:54 +00:00
use nanorand::{Rng, WyRand};
let mut rng = WyRand::new();
2023-07-14 16:52:38 +00:00
let mut buf = Buffer([0; BUF_SIZE]);
2023-07-14 14:30:54 +00:00
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 {
BUF_CHANNEL.send(buf.clone()).await;
2023-07-14 14:30:54 +00:00
count = 0;
} else {
2023-07-14 16:52:38 +00:00
buf.0[count] = adc.read(&mut eeg).await.unwrap();
2023-07-14 14:30:54 +00:00
count += 1;
}
2023-07-14 13:21:19 +00:00
}
}
#[embassy_executor::task]
async fn process_data() {
2023-07-14 13:21:19 +00:00
loop {
let mut buffer = BUF_CHANNEL.recv().await;
// Low-pass filter
// FFT (w/ hanning window)
info!("Running FFT...");
2023-07-06 21:55:56 +00:00
}
}
2023-07-14 14:39:10 +00:00
// async fn set_servo_position(state: State) {
// // Use a map of positions for each state, and move the servos towards it
// // Use lerp and randomness
// // I2C control board manages servos
// info!("Setting position to {}", state.servo_positions());
// }
2023-07-20 16:05:19 +00:00
#[inline]
fn lerp(v0: f32, v1: f32, t: f32) -> f32 {
return (1. - t) * v0 + t * v1;
}