circuitpython/py/ringbuf.c

146 lines
4.0 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
* Copyright (c) 2020 Dan Halbert for Adafruit Industries LLC
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.
*/
// CIRCUITPY-CHANGE: thoroughly reworked
2020-04-21 17:37:22 -04:00
#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) {
2023-08-11 00:30:34 -04:00
bool result = ringbuf_init(r, m_malloc(size), size);
2022-09-16 18:46:02 -04:00
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;
}
2023-01-23 12:40:00 -05:00
int ringbuf_put16(ringbuf_t *r, uint16_t v) {
if (r->size - r->used < 2) {
return -1;
}
ringbuf_put(r, (v >> 8) & 0xff);
ringbuf_put(r, v & 0xff);
return 0;
}
2020-04-21 17:37:22 -04:00
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;
}