Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | 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
 
10
void buffer_init() {
11
    buffer_data->index_read = 0;
12
    buffer_data->index_write = 0;
13
    buffer_data->stored_length = 0;
14
}
15
 
16
char buffer_insert(unsigned char length, unsigned char *msg) {
17
    unsigned char i;
18
 
19
    // Make sure we have enough space to store message
20
    if (length > BUFFER_SIZE - buffer_data->stored_length) {
21
        DBG_PRINT_BUFFER("Buffer: (ERROR) Not enough free space for insert\r\n");
22
        return -1;
23
    }
24
 
25
    // Update the amount of used space in the buffer
26
    buffer_data->stored_length += length;
27
 
28
    // Copy data from msg to buffer
29
    for (i = 0; i < length; i++) {
30
        buffer1[buffer_data->index_write] = *(msg + i);
31
        buffer_data->index_write++; // Will automatically overflow to 0
32
    }
33
 
34
    return 0;
35
}
36
 
37
char buffer_read(unsigned char length, unsigned char *dest) {
38
    unsigned char i;
39
 
40
    // Make sure requested data is less than size of stored data
41
    if (length > buffer_data->stored_length) {
42
        DBG_PRINT_BUFFER("Buffer: (ERROR) Read length exceedes stored length\r\n");
43
        return -1;
44
    }
45
 
46
    // Update the amount of used space in the buffer
47
    buffer_data->stored_length -= length;
48
 
49
    // Copy data from buffer to dest
50
    for (i = 0; i < length; i++) {
51
        *(dest + i) = buffer1[buffer_data->index_read];
52
        buffer_data->index_read++;  // Will automatically overflow to 0
53
    }
54
 
55
    return 0;
56
}