modified ringbuffer

master
Hannes 14 years ago
parent b7392c545c
commit 5a4fe6ec84

@ -14,7 +14,7 @@
* PUT AND GET ARE NOT REENTRANT! * PUT AND GET ARE NOT REENTRANT!
*/ */
void ringbuf_init(ringbuf_t *rb, char* buf, int size){ void ringbuf_init(ringbuf_t *rb, uint16_t * buf, uint8_t size){
rb->startptr = buf; rb->startptr = buf;
rb->size = size; rb->size = size;
rb->readpos = 0; rb->readpos = 0;
@ -24,7 +24,7 @@ void ringbuf_init(ringbuf_t *rb, char* buf, int size){
/* pushes a value onto the ringbuffer /* pushes a value onto the ringbuffer
* returns 0 on success, 1 on buffer full * returns 0 on success, 1 on buffer full
*/ */
uint8_t ringbuf_put(ringbuf_t *rb, uint8_t value){ uint8_t ringbuf_put(ringbuf_t *rb, uint16_t value){
// calculate next ptr pos // calculate next ptr pos
uint8_t next = rb->writepos + 1; uint8_t next = rb->writepos + 1;
if(next >= rb->size){ if(next >= rb->size){
@ -41,10 +41,10 @@ uint8_t ringbuf_put(ringbuf_t *rb, uint8_t value){
} }
/* gets a value from the ringbuffer /* gets a value from the ringbuffer
* returns value on success, -1 on buffer empty * returns 0 on success, -1 on buffer empty
*/ */
int16_t ringbuf_get(ringbuf_t *rb){ int8_t ringbuf_get(ringbuf_t *rb, uint16_t* data){
uint8_t value; //uint16_t value;
uint8_t next; uint8_t next;
// calculate next ptr pos // calculate next ptr pos
next = rb->readpos + 1; next = rb->readpos + 1;
@ -55,12 +55,14 @@ int16_t ringbuf_get(ringbuf_t *rb){
if(rb->readpos == rb->writepos){ if(rb->readpos == rb->writepos){
return(-1); return(-1);
} }
value = *(rb->startptr + rb->readpos); *data = *(rb->startptr + rb->readpos);
rb->readpos = next; rb->readpos = next;
return(value); return(0);
} }

@ -4,15 +4,15 @@
#include <stdint.h> #include <stdint.h>
typedef struct { typedef struct {
char* startptr; uint16_t* startptr;
uint8_t size; uint16_t size;
uint8_t readpos; uint16_t readpos;
uint8_t writepos; uint16_t writepos;
} ringbuf_t; } ringbuf_t;
void ringbuf_init(ringbuf_t* rb, char* buf, int size); void ringbuf_init(ringbuf_t* rb, uint16_t* buf, uint8_t size);
uint8_t ringbuf_put(ringbuf_t *rb, uint8_t value); uint8_t ringbuf_put(ringbuf_t *rb, uint16_t value);
int16_t ringbuf_get(ringbuf_t *rb); int8_t ringbuf_get(ringbuf_t *rb, uint16_t* data);
#endif #endif

Loading…
Cancel
Save