Skip to content

Commit

Permalink
Update to usb-device 0.3 (#138)
Browse files Browse the repository at this point in the history
* Update to usb-device 0.3
* Update docs example to use multi language strings

---------

Co-authored-by: Daniel Kenyon-Jones <github@dlkj.co.uk>
  • Loading branch information
ithinuel and dlkj authored Apr 13, 2024
1 parent d558b79 commit 103a865
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ documentation = "https://docs.rs/usbd-human-interface-device"
license = "MIT"

[dependencies]
usb-device = "0.2"
usb-device = "0.3"
log = { version = "0.4", optional = true }
defmt = { version = "0.3", optional = true }
packed_struct = { version = "0.10", default-features = false }
Expand Down
7 changes: 4 additions & 3 deletions src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::UsbHidError;
use frunk::{HCons, HNil, ToMut};
#[allow(clippy::wildcard_imports)]
use usb_device::class_prelude::*;
use usb_device::descriptor::lang_id::LangID;

pub mod consumer;
pub mod fido;
Expand All @@ -25,7 +26,7 @@ pub trait DeviceHList<'a>: ToMut<'a> {
fn get(&mut self, id: u8) -> Option<&mut dyn InterfaceClass<'a>>;
fn reset(&mut self);
fn write_descriptors(&mut self, writer: &mut DescriptorWriter) -> usb_device::Result<()>;
fn get_string(&mut self, index: StringIndex, lang_id: u16) -> Option<&'a str>;
fn get_string(&mut self, index: StringIndex, lang_id: LangID) -> Option<&'a str>;
fn tick(&mut self) -> Result<(), UsbHidError>;
}

Expand All @@ -40,7 +41,7 @@ impl<'a> DeviceHList<'a> for HNil {
Ok(())
}

fn get_string(&mut self, _: StringIndex, _: u16) -> Option<&'a str> {
fn get_string(&mut self, _: StringIndex, _: LangID) -> Option<&'a str> {
None
}

Expand Down Expand Up @@ -69,7 +70,7 @@ impl<'a, Head: DeviceClass<'a> + 'a, Tail: DeviceHList<'a>> DeviceHList<'a> for
self.tail.write_descriptors(writer)
}

fn get_string(&mut self, index: StringIndex, lang_id: u16) -> Option<&'a str> {
fn get_string(&mut self, index: StringIndex, lang_id: LangID) -> Option<&'a str> {
let s = self.head.interface().get_string(index, lang_id);
if s.is_some() {
s
Expand Down
5 changes: 3 additions & 2 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use usb_device::bus::{StringIndex, UsbBus, UsbBusAllocator};
#[allow(clippy::wildcard_imports)]
use usb_device::class_prelude::*;
use usb_device::class_prelude::{DescriptorWriter, InterfaceNumber};
use usb_device::descriptor::lang_id::LangID;
use usb_device::UsbError;

#[derive(Debug, PackedStruct)]
Expand Down Expand Up @@ -65,7 +66,7 @@ pub trait InterfaceClass<'a> {
fn report_descriptor(&self) -> ReportDescriptor<'_>;
fn id(&self) -> InterfaceNumber;
fn write_descriptors(&self, writer: &mut DescriptorWriter) -> usb_device::Result<()>;
fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&'a str>;
fn get_string(&self, index: StringIndex, _lang_id: LangID) -> Option<&'a str>;
fn reset(&mut self);
fn set_report(&mut self, data: &[u8]) -> usb_device::Result<()>;
fn get_report(&self, data: &mut [u8]) -> usb_device::Result<usize>;
Expand Down Expand Up @@ -467,7 +468,7 @@ where

Ok(())
}
fn get_string(&self, index: StringIndex, _lang_id: u16) -> Option<&'a str> {
fn get_string(&self, index: StringIndex, _lang_id: LangID) -> Option<&'a str> {
self.description_index
.filter(|&i| i == index)
.and(self.config.description)
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@
//! .build(&usb_alloc);
//!
//! let mut usb_dev = UsbDeviceBuilder::new(&usb_alloc, UsbVidPid(0x1209, 0x0001))
//! .manufacturer("usbd-human-interface-device")
//! .product("NKRO Keyboard")
//! .serial_number("TEST")
//! .strings(&[
//! StringDescriptors::new(LangID::EN)
//! .manufacturer("usbd-human-interface-device")
//! .product("NKRO Keyboard")
//! .serial_number("TEST")]
//! ).unwrap()
//! .build();
//!
//! let mut tick_timer = timer.count_down();
Expand Down
3 changes: 2 additions & 1 deletion src/usb_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use frunk::{HCons, HNil, ToMut};
#[allow(clippy::wildcard_imports)]
use usb_device::class_prelude::*;
use usb_device::control::{Recipient, Request};
use usb_device::descriptor::lang_id::LangID;
use usb_device::{control::RequestType, Result};

pub mod prelude {
Expand Down Expand Up @@ -191,7 +192,7 @@ where
Ok(())
}

fn get_string(&self, index: StringIndex, lang_id: u16) -> Option<&str> {
fn get_string(&self, index: StringIndex, lang_id: LangID) -> Option<&str> {
self.devices.borrow_mut().get_string(index, lang_id)
}

Expand Down

0 comments on commit 103a865

Please sign in to comment.