-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLLAPSerial.cpp
376 lines (338 loc) · 11.4 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
//
//
#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
if (0 == strncmp_P(&cMessage[3],PSTR("HELLO----"),9)) {
Serial.print(cMessage); // echo the message
} else if (0 == strncmp_P(&cMessage[3],PSTR("CHDEVID"),7)) {
if (strchr_P(PSTR("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ"), cMessage[10]) != 0 && strchr_P(PSTR("-#@?\\*ABCDEFGHIJKLMNOPQRSTUVWXYZ"), cMessage[11]) != 0)
{
deviceId[0] = cMessage[10];
deviceId[1] = cMessage[11];
Serial.print(cMessage); // echo the message
}
} else {
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(const __FlashStringHelper *ifsh)
{
sendMessage(ifsh,NULL);
}
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)
{
char cValue[7]; // long enough for -32767 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)
{
char cValue[8]; // long enough for -3276.7 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::setDeviceId(char* cId)
{
deviceId[0] = cId[0];
deviceId[1] = cId[1];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This power-saving code was shamelessly stolen from the Jeelabs library with slight modification.
// see https://github.com/jcw/jeelib
// The watchdog timer is only about 10% accurate - varies between chips
#include <avr/sleep.h>
#include <util/atomic.h>
static volatile byte watchdogCounter;
void watchdogEvent() {
++watchdogCounter;
}
ISR(WDT_vect) { watchdogEvent(); }
void watchdogInterrupts (char mode) {
// correct for the fact that WDP3 is *not* in bit position 3!
if (mode & bit(3))
mode ^= bit(3) | bit(WDP3);
// pre-calculate the WDTCSR value, can't do it inside the timed sequence
// we only generate interrupts, no reset
byte wdtcsr = mode >= 0 ? bit(WDIE) | mode : 0;
MCUSR &= ~(1<<WDRF);
ATOMIC_BLOCK(ATOMIC_FORCEON) {
#ifndef WDTCSR
#define WDTCSR WDTCR
#endif
WDTCSR |= (1<<WDCE) | (1<<WDE); // timed sequence
WDTCSR = wdtcsr;
}
}
/// @see http://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html
void powerDown () {
byte adcsraSave = ADCSRA;
ADCSRA &= ~ bit(ADEN); // disable the ADC
// switch off analog comparator - not in Jeelabs' code
ACSR = ACSR & 0x7F; // note if using it then we need to switch this back on when we wake.
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
ATOMIC_BLOCK(ATOMIC_FORCEON) {
sleep_enable();
// sleep_bod_disable(); // can't use this - not in my avr-libc version!
#ifdef BODSE
MCUCR = MCUCR | bit(BODSE) | bit(BODS); // timed sequence
MCUCR = (MCUCR & ~ bit(BODSE)) | bit(BODS);
#endif
}
sleep_cpu();
sleep_disable();
// re-enable what we disabled
ADCSRA = adcsraSave;
}
byte LLAPSerial::sleepForaWhile (word msecs) {
byte ok = 1;
word msleft = msecs;
// only slow down for periods longer than the watchdog granularity
while (msleft >= 16) {
char wdp = 0; // wdp 0..9 corresponds to roughly 16..8192 ms
// calc wdp as log2(msleft/16), i.e. loop & inc while next value is ok
for (word m = msleft; m >= 32; m >>= 1)
if (++wdp >= 9)
break;
watchdogCounter = 0;
watchdogInterrupts(wdp);
powerDown();
watchdogInterrupts(-1); // off
// when interrupted, our best guess is that half the time has passed
word halfms = 8 << wdp;
msleft -= halfms;
if (watchdogCounter == 0) {
ok = 0; // lost some time, but got interrupted
break;
}
msleft -= halfms;
}
// adjust the milli ticks, since we will have missed several
#if defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny85__) || defined (__AVR_ATtiny44__) || defined (__AVR_ATtiny45__)
extern volatile unsigned long millis_timer_millis;
millis_timer_millis += msecs - msleft;
#else
extern volatile unsigned long timer0_millis;
timer0_millis += msecs - msleft;
#endif
return ok; // true if we lost approx the time planned
}
void pin2_isr()
{
sleep_disable();
detachInterrupt(0);
}
void pin3_isr()
{
sleep_disable();
detachInterrupt(1);
}
void LLAPSerial::sleep(byte pinToWakeOn, byte direction, byte bPullup) // full sleep wake on interrupt - pin is 2 or 3
{
byte adcsraSave = ADCSRA;
ADCSRA &= ~ bit(ADEN); // disable the ADC
// switch off analog comparator - not in Jeelabs' code
ACSR = ACSR & 0x7F; // note if using it then we need to switch this back on when we wake.
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
if (pinToWakeOn == 2)
{
pinMode(2,INPUT);
if (bPullup) digitalWrite(2,HIGH); // enable pullup
attachInterrupt(0, pin2_isr, direction);
}
else
{
pinMode(3,INPUT);
if (bPullup) digitalWrite(3,HIGH); // enable pullup
attachInterrupt(1, pin3_isr, direction);
}
cli();
// sleep_bod_disable(); // can't use this - not in my avr-libc version!
#ifdef BODSE
MCUCR = MCUCR | bit(BODSE) | bit(BODS); // timed sequence
MCUCR = (MCUCR & ~ bit(BODSE)) | bit(BODS);
#endif
sei();
sleep_cpu(); // and wait until we are woken
sleep_disable();
// re-enable what we disabled
ADCSRA = adcsraSave;
}
// End of power-saving code.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
LLAP.SerialEvent();
}
LLAPSerial LLAP; // declare the instance