add command channel and reset/bootloader command

This commit is contained in:
ktims committed 2026-11-04 16:29:26 -07:00
1 parent 93a20cf8e1
commit 245c45c682
7 files changed
+197 -45

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ name = "shared"
edition = "2024"
[features]
std = []
std = ["deku/alloc"]
serde = ["dep:serde"]
[dependencies]
+53 -3
View File
@@ -1,6 +1,9 @@
#![no_std]
use bytemuck::NoUninit;
use deku::DekuRead;
#[cfg(feature = "std")]
use deku::DekuWrite;
use num_enum::TryFromPrimitive;
#[cfg(feature = "serde")]
@@ -11,7 +14,7 @@ use serde::{Deserialize, Serialize};
#[repr(u8)]
pub enum AudioState {
/// Knowingly stopped, ie. AltSetting=0. DAC muted, I2S disabled.
///
///
/// AltSetting = 1 -> ARMED
Stopped = 0,
/// Waiting for data. DAC muted, I2S running sending 0s (FIFO not serviced).
@@ -103,14 +106,34 @@ pub struct AudioTelemetrySnapshot {
pub fb_rate_estimate: f32,
}
// Writing is host-side only (needs deku's `alloc`, enabled by the `std` feature)
#[derive(Debug, DekuRead, Clone)]
#[cfg_attr(feature = "std", derive(DekuWrite))]
#[deku(id_type = "u8")]
pub enum AudioDeviceCommand {
#[deku(id = 0)]
Noop,
#[deku(id = 1)]
Reset,
#[deku(id = 2)]
Bootloader,
}
pub mod hid {
use deku::DekuRead;
use deku::{DekuContainerRead, DekuError, DekuRead};
#[cfg(feature = "std")]
use deku::{DekuContainerWrite, DekuWrite};
use usbd_hid::descriptor::generator_prelude::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::{AudioState, AudioTelemetrySnapshot};
use crate::{AudioDeviceCommand, AudioState, AudioTelemetrySnapshot};
/// HID usage IDs identifying each vendor interface. `gen_hid_descriptor` only accepts
/// literals, so the descriptors below repeat these values; keep them in sync.
pub const USAGE_TELEMETRY: u16 = 0x01;
pub const USAGE_COMMAND: u16 = 0x02;
#[derive(DekuRead)]
#[deku(endian = "little")]
@@ -153,4 +176,31 @@ pub mod hid {
}
}
}
#[derive(DekuRead)]
#[cfg_attr(feature = "std", derive(DekuWrite))]
#[deku(endian = "little")]
#[gen_hid_descriptor((collection = APPLICATION, usage_page = VENDOR_DEFINED_START, usage = 0x02, ) = {
cmd=output;
})]
#[repr(C, packed)]
pub struct AudioDeviceCommandReport {
pub cmd: [u8; 32],
}
impl TryFrom<AudioDeviceCommandReport> for AudioDeviceCommand {
type Error = DekuError;
fn try_from(value: AudioDeviceCommandReport) -> Result<Self, Self::Error> {
Ok(AudioDeviceCommand::from_bytes((value.cmd.as_slice(), 0))?.1)
}
}
#[cfg(feature = "std")]
impl From<AudioDeviceCommand> for AudioDeviceCommandReport {
fn from(value: AudioDeviceCommand) -> Self {
let mut buf = [0u8; core::mem::size_of::<Self>()];
value.to_slice(&mut buf).unwrap();
Self { cmd: buf }
}
}
}