Skip to content

Commit be2c50d

Browse files
committed
Separate the BT and WiFi radio states.
1 parent 1e2fb32 commit be2c50d

File tree

6 files changed

+54
-52
lines changed

6 files changed

+54
-52
lines changed

Firmware/RTK_Surveyor/Display.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,15 @@ void paintBatteryLevel()
265265
//Display Bluetooth icon, Bluetooth MAC, or WiFi depending on connection state
266266
void paintWirelessIcon()
267267
{
268+
//TODO resolve if both BT and WiFi are on/connected
268269
if (online.display == true)
269270
{
270271
//Bluetooth icon if paired, or Bluetooth MAC address if not paired
271-
if (radioState == BT_CONNECTED)
272+
if (btState == BT_CONNECTED)
272273
{
273274
displayBitmap(4, 0, BT_Symbol_Width, BT_Symbol_Height, BT_Symbol);
274275
}
275-
else if (radioState == WIFI_ON_NOCONNECTION)
276+
else if (wifiState == WIFI_NOTCONNECTED)
276277
{
277278
//Blink WiFi icon
278279
if (millis() - lastWifiIconUpdate > 500)
@@ -289,7 +290,7 @@ void paintWirelessIcon()
289290
wifiIconDisplayed = false;
290291
}
291292
}
292-
else if (radioState == WIFI_CONNECTED)
293+
else if (wifiState == WIFI_CONNECTED)
293294
{
294295
//Solid WiFi icon
295296
displayBitmap(0, 0, WiFi_Symbol_Width, WiFi_Symbol_Height, WiFi_Symbol);
@@ -1317,7 +1318,7 @@ void displayWiFiConfigNotStarted()
13171318
void displayWiFiConfig()
13181319
{
13191320
//Draw the WiFi icon
1320-
if (radioState == WIFI_ON_NOCONNECTION)
1321+
if (wifiState == WIFI_NOTCONNECTED)
13211322
{
13221323
//Blink WiFi icon
13231324
if (millis() - lastWifiIconUpdate > 500)
@@ -1334,7 +1335,7 @@ void displayWiFiConfig()
13341335
wifiIconDisplayed = false;
13351336
}
13361337
}
1337-
else if (radioState == WIFI_CONNECTED)
1338+
else if (wifiState == WIFI_CONNECTED)
13381339
{
13391340
//Solid WiFi icon
13401341
displayBitmap((oled.getWidth() / 2) - (WiFi_Symbol_Width / 2), 0, WiFi_Symbol_Width, WiFi_Symbol_Height, WiFi_Symbol);

Firmware/RTK_Surveyor/Form.ino

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ void startConfigAP()
1010
#ifdef COMPILE_WIFI
1111
#ifdef COMPILE_AP
1212

13-
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
14-
esp_wifi_init(&wifi_init_config); //Restart WiFi resources
15-
1613
//Check SD Size
1714
if (online.microSD)
1815
{
@@ -179,7 +176,7 @@ void startConfigAP()
179176
#endif
180177
#endif
181178

182-
radioState = WIFI_ON_NOCONNECTION;
179+
wifiState = WIFI_NOTCONNECTED;
183180
}
184181

185182
//Handler for firmware file upload
@@ -282,11 +279,11 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
282279
createSettingsString(settingsCSV);
283280
log_d("Sending command: %s\n\r", settingsCSV);
284281
client->text(settingsCSV);
285-
radioState = WIFI_CONNECTED;
282+
wifiState = WIFI_CONNECTED;
286283
}
287284
else if (type == WS_EVT_DISCONNECT) {
288285
log_d("Websocket client disconnected");
289-
radioState = WIFI_ON_NOCONNECTION;
286+
wifiState = WIFI_NOTCONNECTED;
290287
}
291288
else if (type == WS_EVT_DATA) {
292289
for (int i = 0; i < len; i++) {

Firmware/RTK_Surveyor/States.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void updateSystemState()
152152
if (settings.enableNtripClient == true && ntripClientAttempted == false)
153153
{
154154
//Turn off Bluetooth and turn on WiFi
155-
stopBluetooth();
155+
//stopBluetooth();
156156
startWiFi(settings.ntripClient_wifiSSID, settings.ntripClient_wifiPW);
157157
wifiStartTime = millis();
158158

@@ -219,7 +219,7 @@ void updateSystemState()
219219
byte wifiStatus = WiFi.status();
220220
if (wifiStatus == WL_CONNECTED)
221221
{
222-
radioState = WIFI_CONNECTED;
222+
wifiState = WIFI_CONNECTED;
223223

224224
// Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA
225225
i2cGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP);
@@ -619,7 +619,7 @@ void updateSystemState()
619619
byte wifiStatus = WiFi.status();
620620
if (wifiStatus == WL_CONNECTED)
621621
{
622-
radioState = WIFI_CONNECTED;
622+
wifiState = WIFI_CONNECTED;
623623

624624
changeState(STATE_BASE_TEMP_WIFI_CONNECTED);
625625
}
@@ -850,7 +850,7 @@ void updateSystemState()
850850
byte wifiStatus = WiFi.status();
851851
if (wifiStatus == WL_CONNECTED)
852852
{
853-
radioState = WIFI_CONNECTED;
853+
wifiState = WIFI_CONNECTED;
854854

855855
changeState(STATE_BASE_FIXED_WIFI_CONNECTED);
856856
}
@@ -1194,7 +1194,7 @@ void updateSystemState()
11941194
byte wifiStatus = WiFi.status();
11951195
if (wifiStatus == WL_CONNECTED)
11961196
{
1197-
radioState = WIFI_CONNECTED;
1197+
wifiState = WIFI_CONNECTED;
11981198

11991199
changeState(STATE_KEYS_WIFI_CONNECTED);
12001200
}
@@ -1306,7 +1306,7 @@ void updateSystemState()
13061306
byte wifiStatus = WiFi.status();
13071307
if (wifiStatus == WL_CONNECTED)
13081308
{
1309-
radioState = WIFI_CONNECTED;
1309+
wifiState = WIFI_CONNECTED;
13101310

13111311
changeState(STATE_KEYS_PROVISION_WIFI_CONNECTED);
13121312
}

Firmware/RTK_Surveyor/System.ino

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
//This allows multiple units to be on at same time
44
void startBluetooth()
55
{
6-
if (radioState == WIFI_ON_NOCONNECTION || radioState == WIFI_CONNECTED)
7-
stopWiFi();
8-
9-
if (radioState == RADIO_OFF)
6+
if (btState == BT_OFF)
107
{
118
#ifdef COMPILE_BT
129
char stateName[10];
@@ -21,7 +18,6 @@ void startBluetooth()
2118
if (SerialBT.begin(deviceName, false) == false) //localName, isMaster
2219
{
2320
Serial.println(F("An error occurred initializing Bluetooth"));
24-
radioState = RADIO_OFF;
2521

2622
if (productVariant == RTK_SURVEYOR)
2723
digitalWrite(pin_bluetoothStatusLED, LOW);
@@ -63,7 +59,7 @@ void startBluetooth()
6359
}
6460
#endif
6561

66-
radioState = BT_ON_NOCONNECTION;
62+
btState = BT_NOTCONNECTED;
6763
}
6864

6965
}
@@ -72,7 +68,7 @@ void startBluetooth()
7268
//It also releases as much system resources as possible so that WiFi/caster is more stable
7369
void stopBluetooth()
7470
{
75-
if (radioState == BT_ON_NOCONNECTION || radioState == BT_CONNECTED)
71+
if (btState == BT_NOTCONNECTED || btState == BT_CONNECTED)
7672
{
7773
#ifdef COMPILE_BT
7874
SerialBT.register_callback(NULL);
@@ -82,39 +78,37 @@ void stopBluetooth()
8278
#endif
8379

8480
log_d("Bluetooth turned off");
85-
radioState = RADIO_OFF;
81+
82+
btState = BT_OFF;
8683
}
8784
}
8885

8986
void startWiFi(char* ssid, char* pw)
9087
{
91-
if (radioState == BT_ON_NOCONNECTION || radioState == BT_CONNECTED)
92-
stopBluetooth();
93-
94-
if (radioState == RADIO_OFF)
88+
if (wifiState == WIFI_OFF)
9589
{
9690
#ifdef COMPILE_WIFI
9791
Serial.printf("Connecting to WiFi: %s", ssid);
9892
WiFi.begin(ssid, pw);
9993
#endif
10094

101-
radioState = WIFI_ON_NOCONNECTION;
95+
wifiState = WIFI_NOTCONNECTED;
10296
}
10397
}
10498

10599
//Stop WiFi and release all resources
106100
//See WiFiBluetoothSwitch sketch for more info
107101
void stopWiFi()
108102
{
109-
if (radioState == WIFI_ON_NOCONNECTION || radioState == WIFI_CONNECTED)
103+
if (wifiState == WIFI_NOTCONNECTED || wifiState == WIFI_CONNECTED)
110104
{
111105
#ifdef COMPILE_WIFI
112-
ntripServer.stop();
113-
WiFi.mode(WIFI_OFF);
106+
ntripServer.stop();
107+
WiFi.mode(WIFI_OFF);
114108
#endif
115109

116-
log_d("WiFi Stopped");
117-
radioState = RADIO_OFF;
110+
log_d("WiFi Stopped");
111+
wifiState = WIFI_OFF;
118112
}
119113
}
120114

@@ -448,19 +442,19 @@ void danceLEDs()
448442
}
449443

450444
//Call back for when BT connection event happens (connected/disconnect)
451-
//Used for updating the radioState state machine
445+
//Used for updating the btState state machine
452446
#ifdef COMPILE_BT
453447
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
454448
if (event == ESP_SPP_SRV_OPEN_EVT) {
455449
Serial.println(F("Client Connected"));
456-
radioState = BT_CONNECTED;
450+
btState = BT_CONNECTED;
457451
if (productVariant == RTK_SURVEYOR)
458452
digitalWrite(pin_bluetoothStatusLED, HIGH);
459453
}
460454

461455
if (event == ESP_SPP_CLOSE_EVT ) {
462456
Serial.println(F("Client disconnected"));
463-
radioState = BT_ON_NOCONNECTION;
457+
btState = BT_NOTCONNECTED;
464458
if (productVariant == RTK_SURVEYOR)
465459
digitalWrite(pin_bluetoothStatusLED, LOW);
466460
}

Firmware/RTK_Surveyor/Tasks.ino

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void F9PSerialWriteTask(void *e)
99
{
1010
#ifdef COMPILE_BT
1111
//Receive RTCM corrections or UBX config messages over bluetooth and pass along to ZED
12-
if (radioState == BT_CONNECTED)
12+
if (btState == BT_CONNECTED)
1313
{
1414
while (SerialBT.available())
1515
{
@@ -47,7 +47,7 @@ void F9PSerialReadTask(void *e)
4747
taskYIELD();
4848
}
4949
#ifdef COMPILE_BT
50-
else if (radioState == BT_CONNECTED)
50+
else if (btState == BT_CONNECTED)
5151
{
5252
if (SerialBT.isCongested() == false)
5353
{
@@ -94,21 +94,25 @@ void F9PSerialReadTask(void *e)
9494
}
9595
}
9696

97-
//Control BT status LED according to radioState
97+
//Control BT status LED according to btState
9898
void updateBTled()
9999
{
100100
if (productVariant == RTK_SURVEYOR)
101101
{
102-
if (radioState == BT_ON_NOCONNECTION)
102+
//Blink on/off while we wait for BT connection
103+
if (btState == BT_NOTCONNECTED)
103104
{
104-
//Blink on/off while we wait for BT connection
105105
if (btFadeLevel == 0) btFadeLevel = 255;
106106
else btFadeLevel = 0;
107107
ledcWrite(ledBTChannel, btFadeLevel);
108108
}
109-
else if (radioState == BT_CONNECTED)
109+
110+
//Solid LED if BT Connected
111+
else if (btState == BT_CONNECTED)
110112
ledcWrite(ledBTChannel, 255);
111-
else if (radioState == WIFI_ON_NOCONNECTION || radioState == WIFI_CONNECTED)
113+
114+
//Pulse LED while no BT and we wait for WiFi connection
115+
else if (wifiState == WIFI_NOTCONNECTED || wifiState == WIFI_CONNECTED)
112116
{
113117
//Fade in/out the BT LED during WiFi AP mode
114118
btFadeLevel += pwmFadeAmount;
@@ -157,7 +161,7 @@ void ButtonCheckTask(void *e)
157161
Setup button = 0 | | after short time |
158162
after short time | | (< 500 mSec) |
159163
(< 500 mSec) | | |
160-
STATE_ROVER_NOT_STARTED | | |
164+
STATE_ROVER_NOT_STARTED | | |
161165
V V |
162166
.------------------. .------------------. |
163167
| Test Mode | | WiFi Config Mode |----------'

Firmware/RTK_Surveyor/settings.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,22 @@ typedef enum
116116
ERROR_GPS_CONFIG_FAIL,
117117
} t_errorNumber;
118118

119+
enum WiFiState
120+
{
121+
WIFI_OFF = 0,
122+
WIFI_NOTCONNECTED,
123+
WIFI_CONNECTED,
124+
};
125+
volatile byte wifiState = WIFI_OFF;
126+
119127
//Radio status LED goes from off (LED off), no connection (blinking), to connected (solid)
120-
enum RadioState
128+
enum BTState
121129
{
122-
RADIO_OFF = 0,
123-
BT_ON_NOCONNECTION, //WiFi is off
130+
BT_OFF = 0,
131+
BT_NOTCONNECTED,
124132
BT_CONNECTED,
125-
WIFI_ON_NOCONNECTION, //BT is off
126-
WIFI_CONNECTED,
127133
};
128-
volatile byte radioState = RADIO_OFF;
134+
volatile byte btState = BT_OFF;
129135

130136
//Return values for getByteChoice()
131137
enum returnStatus {

0 commit comments

Comments
 (0)