|
| 1 | +/*========================================================================================== |
| 2 | +SerialRingBuf.h - Ring Buffer for Serial I/O |
| 3 | +
|
| 4 | +MIT License |
| 5 | +
|
| 6 | +Copyright (c) 2025 https://github.com/qqqlab |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +==========================================================================================*/ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +// Ring buffer class |
| 30 | +class SerialRingBuf { |
| 31 | + |
| 32 | +private: |
| 33 | +public: |
| 34 | + uint8_t *buf; |
| 35 | + uint16_t bufsize = 0; //buffer can hold bufsize-1 bytes |
| 36 | + volatile uint16_t head = 0; //previous push position - NOTE: volatile to prevent compiler to optimize things away |
| 37 | + volatile uint16_t tail = 0; //next pop position - NOTE: volatile to prevent compiler to optimize things away |
| 38 | + |
| 39 | +public: |
| 40 | + void begin(uint8_t *buf, uint16_t bufsize) { |
| 41 | + this->buf = buf; |
| 42 | + this->bufsize = bufsize; |
| 43 | + clear(); |
| 44 | + } |
| 45 | + |
| 46 | + //clear buffer |
| 47 | + void clear() { |
| 48 | + tail = head; |
| 49 | + } |
| 50 | + |
| 51 | + //number of bytes in buffer |
| 52 | + uint16_t len() { |
| 53 | + return (head >= tail ? head - tail : bufsize + head - tail); |
| 54 | + } |
| 55 | + |
| 56 | + //number of bytes free in buffer |
| 57 | + uint16_t free_space() { |
| 58 | + return bufsize - 1 - len(); |
| 59 | + } |
| 60 | + |
| 61 | + //push 1 byte in to the buffer - used in IRQ so store in RAM |
| 62 | + uint16_t __not_in_flash_func(push)(uint8_t c) { |
| 63 | + uint16_t next = inc(head); |
| 64 | + if(next == tail) return 0; //buffer full |
| 65 | + buf[head] = c; |
| 66 | + head = next; |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + //push up to data_len bytes |
| 71 | + uint16_t push(const uint8_t *data, size_t data_len) |
| 72 | + { |
| 73 | + //exit if no data |
| 74 | + if (data_len == 0 || data == nullptr) return 0; |
| 75 | + |
| 76 | + //get number of bytes available in ringbuf |
| 77 | + volatile uint16_t avail = free_space(); //volatile to prevent race conditions, available space could increase while this thread is processing |
| 78 | + if(data_len > avail) data_len = avail; |
| 79 | + |
| 80 | + if(data_len == 0) return 0; |
| 81 | + |
| 82 | + //get number of bytes before buf wraps |
| 83 | + uint16_t remaining = bufsize - head; //no need for volatile here, this thread is the only thread writing to head - NOTE: remaining can be larger than data_len, but this is normal |
| 84 | + |
| 85 | + if (data_len <= remaining) { |
| 86 | + memcpy(buf + head, data, data_len); |
| 87 | + if(data_len == remaining) { |
| 88 | + //Serial.printf("nowrap1 data_len=%d head=%d tail=%d remaining=%d\n", data_len, head, tail, remaining); |
| 89 | + head = 0; |
| 90 | + }else{ |
| 91 | + //Serial.printf("nowrap2 data_len=%d head=%d tail=%d remaining=%d\n", data_len, head, tail, remaining); |
| 92 | + head += data_len; |
| 93 | + } |
| 94 | + }else{ |
| 95 | + //Serial.printf("wrap data_len=%d head=%d tail=%d remaining=%d\n", data_len, head, tail, remaining); |
| 96 | + memcpy(buf + head, data, remaining); |
| 97 | + memcpy(buf, data + remaining, data_len - remaining); |
| 98 | + head = data_len - remaining; |
| 99 | + } |
| 100 | + return data_len; |
| 101 | + } |
| 102 | + |
| 103 | + //pop 1 byte from the buffer - used in IRQ so store in RAM |
| 104 | + uint16_t __not_in_flash_func(pop)(uint8_t *c) { |
| 105 | + if(head == tail) return 0; //buffer empty |
| 106 | + *c = buf[tail]; |
| 107 | + tail = inc(tail); |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + //pop up to data_len bytes |
| 112 | + uint16_t pop(uint8_t *data, size_t data_len) |
| 113 | + { |
| 114 | + //exit if no data |
| 115 | + if (data_len == 0 || data == nullptr) return 0; |
| 116 | + |
| 117 | + //get number of bytes in ringbuf |
| 118 | + volatile uint16_t avail = len(); //volatile to prevent race conditions, number of bytes in ringbuf could increase while this thread is processing |
| 119 | + if(data_len > avail) data_len = avail; |
| 120 | + |
| 121 | + if(data_len == 0) return 0; |
| 122 | + |
| 123 | + //get number of bytes before buf wraps |
| 124 | + uint16_t remaining = bufsize - tail; //no need for volatile here, this thread is the only thread writing to tail - NOTE: remaining can be larger than data_len, but this is normal |
| 125 | + |
| 126 | + if (data_len <= remaining) { |
| 127 | + memcpy(data, buf + tail, data_len); |
| 128 | + if(data_len == remaining) { |
| 129 | + //Serial.printf("nowrap1 data_len=%d head=%d tail=%d remaining=%d\n", data_len, head, tail, remaining); |
| 130 | + tail = 0; |
| 131 | + }else{ |
| 132 | + //Serial.printf("nowrap2 data_len=%d head=%d tail=%d remaining=%d\n", data_len, head, tail, remaining); |
| 133 | + tail += data_len; |
| 134 | + } |
| 135 | + }else{ |
| 136 | + //Serial.printf("wrap data_len=%d head=%d tail=%d remaining=%d\n", data_len, head, tail, remaining); |
| 137 | + memcpy(data, buf + tail, remaining); |
| 138 | + memcpy(data + remaining, buf, data_len - remaining); |
| 139 | + tail = data_len - remaining; |
| 140 | + } |
| 141 | + return data_len; |
| 142 | + } |
| 143 | + |
| 144 | +private: |
| 145 | + //increase a buffer position - used in IRQ so store in RAM |
| 146 | + uint16_t __not_in_flash_func(inc)( uint16_t v) { |
| 147 | + return (v < bufsize - 1 ? v + 1 : 0 ); |
| 148 | + } |
| 149 | +}; |
0 commit comments