neptunium-firmware/Drivers/ubx_parser/ringbuffer_char.c
Peter aa3b50eed2 Initial commit. Logging using tinyusb on stm32f302c8t6. WORKING:
WORKING: lps22hb, lsm6dsox, neo-m9n
TODO: sx1262, SD card, freertos
2024-07-06 04:25:33 +08:00

149 lines
4.1 KiB
C

/* The MIT License (MIT)
Copyright (c) 2014 Anders Kalør
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 "ringbuffer_char.h"
/**
* @file
* Implementation of ring buffer functions.
*/
void ring_buffer_init(ring_buffer_t *buffer)
{
buffer->tail_index = 0;
buffer->head_index = 0;
}
void ring_buffer_queue(ring_buffer_t *buffer, char data)
{
/* Is buffer full? */
if (ring_buffer_is_full(buffer))
{
/* Is going to overwrite the oldest byte */
/* Increase tail index */
buffer->tail_index = ((buffer->tail_index + 1) & RING_BUFFER_MASK);
}
/* Place data in buffer */
buffer->buffer[buffer->head_index] = data;
buffer->head_index = ((buffer->head_index + 1) & RING_BUFFER_MASK);
}
void ring_buffer_queue_arr(ring_buffer_t *buffer, const char *data, ring_buffer_size_t size)
{
/* Add bytes; one by one */
ring_buffer_size_t i;
for (i = 0; i < size; i++)
{
ring_buffer_queue(buffer, data[i]);
}
}
uint8_t ring_buffer_dequeue(ring_buffer_t *buffer, char *data)
{
if (ring_buffer_is_empty(buffer))
{
/* No items */
return 0;
}
*data = buffer->buffer[buffer->tail_index];
buffer->tail_index = ((buffer->tail_index + 1) & RING_BUFFER_MASK);
return 1;
}
uint8_t ring_buffer_dequeue_arr(ring_buffer_t *buffer, char *data, ring_buffer_size_t len)
{
if (ring_buffer_is_empty(buffer))
{
/* No items */
return 0;
}
char *data_ptr = data;
ring_buffer_size_t cnt = 0;
while ((cnt < len) && ring_buffer_dequeue(buffer, data_ptr))
{
cnt++;
data_ptr++;
}
return cnt;
}
uint8_t ring_buffer_peek(ring_buffer_t *buffer, char *data, ring_buffer_size_t index)
{
if (index >= ring_buffer_num_items(buffer))
{
/* No items at index */
return 0;
}
/* Add index to pointer */
ring_buffer_size_t data_index = ((buffer->tail_index + index) & RING_BUFFER_MASK);
*data = buffer->buffer[data_index];
return 1;
}
/**
* Returns whether a ring buffer is empty.
* @param buffer The buffer for which it should be returned whether it is empty.
* @return 1 if empty; 0 otherwise.
*/
uint8_t ring_buffer_is_empty(ring_buffer_t *buffer)
{
return (buffer->head_index == buffer->tail_index);
}
/**
* Returns whether a ring buffer is full.
* @param buffer The buffer for which it should be returned whether it is full.
* @return 1 if full; 0 otherwise.
*/
uint8_t ring_buffer_is_full(ring_buffer_t *buffer)
{
return ((buffer->head_index - buffer->tail_index) & RING_BUFFER_MASK) == RING_BUFFER_MASK;
}
/**
* Returns the number of items in a ring buffer.
* @param buffer The buffer for which the number of items should be returned.
* @return The number of items in the ring buffer.
*/
ring_buffer_size_t ring_buffer_num_items(ring_buffer_t *buffer)
{
ring_buffer_size_t n = ((buffer->head_index - buffer->tail_index) & RING_BUFFER_MASK);
return n;
}
/* Dequeue an array, no return (only moving the tail) */
uint8_t ring_buffer_dequeue_arr_no_ret(ring_buffer_t *buffer, ring_buffer_size_t len)
{
if (ring_buffer_is_empty(buffer))
{
/* No items */
return 0;
}
buffer->tail_index = ((buffer->tail_index + len) & RING_BUFFER_MASK);
return 1;
}