#include #include "stdint.h" #include "circular_buffer.h" CB_StatusTypeDef cb_init(CB_TypeDef *cb, void *buffer, uint16_t capacity, uint8_t item_size) { if (buffer == NULL || cb == NULL) { return CB_ERROR; } cb->buffer = buffer; cb->buffer_end = (uint8_t *)cb->buffer + capacity * item_size; cb->capacity = capacity; cb->count = 0; cb->item_size = item_size; cb->head = cb->buffer; cb->tail = cb->buffer; return CB_OK; } CB_StatusTypeDef cb_push_back(CB_TypeDef *cb, const void *item) { if (cb == NULL) { return CB_ERROR; } if (cb->count == cb->capacity) { return CB_FULL; } memcpy(cb->head, item, cb->item_size); cb->head = (uint8_t *)cb->head + cb->item_size; if (cb->head == cb->buffer_end) cb->head = cb->buffer; cb->count++; return CB_OK; } CB_StatusTypeDef cb_pop_front(CB_TypeDef *cb, void *item) { if (cb == NULL) { return CB_ERROR; } if (cb->count == 0) { return CB_EMPTY; } memcpy(item, cb->tail, cb->item_size); cb->tail = (uint8_t *)cb->tail + cb->item_size; if (cb->tail == cb->buffer_end) cb->tail = cb->buffer; cb->count--; return CB_OK; }