-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBee.cpp
136 lines (120 loc) · 3.19 KB
/
Bee.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Bee.h"
#include "resp.h"
// Bee constructor initializes variables that are used to determine the state
// within Next.
Bee::Bee()
{
this->cursor = 0;
}
// replyOK writes a status message with value RESP_OK to the connection
// channel.
void Bee::replyOK()
{
this->Write((unsigned char *)RESP_OK, 5);
}
// replyError writes a status message with value RESP_ERROR to the connection
// channel.
void Bee::replyError()
{
this->Write((unsigned char *)RESP_ERROR, 6);
}
// OnMessage implements default actions when receiving messages.
respObject* Bee::OnMessage(respObject *in)
{
int pin;
if (RESP_TOKEN_EQUALS(in, 0, (unsigned char *)"SET")) {
// SET {pin} {ON|OFF}
pin = RESP_TOKEN_TO_INT(in, 1);
pinMode(pin, OUTPUT);
if (RESP_TOKEN_EQUALS(in, 2, "1")) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
// Returns status OK.
return createRespString(RESP_OBJECT_STATUS, (unsigned char *)"OK");
} else if (RESP_TOKEN_EQUALS(in, 0, "GET")) {
// GET {pin}
pin = RESP_TOKEN_TO_INT(in, 1);
pinMode(pin, INPUT);
// Returns integer.
return createRespInteger(digitalRead(pin));
} else if (RESP_TOKEN_EQUALS(in, 0, "READ")) {
// READ {pin}
pin = RESP_TOKEN_TO_INT(in, 1);
pinMode(pin, INPUT);
// Returns integer.
return createRespInteger(analogRead(pin));
} else if (RESP_TOKEN_EQUALS(in, 0, "WRITE")) {
// WRITE {pin} {0-255 value}
pin = RESP_TOKEN_TO_INT(in, 1);
pinMode(pin, OUTPUT);
analogWrite(pin, RESP_TOKEN_TO_INT(in, 2));
// Returns status OK.
return createRespString(RESP_OBJECT_STATUS, (unsigned char*)"OK");
}
// If nothing matched, return NULL, a NULL value will be catched by Next and
// transformed into an error.
return NULL;
}
// NextMessage creates a RESP message with consecutive calls to Read.
bool Bee::NextMessage()
{
int i, ok;
unsigned char c;
bool gotLine;
respObject *in = NULL;
respObject *out = NULL;
gotLine = false;
// Cleaning initial buffer.
if (this->cursor == 0) {
for (i = 0; i < BUFLEN; i++) {
this->buf[i] = 0;
}
}
// Reading one byte at a time and advancing cursor.
for (gotLine = false; !gotLine && this->Read(&c); this->cursor++) {
if (this->cursor >= BUFLEN) {
this->replyError();
this->cursor = 0;
return false;
}
this->buf[this->cursor] = c;
if (this->cursor > 0) {
if (this->buf[this->cursor] == '\n' && this->buf[this->cursor - 1] == '\r') {
gotLine = true;
}
}
}
// Processing line.
if (gotLine) {
// Attempt to decode message.
ok = respDecode(&in, this->buf);
if (ok < 0) {
Serial.print("RESP: ");
Serial.println(ok, DEC);
if (ok != RESP_ERROR_INCOMPLETE_MESSAGE) {
this->cursor = 0;
this->replyError();
}
return false;
}
out = this->OnMessage(in);
freeRespObject(in);
if (out == NULL) {
this->cursor = 0;
this->replyError();
return false;
}
ok = respEncode(out, this->buf);
if (ok > 0) {
this->Write(this->buf, ok);
} else {
this->replyError();
}
freeRespObject(out);
this->cursor = 0;
return true;
}
return false;
}