-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrfBeeSerial.pde
402 lines (355 loc) · 10.7 KB
/
rfBeeSerial.pde
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// rfBeeSerial.pde serial interface to rfBee
// see www.seeedstudio.com for details and ordering rfBee hardware.
// Copyright (c) 2010 Hans Klunder <hans.klunder (at) bigfoot.com>
// Author: Hans Klunder, based on the original Rfbee v1.0 firmware by Seeedstudio
// Version: August 18, 2010
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "rfBeeSerial.h"
//
byte getNumParamData(int *result, int size){
DEBUGPRINT()
// try to read a number
byte c;
int value=0;
boolean valid=false;
int pos=4; // we start to read at pos 5 as 0-1 = AT and 2-3 = CMD
if (serialData[pos] == SERIALCMDTERMINATOR ) // no data was available
return NOTHING;
while (size-- > 0){
c=serialData[pos++];
if ( c== SERIALCMDTERMINATOR) // no more data available
break;
if ((c < '0') || (c > '9')) // illegal char
return ERR;
// got a digit
valid=true;
value=(value*10)+ (c -'0');
}
if (valid){
*result=value;
return OK;
}
return ERR;
}
// simple standardized setup for commands that only modify config
int modifyConfig(byte configLabel, byte paramSize, byte paramMaxValue){
DEBUGPRINT()
int param;
byte result=getNumParamData(¶m ,paramSize);
if (result == OK){
if (param <= paramMaxValue){
Config.set(configLabel,param);
return MODIFIED;
}
}
if (result == NOTHING){
// return current setting
Serial.println(Config.get(configLabel),DEC);
return(OK);
}
return ERR;
}
int processSerialCmd(byte size){
DEBUGPRINT()
int result=MODIFIED;
byte configItem; // the ID used in the EEPROM
byte paramDigits; // how many digits for the parameter
byte maxValue; // maximum value of the parameter
byte postProcess; // do we need to call the function to perform extra actions on change
AT_Command_Function_t function; // the function which does the real work on change
// read the AT
if (strncasecmp("AT",(char *)serialData,2)==0){
// read the command
for(int i=0;i<=sizeof(atCommands)/sizeof(AT_Command_t);i++){
// do we have a known command
if (strncasecmp_P((char *) serialData+2 , (PGM_P) pgm_read_word(&(atCommands[i].name)), 2)==0){
// get the data from PROGMEM
configItem=pgm_read_byte(&(atCommands[i].configItem));
paramDigits=pgm_read_byte(&(atCommands[i].paramDigits));
maxValue=pgm_read_byte(&(atCommands[i].maxValue));
postProcess=pgm_read_byte(&(atCommands[i].postProcess));
function= (AT_Command_Function_t) pgm_read_word(&(atCommands[i].function));
if (paramDigits > 0)
result=modifyConfig(configItem, paramDigits, maxValue);
if (result == MODIFIED){
result=OK; // config only commands always return OK
if (postProcess==true)
result=function(); // call the command function
}
return(result); // return the result of the execution of the function linked to the command
}
}
}
return ERR;
}
void readSerialCmd(){
DEBUGPRINT()
int result;
char data;
static byte pos=0;
while(Serial.available() && (serialMode == SERIALCMDMODE)){ //ATSL changes commandmode while there is a char waiting in the serial buffer.
result=NOTHING;
data=Serial.read();
serialData[pos++]=data; //serialData is our global serial buffer
if (data == SERIALCMDTERMINATOR){
if (pos > 3){ // we need 4 bytes
result=processSerialCmd(pos);
}
else
result=ERR;
pos=0;
}
// check if we don't overrun the buffer, if so empty it
if (pos > BUFFLEN){
result=ERR;
pos=0;
}
if (result == OK)
Serial.println("ok");
if (result == ERR)
Serial.println("error");
}
}
void readSerialData(){
DEBUGPRINT()
byte len;
byte data;
byte fifoSize=0;
static byte plus=0;
static byte pos=0;
byte rfBeeMode;
// insert any plusses from last round
for(int i=pos; i< plus;i++) //be careful, i should start from pos, -changed by Icing
serialData[i]='+';
len=Serial.available()+plus+pos;
if (len > BUFFLEN ) len=BUFFLEN; //only process at most BUFFLEN chars
// check how much space we have in the TX fifo
fifoSize=txFifoFree();// the fifoSize should be the number of bytes in TX FIFO
if(fifoSize <= 0){
Serial.flush();
//CCx.Strobe(CCx_SFTX);
plus = 0;
pos = 0;
return;
}
if (len > fifoSize) len=fifoSize; // don't overflow the TX fifo
for(byte i=plus+pos; i< len;i++){
data=Serial.read();
serialData[i]=data; //serialData is our global serial buffer
if (data == '+')
plus++;
else
plus=0;
if (plus == 3){
len=i-2; // do not send the last 2 plusses
plus=0;
serialMode=SERIALCMDMODE;
CCx.Strobe(CCx_SIDLE);
Serial.println("ok, starting cmd mode");
break; // jump out of the loop, but still send the remaining chars in the buffer
}
}
if (plus > 0) // save any trailing plusses for the next round
len-=plus;
// check if we have more input than the transmitThreshold, if we have just switched to commandmode send the current buffer anyway.
if ((serialMode!=SERIALCMDMODE) && (len < Config.get(CONFIG_TX_THRESHOLD))){
pos=len; // keep the current bytes in the buffer and wait till next round.
return;
}
if (len > 0){
rfBeeMode=Config.get(CONFIG_RFBEE_MODE);
//only when TRANSMIT_MODE or TRANSCEIVE,transmit the buffer data,otherwise ignore
if( rfBeeMode == TRANSMIT_MODE || rfBeeMode == TRANSCEIVE_MODE )
transmitData(serialData,len,Config.get(CONFIG_MY_ADDR),Config.get(CONFIG_DEST_ADDR));
pos=0; // serial databuffer is free again.
}
}
// write a text label from progmem, uses less bytes than individual Serial.println()
//void writeSerialLabel(byte i){
// char buffer[64];
// strcpy_P(buffer, (char * )pgm_read_word(&(labels[i])));
// Serial.println(buffer);
//}
//
void writeSerialError(){
DEBUGPRINT()
char buffer[64];
strcpy_P(buffer, (char * )pgm_read_word(&(error_codes[errNo])));
Serial.print("error: ");
Serial.println(buffer);
}
// read data from CCx and write it to Serial based on the selected output format
void writeSerialData(){
DEBUGPRINT()
byte rxData[CCx_PACKT_LEN];
byte len;
byte srcAddress;
byte destAddress;
char rssi;
byte lqi;
int result;
byte of;
result=receiveData(rxData, &len, &srcAddress, &destAddress, (byte *)&rssi , &lqi);
if (result == ERR) {
writeSerialError();
return;
}
if (result == NOTHING)
return;
// write to serial based on output format:
// 0: payload only
// 1: source, dest, payload
// 2: payload len, source, dest, payload, rssi, lqi
// 3: payload len, source, dest, payload,",", rssi (DEC),",",lqi (DEC)
of=Config.get(CONFIG_OUTPUT_FORMAT);
if(of == 3){
Serial.print(len,DEC); // len is size of data EXCLUDING address bytes !
Serial.print(',');
// write source & destination
Serial.print(srcAddress,DEC);
Serial.print(',');
Serial.print(destAddress,DEC);
Serial.print(',');
// write data
Serial.write(rxData,len);
Serial.print(',');
// write rssi en lqi
Serial.print((int)rssi);
Serial.print(',');
Serial.println(lqi,DEC);
}
else{
if( of > 1 )
Serial.print(len); // len is size of data EXCLUDING address bytes !
if( of > 0 ) {
// write source & destination
Serial.print(srcAddress);
Serial.print(destAddress);
}
// write data
Serial.write(rxData,len);
// write rssi en lqi
if( of > 1 ) {
Serial.print(rssi);
Serial.print(lqi);
}
}
}
int setMyAddress(){
DEBUGPRINT();
CCx.Write(CCx_ADDR,Config.get(CONFIG_MY_ADDR));
return OK;
}
int setAddressCheck(){
DEBUGPRINT();
CCx.Write(CCx_PKTCTRL1, Config.get(CONFIG_ADDR_CHECK) | 0x04);
return OK;
}
int setPowerAmplifier(){
DEBUGPRINT();
CCx.setPA(Config.get(CONFIG_CONFIG_ID), Config.get(CONFIG_PAINDEX));
return OK;
}
int changeUartBaudRate(){
DEBUGPRINT()
Serial.println("ok");
Serial.flush();
delay(1);
setUartBaudRate();
}
int setUartBaudRate(){
Serial.begin(pgm_read_dword(&baudRateTable[Config.get(CONFIG_BDINDEX)]));
return NOTHING; // we already sent an ok.
}
int showFirmwareVersion(){
DEBUGPRINT()
Serial.println(((float) FIRMWAREVERSION)/10,1);
return OK;
}
int showHardwareVersion(){
DEBUGPRINT()
Serial.println(((float)Config.get(CONFIG_HW_VERSION))/10,1);
return OK;
}
int resetConfig(){
DEBUGPRINT()
Config.reset();
return OK;
}
int setCCxConfig(){
DEBUGPRINT()
// load the appropriate config table
byte cfg=Config.get(CONFIG_CONFIG_ID);
CCx.Setup(cfg);
CCx.ReadSetup();
// and restore the config settings
//setMyAddress();
//setAddressCheck();
setPowerAmplifier();
//setRFBeeMode();
return OK;
}
int setSerialDataMode(){
DEBUGPRINT()
serialMode = SERIALDATAMODE;
return OK;
}
int setRFBeeMode(){
DEBUGPRINT()
// CCx_MCSM1 is configured to have TX and RX return to proper state on completion or timeout
switch (Config.get(CONFIG_RFBEE_MODE))
{
case TRANSMIT_MODE:
CCx.Strobe(CCx_SIDLE);
delay(1);
CCx.Write(CCx_MCSM1 , 0x00 );//TXOFF_MODE->stay in IDLE
CCx.Strobe(CCx_SFTX);
break;
case RECEIVE_MODE:
CCx.Strobe(CCx_SIDLE);
delay(1);
CCx.Write(CCx_MCSM1 , 0x0C );//RXOFF_MODE->stay in RX
CCx.Strobe(CCx_SFRX);
CCx.Strobe(CCx_SRX);
break;
case TRANSCEIVE_MODE:
CCx.Strobe(CCx_SIDLE);
delay(1);
CCx.Write(CCx_MCSM1 , 0x0F );//RXOFF_MODE and TXOFF_MODE stay in RX
CCx.Strobe(CCx_SFTX);
CCx.Strobe(CCx_SFRX);
CCx.Strobe(CCx_SRX);
break;
case LOWPOWER_MODE:
CCx.Strobe(CCx_SIDLE);
break;
default:
break;
}
return OK;
}
// put the rfbee into sleep
int setSleepMode(){
DEBUGPRINT("going to sleep")
CCx.Strobe(CCx_SIDLE);
CCx.Strobe(CCx_SPWD);
sleepNow(SLEEP_MODE_IDLE);
//sleepNow(SLEEP_MODE_PWR_DOWN);
DEBUGPRINT("just woke up")
setRFBeeMode();
setSerialDataMode();
return NOTHING;
}