diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-03-28 02:20:01 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-04-06 05:38:11 +0200 |
| commit | a2f5763a67441e5e6c981407938c21c58e9eb669 (patch) | |
| tree | 48f5d58eaf04e6a85da706e6960ac96cf5cd02ab /embassy-usb/src/builder.rs | |
| parent | a062baae3837b453f0c05d0730ba465c6d2c2446 (diff) | |
usb: add `add_class` to builder, so that `FooBarClass::new(&mut builder)` can set up everything.
Diffstat (limited to 'embassy-usb/src/builder.rs')
| -rw-r--r-- | embassy-usb/src/builder.rs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/embassy-usb/src/builder.rs b/embassy-usb/src/builder.rs index bcb838ff5..491acf4d3 100644 --- a/embassy-usb/src/builder.rs +++ b/embassy-usb/src/builder.rs | |||
| @@ -1,8 +1,11 @@ | |||
| 1 | use heapless::Vec; | ||
| 2 | |||
| 1 | use super::class::UsbClass; | 3 | use super::class::UsbClass; |
| 2 | use super::descriptor::{BosWriter, DescriptorWriter}; | 4 | use super::descriptor::{BosWriter, DescriptorWriter}; |
| 3 | use super::driver::{Driver, EndpointAllocError}; | 5 | use super::driver::{Driver, EndpointAllocError}; |
| 4 | use super::types::*; | 6 | use super::types::*; |
| 5 | use super::UsbDevice; | 7 | use super::UsbDevice; |
| 8 | use super::MAX_CLASS_COUNT; | ||
| 6 | 9 | ||
| 7 | #[derive(Debug, Copy, Clone)] | 10 | #[derive(Debug, Copy, Clone)] |
| 8 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 11 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -116,6 +119,7 @@ impl<'a> Config<'a> { | |||
| 116 | /// Used to build new [`UsbDevice`]s. | 119 | /// Used to build new [`UsbDevice`]s. |
| 117 | pub struct UsbDeviceBuilder<'d, D: Driver<'d>> { | 120 | pub struct UsbDeviceBuilder<'d, D: Driver<'d>> { |
| 118 | config: Config<'d>, | 121 | config: Config<'d>, |
| 122 | classes: Vec<&'d mut dyn UsbClass, MAX_CLASS_COUNT>, | ||
| 119 | 123 | ||
| 120 | bus: D, | 124 | bus: D, |
| 121 | next_interface_number: u8, | 125 | next_interface_number: u8, |
| @@ -165,6 +169,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 165 | UsbDeviceBuilder { | 169 | UsbDeviceBuilder { |
| 166 | bus, | 170 | bus, |
| 167 | config, | 171 | config, |
| 172 | classes: Vec::new(), | ||
| 168 | next_interface_number: 0, | 173 | next_interface_number: 0, |
| 169 | next_string_index: 4, | 174 | next_string_index: 4, |
| 170 | 175 | ||
| @@ -175,7 +180,7 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 175 | } | 180 | } |
| 176 | 181 | ||
| 177 | /// Creates the [`UsbDevice`] instance with the configuration in this builder. | 182 | /// Creates the [`UsbDevice`] instance with the configuration in this builder. |
| 178 | pub fn build(mut self, classes: &'d mut [&'d mut dyn UsbClass]) -> UsbDevice<'d, D> { | 183 | pub fn build(mut self) -> UsbDevice<'d, D> { |
| 179 | self.config_descriptor.end_configuration(); | 184 | self.config_descriptor.end_configuration(); |
| 180 | self.bos_descriptor.end_bos(); | 185 | self.bos_descriptor.end_bos(); |
| 181 | 186 | ||
| @@ -185,10 +190,16 @@ impl<'d, D: Driver<'d>> UsbDeviceBuilder<'d, D> { | |||
| 185 | self.device_descriptor.into_buf(), | 190 | self.device_descriptor.into_buf(), |
| 186 | self.config_descriptor.into_buf(), | 191 | self.config_descriptor.into_buf(), |
| 187 | self.bos_descriptor.writer.into_buf(), | 192 | self.bos_descriptor.writer.into_buf(), |
| 188 | classes, | 193 | self.classes, |
| 189 | ) | 194 | ) |
| 190 | } | 195 | } |
| 191 | 196 | ||
| 197 | pub fn add_class(&mut self, class: &'d mut dyn UsbClass) { | ||
| 198 | if self.classes.push(class).is_err() { | ||
| 199 | panic!("max class count reached") | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 192 | /// Allocates a new interface number. | 203 | /// Allocates a new interface number. |
| 193 | pub fn alloc_interface(&mut self) -> InterfaceNumber { | 204 | pub fn alloc_interface(&mut self) -> InterfaceNumber { |
| 194 | let number = self.next_interface_number; | 205 | let number = self.next_interface_number; |
