@@ -257,35 +257,97 @@ uint8_t systemRead()
257257// Check the train button and change state accordingly
258258void updateButton ()
259259{
260- static TrainStates trainState = TRAIN_NO_PRESS;
260+ bool buttonPressed;
261+ static bool buttonWasPressed;
262+ uint32_t deltaTime;
263+ static Settings originalSettings;
264+ static uint32_t pressedTime;
261265
262266 if (trainBtn != NULL )
263267 {
264- trainBtn->read ();
268+ // Determine the previous state of the button
269+ buttonPressed = trainBtn->read ();
265270
266- if (trainState == TRAIN_NO_PRESS && trainBtn->pressedFor (trainButtonTime))
271+ // Determine the current button state
272+ if (buttonWasPressed)
267273 {
268- trainState = TRAIN_PRESSED;
269-
270- trainViaButton = true ;
271- trainingSettings = settings;
272- selectTraining ();
273- }
274- else if (trainState == TRAIN_PRESSED && trainBtn->wasReleased ())
275- {
276- trainState = TRAIN_IN_PROCESS;
274+ // Update the LEDs
275+ deltaTime = millis () - pressedTime;
276+ buttonLeds (deltaTime);
277+ if (!buttonPressed)
278+ {
279+ // Button just released
280+ settings = originalSettings;
281+
282+ // Check for reset to factory settings
283+ if (deltaTime >= trainButtonFactoryResetTime)
284+ {
285+ // Erase the EEPROM
286+ nvmErase ();
287+
288+ // Reboot the radio
289+ commandReset ();
290+ }
291+
292+ // Starting training or exiting training via the button is only possible
293+ // when the radio is not in command mode!
294+ else if (!inCommandMode)
295+ {
296+ // Check for server training request
297+ if (deltaTime >= trainButtonServerTime)
298+ {
299+ // Set the training settings
300+ trainingSettings = settings;
301+ inTraining = true ;
302+ if (settings.operatingMode == MODE_POINT_TO_POINT)
303+ {
304+ // Select a random netID and encryption key
305+ generateRandomKeysID (&trainingSettings);
306+ }
307+ beginTrainingServer ();
308+ }
309+
310+ // Check for client training request
311+ else if (deltaTime >= trainButtonClientTime)
312+ {
313+ trainingSettings = settings; // Set the training settings
314+ inTraining = true ;
315+ beginTrainingClient ();
316+ }
317+
318+ // Exit training
319+ else
320+ {
321+ // The user has stopped training with the button
322+ // Restore the previous settings
323+ settings = originalSettings;
324+ if (inTraining)
325+ {
326+ inTraining = false ;
327+ changeState (RADIO_RESET);
328+ }
329+ }
330+ }
331+ }
277332 }
278- else if (trainState == TRAIN_IN_PROCESS && trainBtn->wasReleased ())
279- {
280- settings = trainingSettings; // Return to original radio settings
281333
282- recordSystemSettings (); // Record original settings
334+ // The button was not previously pressed
335+ else
336+ {
337+ // Determine if the button is pressed
338+ if (buttonPressed)
339+ {
340+ // Button just pressed
341+ pressedTime = millis ();
283342
284- // Reboot the radio
285- petWDT ();
286- systemFlush () ;
287- systemReset ();
343+ // Save the previous led state
344+ if (!inTraining)
345+ originalSettings = settings ;
346+ }
288347 }
348+
349+ // Save the current button state
350+ buttonWasPressed = buttonPressed;
289351 }
290352}
291353
@@ -1001,6 +1063,36 @@ void updateCylonLEDs()
10011063 blinkRadioTxLed (false );
10021064}
10031065
1066+ // Acknowledge the button press
1067+ void buttonLeds (uint32_t pressedMilliseconds)
1068+ {
1069+ const uint32_t blinkTime = 1000 / 4 ; // Blink 4 times per second
1070+ uint8_t on;
1071+ const uint32_t onTime = blinkTime / 2 ;
1072+ uint8_t seconds;
1073+
1074+ // Display the number of seconds the button has been pushed
1075+ seconds = pressedMilliseconds / 1000 ;
1076+ setRSSI ((pressedMilliseconds >= trainButtonFactoryResetTime) ? 15 : seconds);
1077+
1078+ if (!inCommandMode)
1079+ {
1080+ // Determine the blink state
1081+ on = (pressedMilliseconds % blinkTime) >= onTime;
1082+
1083+ // Determine which LED to blink
1084+ if (pressedMilliseconds >= trainButtonServerTime)
1085+ {
1086+ // Blink the blue LED
1087+ digitalWrite (BLUE_LED, on);
1088+ digitalWrite (YELLOW_LED, 0 );
1089+ }
1090+ else if (pressedMilliseconds >= trainButtonClientTime)
1091+ // Blink the yellow LED
1092+ digitalWrite (YELLOW_LED, on);
1093+ }
1094+ }
1095+
10041096// Update the LED values depending upon the selected display
10051097// This is the only function that touches the LEDs
10061098void updateLeds ()
0 commit comments