Skip to content

Commit 2061dfa

Browse files
author
Nathan Seidle
committed
Start of menu system
1 parent 1707762 commit 2061dfa

File tree

5 files changed

+244
-19
lines changed

5 files changed

+244
-19
lines changed

Firmware/RTK_Enclosed/RTK_Enclosed.ino

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@
4343
If more than 1Hz, turn off SV sentences.
4444
*/
4545

46+
const int FIRMWARE_VERSION_MAJOR = 1;
47+
const int FIRMWARE_VERSION_MINOR = 6;
48+
49+
#include "settings.h"
50+
51+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
52+
//Hardware connections v11
53+
const int positionAccuracyLED_1cm = 2;
54+
const int baseStatusLED = 4;
55+
const int baseSwitch = 5;
56+
const int bluetoothStatusLED = 12;
57+
const int positionAccuracyLED_100cm = 13;
58+
const int positionAccuracyLED_10cm = 15;
59+
const byte PIN_MICROSD_CHIP_SELECT = 25;
60+
const int zed_tx_ready = 26;
61+
const int zed_reset = 27;
62+
const int batteryLevelLED_Red = 32;
63+
const int batteryLevelLED_Green = 33;
64+
const int batteryLevel_alert = 36;
65+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
66+
67+
//microSD Interface
68+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
69+
#include <SPI.h>
70+
#include <SdFat.h> //SdFat (FAT32) by Bill Greiman: http://librarymanager/All#SdFat
71+
SdFat sd;
72+
SdFile sensorDataFile; //File that all sensor data is written to
73+
SdFile serialDataFile; //File that all incoming serial data is written to
74+
//#define PRINT_LAST_WRITE_TIME // Uncomment this line to enable the 'measure the time between writes' diagnostic
75+
76+
char sensorDataFileName[30] = ""; //We keep a record of this file name so that we can re-open it upon wakeup from sleep
77+
char serialDataFileName[30] = ""; //We keep a record of this file name so that we can re-open it upon wakeup from sleep
78+
const int sdPowerDownDelay = 100; //Delay for this many ms before turning off the SD card power
79+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
80+
4681
#include <Wire.h> //Needed for I2C to GPS
4782

4883
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
@@ -104,22 +139,6 @@ uint8_t rBuffer[SERIAL_SIZE_RX]; //Buffer for reading F9P
104139
uint8_t wBuffer[SERIAL_SIZE_RX]; //Buffer for writing to F9P
105140
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
106141

107-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
108-
//Hardware connections v11
109-
const int positionAccuracyLED_1cm = 2;
110-
const int baseStatusLED = 4;
111-
const int baseSwitch = 5;
112-
const int bluetoothStatusLED = 12;
113-
const int positionAccuracyLED_100cm = 13;
114-
const int positionAccuracyLED_10cm = 15;
115-
const int sd_cs = 25;
116-
const int zed_tx_ready = 26;
117-
const int zed_reset = 27;
118-
const int batteryLevelLED_Red = 32;
119-
const int batteryLevelLED_Green = 33;
120-
const int batteryLevel_alert = 36;
121-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
122-
123142
//Freeze and blink LEDs if we hit a bad error
124143
typedef enum
125144
{
@@ -149,19 +168,30 @@ volatile BaseState baseState = BASE_OFF;
149168
unsigned long baseStateBlinkTime = 0;
150169
const unsigned long maxSurveyInWait_s = 60L * 15L; //Re-start survey-in after X seconds
151170

171+
//Return values for getByteChoice()
172+
enum returnStatus {
173+
STATUS_GETBYTE_TIMEOUT = 255,
174+
STATUS_GETNUMBER_TIMEOUT = -123455555,
175+
STATUS_PRESSED_X,
176+
};
177+
178+
//Global variables
179+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
180+
const byte menuTimeout = 15; //Menus will exit/timeout after this number of seconds
152181
uint32_t lastBluetoothLEDBlink = 0;
153182
uint32_t lastRoverUpdate = 0;
154183
uint32_t lastBaseUpdate = 0;
155184
uint32_t lastBattUpdate = 0;
156185

157186
uint32_t lastTime = 0;
187+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
158188

159189
void setup()
160190
{
161191
Serial.begin(115200); //UART0 for programming and debugging
162192
Serial.setRxBufferSize(SERIAL_SIZE_RX);
163193
Serial.setTimeout(1);
164-
194+
165195
GPS.begin(115200); //UART2 on pins 16/17 for SPP. The ZED-F9P will be configured to output NMEA over its UART1 at 115200bps.
166196
GPS.setRxBufferSize(SERIAL_SIZE_RX);
167197
GPS.setTimeout(1);
@@ -258,6 +288,8 @@ void setup()
258288
}
259289
Serial.println(F("GPS configuration complete"));
260290

291+
beginSD(); //Test if SD is present
292+
261293
danceLEDs(); //Turn on LEDs like a car dashboard
262294

263295
//myGPS.enableDebugging(); //Enable debug messages over Serial (default)
@@ -321,6 +353,63 @@ void loop()
321353
}
322354

323355
updateBattLEDs();
356+
357+
//Menu system via ESP32 USB connection
358+
if (Serial.available()) menuMain(); //Present user menu
359+
324360
delay(10); //Required if no other I2C or functions are called
325361

326362
}
363+
364+
void beginSD()
365+
{
366+
pinMode(PIN_MICROSD_CHIP_SELECT, OUTPUT);
367+
digitalWrite(PIN_MICROSD_CHIP_SELECT, HIGH); //Be sure SD is deselected
368+
369+
if (settings.enableSD == true)
370+
{
371+
// For reasons I don't understand, we seem to have to wait for at least 1ms after SPI.begin before we call microSDPowerOn.
372+
// If you comment the next line, the Artemis resets at microSDPowerOn when beginSD is called from wakeFromSleep...
373+
// But only on one of my V10 red boards. The second one I have doesn't seem to need the delay!?
374+
delay(1);
375+
376+
// microSDPowerOn();
377+
378+
//Max power up time is 250ms: https://www.kingston.com/datasheets/SDCIT-specsheet-64gb_en.pdf
379+
//Max current is 200mA average across 1s, peak 300mA
380+
for (int i = 0; i < 10; i++) //Wait
381+
{
382+
delay(1);
383+
}
384+
385+
if (sd.begin(PIN_MICROSD_CHIP_SELECT, SD_SCK_MHZ(24)) == false) //Standard SdFat
386+
{
387+
printDebug("SD init failed (first attempt). Trying again...\r\n");
388+
for (int i = 0; i < 250; i++) //Give SD more time to power up, then try again
389+
{
390+
delay(1);
391+
}
392+
if (sd.begin(PIN_MICROSD_CHIP_SELECT, SD_SCK_MHZ(24)) == false) //Standard SdFat
393+
{
394+
Serial.println(F("SD init failed (second attempt). Is card present? Formatted?"));
395+
digitalWrite(PIN_MICROSD_CHIP_SELECT, HIGH); //Be sure SD is deselected
396+
online.microSD = false;
397+
return;
398+
}
399+
}
400+
401+
//Change to root directory. All new file creation will be in root.
402+
if (sd.chdir() == false)
403+
{
404+
Serial.println(F("SD change directory failed"));
405+
online.microSD = false;
406+
return;
407+
}
408+
409+
online.microSD = true;
410+
}
411+
else
412+
{
413+
online.microSD = false;
414+
}
415+
}

Firmware/RTK_Enclosed/System.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ void F9PSerialWriteTask(void *e)
1414
auto s = Serial.readBytes(wBuffer, SERIAL_SIZE_RX);
1515
GPS.write(wBuffer, s);
1616
}
17-
// else if (SerialBT.connected() && SerialBT.available())
18-
17+
// else if (SerialBT.connected() && SerialBT.available())
18+
1919
if (SerialBT.available())
2020
{
2121
while (SerialBT.available())

Firmware/RTK_Enclosed/menuMain.ino

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//Display the options
2+
//If user doesn't respond within a few seconds, return to main loop
3+
void menuMain()
4+
{
5+
while (1)
6+
{
7+
Serial.println();
8+
Serial.println(F("Menu: Main Menu"));
9+
10+
Serial.println(F("1) Configure Terminal Output"));
11+
12+
Serial.println(F("2) Configure Time Stamp"));
13+
14+
if (settings.enableSD && online.microSD)
15+
Serial.println(F("s) SD Card File Transfer"));
16+
17+
Serial.println(F("r) Reset all settings to default"));
18+
19+
Serial.println(F("x) Exit"));
20+
21+
byte incoming = getByteChoice(menuTimeout); //Timeout after x seconds
22+
23+
if (incoming == '1') {}
24+
//menuLogRate();
25+
else if (incoming == '2') {}
26+
//menuTimeStamp();
27+
else if (incoming == 'r')
28+
{
29+
// Serial.println(F("\r\nResetting to factory defaults. Press 'y' to confirm:"));
30+
// byte bContinue = getByteChoice(menuTimeout);
31+
// if (bContinue == 'y')
32+
// {
33+
// EEPROM.erase();
34+
// if (sd.exists("OLA_settings.txt"))
35+
// sd.remove("OLA_settings.txt");
36+
// if (sd.exists("OLA_deviceSettings.txt"))
37+
// sd.remove("OLA_deviceSettings.txt");
38+
//
39+
// Serial.print(F("Settings erased. Please reset OpenLog Artemis and open a terminal at "));
40+
// Serial.print((String)settings.serialTerminalBaudRate);
41+
// Serial.println(F("bps..."));
42+
// while (1);
43+
// else
44+
// Serial.println(F("Reset aborted"));
45+
}
46+
else if (incoming == 'x')
47+
break;
48+
else if (incoming == STATUS_GETBYTE_TIMEOUT)
49+
break;
50+
else
51+
printUnknown(incoming);
52+
}
53+
54+
//recordSystemSettings(); //Once all menus have exited, record the new settings to EEPROM and config file
55+
56+
//recordDeviceSettingsToFile(); //Record the current devices settings to device config file
57+
58+
while (Serial.available()) Serial.read(); //Empty buffer of any newline chars
59+
}

Firmware/RTK_Enclosed/settings.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//This is all the settings that can be set on RTK Surveyor. It's recorded to NVM and the config file.
2+
struct struct_settings {
3+
bool printDebugMessages = false;
4+
bool enableSD = true;
5+
bool enableDisplay = true;
6+
} settings;
7+
8+
//These are the devices on board RTK Surveyor that may be on or offline.
9+
struct struct_online {
10+
bool microSD = false;
11+
bool display = false;
12+
bool dataLogging = false;
13+
bool serialLogging = false;
14+
bool serialOutput = false;
15+
} online;

Firmware/RTK_Enclosed/support.ino

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
void printDebug(String thingToPrint)
2+
{
3+
if(settings.printDebugMessages == true)
4+
{
5+
Serial.print(thingToPrint);
6+
}
7+
}
8+
9+
//Option not known
10+
void printUnknown(uint8_t unknownChoice)
11+
{
12+
Serial.print(F("Unknown choice: "));
13+
Serial.write(unknownChoice);
14+
Serial.println();
15+
}
16+
void printUnknown(int unknownValue)
17+
{
18+
Serial.print(F("Unknown value: "));
19+
Serial.write(unknownValue);
20+
Serial.println();
21+
}
22+
23+
//Get single byte from user
24+
//Waits for and returns the character that the user provides
25+
//Returns STATUS_GETNUMBER_TIMEOUT if input times out
26+
//Returns 'x' if user presses 'x'
27+
uint8_t getByteChoice(int numberOfSeconds)
28+
{
29+
Serial.flush();
30+
for (int i = 0; i < 50; i++) //Wait for any incoming chars to hit buffer
31+
{
32+
//checkBattery();
33+
delay(1);
34+
}
35+
while (Serial.available() > 0) Serial.read(); //Clear buffer
36+
37+
long startTime = millis();
38+
byte incoming;
39+
while (1)
40+
{
41+
if (Serial.available() > 0)
42+
{
43+
incoming = Serial.read();
44+
// Serial.print(F("byte: 0x"));
45+
// Serial.println(incoming, HEX);
46+
if (incoming >= 'a' && incoming <= 'z') break;
47+
if (incoming >= 'A' && incoming <= 'Z') break;
48+
if (incoming >= '0' && incoming <= '9') break;
49+
}
50+
51+
if ( (millis() - startTime) / 1000 >= numberOfSeconds)
52+
{
53+
Serial.println(F("No user input received."));
54+
return (STATUS_GETBYTE_TIMEOUT); //Timeout. No user input.
55+
}
56+
57+
//checkBattery();
58+
delay(1);
59+
}
60+
61+
return (incoming);
62+
}

0 commit comments

Comments
 (0)