forked from mircose/RFTide-shield-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTFide.cpp
406 lines (349 loc) · 8.03 KB
/
RTFide.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
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
403
404
405
406
/*
RFTide.cpp - Library for Aurel RTFide modules.
Created by Mirco Segatello, July 2013.
*/
#include "RFTide.h"
IOSTATUS InOutStatus = IOSTATUS();
NTSTATUS NetStatus = NTSTATUS();
/************ Constructor **********/
RFTide::RFTide(byte RX_pin, byte TX_pin, byte EN_pin)
{
pinMode(EN_pin, OUTPUT);
digitalWrite(EN_pin, LOW);
_EN_pin = EN_pin;
Status = ANSWER;
softwareSerial = new SoftwareSerial(RX_pin, TX_pin);
softwareSerial->begin(19200);
//clear softwareSerial input buffer
char c;
while (softwareSerial->available() > 0) {
c = softwareSerial->read();
}
}
/************ High Level Function **********/
byte RFTide::status()
{
return Status;
}
void RFTide::sendPacket(byte nodeAddress, char payload[])
{
#ifdef DEBUG
Serial.println("CMD sendPacket");
Serial.print("RFTide->send: ");
Serial.println(payload);
#endif
outPacket[CommandLSB]=CMD_SNDPCK;
outPacket[CommandMSB]=0x00;
outPacket[Address]=nodeAddress;
//setZeroPayload();
for (byte i=0; i<8; i++)
{
outPacket[i+3]=payload[i];
}
sendPacket(outPacket);
readAnswer();
}
int RFTide::version()
{
//return val: 100 means library version 1.00
return VERSION;
}
void RFTide::enable()
{
digitalWrite(_EN_pin, HIGH);
delay(200); // for warmup module
}
void RFTide::disable()
{
digitalWrite(_EN_pin, LOW);
}
byte RFTide::readPacket(byte packet[])
{
readAnswer(); //Try to read packet
if (Status == RECPACKET) {
packet[0] = inPacket[Byte0];
packet[1] = inPacket[Byte1];
packet[2] = inPacket[Byte2];
packet[3] = inPacket[Byte3];
packet[4] = inPacket[Byte4];
packet[5] = inPacket[Byte5];
packet[6] = inPacket[Byte6];
packet[7] = inPacket[Byte7];
return inPacket[Address];
}
else
return 255;
}
void RFTide::remoteIoSet(byte nodeAddress, byte payload)
{
if (payload>1) {
Serial.print("BAD payload!");
exit;
}
#ifdef DEBUG
Serial.println("CMD RemoteIoSet");
#endif
outPacket[CommandLSB]=CMD_RIOSET;
outPacket[CommandMSB]=0x00;
outPacket[Address]=nodeAddress;
setZeroPayload();
outPacket[Byte0]=payload;
sendPacket(outPacket);
readAnswer();
}
void RFTide::remoteIoReset(byte nodeAddress, byte payload)
{
if (payload>1) {
Serial.print("BAD payload!");
exit;
}
#ifdef DEBUG
Serial.println("CMD RemoteIoReset");
#endif
outPacket[CommandLSB]=CMD_RIORST;
outPacket[CommandMSB]=0x00;
outPacket[Address]=nodeAddress;
setZeroPayload();
outPacket[Byte0]=payload;
sendPacket(outPacket);
readAnswer();
}
NTSTATUS RFTide::ping(byte nodeAddress)
{
//ping remote module
#ifdef DEBUG
Serial.println("CMD Ping");
#endif
outPacket[CommandLSB]=CMD_PING;
outPacket[CommandMSB]=0x00;
outPacket[Address]=nodeAddress;
setZeroPayload();
sendPacket(outPacket);
readAnswer();
NetStatus.getAddress = inPacket[Address]; //address di chi ha risposto
NetStatus.getNetworkID = 0;
NetStatus.getFWversion = inPacket[Byte6]*10+inPacket[Byte5];
NetStatus.getProfile = inPacket[Byte7];
return NetStatus;
}
void RFTide::resetLocalDevice()
{
//reset locad device
#ifdef DEBUG
Serial.println("CMD Reset Local Device");
#endif
outPacket[CommandLSB]=CMD_RESET;
outPacket[CommandMSB]=0x00;
outPacket[Address]=0x00;
setZeroPayload();
sendPacket(outPacket);
readAnswer();
}
void RFTide::progMessage(byte nodeAddress, byte nodeProfile)
{
//program a new remote node
#ifdef DEBUG
Serial.println("CMD Program Message");
#endif
outPacket[CommandLSB]=CMD_PRGMSG;
outPacket[CommandMSB]=0x00;
outPacket[Address]=nodeAddress;
setZeroPayload();
outPacket[Byte7]=nodeProfile;
sendPacket(outPacket);
readAnswer();
}
void RFTide::localIoSet(byte payload)
{
if (payload>1) {
Serial.print("BAD payload!");
exit;
}
#ifdef DEBUG
Serial.println("CMD LocalIoSet");
#endif
outPacket[CommandLSB]=CMD_LIOSET;
outPacket[CommandMSB]=0x00;
outPacket[Address]=0;
setZeroPayload();
outPacket[Byte0]=payload;
sendPacket(outPacket);
readAnswer();
}
void RFTide::localIoReset(byte payload)
{
#ifdef DEBUG
Serial.println("CMD LocalIoReset");
#endif
outPacket[CommandLSB]=CMD_LIORST;
outPacket[CommandMSB]=0x00;
outPacket[Address]=0;
setZeroPayload();
outPacket[Byte0]=payload;
sendPacket(outPacket);
readAnswer();
}
NTSTATUS RFTide::localStatus()
{
#ifdef DEBUG
Serial.println("CMD LocalStatus");
#endif
outPacket[CommandLSB]=CMD_LSTATS;
outPacket[CommandMSB]=0x00;
outPacket[Address]=0;
setZeroPayload();
sendPacket(outPacket);
readAnswer();
NetStatus.getAddress = inPacket[Byte0];
NetStatus.getNetworkID = inPacket[Byte1]+inPacket[Byte2]<<8+inPacket[Byte3]<<16+inPacket[Byte4]<<24;
NetStatus.getFWversion = inPacket[Byte6]*10+inPacket[Byte5];
NetStatus.getProfile = inPacket[Byte7];
return NetStatus;
}
byte RFTide::getLocalAddress()
{
return NetStatus.getAddress;
}
unsigned long RFTide::getLocalNetworkID()
{
return NetStatus.getNetworkID;
}
int RFTide::getLocalFWversion()
{
return NetStatus.getFWversion;
}
byte RFTide::getLocalProfile()
{
return NetStatus.getProfile;
}
IOSTATUS RFTide::remoteIoRead(byte nodeAddress)
{
#ifdef DEBUG
Serial.println("CMD LocalIoRead");
#endif
outPacket[CommandLSB]=CMD_RIORAD;
outPacket[CommandMSB]=0x00;
outPacket[Address]=nodeAddress;
setZeroPayload();
sendPacket(outPacket);
readAnswer();
InOutStatus.getGPIN0 = inPacket[Byte0];
InOutStatus.getGPIN1 = inPacket[Byte1];
InOutStatus.getGPOUT0 = inPacket[Byte2];
InOutStatus.getGPOUT1 = inPacket[Byte3];
InOutStatus.getADC0 = inPacket[Byte4]+inPacket[Byte5]<<8;
InOutStatus.getADC1 = inPacket[Byte6]+inPacket[Byte7]<<8;
return InOutStatus;
}
IOSTATUS RFTide::localIoRead()
{
#ifdef DEBUG
Serial.println("CMD LocalIoRead");
#endif
outPacket[CommandLSB]=CMD_LIORAD;
outPacket[CommandMSB]=0x00;
outPacket[Address]=0;
setZeroPayload();
sendPacket(outPacket);
readAnswer();
InOutStatus.getGPIN0 = inPacket[Byte0];
InOutStatus.getGPIN1 = inPacket[Byte1];
InOutStatus.getGPOUT0 = inPacket[Byte2];
InOutStatus.getGPOUT1 = inPacket[Byte3];
InOutStatus.getADC0 = inPacket[Byte4]+inPacket[Byte5]<<8;
InOutStatus.getADC1 = inPacket[Byte6]+inPacket[Byte7]<<8;
return InOutStatus;
}
byte RFTide::getGPIN0()
{
return InOutStatus.getGPIN0;
}
byte RFTide::getGPIN1()
{
return InOutStatus.getGPIN1;
}
byte RFTide::getGPOUT0()
{
return InOutStatus.getGPOUT0;
}
byte RFTide::getGPOUT1()
{
return InOutStatus.getGPOUT1;
}
/************ low level data pushing commands **********/
void RFTide::setZeroPayload()
{
// set null payload
for (byte i=Byte0; i<=Byte7; i++) {
outPacket[i]=0;
}
}
void RFTide::sendPacket(byte outByte[])
{
// send 11 byte fixed size packet
for (byte i=0; i<11; i++) {
softwareSerial->write(outByte[i]);
}
}
void RFTide::readAnswer()
{
unsigned long time = millis();
// wait for answer
while ((millis() - time < TIMEOUTANSWER) && (softwareSerial->available() < 11) ) {
}
if ((softwareSerial->available())==0) {
// empty buffer NO ANSWER
#ifdef DEBUG
Serial.println("NO HANSWER!");
#endif
Status = NOANSWER;
for (byte i=0; i<11; i++) {
inPacket[i] = 0;
}
}
else if ((softwareSerial->available())<11) {
// packet non complete size < 11 byte
Status = BRKANSWER;
}
else if ((softwareSerial->available())==11) {
// packet OK read 11 byte fixed size
#ifdef DEBUG
Serial.print("HANSWER (bin): ");
#endif
for (byte i=0; i<11; i++) {
inPacket[i] = softwareSerial->read();
#ifdef DEBUG
Serial.print(inPacket[i], BIN);
Serial.print(".");
#endif
}
#ifdef DEBUG
Serial.println();
#endif
if (inPacket[CommandLSB]==CMD_ANSMSG) {
Status = ANSWER; //is answer
}
else if (inPacket[CommandLSB]==CMD_SNDPCK) {
Status = RECPACKET;
}
else {
Status = BADANSWER;
}
}
else {
// buffer over size >11 byte
#ifdef DEBUG
Serial.print("BAD BUFFER!");
#endif
Status = BADANSWER;
// clear buffer
while (softwareSerial->available() > 0) {
char c = char(softwareSerial->read());
Serial.print(c, BIN);
Serial.print(".");
}
for (byte i=0; i<11; i++) {
inPacket[i] = 0;
}
}
}