feat(ring_buffer): add ring_buffer struct

This commit is contained in:
Malo Lecomte 2021-07-16 18:07:57 +02:00
parent b33bf5aef6
commit f7246ffcdb
3 changed files with 38 additions and 1 deletions

@ -35,7 +35,8 @@ OBJS = \
idt.o \
isr.o \
pic.o \
pic/keyboard.o
pic/keyboard.o \
utils/ring_buffer.o
DEPS = $(OBJS:.o=.d)

18
k/utils/ring_buffer.c Normal file

@ -0,0 +1,18 @@
#include "ring_buffer.h"
uint8_t read_entry(struct ring_buffer *buf)
{
if (buf->read >= buf->write)
return 0;
uint8_t data = buf->buffer[buf->read];
buf->read = (buf->read + 1) % RING_BUFFER_SIZE;
return data;
}
void write_entry(struct ring_buffer *buf, uint8_t entry)
{
buf->buffer[buf->write] = entry;
buf->write = (buf->write + 1) % RING_BUFFER_SIZE;
}

18
k/utils/ring_buffer.h Normal file

@ -0,0 +1,18 @@
#ifndef RING_BUFFER_H
#define RING_BUFFER_H
#define RING_BUFFER_SIZE 32
#include <stdint.h>
struct ring_buffer
{
uint8_t buffer[RING_BUFFER_SIZE];
uint8_t read;
uint8_t write;
};
uint8_t read_entry(struct ring_buffer *buf);
void write_entry(struct ring_buffer *buf, uint8_t entry);
#endif /* !RING_BUFFER_H */