forked from mtongnz/espDMX
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rdmFIFO.cpp
100 lines (71 loc) · 1.65 KB
/
rdmFIFO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "rdmFIFO.h"
rdmFIFO::rdmFIFO(void) {
init();
}
void rdmFIFO::init() {
RDMfifoMaxSize = RDM_MAX_RDM_QUEUE;
RDMfifoAllocated = 0;
RDMfifoSize = 0;
}
bool rdmFIFO::push(rdm_data* a) {
rdm_data* b;
b = resize(RDMfifoSize + 1);
if (b == NULL)
return false;
memcpy(b, a, sizeof(rdm_data));
// Do endian flip if its backwards
if (b->buffer[0] == E120_SC_SUB_MESSAGE && b->buffer[1] == E120_SC_RDM) {
b->endianFlip();
}
return true;
}
rdm_data* rdmFIFO::peek() {
if (isEmpty())
return NULL;
return content[0];
}
bool rdmFIFO::pop(rdm_data* a) {
if (isEmpty())
return false;
// Data stored in buffer big endian - esp8266 is little endian
content[0]->endianFlip();
memcpy(a, content[0], sizeof(rdm_data));
resize(RDMfifoSize - 1);
return true;
}
bool rdmFIFO::isEmpty() {
return (RDMfifoSize == 0);
}
bool rdmFIFO::notEmpty() {
return (RDMfifoSize != 0);
}
bool rdmFIFO::isFull() {
return (RDMfifoSize == RDMfifoMaxSize);
}
uint8_t rdmFIFO::count() {
return RDMfifoSize;
}
uint8_t rdmFIFO::space() {
return (RDMfifoMaxSize - RDMfifoSize);
}
rdm_data* rdmFIFO::resize(uint8_t s) {
if (s > RDMfifoMaxSize)
return NULL;
if (s < RDMfifoSize) {
//free(content[0]);
rdm_data* a = content[0];
for (uint8_t x = 1; x < RDMfifoSize; x++)
content[x-1] = content[x];
content[RDMfifoSize-1] = a;
}
if (s > RDMfifoAllocated) { // RDMfifoSize) {
if (!(content[s-1] = (rdm_data*)malloc(sizeof(rdm_data))))
return false;
RDMfifoAllocated = s;
}
RDMfifoSize = s;
return (content[s-1]);
}
void rdmFIFO::empty() {
RDMfifoSize = 0;
}