Subversion Repositories Code-Repo

Rev

Rev 114 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 Kevin 1
#include "buffer.h"
2
#include "maindefs.h"
3
 
4
#pragma udata buffer
5
unsigned char buffer[BUFFER_SIZE];
6
#pragma udata
7
BUFFER_DATA *buffer_data;
8
 
9
void buffer_init(BUFFER_DATA *data) {
10
    buffer_data = data;
11
    buffer_data->index_read = 0;
12
    buffer_data->index_write = 0;
13
    buffer_data->stored_length = 0;
14
    buffer_data->buffer = buffer;
15
}
16
 
17
char buffer_insert_one(unsigned char c) {
18
    if (BUFFER_SIZE - buffer_data->stored_length == 0) {
19
        DBG_PRINT_BUFFER("Buffer: (ERROR) Not enough free space for insert\r\n");
20
        return -1;
21
    }
22
 
23
    // Update the amount of used space in the buffer
24
    buffer_data->stored_length += 1;
25
 
26
    // Copy data from msg to buffer
27
    buffer_data->buffer[buffer_data->index_write] = c;
28
    buffer_data->index_write++;
29
    if (buffer_data->index_write == BUFFER_SIZE) {
30
        buffer_data->index_write = 0;
31
    }
32
 
33
    return 0;
34
}
35
 
36
char buffer_insert(unsigned char length, unsigned char *msg) {
37
    unsigned char i;
38
 
39
    // Make sure we have enough space to store message
40
    if (length > BUFFER_SIZE - buffer_data->stored_length) {
41
        DBG_PRINT_BUFFER("Buffer: (ERROR) Not enough free space for insert\r\n");
42
        return -1;
43
    }
44
 
45
    // Update the amount of used space in the buffer
46
    buffer_data->stored_length += length;
47
 
48
    // Copy data from msg to buffer
49
    for (i = 0; i < length; i++) {
50
        buffer_data->buffer[buffer_data->index_write] = *(msg + i);
51
        buffer_data->index_write++;
52
        if (buffer_data->index_write == BUFFER_SIZE) {
53
            buffer_data->index_write = 0;
54
        }
55
    }
56
 
57
    return 0;
58
}
59
 
60
char buffer_read(unsigned char length, unsigned char *dest) {
61
    unsigned char i;
62
 
63
    // Make sure requested data is less than size of stored data
64
    if (length > buffer_data->stored_length) {
65
        DBG_PRINT_BUFFER("Buffer: (ERROR) Read length exceedes stored length\r\n");
66
        return -1;
67
    }
68
 
69
    // Update the amount of used space in the buffer
70
    buffer_data->stored_length -= length;
71
 
72
    // Copy data from buffer to dest
73
    for (i = 0; i < length; i++) {
74
        *(dest + i) = buffer_data->buffer[buffer_data->index_read];
75
        buffer_data->index_read++;
76
        if (buffer_data->index_read == BUFFER_SIZE) {
77
            buffer_data->index_read = 0;
78
        }
79
    }
80
 
81
    return 0;
82
}
83
 
84
unsigned int buffer_free_space() {
85
    return BUFFER_SIZE - buffer_data->stored_length;
86
}