1
Fork 0

front panel write data

This commit is contained in:
Andy Killorin 2025-01-18 15:04:34 -05:00
parent bd7b3fb287
commit f7ab1c119a
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
3 changed files with 22 additions and 5 deletions

1
interface/Cargo.lock generated
View file

@ -393,6 +393,7 @@ dependencies = [
"anyhow", "anyhow",
"common", "common",
"cpal", "cpal",
"heapless",
"pitch-detection", "pitch-detection",
"postcard", "postcard",
"rust-music-theory", "rust-music-theory",

View file

@ -12,3 +12,4 @@ rust-music-theory = {git = "https://github.com/the-drunk-coder/rust-music-theory
serde = "1.0.217" serde = "1.0.217"
tokio = { version = "1.43.0", features = ["full"] } tokio = { version = "1.43.0", features = ["full"] }
common = {path = "../common"} common = {path = "../common"}
heapless = "0.7.0"

View file

@ -2,11 +2,11 @@
use std::{thread::sleep, time::Duration}; use std::{thread::sleep, time::Duration};
use anyhow::{Context, Ok, Result}; use anyhow::{Context, Ok, Result};
use common::TelemetryPacket; use common::{ControlPacket, TelemetryPacket};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use pitch_detection::{detector::{mcleod::McLeodDetector, PitchDetector}, utils}; use pitch_detection::{detector::{mcleod::McLeodDetector, PitchDetector}, utils};
use rust_music_theory::note::{Note, NoteLetter, Pitch, Tuning}; use rust_music_theory::note::{Note, NoteLetter, Pitch, Tuning};
use tokio::{io::{AsyncReadExt, WriteHalf}, net::{tcp::{OwnedReadHalf, OwnedWriteHalf}, TcpStream}, sync::mpsc}; use tokio::{io::{AsyncReadExt, AsyncWriteExt, BufWriter, WriteHalf}, net::{tcp::{OwnedReadHalf, OwnedWriteHalf}, TcpStream}, sync::mpsc};
fn main() -> Result<()> { fn main() -> Result<()> {
// assumes pulseaudio system with f32 samples and 2204 sample packets // assumes pulseaudio system with f32 samples and 2204 sample packets
@ -75,8 +75,11 @@ async fn telemetry_handler(mut telem: OwnedReadHalf) -> Result<()> {
} }
} }
async fn controller(mut notes: mpsc::Receiver<(Option<pitch_detection::Pitch<f32>>, f32)>, control: OwnedWriteHalf) -> Result<()> { async fn controller(mut notes: mpsc::Receiver<(Option<pitch_detection::Pitch<f32>>, f32)>, controller: OwnedWriteHalf) -> Result<()> {
let mut controller = BufWriter::new(controller);
loop { loop {
let mut control = ControlPacket::Stop;
let (note,vol) = notes.recv().await.context("channel closed")?; let (note,vol) = notes.recv().await.context("channel closed")?;
if let Some(note) = note { if let Some(note) = note {
//dbg!(note.frequency); //dbg!(note.frequency);
@ -86,31 +89,43 @@ async fn controller(mut notes: mpsc::Receiver<(Option<pitch_detection::Pitch<f32
match note.pitch { match note.pitch {
Pitch { letter: NoteLetter::A, accidental: 0} => { Pitch { letter: NoteLetter::A, accidental: 0} => {
println!("forward"); println!("forward");
control = ControlPacket::Twist(1.0, 0.0);
} }
Pitch { letter: NoteLetter::A, accidental: 1} => { Pitch { letter: NoteLetter::A, accidental: 1} => {
println!("backward"); println!("backward");
control = ControlPacket::Twist(-1.0, 0.0);
} }
Pitch { letter: NoteLetter::C, accidental: 0} => { Pitch { letter: NoteLetter::C, accidental: 0} => {
println!("right"); println!("right");
control = ControlPacket::Twist(0.0, 1.0);
} }
Pitch { letter: NoteLetter::C, accidental: 1} => { Pitch { letter: NoteLetter::C, accidental: 1} => {
println!("left"); println!("left");
control = ControlPacket::Twist(0.0, -1.0);
} }
Pitch { letter: NoteLetter::G, accidental: 0} => { Pitch { letter: NoteLetter::G, accidental: 0} => {
println!("fire"); println!("fire");
control = ControlPacket::Fire;
} }
Pitch { letter: NoteLetter::E, accidental: 0} => { Pitch { letter: NoteLetter::E, accidental: 0} => {
println!("fire"); println!("fire");
control = ControlPacket::Fire;
} }
pitch => { _pitch => {
if vol > 3000. { if vol > 3000. {
println!("fire"); println!("fire");
control = ControlPacket::FireOverride(1.0);
} else { } else {
//dbg!(pitch); //dbg!(pitch);
} }
} }
} }
let control: heapless::Vec<u8, 2048> = postcard::to_vec(&control)?;
controller.write_u32(control.len() as u32).await?;
controller.write_all(&control).await?;
controller.flush().await?;
} }
} }
} }