-
Notifications
You must be signed in to change notification settings - Fork 0
/
busyboard.h
55 lines (53 loc) · 1.22 KB
/
busyboard.h
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
#pragma once
#include "publishedVar.h"
namespace bb
{
char rxByte;
char prevByte = '\0';
uint8_t rxIndex = 0;
enum : byte {RESET,TYPE,MSG};
uint8_t state = RESET;
uint8_t activeTopic = -1;
bool receiveMessage(uint8_t &topic, char * rxBytes)
{
bool newMessage = false;
if(Serial.available())
{
rxByte = Serial.read();
if(prevByte == '$' and rxByte == '{')
{
state = TYPE;
rxIndex = 0;
rxBytes[0] = '\0';
}
else if(rxByte == '}')
{
state = RESET;
rxBytes[rxIndex] = '\0';
topic = activeTopic;
rxIndex = 0;
newMessage = true;
}
else if(state == TYPE)
{
if(isDigit(rxByte))
{
activeTopic = String(rxByte).toInt();
state = MSG;
}
else
state = RESET;
}
else if(state == MSG)
{
if(rxByte != ',')
{
rxBytes[rxIndex] = rxByte;
rxIndex += 1;
}
}
}
prevByte = rxByte;
return newMessage;
}
}