-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodem.ino
356 lines (328 loc) · 7.99 KB
/
modem.ino
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
#include "modem.h"
#ifdef PANSTAMP_NRG
#include "timer1a0.h"
#define TIMER timer1a0
#define RESET_TIMER()
#define INIT_TIMER() TIMER.attachInterrupt(isrT1event)
#define START_TIMER() TIMER.start(1000)
#define STOP_TIMER() TIMER.stop()
#elif PANSTAMP_AVR
#include <avr/wdt.h>
#include "TimerOne.h"
byte t1Ticks = 0; // Timer 1 ticks
#define TIMER Timer1
#define RESET_TIMER() t1Ticks = 0
#define INIT_TIMER() TIMER.initialize(TIMER1_TICK_PERIOD_US); TIMER.attachInterrupt(isrT1event)
#define START_TIMER() RESET_TIMER(); TIMER.attachInterrupt(isrT1event)
#define STOP_TIMER() TIMER.detachInterrupt()
#endif
/**
* LED pin
*/
#define LEDPIN 4
byte charToHex(byte ch);
/**
* This function is called whenever a wireless packet is received
*/
void rfPacketReceived(CCPACKET *packet)
{
if (packet->length > 0)
{
// Serial.println("Actual Packet \n");
// Serial.println(packet->data[2]);
rxPacket = packet;
packetAvailable = true;
}
}
/**
* isrT1event
*
* Timer1 interrupt routine
*/
void isrT1event(void)
{
#ifdef PANSTAMP_AVR
if (t1Ticks == MAX_SERIAL_SILENCE_TK)
{
#endif
// Detach Timer1 interrupt
STOP_TIMER();
RESET_TIMER();
// Pending "+++" command?
if (!strcmp(strSerial, AT_GOTO_CMDMODE))
{
panstamp.rxOff(); // Disable wireless reception
Serial.println("OK-Command mode");
serMode = SERMODE_COMMAND;
}
memset(strSerial, 0, sizeof(strSerial));
len = 0;
#ifdef PANSTAMP_AVR
}
else
t1Ticks++;
#endif
}
/**
* handleSerialCmd
*
* Handle serial command received
*
* 'command' AT command received
*/
void handleSerialCmd(char* command)
{
byte i, len;
byte arrV[2];
CCPACKET packet;
ATQUERY atQuery = ATQUERY_REQUEST;
// Data mode?
if (serMode == SERMODE_DATA)
{
packet.length = strlen(command)/2;
if (packet.length > 0)
{
// Convert ASCII string into array of bytes
for(i=0 ; i<packet.length ; i++)
{
packet.data[i] = charToHex(command[i*2]) << 4;
packet.data[i] |= charToHex(command[i*2 + 1]);
}
// Send packet via RF
panstamp.radio.sendData(packet);
}
}
// Command mode?
else // serMode = SERMODE_COMMAND
{
len = strlen(command);
if (len < 5)
{
// Basic attention command
if (!strcmp(strSerial, AT_ATTENTION))
Serial.println("OK");
// Reset modem
else if (!strcmp(strSerial, AT_RESET))
{
Serial.println("OK");
panstamp.reset();
}
// Go to serial data mode
else if (!strcmp(strSerial, AT_GOTO_DATAMODE))
{
serMode = SERMODE_DATA;
Serial.println("OK-Data mode");
panstamp.rxOn(); // Enable wireless reception
}
}
// Set new value
else
{
if ((strSerial[4] == '=') && (len >= 6))
{
// Get new value
i = (charToHex(strSerial[5]) << 4) & 0xF0;
i |= charToHex(strSerial[6]) & 0x0F;
atQuery = ATQUERY_COMMAND;
}
// Hardware version
if (!strncmp(strSerial, AT_HVERSION, 4))
{
if (atQuery == ATQUERY_REQUEST)
Serial.println(HARDWARE_VERSION, HEX);
}
// Firmware version
else if (!strncmp(strSerial, AT_FVERSION, 4))
{
if (atQuery == ATQUERY_REQUEST)
Serial.println(FIRMWARE_VERSION, HEX);
}
// Frequency channel
else if (!strncmp(strSerial, AT_FREQCHANNEL, 4))
{
if (atQuery == ATQUERY_COMMAND)
{
panstamp.radio.setChannel(i);
Serial.println("OK");
}
else
Serial.println(panstamp.radio.channel, HEX);
}
// Synchronization word
else if (!strncmp(strSerial, AT_SYNCWORD, 4))
{
if (atQuery == ATQUERY_COMMAND)
{
if ((len-5) == 4)
{
arrV[0] = charToHex(strSerial[5]) << 4;
arrV[0] |= charToHex(strSerial[6]);
arrV[1] = charToHex(strSerial[7]) << 4;
arrV[1] |= charToHex(strSerial[8]);
panstamp.radio.setSyncWord(arrV);
Serial.println("OK");
}
else
Serial.println("ERROR");
}
else
{
Serial.println((unsigned int)panstamp.radio.syncWord[0] << 8 | panstamp.radio.syncWord[1], HEX);
}
}
// Device address
else if (!strncmp(strSerial, AT_DEVADDRESS, 4))
{
if (atQuery == ATQUERY_COMMAND)
{
panstamp.radio.setDevAddress(i);
Serial.println("OK");
}
else
Serial.println(panstamp.radio.devAddress, HEX);
}
// Address check
else if (!strncmp(strSerial, AT_ADDRCHECK, 4))
{
if (atQuery == ATQUERY_COMMAND)
{
if (i == 0)
{
panstamp.radio.disableAddressCheck();
Serial.println("OK");
}
else if (i == 1)
{
panstamp.radio.enableAddressCheck();
Serial.println("OK");
}
else
Serial.println("ERROR");
}
else
Serial.println("ERROR");
}
else
Serial.println("ERROR");
}
}
}
/**
* setup
*
* Arduino setup function
*/
void setup()
{
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, HIGH);
// Reset serial buffer
//memset(strSerial, 0, sizeof(strSerial));
Serial.begin(9600);
//Serial.begin(57600);
Serial.flush();
Serial.println("");
// Default mode is COMMAND
// Serial.println("Modem ready!");
// Disable address check from the RF IC
panstamp.radio.disableAddressCheck();
//panstamp.radio.setDevAddress(21);
// Declare RF callback function
panstamp.attachInterrupt(rfPacketReceived);
// Initialize Timer object
INIT_TIMER();
digitalWrite(LEDPIN, LOW);
}
/**
* loop
*
* Arduino main loop
*/
void loop()
{
// Read wireless packet?
if (packetAvailable)
{
digitalWrite(LEDPIN, HIGH);
// Disable wireless reception
panstamp.rxOff();
byte i;
packetAvailable = false;
if (serMode == SERMODE_DATA)
{
/* Serial.print("(");
if (rxPacket->rssi < 0x10)
Serial.print("0");
Serial.print(rxPacket->rssi, DEC);
if (rxPacket->lqi < 0x10)
Serial.print("0");
Serial.print(rxPacket->lqi, DEC);
Serial.print(")");
Serial.print(rxPacket->length, DEC);*/
// Serial.println("Data in the packet\n");
for(i=0 ; i<rxPacket->length ; i++)
{
if (rxPacket->data[i] < 10)
Serial.print(0, DEC); // Leading zero
Serial.print(rxPacket->data[i], DEC);
}
Serial.print("\n");
Serial.flush();
panstamp.sleepSec(1);
}
// Enable wireless reception
panstamp.rxOn();
digitalWrite(LEDPIN, LOW);
}
#if 0
// Read serial command
if (Serial.available() > 0)
{
// Disable wireless reception
panstamp.rxOff();
ch = Serial.read();
if (len >= SERIAL_BUF_LEN-1)
{
memset(strSerial, 0, sizeof(strSerial));
len = 0;
}
else if (ch == 0x0D)
{
STOP_TIMER();
strSerial[len] = 0;
handleSerialCmd(strSerial);
memset(strSerial, 0, sizeof(strSerial));
len = 0;
}
else
{
strSerial[len] = ch;
len++;
START_TIMER();
}
// Enable wireless reception
panstamp.rxOn();
}
#endif
}
/**
* charToHex
*
* 'ch' Character to be converted to hexadecimal
*
* Returns:
* Hex value
*/
byte charToHex(byte ch)
{
byte val;
if (ch >= 'A' && ch <= 'F')
val = ch - 55;
else if (ch >= 'a' && ch <= 'f')
val = ch - 87;
else if (ch >= '0' && ch <= '9')
val = ch - 48;
else
val = 0x00;
return val;
}