feat(impl): remove RingBuffer implementation
This commit is contained in:
parent
68c1ff13eb
commit
71d952dce5
|
@ -9,106 +9,7 @@ use crate::{
|
||||||
SoftSerialWriter,
|
SoftSerialWriter,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[cfg(feature = "ring-buffer")]
|
||||||
pub struct RingBuffer<const N: usize> {
|
|
||||||
buf: [u8; N],
|
|
||||||
mask: usize,
|
|
||||||
head: usize,
|
|
||||||
tail: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> Default for RingBuffer<N> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> RingBuffer<N> {
|
|
||||||
#[inline]
|
|
||||||
pub const fn new() -> Self {
|
|
||||||
if N.is_power_of_two() {
|
|
||||||
Self {
|
|
||||||
buf: [0; N],
|
|
||||||
mask: N - 1,
|
|
||||||
head: 0,
|
|
||||||
tail: 0,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("Buffer capacity isn't power of two");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn get_buffer(&self) -> &[u8] {
|
|
||||||
&self.buf
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.head == self.tail
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn is_full(&self) -> bool {
|
|
||||||
(self.head + 1) & self.mask == self.tail
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn capacity(&self) -> usize {
|
|
||||||
N
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.head.overflowing_sub(self.tail).0 & self.mask
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn clear(&mut self) {
|
|
||||||
self.buf = [0; N];
|
|
||||||
self.head = 0;
|
|
||||||
self.tail = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn push(&mut self, value: u8) -> Option<()> {
|
|
||||||
let (head, tail) = (self.head, self.tail);
|
|
||||||
let next_head = (head + 1) & self.mask;
|
|
||||||
|
|
||||||
if next_head == tail {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.buf[head] = value;
|
|
||||||
self.head = next_head;
|
|
||||||
|
|
||||||
Some(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn pop(&mut self) -> Option<u8> {
|
|
||||||
let (head, mut tail) = (self.head, self.tail);
|
|
||||||
|
|
||||||
if head == tail {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let value = self.buf[tail];
|
|
||||||
tail = (tail + 1) & self.mask;
|
|
||||||
self.tail = tail;
|
|
||||||
|
|
||||||
Some(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> Iterator for RingBuffer<N> {
|
|
||||||
type Item = u8;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
self.pop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize, P> SoftSerialWriter<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
impl<const N: usize, P> SoftSerialWriter<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
|
@ -124,6 +25,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ring-buffer")]
|
||||||
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
impl<const N: usize, P> SoftSerialReader<P, &mut RingBuffer<N>> for HalfDuplexSerial<P>
|
||||||
where
|
where
|
||||||
P: PinOps + StaticPinOps,
|
P: PinOps + StaticPinOps,
|
||||||
|
@ -137,7 +39,7 @@ where
|
||||||
self.sync_reciever();
|
self.sync_reciever();
|
||||||
|
|
||||||
if let Ok(byte) = self.read_byte() {
|
if let Ok(byte) = self.read_byte() {
|
||||||
recieve_data.push(byte).unwrap_or(());
|
recieve_data.push(byte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue