hid_io_protocol/
buffer.rs

1/* Copyright (C) 2020-2021 by Jacob Alexander
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22// ----- Crates -----
23
24use heapless::spsc::Queue;
25use heapless::Vec;
26
27// ----- Enumerations -----
28
29// ----- Structs -----
30
31/// HID-IO byte buffer
32/// This buffer is a queue of vecs with static allocation
33/// Each vec is fixed sized as HID-IO interface
34/// has a fixed transport payload (even if the actual size of the
35/// message is less).
36/// This buffer has no notion of packet size so it must store the
37/// full transport payload.
38/// In the minimal scenario a queue size of 1 is used.
39///
40/// Common HID-IO Vec capacities
41/// - 7 bytes (USB 2.0 LS /w HID ID byte)
42/// - 8 bytes (USB 2.0 LS)
43/// - 63 bytes (USB 2.0 FS /w HID ID byte)
44/// - 64 bytes (USB 2.0 FS)
45/// - 1023 bytes (USB 2.0 HS /w HID ID byte)
46/// - 1024 bytes (USB 2.0 HS)
47///
48/// The maximum queue size is 255
49pub struct Buffer<const Q: usize, const N: usize> {
50    queue: Queue<Vec<u8, N>, Q>,
51}
52
53// ----- Implementations -----
54
55impl<const Q: usize, const N: usize> Default for Buffer<Q, N> {
56    fn default() -> Self {
57        Buffer {
58            queue: Queue::new(),
59        }
60    }
61}
62
63impl<const Q: usize, const N: usize> Buffer<Q, N> {
64    /// Constructor for Buffer
65    ///
66    /// # Remarks
67    /// Initialize as blank
68    /// This buffer has a limit of 65535 elements
69    pub fn new() -> Buffer<Q, N> {
70        Buffer {
71            ..Default::default()
72        }
73    }
74
75    /// Checks the first item array
76    /// Returns None if there are no items in the queue
77    /// Does not dequeue
78    pub fn peek(&self) -> Option<&Vec<u8, N>> {
79        self.queue.peek()
80    }
81
82    /// Dequeues and returns the first item array
83    /// Returns None if there are no items in the queue
84    pub fn dequeue(&mut self) -> Option<Vec<u8, N>> {
85        self.queue.dequeue()
86    }
87
88    /// Enqueues
89    /// Returns the array if there's not enough space
90    pub fn enqueue(&mut self, data: Vec<u8, N>) -> Result<(), Vec<u8, N>> {
91        self.queue.enqueue(data)
92    }
93
94    /// Clears the buffer
95    /// Needed for some error conditions
96    pub fn clear(&mut self) {
97        while !self.queue.is_empty() {
98            self.dequeue();
99        }
100    }
101
102    /// Capacity of buffer
103    pub fn capacity(&self) -> usize {
104        self.queue.capacity()
105    }
106
107    /// Number of elements stored in the buffer
108    pub fn len(&self) -> usize {
109        self.queue.len()
110    }
111
112    /// Buffer empty
113    pub fn is_empty(&self) -> bool {
114        self.queue.is_empty()
115    }
116
117    /// Buffer full
118    pub fn is_full(&self) -> bool {
119        self.len() == self.capacity()
120    }
121}