Blame | Last modification | View Log | Download | RSS feed
#include "buffer.h"#include "maindefs.h"#pragma udata buffer1static unsigned char buffer1[256];#pragma udatastatic BUFFER_DATA *buffer_data;void buffer_init(BUFFER_DATA *data) {buffer_data = data;buffer_data->index_read = 0;buffer_data->index_write = 0;buffer_data->stored_length = 0;}char buffer_insert(unsigned char length, unsigned char *msg) {unsigned char i;// Make sure we have enough space to store messageif (length > BUFFER_SIZE - buffer_data->stored_length) {DBG_PRINT_BUFFER("Buffer: (ERROR) Not enough free space for insert\r\n");return -1;}// Update the amount of used space in the bufferbuffer_data->stored_length += length;// Copy data from msg to bufferfor (i = 0; i < length; i++) {buffer1[buffer_data->index_write] = *(msg + i);buffer_data->index_write++; // Will automatically overflow to 0}return 0;}char buffer_read(unsigned char length, unsigned char *dest) {unsigned char i;// Make sure requested data is less than size of stored dataif (length > buffer_data->stored_length) {DBG_PRINT_BUFFER("Buffer: (ERROR) Read length exceedes stored length\r\n");return -1;}// Update the amount of used space in the bufferbuffer_data->stored_length -= length;// Copy data from buffer to destfor (i = 0; i < length; i++) {*(dest + i) = buffer1[buffer_data->index_read];buffer_data->index_read++; // Will automatically overflow to 0}return 0;}