Skip to content

Commit 674e724

Browse files
committed
Updating to heapless 0.7
- Breaking api change to use const generics - Due to some limitations 2 extra parameters are needed (which can be removed in the future once const generic expressions are added)
1 parent c4f514e commit 674e724

File tree

10 files changed

+291
-280
lines changed

10 files changed

+291
-280
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ flexi_logger = "^0.17"
9797
futures = { version = "^0.3", optional = true }
9898
futures-util = { version = "^0.3", optional = true }
9999
glob = { version = "^0.3", optional = true }
100-
heapless = { version = "^0.6" }
100+
heapless = { version = "^0.7" }
101101
hidapi = { version = "1.2.4", default-features = false, features = ["linux-static-hidraw"], optional = true }
102102
hid-io-protocol = { path = "hid-io-protocol", version = "0.1.0" }
103103
libc = { version = "^0.2", optional = true }

hid-io-protocol/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ server = ["log", "bincode_core/std"]
3636
[dependencies]
3737
arraydeque = { version = "^0.4", default-features = false }
3838
bincode_core = { git = "https://github.com/bincode-org/bincode-core.git", version = "0.1.0" }
39-
heapless = { version = "^0.6" }
39+
heapless = { version = "^0.7" }
4040
log = { version = "^0.4", default-features = false, optional = true }
4141
num_enum = { version = "^0.5", default-features = false }
4242
serde = { version = "^1.0", default-features = false }

hid-io-protocol/README.md

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,20 @@ In each way a CommandInterface struct is created and the Commands trait is imple
5656
Option 1 is simpler as hid-io-protocol can handle all processing from a hidraw interface. Device libraries usually go for this.
5757

5858
```rust
59-
type BufChunk = U64;
60-
type IdLen = U10;
61-
type MessageLen = U256;
62-
type RxBuf = U8;
63-
type SerializationLen = U276;
64-
type TxBuf = U8;
59+
const BufChunk: usize = U64;
60+
const IdLen: usize = U10;
61+
const MessageLen: usize = U256;
62+
const RxBuf: usize = U8;
63+
const SerializationLen: usize = U276;
64+
const TxBuf: usize = U8;
6565

6666
let ids = [
6767
HidIoCommandID::SupportedIDs,
6868
/* Add supported ids */
6969
/* This is the master list, if it's not listed here comamnds will not work */
7070
];
7171

72-
let intf = CommandInterface::<TxBuf, RxBuf, BufChunk, MessageLen, SerializationLen, IdLen>::new(&ids).unwrap();
72+
let intf = CommandInterface::<TxBuf, RxBuf, BufChunk, MessageLen, {MessageLen - 1}, {MessageLen - 4}, SerializationLen, IdLen>::new(&ids).unwrap();
7373
}
7474

7575
// The max length must equal BufChunk (e.g. 64 bytes)
@@ -101,16 +101,15 @@ match intf.tx_bytebuf.dequeue() {
101101
}
102102

103103
struct CommandInterface<
104-
TX: ArrayLength<Vec<u8, N>>,
105-
RX: ArrayLength<Vec<u8, N>>,
106-
N: ArrayLength<u8>,
107-
H: ArrayLength<u8>,
108-
S: ArrayLength<u8>,
109-
ID: ArrayLength<HidIoCommandID> + ArrayLength<u8>,
110-
> where
111-
H: core::fmt::Debug,
112-
H: Sub<B1>,
113-
{
104+
const TX: usize,
105+
const RX: usize,
106+
const N: usize,
107+
const H: usize,
108+
const HSUB1: usize,
109+
const HSUB4: usize,
110+
const S: usize,
111+
const ID: usize,
112+
> {
114113
ids: Vec<HidIoCommandID, ID>,
115114
rx_bytebuf: buffer::Buffer<RX, N>,
116115
rx_packetbuf: HidIoPacketBuffer<H>,
@@ -119,20 +118,19 @@ struct CommandInterface<
119118
}
120119

121120
impl<
122-
TX: ArrayLength<Vec<u8, N>>,
123-
RX: ArrayLength<Vec<u8, N>>,
124-
N: ArrayLength<u8>,
125-
H: ArrayLength<u8>,
126-
S: ArrayLength<u8>,
127-
ID: ArrayLength<HidIoCommandID> + ArrayLength<u8>,
128-
> CommandInterface<TX, RX, N, H, S, ID>
129-
where
130-
H: core::fmt::Debug,
131-
H: Sub<B1>,
121+
const TX: usize,
122+
const RX: usize,
123+
const N: usize,
124+
const H: usize,
125+
const HSUB1: usize,
126+
const HSUB4: usize,
127+
const S: usize,
128+
const ID: usize,
129+
> CommandInterface<TX, RX, N, H, HSUB1, HSUB4, S, ID>
132130
{
133131
fn new(
134132
ids: &[HidIoCommandID],
135-
) -> Result<CommandInterface<TX, RX, N, H, S, ID>, CommandError> {
133+
) -> Result<CommandInterface<TX, RX, N, H, HSUB1, HSUB4, S, ID>, CommandError> {
136134
// Make sure we have a large enough id vec
137135
let ids = match Vec::from_slice(ids) {
138136
Ok(ids) => ids,
@@ -191,10 +189,7 @@ where
191189
/// Process rx buffer until empty
192190
/// Handles flushing tx->rx, decoding, then processing buffers
193191
/// Returns the number of buffers processed
194-
pub fn process_rx(&mut self) -> Result<u8, CommandError>
195-
where
196-
<H as Sub<B1>>::Output: ArrayLength<u8>,
197-
{
192+
pub fn process_rx(&mut self) -> Result<u8, CommandError> {
198193
// Decode bytes into buffer
199194
while (self.rx_packetbuffer_decode()? {
200195
// Process rx buffer
@@ -216,18 +211,17 @@ where
216211
/// S - Serialization buffer size
217212
/// ID - Max number of HidIoCommandIDs
218213
impl<
219-
TX: ArrayLength<Vec<u8, N>>,
220-
RX: ArrayLength<Vec<u8, N>>,
221-
N: ArrayLength<u8>,
222-
H: ArrayLength<u8>,
223-
S: ArrayLength<u8>,
224-
ID: ArrayLength<HidIoCommandID> + ArrayLength<u8>,
225-
> Commands<H, ID> for CommandInterface<TX, RX, N, H, S, ID>
226-
where
227-
H: core::fmt::Debug + Sub<B1>,
228-
{
214+
const TX: usize,
215+
const RX: usize,
216+
const N: usize,
217+
const H: usize,
218+
const HSUB1: usize,
219+
const HSUB4: usize,
220+
const S: usize,
221+
const ID: usize,
222+
> Commands<H, ID> for CommandInterface<TX, RX, N, H, HSUB1, HSUB4, S, ID> {
229223
fn default_packet_chunk(&self) -> u32 {
230-
<N as Unsigned>::to_u32()
224+
N as u32
231225
}
232226

233227
fn tx_packetbuffer_send(&mut self, buf: &mut HidIoPacketBuffer<H>) -> Result<(), CommandError> {
@@ -247,8 +241,8 @@ where
247241
// was serialized
248242
// The first byte is a serde type and is dropped
249243
let data = &self.serial_buf;
250-
for pos in (1..data.len()).step_by(<N as Unsigned>::to_usize()) {
251-
let len = core::cmp::min(<N as Unsigned>::to_usize(), data.len() - pos);
244+
for pos in (1..data.len()).step_by(N) {
245+
let len = core::cmp::min(N, data.len() - pos);
252246
match self
253247
.tx_bytebuf
254248
.enqueue(match Vec::from_slice(&data[pos..len + pos]) {
@@ -279,7 +273,7 @@ hid-io-core uses Option 2 as different threads handle byte ingest and message ha
279273

280274
```rust
281275
struct CommandInterface {}
282-
impl Commands<mailbox::HidIoPacketBufferDataSize, U0> for CommandInterface {
276+
impl Commands<mailbox::HidIoPacketBufferDataSize, 0> for CommandInterface {
283277
fn tx_packetbuffer_send(
284278
&mut self,
285279
buf: &mut mailbox::HidIoPacketBuffer,

hid-io-protocol/src/buffer.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
// ----- Crates -----
2323

24-
use super::*;
2524
use heapless::spsc::Queue;
2625
use heapless::Vec;
2726

@@ -47,23 +46,21 @@ use heapless::Vec;
4746
/// - 1024 bytes (USB 2.0 HS)
4847
///
4948
/// The maximum queue size is 255
50-
pub struct Buffer<Q: ArrayLength<Vec<u8, N>>, N: ArrayLength<u8>> {
51-
queue: Queue<Vec<u8, N>, Q, u8>,
49+
pub struct Buffer<const Q: usize, const N: usize> {
50+
queue: Queue<Vec<u8, N>, Q>,
5251
}
5352

5453
// ----- Implementations -----
5554

56-
impl<Q, N> Default for Buffer<Q, N>
57-
where
58-
Q: ArrayLength<Vec<u8, N>>,
59-
N: ArrayLength<u8>,
60-
{
55+
impl<const Q: usize, const N: usize> Default for Buffer<Q, N> {
6156
fn default() -> Self {
62-
Buffer { queue: Queue::u8() }
57+
Buffer {
58+
queue: Queue::new(),
59+
}
6360
}
6461
}
6562

66-
impl<Q: ArrayLength<Vec<u8, N>>, N: ArrayLength<u8>> Buffer<Q, N> {
63+
impl<const Q: usize, const N: usize> Buffer<Q, N> {
6764
/// Constructor for Buffer
6865
///
6966
/// # Remarks
@@ -103,12 +100,12 @@ impl<Q: ArrayLength<Vec<u8, N>>, N: ArrayLength<u8>> Buffer<Q, N> {
103100
}
104101

105102
/// Capacity of buffer
106-
pub fn capacity(&self) -> u8 {
103+
pub fn capacity(&self) -> usize {
107104
self.queue.capacity()
108105
}
109106

110107
/// Number of elements stored in the buffer
111-
pub fn len(&self) -> u8 {
108+
pub fn len(&self) -> usize {
112109
self.queue.len()
113110
}
114111

0 commit comments

Comments
 (0)