diff options
| author | Thales Fragoso <[email protected]> | 2021-03-07 20:15:40 -0300 |
|---|---|---|
| committer | Thales Fragoso <[email protected]> | 2021-03-07 20:15:40 -0300 |
| commit | 15c3e78408a2835003b9d7dee0b7fbae544d10ba (patch) | |
| tree | 2f511983d6efa69c5055e9aad888ce0899624cd0 /embassy-extras/src/ring_buffer.rs | |
| parent | f922cf1609c9baebf8dead87161df215f35df449 (diff) | |
Move nRF's util into a separate crate
Diffstat (limited to 'embassy-extras/src/ring_buffer.rs')
| -rw-r--r-- | embassy-extras/src/ring_buffer.rs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/embassy-extras/src/ring_buffer.rs b/embassy-extras/src/ring_buffer.rs new file mode 100644 index 000000000..f2b9f7359 --- /dev/null +++ b/embassy-extras/src/ring_buffer.rs | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | use crate::fmt::{assert, *}; | ||
| 2 | |||
| 3 | pub struct RingBuffer<'a> { | ||
| 4 | buf: &'a mut [u8], | ||
| 5 | start: usize, | ||
| 6 | end: usize, | ||
| 7 | empty: bool, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl<'a> RingBuffer<'a> { | ||
| 11 | pub fn new(buf: &'a mut [u8]) -> Self { | ||
| 12 | Self { | ||
| 13 | buf, | ||
| 14 | start: 0, | ||
| 15 | end: 0, | ||
| 16 | empty: true, | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | pub fn push_buf(&mut self) -> &mut [u8] { | ||
| 21 | if self.start == self.end && !self.empty { | ||
| 22 | trace!(" ringbuf: push_buf empty"); | ||
| 23 | return &mut self.buf[..0]; | ||
| 24 | } | ||
| 25 | |||
| 26 | let n = if self.start <= self.end { | ||
| 27 | self.buf.len() - self.end | ||
| 28 | } else { | ||
| 29 | self.start - self.end | ||
| 30 | }; | ||
| 31 | |||
| 32 | trace!(" ringbuf: push_buf {:?}..{:?}", self.end, self.end + n); | ||
| 33 | &mut self.buf[self.end..self.end + n] | ||
| 34 | } | ||
| 35 | |||
| 36 | pub fn push(&mut self, n: usize) { | ||
| 37 | trace!(" ringbuf: push {:?}", n); | ||
| 38 | if n == 0 { | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | self.end = self.wrap(self.end + n); | ||
| 43 | self.empty = false; | ||
| 44 | } | ||
| 45 | |||
| 46 | pub fn pop_buf(&mut self) -> &mut [u8] { | ||
| 47 | if self.empty { | ||
| 48 | trace!(" ringbuf: pop_buf empty"); | ||
| 49 | return &mut self.buf[..0]; | ||
| 50 | } | ||
| 51 | |||
| 52 | let n = if self.end <= self.start { | ||
| 53 | self.buf.len() - self.start | ||
| 54 | } else { | ||
| 55 | self.end - self.start | ||
| 56 | }; | ||
| 57 | |||
| 58 | trace!(" ringbuf: pop_buf {:?}..{:?}", self.start, self.start + n); | ||
| 59 | &mut self.buf[self.start..self.start + n] | ||
| 60 | } | ||
| 61 | |||
| 62 | pub fn pop(&mut self, n: usize) { | ||
| 63 | trace!(" ringbuf: pop {:?}", n); | ||
| 64 | if n == 0 { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | self.start = self.wrap(self.start + n); | ||
| 69 | self.empty = self.start == self.end; | ||
| 70 | } | ||
| 71 | |||
| 72 | fn wrap(&self, n: usize) -> usize { | ||
| 73 | assert!(n <= self.buf.len()); | ||
| 74 | if n == self.buf.len() { | ||
| 75 | 0 | ||
| 76 | } else { | ||
| 77 | n | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
