-
Notifications
You must be signed in to change notification settings - Fork 17
/
boilermakeBoard.cpp
447 lines (375 loc) · 11.8 KB
/
boilermakeBoard.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* vim: set ts=2 sw=2 sts=2 et! : */
//
// BoilerMake Fall 2014 Badge Code
//
// These boards are equivalent to an Arduino Leonardo
// And contain an ATMega32U4 microprocessor and an nRF24L01 radio
//
// Lior Ben-Yehoshua ([email protected])
// Viraj Sinha ([email protected])
// Scott Opell ([email protected])
//
// 10/15/2014
//
// Happy Hacking!
//
#define MAX_TERMINAL_LINE_LEN 40
#define MAX_TERMINAL_WORDS 7
// 14 is strlen("send FFFF -m ")
// the max message length able to be sent from the terminal is
// total terminal line length MINUS the rest of the message
#define MAX_TERMINAL_MESSAGE_LEN MAX_TERMINAL_LINE_LEN - 14
#include <RF24.h>
#include <SPI.h>
#include <EEPROM.h>
void welcomeMessage(void);
void printHelpText(void);
void printPrompt(void);
void networkRead(void);
void serialRead(void);
void handleSerialData(char[], byte);
void setValue(word);
void handlePayload(struct payload *);
void ledDisplay(byte);
void displayDemo();
// Maps commands to integers
const byte PING = 0; // Ping
const byte LED = 1; // LED pattern
const byte MESS = 2; // Message
const byte DEMO = 3; // Demo Pattern
// Global Variables
int SROEPin = 3; // using digital pin 3 for SR !OE
int SRLatchPin = 8; // using digital pin 4 for SR latch
boolean terminalConnect = false; // indicates if the terminal has connected to the board yet
// nRF24L01 radio static initializations
RF24 radio(9,10); // Setup nRF24L01 on SPI bus using pins 9 & 10 as CE & CSN, respectively
uint16_t this_node_address = (EEPROM.read(0) << 8) | EEPROM.read(1); // Radio address for this node
struct payload{ // Payload structure
byte command;
byte led_pattern;
char message[30];
};
// This runs once on boot
void setup() {
Serial.begin(9600);
// SPI initializations
SPI.begin();
SPI.setBitOrder(MSBFIRST); // nRF requires MSB first
SPI.setClockDivider(16); // Set SPI clock to 16 MHz / 16 = 1 MHz
// nRF radio initializations
radio.begin();
radio.setDataRate(RF24_1MBPS); // 1Mbps transfer rate
radio.setCRCLength(RF24_CRC_16); // 16-bit CRC
radio.setChannel(80); // Channel center frequency = 2.4005 GHz + (Channel# * 1 MHz)
radio.setRetries(200, 5); // set the delay and number of retries upon failed transmit
radio.openReadingPipe(0, this_node_address); // Open this address
radio.startListening(); // Start listening on opened address
// Shift register pin initializations
pinMode(SROEPin, OUTPUT);
pinMode(SRLatchPin, OUTPUT);
digitalWrite(SROEPin, HIGH);
digitalWrite(SRLatchPin, LOW);
// Display welcome message
welcomeMessage();
// make the pretty LEDs happen
ledDisplay(2);
}
// This loops forever
void loop() {
// Displays welcome message if serial terminal connected after program setup
if (Serial && !terminalConnect) {
welcomeMessage();
terminalConnect = true;
} else if (!Serial && terminalConnect) {
terminalConnect = false;
}
networkRead(); // Read from network
serialRead(); // Read from serial
}
// Handle reading from the radio
void networkRead() {
while (radio.available()) {
struct payload * current_payload = (struct payload *) malloc(sizeof(struct payload));
// Fetch the payload, and see if this was the last one.
radio.read( current_payload, sizeof(struct payload) );
handlePayload(current_payload);
}
}
// Get user input from serial terminal
void serialRead() {
char inData[MAX_TERMINAL_LINE_LEN]; // allocate space for incoming serial string
byte index = 0; // Index into array, where to store chracter
char inChar; // Where to store the character to read
// fills up characters from the terminal, leaving room for null terminator
while ( index < MAX_TERMINAL_LINE_LEN - 1 && Serial.available() > 0){
inChar = Serial.read();
if (inChar == '\r'){
inData[index] = '\0';
break;
} else {
inData[index] = inChar;
index++;
}
}
if (index > 0){ // if we read some data, then process it
Serial.println(inData);
handleSerialData(inData, index);
printPrompt();
}
}
// Handle received commands from user obtained via the serial termina
void handleSerialData(char inData[], byte index) {
// tokenize the input from the terminal by spaces
char * words[MAX_TERMINAL_WORDS];
byte current_word_index = 0;
char * p = strtok(inData, " ");
while(p != NULL) {
words[current_word_index++] = p;
p = strtok(NULL, " ");
}
if (strcmp(words[0], "help") == 0) {
printHelpText();
} else if (strcmp(words[0], "send") == 0) {
// checks if address field was given valid characters
if ((strspn(words[1], "1234567890AaBbCcDdEeFf") <= 4)
&& (strspn(words[1], "1234567890AaBbCcDdEeFf") > 0)) {
uint16_t TOaddr = strtol(words[1], NULL, 16);
if (strncmp(words[2], "-p", 2) == 0) { // Send ping
struct payload myPayload = {PING, '\0', {'\0'}};
size_t len = sizeof(PING) + sizeof('\0') * 2;
radio.stopListening();
radio.openWritingPipe(TOaddr);
radio.write(&myPayload, len);
radio.startListening();
} else if (strcmp(words[2], "-l") == 0) { // Send LED pattern
if (strspn(words[3], "1234567890") == 1) {
byte led_patt = (byte) atoi(words[3]);
struct payload myPayload = {LED, led_patt, {'\0'}};
size_t len = sizeof(LED) + sizeof(led_patt) + sizeof('\0');
radio.stopListening();
radio.openWritingPipe(TOaddr);
radio.write(&myPayload, len);
radio.startListening();
}
else {
Serial.println(" Invalid LED pattern field.");
}
} else if (strcmp(words[2], "-m") == 0) { // Send message
char str_msg[MAX_TERMINAL_MESSAGE_LEN];
char * curr_pos = str_msg;
for (int i = 3; i < current_word_index; i++){
byte curr_len = strlen(words[i]);
strncpy(curr_pos, words[i], curr_len);
curr_pos += curr_len;
// this will add in the space characters that tokenizing removes
if (i < current_word_index - 1){
strncpy(curr_pos, " ", 1);
curr_pos++;
}
}
struct payload myPayload = {MESS, '\0', {}};
// the end of the string minus the start of the string gives the length
memcpy(&myPayload.message, str_msg, curr_pos - str_msg);
Serial.println(myPayload.message);
radio.stopListening();
radio.openWritingPipe(TOaddr);
radio.write(&myPayload, sizeof(myPayload));
radio.startListening();
}
else {
Serial.println(" Invalid command field.");
}
}
else {
Serial.println(" Invalid address field.");
}
} else if (strcmp(words[0], "channel") == 0) {
// Set radio channel
byte chn = (byte) atoi(words[1]);
if (chn >= 0 && chn <= 83) {
Serial.print("Channel is now set to ");
Serial.println(chn);
radio.setChannel(chn);
} else {
Serial.println(" Invalid channel number. Legal channels are 0 - 83.");
}
} else if (strcmp(words[0], "radio") == 0) {
// Turn radio listening on/off
if (strcmp(words[1], "on") == 0) {
Serial.println("Radio is now in listening mode");
radio.startListening();
} else if (strcmp(words[1], "off") == 0) {
Serial.println("Radio is now NOT in listening mode");
radio.stopListening();
} else {
Serial.println(" Invalid syntax.");
}
}
}
// Grab message received by nRF for this node
void handlePayload(struct payload * myPayload) {
switch(myPayload->command) {
case PING:
Serial.println("Someone pinged us!");
printPrompt();
break;
case LED:
ledDisplay(myPayload->led_pattern);
break;
case MESS:
Serial.print("Message:\r\n ");
Serial.println(myPayload->message);
printPrompt();
break;
case DEMO:
displayDemo();
break;
default:
Serial.println(" Invalid command received.");
break;
}
free(myPayload); // Deallocate payload memory block
}
void printPrompt(void){
Serial.print("> ");
}
/*
// Display LED pattern
// LED numbering:
9
8 10
7 11
6 12
5 13
4 14
3 15
2 16
1
shift register 1-8: AA
shift register 9-16: BB
setValue data in hex: 0xAABB
where AA in binary = 0b[8][7][6][5][4][3][2][1]
BB in binary = 0b[16][15][14][13][12][11][10][9]
*/
void ledDisplay(byte pattern) {
setValue(0x0000);
digitalWrite(SROEPin, LOW);
if(pattern == 0) {
word pattern = 0x0000; // variable used in shifting process
int del = 62; // ms value for delay between LED toggles
for(int i=0; i<16; i++) {
pattern = (pattern << 1) | 0x0001;
setValue(pattern);
delay(del);
}
for(int i=0; i<16; i++) {
pattern = (pattern << 1);
setValue(pattern);
delay(del);
}
}
else if(pattern == 1) {
word pattern = 0x0000; // variable used in shifting process
int del = 62; // ms value for delay between LED toggles
for (int i = 0; i < 16; i++) {
pattern = (pattern >> 1) | 0x8000;
setValue(pattern);
delay(del);
}
for (int i=0; i<16; i++) {
pattern = (pattern >> 1);
setValue(pattern);
delay(del);
}
}
else if(pattern == 2) {
int del = 100;
setValue(0x1010);
delay(del);
setValue(0x3838);
delay(del);
setValue(0x7C7C);
delay(del);
setValue(0xFEFE);
delay(del);
setValue(0xFFFF);
delay(del);
setValue(0xEFEF);
delay(del);
setValue(0xC7C7);
delay(del);
setValue(0x8383);
delay(del);
setValue(0x0101);
delay(del);
setValue(0x0000);
delay(del);
}
else if(pattern == 3) {
word pattern = 0x0101;
int del = 125;
setValue(pattern);
for(int i=0; i<8; i++) {
delay(del);
pattern = (pattern << 1);
setValue(pattern);
}
}
else if(pattern == 4) {
for (int i = 0; i < 4; i++) {
setValue(0xFFFF);
delay(125);
setValue(0x0000);
delay(125);
}
}
digitalWrite(SROEPin, HIGH);
}
// LED display demo
void displayDemo() {
digitalWrite(SROEPin, LOW);
for (int i = 0; i < 100; i++) {
setValue(0xAAAA);
delay(125);
setValue(0x5555);
delay(125);
}
digitalWrite(SROEPin, HIGH);
}
// Sends word sized value to both SRs & latches output pins
void setValue(word value) {
byte Hvalue = value >> 8;
byte Lvalue = value & 0x00FF;
SPI.transfer(Lvalue);
SPI.transfer(Hvalue);
digitalWrite(SRLatchPin, HIGH);
digitalWrite(SRLatchPin, LOW);
}
// Prints 'help' command
void printHelpText() {
Serial.println("Available commands:");
Serial.println(" help - displays commands list.");
Serial.println();
Serial.println(" send [addr] [command] [data] - send packets to other node.");
Serial.println(" [addr] - address of destination node, as a 4 digit hex value");
Serial.println(" [command] - command field.");
Serial.println(" -p - ping destination node.");
Serial.println(" -l - send LED pattern to destination node.");
Serial.println(" - [data] - LED pattern. Valid range: 0-255.");
Serial.println(" -m - send message to destination node.");
Serial.println(" - [data] - message to be sent. Max 26 characters.");
Serial.println();
Serial.println(" channel [val] - change channel of your node.");
Serial.println(" - [val] - new channel. Valid range: 0-83.");
Serial.println();
Serial.println(" radio [on | off] - turn radio on or off");
}
void welcomeMessage(void) {
char hex_addr[10];
sprintf(hex_addr, "%04x", this_node_address);
Serial.print("\r\nWelcome to the BoilerMake Hackathon Badge Network...\r\n\n");
Serial.print("Your address: ");
Serial.println(hex_addr);
Serial.print("\nAll commands must be terminated with a carriage return.\r\n"
"Type 'help' for a list of available commands.\r\n\n> ");
}