-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathFlowSerialRead.h
More file actions
80 lines (70 loc) · 1.56 KB
/
FlowSerialRead.h
File metadata and controls
80 lines (70 loc) · 1.56 KB
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
#define FlowSerialBegin Serial.begin
#define FlowSerialAvailable Serial.available
#define FlowSerialFlush Serial.flush
int FlowSerialPosition = 0;
int FlowSerialTrigger = 8;
long fsr_startMillis;
int FlowSerialRead() {
FlowSerialPosition++;
if (FlowSerialPosition == FlowSerialTrigger) {
FlowSerialPosition = 0;
Serial.write(0x01);
Serial.flush();
}
return Serial.read();
}
void FlowSerialWrite(uint8_t data) {
Serial.write(0x02);
Serial.write(data);
}
void FlowSerialPrint(char data)
{
FlowSerialWrite(data);
}
void FlowSerialPrint(String& data)
{
int len = data.length();
for (int x = 0; x < len; x++)
FlowSerialWrite(data.charAt(x));
}
void FlowSerialDebugPrintLn(String& data)
{
Serial.write(0x15);
Serial.println(data);
}
void FlowSerialDebugPrintLn(const char str[]) {
Serial.write(0x15);
Serial.println(str);
}
void FlowSerialPrint(const char str[]) {
for (int x = 0; x < strlen(str); x++)
FlowSerialWrite(str[x]);
}
int FlowSerialTimedRead()
{
int c;
fsr_startMillis = millis();
do {
c = Serial.read();
if (c >= 0) {
FlowSerialPosition++;
if (FlowSerialPosition == FlowSerialTrigger) {
FlowSerialPosition = 0;
Serial.write(0x01);
Serial.flush();
}
return c;
}
} while (millis() - fsr_startMillis < 100);
return -1; // -1 indicates timeout
}
String FlowSerialReadStringUntil(char terminator) {
String ret;
int c = FlowSerialTimedRead();
while (c >= 0 && c != terminator)
{
ret += (char)c;
c = FlowSerialTimedRead();
}
return ret;
}