Skip to content

Commit bdae0b3

Browse files
committed
Add button checking. Add training command.
1 parent e133ac8 commit bdae0b3

File tree

6 files changed

+111
-34
lines changed

6 files changed

+111
-34
lines changed

Firmware/LoRaSerial_Firmware/Begin.ino

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void beginBoard()
4141

4242
pin_trigger = 13;
4343

44+
pin_setupButton = 0;
45+
4446
//Debug
4547
pinMode(pin_trigger, OUTPUT);
4648
digitalWrite(pin_trigger, HIGH);
@@ -139,6 +141,15 @@ void beginLoRa()
139141
changeState(RADIO_BROADCASTING_RECEIVING_STANDBY);
140142
}
141143

144+
void beginButton()
145+
{
146+
if (pin_setupButton != 255)
147+
{
148+
trainBtn = new Button(pin_setupButton); //Create the button
149+
trainBtn->begin();
150+
}
151+
}
152+
142153
void beginWDT()
143154
{
144155
#if defined(ARDUINO_ARCH_SAMD)

Firmware/LoRaSerial_Firmware/Commands.ino

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ void commandMode()
5959
reportOK();
6060
return;
6161
break;
62+
case ('T'): //Enter training mode
63+
reportOK();
64+
beginTraining();
65+
return;
66+
break;
67+
case ('F'): //Enter training mode and return to factory defaults
68+
reportOK();
69+
beginDefaultTraining();
70+
return;
71+
break;
6272
case ('Z'): //Reboots the radio
6373
reportOK();
6474
Serial.flush();
@@ -136,10 +146,6 @@ void commandMode()
136146
reportOK();
137147
}
138148
break;
139-
case ('T'): //AT&T - Disable debugging report
140-
settings.displayPacketQuality = false;
141-
reportOK();
142-
break;
143149
default:
144150
reportERROR();
145151
break;
@@ -148,7 +154,7 @@ void commandMode()
148154
else
149155
{
150156
//RSSI
151-
if (strcmp_P(commandBuffer, PSTR("AT&T=RSSI")) == 0) //Enable debugging report
157+
if (strcmp_P(commandBuffer, PSTR("AT&T=RSSI")) == 0) //Enable packet quality reporting
152158
{
153159
settings.displayPacketQuality = true;
154160
reportOK();

Firmware/LoRaSerial_Firmware/LoRaSerial_Firmware.ino

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ uint8_t pin_linkLED = 255;
7373
uint8_t pin_activityLED = 255;
7474
uint8_t pin_txLED = 255;
7575
uint8_t pin_rxLED = 255;
76+
uint8_t pin_setupButton = 255;
7677

7778
uint8_t pin_trigger = 255;
7879
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
@@ -117,6 +118,15 @@ WDTZero myWatchDog;
117118
#endif
118119
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
119120

121+
//Buttons - Interrupt driven and debounce
122+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
123+
#include <JC_Button.h> // http://librarymanager/All#JC_Button
124+
Button *trainBtn = NULL; //We can't instantiate the button here because we don't yet know what pin number to use
125+
126+
const int trainButtonTime = 4000; //ms press and hold before entering training
127+
const int trainWithDefaultsButtonTime = 10000; //ms press and hold before entering training
128+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
129+
120130
//Global variables - Serial
121131
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
122132
//Buffer to store bytes incoming from serial before broadcasting over LoRa
@@ -169,6 +179,8 @@ bool expectingAck = false; //Used by various send packet functions
169179

170180
float frequencyCorrection = 0; //Adjust receive freq based on the last packet received freqError
171181

182+
unsigned long lastTrainBlink = 0; //Controls LED during training
183+
172184
Settings originalSettings; //Create a duplicate of settings during training so that we can resort as needed
173185
uint8_t trainNetID; //New netID passed during training
174186
uint8_t trainEncryptionKey[16]; //New AES key passed during training
@@ -183,9 +195,7 @@ long stopTime = 0;
183195

184196
void setup()
185197
{
186-
//eepromErase();
187-
188-
Serial.begin(57600);
198+
Serial.begin(57600); //Default for debug messages before board begins
189199

190200
#if defined(ENABLE_DEVELOPER)
191201
//Wait for serial to come online for debug printing
@@ -206,18 +216,12 @@ void setup()
206216
beginBoard(); //Determine what hardware platform we are running on
207217

208218
//settings.airSpeed = 19200;
209-
// settings.maxDwellTime = 400;
210-
// settings.frequencyHop = true;
211219
//settings.debug = true; //Enable trigger pin events
212-
// //settings.heartbeatTimeout = 2000;
213-
// settings.displayPacketQuality = false;
214-
//settings.autoTuneFrequency = false;
215-
//settings.encryptData = false; //No ecrypt, no scramble works
216-
//settings.dataScrambling = false; //Encrypt, no scramble works
217-
//settings.pointToPoint = false; //Encrypt, scramble works
218220

219221
beginLoRa(); //Start radio
220222

223+
beginButton(); //Start watching the train button
224+
221225
beginWDT(); //Start watchdog timer
222226

223227
Serial.println(F("LRS"));
@@ -230,21 +234,9 @@ void loop()
230234
{
231235
petWDT();
232236

233-
//updateSerial(); //Store incoming and print outgoing
234-
235-
if (Serial.available())
236-
{
237-
byte incoming = Serial.read();
238-
239-
if (incoming == 't')
240-
{
241-
beginTraining();
242-
}
243-
else if (incoming == 'r')
244-
{
245-
beginDefaultTraining();
246-
}
247-
}
237+
updateButton();
238+
239+
updateSerial(); //Store incoming and print outgoing
248240

249241
updateRadioState(); //Ping/ack/send packets as needed
250242
}

Firmware/LoRaSerial_Firmware/Radio.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,17 @@ void generateHopTable()
550550
//Feed random number generator with our specific platform settings
551551
//Use settings that must be identical to have a functioning link.
552552
//For example, we do not use coding rate because two radios can communicate with different coding rate values
553-
myRandSeed = settings.netID + settings.airSpeed + settings.numberOfChannels
553+
myRandSeed = settings.airSpeed + settings.netID + settings.pointToPoint + settings.encryptData
554+
+ settings.dataScrambling
554555
+ (uint16_t)settings.frequencyMin + (uint16_t)settings.frequencyMax
555-
+ (uint16_t)settings.radioBandwidth + settings.radioSpreadFactor + settings.frequencyHop;
556+
+ settings.numberOfChannels + settings.frequencyHop + settings.maxDwellTime
557+
+ (uint16_t)settings.radioBandwidth + settings.radioSpreadFactor;
556558

557-
//TODO add pointToPoint, encryptData, encryptionKey, dataScrambling, maxDwellTime,
559+
if (settings.encryptData == true)
560+
{
561+
for (int x = 0 ; x < sizeof(settings.encryptionKey) ; x++)
562+
myRandSeed += settings.encryptionKey[x];
563+
}
558564

559565
//'Randomly' shuffle list based on our specific seed
560566
shuffle(channels, settings.numberOfChannels);

Firmware/LoRaSerial_Firmware/System.ino

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,56 @@
1+
//Check the train button and change state accordingly
2+
void updateButton()
3+
{
4+
if (trainBtn != NULL)
5+
{
6+
trainBtn->read();
7+
8+
if (trainState == TRAIN_NO_PRESS && trainBtn->pressedFor(trainButtonTime))
9+
{
10+
trainState = TRAIN_PRESSED_4S;
11+
lastTrainBlink = millis();
12+
}
13+
else if (trainState == TRAIN_PRESSED_4S && trainBtn->wasReleased())
14+
{
15+
digitalWrite(pin_linkLED, LOW);
16+
17+
beginTraining();
18+
19+
trainState = TRAIN_NO_PRESS;
20+
}
21+
else if (trainState == TRAIN_PRESSED_4S && trainBtn->pressedFor(trainWithDefaultsButtonTime))
22+
{
23+
trainState = TRAIN_PRESSED_10S;
24+
}
25+
else if (trainState == TRAIN_PRESSED_10S && trainBtn->wasReleased())
26+
{
27+
digitalWrite(pin_linkLED, LOW);
28+
29+
beginDefaultTraining();
30+
31+
trainState = TRAIN_NO_PRESS;
32+
}
33+
34+
//Blink LEDs according to our state while we wait for user to release button
35+
if (trainState == TRAIN_PRESSED_4S)
36+
{
37+
if (millis() - lastTrainBlink > 500) //Slow blink
38+
{
39+
lastTrainBlink = millis();
40+
digitalWrite(pin_linkLED, !digitalRead(pin_linkLED));
41+
}
42+
}
43+
else if (trainState == TRAIN_PRESSED_10S)
44+
{
45+
if (millis() - lastTrainBlink > 100) //Fast blink
46+
{
47+
lastTrainBlink = millis();
48+
digitalWrite(pin_linkLED, !digitalRead(pin_linkLED));
49+
}
50+
}
51+
}
52+
}
53+
154
//Platform specific reset commands
255
void systemReset()
356
{

Firmware/LoRaSerial_Firmware/settings.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ typedef enum
3131
PROCESS_TRAINING_DATA_PACKET,
3232
} PacketType;
3333

34+
//Train button states
35+
typedef enum
36+
{
37+
TRAIN_NO_PRESS = 0,
38+
TRAIN_PRESSED_4S,
39+
TRAIN_PRESSED_10S,
40+
} TrainStates;
41+
TrainStates trainState = TRAIN_NO_PRESS;
42+
3443
enum
3544
{
3645
TRIGGER_ACK_PROCESSED = 25,

0 commit comments

Comments
 (0)