use fugit::ExtU32;
use packed_struct::prelude::*;
#[allow(clippy::wildcard_imports)]
use usb_device::class_prelude::*;
use usb_device::UsbError;
use crate::hid_class::prelude::*;
use crate::interface::raw::{RawInterface, RawInterfaceConfig};
use crate::interface::{InterfaceClass, UsbAllocatable};
use crate::page::Consumer;
#[rustfmt::skip]
pub const MULTIPLE_CODE_REPORT_DESCRIPTOR: &[u8] = &[
0x05, 0x0C, 0x09, 0x01, 0xA1, 0x01, 0x75, 0x10, 0x95, 0x04, 0x15, 0x00, 0x26, 0x9C, 0x02, 0x19, 0x00, 0x2A, 0x9C, 0x02, 0x81, 0x00, 0xC0, ];
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default, PackedStruct)]
#[packed_struct(endian = "lsb", size_bytes = "8")]
pub struct MultipleConsumerReport {
#[packed_field(ty = "enum", element_size_bytes = "2")]
pub codes: [Consumer; 4],
}
#[allow(clippy::doc_markdown)]
#[rustfmt::skip]
pub const FIXED_FUNCTION_REPORT_DESCRIPTOR: &[u8] = &[
0x05, 0x0C, 0x09, 0x01, 0xA1, 0x01, 0x05, 0x0C, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x07, 0x09, 0xB5, 0x09, 0xB6, 0x09, 0xB7, 0x09, 0xCD, 0x09, 0xE2, 0x09, 0xE9, 0x09, 0xEA, 0x81, 0x02, 0x95, 0x01, 0x81, 0x01, 0xC0, ];
#[derive(Clone, Copy, Debug, Eq, PartialEq, PackedStruct)]
#[packed_struct(endian = "lsb", bit_numbering = "lsb0", size_bytes = "1")]
pub struct FixedFunctionReport {
#[packed_field(bits = "0")]
pub next: bool,
#[packed_field(bits = "1")]
pub previous: bool,
#[packed_field(bits = "2")]
pub stop: bool,
#[packed_field(bits = "3")]
pub play_pause: bool,
#[packed_field(bits = "4")]
pub mute: bool,
#[packed_field(bits = "5")]
pub volume_increment: bool,
#[packed_field(bits = "6")]
pub volume_decrement: bool,
}
pub struct ConsumerControlInterface<'a, B: UsbBus> {
inner: RawInterface<'a, B>,
}
impl<'a, B: UsbBus> ConsumerControlInterface<'a, B> {
pub fn write_report(&mut self, report: &MultipleConsumerReport) -> usb_device::Result<usize> {
let data = report.pack().map_err(|_| {
error!("Error packing MultipleConsumerReport");
UsbError::ParseError
})?;
self.inner.write_report(&data)
}
}
impl<'a, B: UsbBus> InterfaceClass<'a, B> for ConsumerControlInterface<'a, B> {
fn interface(&mut self) -> &mut RawInterface<'a, B> {
&mut self.inner
}
fn reset(&mut self) {}
}
pub struct ConsumerControlConfig<'a> {
interface: RawInterfaceConfig<'a>,
}
impl<'a> ConsumerControlConfig<'a> {
fn new(interface: RawInterfaceConfig<'a>) -> Self {
Self { interface }
}
}
impl<'a> Default for ConsumerControlConfig<'a> {
#[must_use]
fn default() -> Self {
Self::new(
unwrap!(
unwrap!(RawInterfaceBuilder::new(MULTIPLE_CODE_REPORT_DESCRIPTOR))
.description("Consumer Control")
.in_endpoint(UsbPacketSize::Bytes8, 50.millis())
)
.without_out_endpoint()
.build(),
)
}
}
impl<'a, B: UsbBus + 'a> UsbAllocatable<'a, B> for ConsumerControlConfig<'a> {
type Allocated = ConsumerControlInterface<'a, B>;
fn allocate(self, usb_alloc: &'a UsbBusAllocator<B>) -> Self::Allocated {
Self::Allocated {
inner: RawInterface::new(usb_alloc, self.interface),
}
}
}
pub struct ConsumerControlFixedInterface<'a, B: UsbBus> {
inner: RawInterface<'a, B>,
}
impl<'a, B: UsbBus> ConsumerControlFixedInterface<'a, B> {
pub fn write_report(&mut self, report: &FixedFunctionReport) -> usb_device::Result<usize> {
let data = report.pack().map_err(|_| {
error!("Error packing MultipleConsumerReport");
UsbError::ParseError
})?;
self.inner.write_report(&data)
}
}
impl<'a, B: UsbBus> InterfaceClass<'a, B> for ConsumerControlFixedInterface<'a, B> {
fn interface(&mut self) -> &mut RawInterface<'a, B> {
&mut self.inner
}
fn reset(&mut self) {}
}
pub struct ConsumerControlFixedConfig<'a> {
interface: RawInterfaceConfig<'a>,
}
impl<'a> ConsumerControlFixedConfig<'a> {
fn new(interface: RawInterfaceConfig<'a>) -> Self {
Self { interface }
}
}
impl<'a> Default for ConsumerControlFixedConfig<'a> {
#[must_use]
fn default() -> Self {
Self::new(
unwrap!(
unwrap!(RawInterfaceBuilder::new(FIXED_FUNCTION_REPORT_DESCRIPTOR))
.description("Consumer Control")
.in_endpoint(UsbPacketSize::Bytes8, 50.millis())
)
.without_out_endpoint()
.build(),
)
}
}
impl<'a, B: UsbBus + 'a> UsbAllocatable<'a, B> for ConsumerControlFixedConfig<'a> {
type Allocated = ConsumerControlFixedInterface<'a, B>;
fn allocate(self, usb_alloc: &'a UsbBusAllocator<B>) -> Self::Allocated {
Self::Allocated {
inner: RawInterface::new(usb_alloc, self.interface),
}
}
}