#ifndef _Cirule_Buffer_h__
#define _Cirule_Buffer_h__

#include <stdint.h>
#ifndef min
#define min(x,y)(x<y)?x:y;
#endif

#define CBUFF_SUCCESS 0
#define CBUFF_FULL -1
#define CBUFF_EMPTY -2

typedef struct {
	uint8_t * buffer;
	int16_t buffer_len;
	int16_t w_pos;
	int16_t r_pos;
}c_buffer_t;

//used by dma
static __inline__ void circle_update_write_position(c_buffer_t *cbuff, int pos){
	cbuff->w_pos = pos;
}

static __inline__ int circle_get_read_position(c_buffer_t *cbuff){
	return cbuff->r_pos;
}
static __inline__ int circle_get_write_space(c_buffer_t *cbuff){
	int16_t size = (cbuff->w_pos >= cbuff->r_pos)?(cbuff->buffer_len - cbuff->w_pos + cbuff->r_pos):(cbuff->r_pos - cbuff->w_pos);
	size -= 1;
	if (size <= 0){
		return CBUFF_FULL;
	}
	return size;
}

void circle_buffer_init(c_buffer_t *cbuff, uint8_t *buffer, int16_t max_len);
void circle_reset(c_buffer_t *cbuff);
int circle_put_one_data(c_buffer_t *cbuff, uint8_t data);
int circle_put_data(c_buffer_t *cbuff, uint8_t *data, int16_t len);
int circle_get_one_data(c_buffer_t *cbuff, uint8_t *data);
int circle_get_data(c_buffer_t *cbuffer, uint8_t *data, int16_t len);

#endif /* _Cirule_Buffer_h__ */

