Updated ring buffer
Updated ring buffer to match use case, one write head with negative read index
This commit is contained in:
@@ -59,6 +59,7 @@ target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
|||||||
# Add sources to executable
|
# Add sources to executable
|
||||||
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
|
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
|
||||||
# Add user sources here
|
# Add user sources here
|
||||||
|
Core/Src/ring_buffer.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Link directories setup
|
# Link directories setup
|
||||||
|
|||||||
@@ -6,9 +6,8 @@
|
|||||||
typedef struct ring_buffer_t {
|
typedef struct ring_buffer_t {
|
||||||
uint32_t buffer[256];
|
uint32_t buffer[256];
|
||||||
uint8_t w_head;
|
uint8_t w_head;
|
||||||
uint8_t r_head;
|
|
||||||
} ring_buffer_t;
|
} ring_buffer_t;
|
||||||
|
|
||||||
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value);
|
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value);
|
||||||
|
|
||||||
inline int ringBufferPop(ring_buffer_t *rb, uint32_t *output);
|
inline uint32_t ringBufferRead(ring_buffer_t *rb, uint8_t idx);
|
||||||
|
|||||||
@@ -9,20 +9,9 @@
|
|||||||
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value) {
|
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value) {
|
||||||
rb->buffer[rb->w_head] = value;
|
rb->buffer[rb->w_head] = value;
|
||||||
rb->w_head++;
|
rb->w_head++;
|
||||||
if (rb->w_head == rb->r_head) {
|
|
||||||
// if write head catches up to read head, crash program as consumer task
|
|
||||||
// should keep up with write head
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int ringBufferPop(ring_buffer_t *rb, uint32_t *output) {
|
inline uint32_t ringBufferRead(ring_buffer_t *rb, uint8_t idx) {
|
||||||
if (rb->w_head == rb->r_head) {
|
return rb->buffer[rb->w_head - 1 - idx];
|
||||||
// if read head is at write head, buffer is empty so nothing to pop
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
*output = rb->buffer[rb->r_head];
|
|
||||||
rb->r_head++;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user