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