Adding alsa lib and cpal

This commit is contained in:
2025-06-28 14:28:32 +00:00
parent 535822198b
commit 66c4aeffa6
11 changed files with 570 additions and 64 deletions
+1 -1
View File
@@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
artnet_protocol = "0.4.3"
cpal = "0.16.0"
crossbeam = "0.8.4"
ctrlc = { version = "3.4.7", features = ["termination"] }
env_logger = "0.11.8"
@@ -12,7 +13,6 @@ log = "0.4.27"
nix = "0.30.1"
rpi-mailbox = "0.3.0"
rppal = "0.22.1"
# tokio = { version = "1.45.1", features = ["full"] }
[features]
default = ["rpizero2", "16channel"]
+33 -19
View File
@@ -27,6 +27,12 @@ pub enum AppMode {
Manual,
}
impl Default for AppMode {
fn default() -> Self {
Self::Standalone
}
}
impl AppMode {
pub fn for_each<F: FnMut(AppMode)>(mut f: F) {
f(AppMode::Diagnostics);
@@ -36,13 +42,25 @@ impl AppMode {
}
}
impl From<usize> for AppMode {
fn from(value: usize) -> Self {
impl From<Option<usize>> for AppMode {
fn from(value: Option<usize>) -> Self {
match value {
1 => AppMode::ArtNet,
2 => AppMode::Standalone,
3 => AppMode::Manual,
_ => AppMode::Diagnostics,
Some(0) => AppMode::Diagnostics,
Some(1) => AppMode::ArtNet,
Some(2) => AppMode::Standalone,
Some(3) => AppMode::Manual,
_ => AppMode::default(),
}
}
}
impl Into<Box<dyn AppModeHandler + Send>> for AppMode {
fn into(self) -> Box<dyn AppModeHandler + Send> {
match self {
AppMode::Diagnostics => Box::new(diagnostics::DiagnosticsMode::new()),
AppMode::ArtNet => Box::new(artnet::ArtNetMode::new()),
AppMode::Standalone => Box::new(standalone::StandaloneMode::new()),
AppMode::Manual => Box::new(manual::ManualMode::new()),
}
}
}
@@ -56,23 +74,19 @@ pub struct ModeManager {
impl ModeManager {
pub fn new(mode_rx: Receiver<Message>) -> Self {
let mut handlers: HashMap<AppMode, Box<dyn AppModeHandler + Send>> = HashMap::new();
handlers.insert(AppMode::Manual, Box::new(manual::ManualMode::new()));
handlers.insert(
AppMode::Standalone,
Box::new(standalone::StandaloneMode::new()),
);
handlers.insert(
AppMode::Diagnostics,
Box::new(diagnostics::DiagnosticsMode::new()),
);
handlers.insert(AppMode::ArtNet, Box::new(artnet::ArtNetMode::new()));
AppMode::for_each(|mode| {
handlers.insert(mode, mode.into());
});
let mode_manager = ModeManager {
let mut mode_manager = ModeManager {
mode_handler_map: handlers,
mode: Arc::new(Mutex::new(AppMode::Diagnostics)),
mode: Arc::new(Mutex::new(AppMode::default())),
mode_rx: mode_rx.clone(),
};
log::info!("Starting app with mode {:?}", AppMode::default());
mode_manager.get_handler(None).enter();
mode_manager
}
@@ -80,7 +94,7 @@ impl ModeManager {
if let Ok(Message::ModeChanged { mode: next }) = self.mode_rx.try_recv() {
let current = self.get_current_mode();
if current != next {
log::info!(" Changing mode from {:?} to {:?}", current, next);
log::info!("Switching mode from {:?} to {:?}", current, next);
self.get_handler(Some(current)).exit();
self.get_handler(Some(next)).enter();
self.set_current_mode(next);
@@ -14,9 +14,6 @@ pub struct ArtNetMode {
}
impl ArtNetMode {
const _SHORT: &str = "LightSabre\0";
const _SHORT_PAD: [u8; 18 - Self::_SHORT.len()] = [0; 7];
pub fn new() -> Self {
ArtNetMode::default()
}
@@ -41,8 +41,8 @@ impl AppModeHandler for DiagnosticsMode {
}
fn run(&mut self, led_driver: &mut LedDriver) {
log::trace!("[Diagnostics] Running...");
if self.cycle_count % 50 == 0 {
log::trace!("[Diagnostics] Running...");
let (led, color) = match self.led_iterator.next() {
Some(led) => {
led_driver.set_color(**self.color_iterator.peek().unwrap(), led);
@@ -1,3 +1,4 @@
use cpal::traits::{DeviceTrait, HostTrait};
use rppal::gpio::Gpio;
use crate::cputasks::modes::AppModeHandler;
@@ -29,6 +30,23 @@ impl StandaloneMode {
impl AppModeHandler for StandaloneMode {
fn enter(&mut self) {
log::debug!("[Standalone] Entering Standalone Mode");
let host = cpal::default_host();
// default_input_device()
host.devices()
.unwrap()
.for_each(|device| log::info!("{:?}", device.name()));
if let Some(device) = host
.input_devices()
.unwrap()
.find(|device| device.name() == Ok(String::from("snd_rpi_googlevoicehat_soundcar")))
{
log::info!("Found record device {:?}", device.name());
log::info!("Default config : {:?}", device.default_input_config())
} else {
log::error!("Record device not found");
}
}
fn run(&mut self, _: &mut LedDriver) {
+2 -5
View File
@@ -39,10 +39,7 @@ impl Selector {
.expect("Failed to set interrupt");
}
pub fn get_current_index(&self) -> usize {
self.selector_pins
.iter()
.position(|pin| pin.is_low())
.unwrap_or(0) // Default to 0 if no pin is low
pub fn get_current_index(&self) -> Option<usize> {
self.selector_pins.iter().position(|pin| pin.is_low())
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ impl SelectorTask {
AppMode::for_each(|mode| {
log::debug!(
"Setting up selector callback for mode: ({}){:?}",
mode as usize,
mode as isize,
mode
);
let tx = tx.clone();