circuitpython/py/ringbuf.c

189 lines
5.4 KiB
C
Raw Normal View History

2020-04-21 17:37:22 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Paul Sokolovsky
* Copyright (c) 2019 Jim Mussared
2020-04-21 17:37:22 -04:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "ringbuf.h"
2022-09-16 18:46:02 -04:00
bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t size) {
r->buf = buf;
2022-09-16 18:46:02 -04:00
r->size = size;
r->used = 0;
r->next_read = 0;
r->next_write = 0;
return r->buf != NULL;
}
2022-09-16 18:46:02 -04:00
// Dynamic initialization. This should be accessible from a root pointer..
bool ringbuf_alloc(ringbuf_t *r, size_t size, bool long_lived) {
bool result = ringbuf_init(r, gc_alloc(size, false, long_lived), size);
return result;
2020-04-21 17:37:22 -04:00
}
2022-09-16 18:46:02 -04:00
void ringbuf_deinit(ringbuf_t *r) {
// Free buf by doing nothing and letting gc take care of it. If the VM has finished already,
// this will be safe.
r->buf = (uint8_t *)NULL;
2020-04-21 22:40:12 -04:00
r->size = 0;
ringbuf_clear(r);
}
2022-09-16 18:46:02 -04:00
size_t ringbuf_size(ringbuf_t *r) {
return r->size;
2020-04-21 17:37:22 -04:00
}
2022-09-16 18:46:02 -04:00
// Return -1 if buffer is empty, else return byte fetched.
2020-04-21 17:37:22 -04:00
int ringbuf_get(ringbuf_t *r) {
2022-09-16 18:46:02 -04:00
if (r->used < 1) {
2020-04-21 17:37:22 -04:00
return -1;
}
2022-09-16 18:46:02 -04:00
uint8_t v = r->buf[r->next_read];
r->next_read++;
if (r->next_read >= r->size) {
r->next_read = 0;
2020-04-21 17:37:22 -04:00
}
2022-09-16 18:46:02 -04:00
r->used--;
2020-04-21 17:37:22 -04:00
return v;
}
int ringbuf_get16(ringbuf_t *r) {
2022-09-16 18:46:02 -04:00
if (r->used < 2) {
return -1;
}
2022-09-16 18:46:02 -04:00
int high_byte = ringbuf_get(r);
int low_byte = ringbuf_get(r);
return (high_byte << 8) | low_byte;
}
2022-09-16 18:46:02 -04:00
// Return -1 if no room in buffer, else return 0.
2020-04-21 17:37:22 -04:00
int ringbuf_put(ringbuf_t *r, uint8_t v) {
2022-09-16 18:46:02 -04:00
if (r->used >= r->size) {
2020-04-21 17:37:22 -04:00
return -1;
}
2022-09-16 18:46:02 -04:00
r->buf[r->next_write] = v;
r->next_write++;
if (r->next_write >= r->size) {
r->next_write = 0;
}
r->used++;
2020-04-21 17:37:22 -04:00
return 0;
}
void ringbuf_clear(ringbuf_t *r) {
2022-09-16 18:46:02 -04:00
r->next_write = 0;
r->next_read = 0;
r->used = 0;
2020-04-21 17:37:22 -04:00
}
// Number of free slots that can be written.
2020-04-21 22:40:12 -04:00
size_t ringbuf_num_empty(ringbuf_t *r) {
2022-09-16 18:46:02 -04:00
return r->size - r->used;
2020-04-21 17:37:22 -04:00
}
// Number of bytes available to read.
2020-04-21 22:40:12 -04:00
size_t ringbuf_num_filled(ringbuf_t *r) {
2022-09-16 18:46:02 -04:00
return r->used;
2020-04-21 17:37:22 -04:00
}
// If the ring buffer fills up, not all bytes will be written.
// Returns how many bytes were successfully written.
size_t ringbuf_put_n(ringbuf_t *r, const uint8_t *buf, size_t bufsize) {
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < bufsize; i++) {
if (ringbuf_put(r, buf[i]) < 0) {
2020-04-21 17:37:22 -04:00
// If ringbuf is full, give up and return how many bytes
// we wrote so far.
return i;
}
}
return bufsize;
}
// Returns how many bytes were fetched.
2021-03-15 09:57:36 -04:00
size_t ringbuf_get_n(ringbuf_t *r, uint8_t *buf, size_t bufsize) {
for (size_t i = 0; i < bufsize; i++) {
2020-04-21 17:37:22 -04:00
int b = ringbuf_get(r);
if (b < 0) {
return i;
}
buf[i] = b;
}
return bufsize;
}
int ringbuf_put16(ringbuf_t *r, uint16_t v) {
2022-09-16 18:46:02 -04:00
if (r->size - r->used < 2) {
return -1;
}
2022-09-16 18:46:02 -04:00
ringbuf_put(r, (v >> 8) & 0xff);
ringbuf_put(r, v & 0xff);
return 0;
}
2023-01-20 02:57:00 -05:00
// Returns:
// 1: Success
// 0: Not enough data available to complete read (try again later)
// -1: Requested read is larger than buffer - will never succeed
int ringbuf_read(ringbuf_t *r, void *data, size_t data_len) {
if (ringbuf_avail(r) < data_len) {
return (r->size <= data_len) ? -1 : 0;
}
uint32_t iget = r->next_read;
uint32_t iget_a = (iget + data_len) % r->size;
uint8_t *datap = data;
if (iget_a < iget) {
// Copy part of the data from the space left at the end of the buffer
memcpy(datap, r->buf + iget, r->size - iget);
datap += (r->size - iget);
iget = 0;
}
memcpy(datap, r->buf + iget, iget_a - iget);
r->next_read = iget_a;
return 1;
}
// Returns:
// 1: Success
// 0: Not enough free space available to complete write (try again later)
// -1: Requested write is larger than buffer - will never succeed
int ringbuf_write(ringbuf_t *r, const void *data, size_t data_len) {
if (ringbuf_free(r) < data_len) {
return (r->size <= data_len) ? -1 : 0;
}
uint32_t iput = r->next_write;
uint32_t iput_a = (iput + data_len) % r->size;
const uint8_t *datap = data;
if (iput_a < iput) {
// Copy part of the data to the end of the buffer
memcpy(r->buf + iput, datap, r->size - iput);
datap += (r->size - iput);
iput = 0;
}
memcpy(r->buf + iput, datap, iput_a - iput);
r->next_write = iput_a;
return 1;
}