forked from CisecoPlc/LLAPSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLAPSerial.cpp
295 lines (268 loc) · 8.42 KB
/
LLAPSerial.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// Modified LLAPSerial library to remove the power / sleep elemets of the class
//
#include "LLAPSerial.h"
/*
const CTable __code Commands[] = {
{9,{'A','C','K','-','-','-','-','-','-'},cmdAck}, // must be the first entry
{0,{'A','P','V','E','R','-','-','-','-'},cmdPVer}, // Protocol version
{0,{'D','E','V','T','Y','P','E','-','-'},cmdDevType}, // Device Type
{0,{'D','E','V','N','A','M','E','-','-'},cmdDevName}, // Device Name
{0,{'H','E','L','L','O','-','-','-','-'},cmdHello}, // Echo
{3,{'S','E','R','#','#','#','#','#','#'},cmdSer}, // Serial Number
{0,{'$','E','R','-','-','-','-','-','-'},cmdSerReset}, // Serial Number
{0,{'F','V','E','R','-','-','-','-','-'},cmdFVer}, // Software revision
{7,{'C','H','D','E','V','I','D','#','#'},cmdDevID}, // Device ID
{5,{'P','A','N','I','D','#','#','#','#'},cmdPanID}, // PANID
{0,{'R','E','B','O','O','T','-','-','-'},cmdReset}, // reset
{7,{'R','E','T','R','I','E','S','#','#'},cmdRetries}, // set # retrys
{4,{'B','A','T','T','-','-','-','-','-'},cmdBatt}, // request battery voltage
{4,{'S','A','V','E','-','-','-','-','-'},cmdSave}, // Save config to flash
#if defined(APSLEEP) // cyclic sleep
{0,{'I','N','T','V','L','#','#','#','#'},cmdInterval}, // SET cyclic sleep interval - 999S - three digits + timescale
// T=mS, S=S, M=mins, H=Hours, D=days
{0,{'C','Y','C','L','E','-','-','-','-'},cmdCyclic}, // activate cyclic sleep
{0,{'W','A','K','E','-','-','-','-','-'},cmdDeactivate}, // deactivate programmed behaviour (cyclic sleep etc)
{5,{'W','A','K','E','C','#','#','#','-'},cmdSetSleepCount}, // set the sleep count until awake is sent
#endif
// allow all device to do a one shot sleep (including DALLAS)
{0,{'S','L','E','E','P','#','#','#','#'},cmdActivate}, // activate Sleeping mode - one shot or sleep until interrupt
*/
void LLAPSerial::init()
{
sMessage.reserve(10);
bMsgReceived = false;
deviceId[0] = '-';
deviceId[1] = '-';
}
void LLAPSerial::init(char* dID)
{
init();
bMsgReceived = false;
setDeviceId(dID);
cMessage[12]=0; // ensure terminated
}
void LLAPSerial::processMessage(){
//if (LLAP.cMessage[0] != 'a') return; //not needed as already checked
if (cMessage[1] != deviceId[0]) return;
if (cMessage[2] != deviceId[1]) return;
// now we have LLAP.cMessage[3] to LLAP.cMessage[11] as the actual message
sMessage = String(&cMessage[3]); // let the main program deal with it
bMsgReceived = true;
}
void LLAPSerial::SerialEvent()
{
if (bMsgReceived) return; //get out if previous message not yet processed
if (Serial.available() >= 12) {
// get the new byte:
char inChar = (char)Serial.peek();
if (inChar == 'a') {
for (byte i = 0; i<12; i++) {
inChar = (char)Serial.read();
cMessage[i] = inChar;
if (i < 11 && Serial.peek() == 'a') {
// out of synch so abort and pick it up next time round
return;
}
}
cMessage[12]=0;
processMessage();
}
else
Serial.read(); // throw away the character
}
}
void LLAPSerial::sendMessage(String sToSend)
{
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < sToSend.length())
cMessage[i+3] = sToSend.charAt(i);
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::sendMessage(char* sToSend)
{
sendMessage(sToSend,NULL);
}
void LLAPSerial::sendMessage(char* sToSend, char* valueToSend)
{
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < strlen(sToSend))
cMessage[i+3] = sToSend[i];
else if (i < strlen(sToSend) + strlen(valueToSend))
cMessage[i+3] = valueToSend[i - strlen(sToSend)];
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::sendMessage(String sToSend, char* valueToSend)
{
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < sToSend.length())
cMessage[i+3] = sToSend.charAt(i);
else if (i < sToSend.length() + strlen(valueToSend))
cMessage[i+3] = valueToSend[i - sToSend.length()];
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
// This appears to enable flag memeory to be used rather than program !
void LLAPSerial::sendMessage(const __FlashStringHelper *ifsh)
{
sendMessage(ifsh,NULL);
}
// Send message with padding -
void LLAPSerial::sendMessage(const __FlashStringHelper *ifsh, char* valueToSend)
{
const char PROGMEM *p = (const char PROGMEM *)ifsh;
byte eos = 0;
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (!eos)
{
cMessage[i+3] = pgm_read_byte(p++);
if (!cMessage[i+3]) // end of string
{
eos = i-3;
}
}
if (eos)
{
if (i < eos + strlen(valueToSend))
cMessage[i+3] = valueToSend[i - eos];
else
cMessage[i+3] = '-';
}
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::sendInt(String sToSend, int value)
{
// 111
// 0123456789012
// aAA0123456789
// aAA-32767----
// aAA32768-----
char cValue[7]; // long enough for -32767 to 32768 and the trailing zero
itoa(value, cValue,10);
byte cValuePtr = 0;
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < sToSend.length())
cMessage[i+3] = sToSend.charAt(i);
else if (cValuePtr < 7 && cValue[cValuePtr] !=0)
cMessage[i+3] = cValue[cValuePtr++];
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::sendIntWithDP(String sToSend, int value, byte decimalPlaces)
{
// 111
// 0123456789012
// aAA0123456789
// aAA-3276.7---
// aAA3276.8----
char cValue[8]; // long enough for -3276.7 to 3276.8 and the trailing zero
byte cValuePtr=0;
itoa(value, cValue,10);
char* cp = &cValue[strlen(cValue)];
*(cp+1) = 0; // new terminator
while (decimalPlaces-- && --cp )
{
*(cp+1) = *cp;
}
*cp = '.';
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < sToSend.length())
cMessage[i+3] = sToSend.charAt(i);
else if (cValuePtr < 8 && cValue[cValuePtr] !=0)
cMessage[i+3] = cValue[cValuePtr++];
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::sendIntWithPad(String sToSend, int value, byte length)
{
// 111
// 0123456789012
// aAAcccc-32767
// aAAcccc000327
char cValue[7]; // long enough for -32767 to 32768 and the trailing zero
itoa(value, cValue,10);
byte cValuePtr = 0;
cMessage[0] = 'a';cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < sToSend.length())
cMessage[i+3] = sToSend.charAt(i);
else if (i < sToSend.length() + length - strlen(cValue))
cMessage[i+3] = '0';
else if (cValuePtr < 7 && cValue[cValuePtr] !=0)
cMessage[i+3] = cValue[cValuePtr++];
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::sendIntWithTerminator(String sToSend, int value, byte length, char terminator)
{
// 111
// 0123456789012
// aAA0123456789
// aAA-32767----
char cValue[7]; // long enough for -32767 to 32768 and the trailing zero
itoa(value, cValue,10);
byte cValuePtr = 0;
cMessage[0] = 'a';
cMessage[1] = deviceId[0];
cMessage[2] = deviceId[1];
for (byte i = 0; i<9; i++) {
if (i < sToSend.length())
cMessage[i+3] = sToSend.charAt(i);
else if (i < sToSend.length() + length - strlen(cValue))
cMessage[i+3] = '0';
else if (cValuePtr < 7 && cValue[cValuePtr] !=0)
cMessage[i+3] = cValue[cValuePtr++];
else if (cValue[cValuePtr] == 0)
cMessage[i+3] = terminator;
else
cMessage[i+3] = '-';
}
Serial.print(cMessage);
Serial.flush();
}
void LLAPSerial::setDeviceId(char* cId)
{
deviceId[0] = cId[0];
deviceId[1] = cId[1];
}