Subversion Repositories Code-Repo

Rev

Rev 111 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
111 Kevin 1
#include "buffer.h"
2
#include "maindefs.h"
3
 
4
#pragma udata buffer1
5
static unsigned char buffer1[256];
6
#pragma udata
7
 
8
static BUFFER_DATA *buffer_data;
9
 
112 Kevin 10
void buffer_init(BUFFER_DATA *data) {
11
    buffer_data = data;
111 Kevin 12
    buffer_data->index_read = 0;
13
    buffer_data->index_write = 0;
14
    buffer_data->stored_length = 0;
15
}
16
 
17
char buffer_insert(unsigned char length, unsigned char *msg) {
18
    unsigned char i;
19
 
20
    // Make sure we have enough space to store message
21
    if (length > BUFFER_SIZE - buffer_data->stored_length) {
22
        DBG_PRINT_BUFFER("Buffer: (ERROR) Not enough free space for insert\r\n");
23
        return -1;
24
    }
25
 
26
    // Update the amount of used space in the buffer
27
    buffer_data->stored_length += length;
28
 
29
    // Copy data from msg to buffer
30
    for (i = 0; i < length; i++) {
31
        buffer1[buffer_data->index_write] = *(msg + i);
32
        buffer_data->index_write++; // Will automatically overflow to 0
33
    }
34
 
35
    return 0;
36
}
37
 
38
char buffer_read(unsigned char length, unsigned char *dest) {
39
    unsigned char i;
40
 
41
    // Make sure requested data is less than size of stored data
42
    if (length > buffer_data->stored_length) {
43
        DBG_PRINT_BUFFER("Buffer: (ERROR) Read length exceedes stored length\r\n");
44
        return -1;
45
    }
46
 
47
    // Update the amount of used space in the buffer
48
    buffer_data->stored_length -= length;
49
 
50
    // Copy data from buffer to dest
51
    for (i = 0; i < length; i++) {
52
        *(dest + i) = buffer1[buffer_data->index_read];
53
        buffer_data->index_read++;  // Will automatically overflow to 0
54
    }
55
 
56
    return 0;
57
}