-
Notifications
You must be signed in to change notification settings - Fork 12
/
a_Queue.ino
79 lines (70 loc) · 1.63 KB
/
a_Queue.ino
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
#define MEWPRO_BUFFER_LENGTH 64
byte queue[MEWPRO_BUFFER_LENGTH];
volatile int queueb = 0, queuee = 0;
boolean waiting = false; // don't read the next command from the queue
boolean serialfirst = false;
byte buf[MEWPRO_BUFFER_LENGTH], recv[MEWPRO_BUFFER_LENGTH];
int bufp = 1;
volatile int recvb = 0, recve = 0;
#define RECV(a) (recv[(recvb + (a)) % MEWPRO_BUFFER_LENGTH])
boolean inputAvailable()
{
if (!waiting && (queueb != queuee || Serial_available())) {
return true;
}
return false;
}
byte myRead()
{
if (serialfirst && Serial_available()) {
return Serial_read();
}
if (queueb != queuee) {
byte c = queue[queueb];
queueb = (queueb + 1) % MEWPRO_BUFFER_LENGTH;
serialfirst = false;
return c;
}
serialfirst = true;
return Serial_read();
}
// Utility functions
void queueIn(const __FlashStringHelper *p)
{
int i;
char c;
for (i = 0; (c = pgm_read_byte((char PROGMEM *)p + i)) != 0; i++) {
queue[(queuee + i) % MEWPRO_BUFFER_LENGTH] = c;
}
queue[(queuee + i) % MEWPRO_BUFFER_LENGTH] = '\n';
queuee = (queuee + i + 1) % MEWPRO_BUFFER_LENGTH;
}
void queueIn(const char *p)
{
int i;
char c;
for (i = 0; (c = *(p + i)) != 0; i++) {
queue[(queuee + i) % MEWPRO_BUFFER_LENGTH] = c;
}
queue[(queuee + i) % MEWPRO_BUFFER_LENGTH] = '\n';
queuee = (queuee + i + 1) % MEWPRO_BUFFER_LENGTH;
}
void __emptyInputBuffer()
{
while (inputAvailable()) {
if (myRead() == '\n') {
return;
}
}
}
void emptyQueue()
{
queueb = queuee = 0;
recvb = recve = 0;
bufp = 1;
waiting = false;
serialfirst = false;
while (Serial_available()) { // clear serial buffer
Serial_read();
}
}