Skip to content

Commit ef44b76

Browse files
committed
Merge branch 'airplane-mode' of https://github.com/evergreen22/InfiniTime into evergreen22-airplane-mode
Apply a few changes that were requested in the PR during the review. # Conflicts: # src/CMakeLists.txt # src/displayapp/Apps.h # src/displayapp/DisplayApp.cpp # src/displayapp/Messages.h # src/displayapp/screens/settings/Settings.cpp
1 parent 40cdb54 commit ef44b76

21 files changed

Lines changed: 179 additions & 124 deletions

src/components/ble/BleController.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22

33
using namespace Pinetime::Controllers;
44

5-
void Ble::SetConnectState(Ble::ConnectStates newState) {
6-
connectionState = newState;
5+
bool Ble::IsConnected() const {
6+
return isConnected;
77
}
88

9-
Ble::ConnectStates Ble::GetConnectState() const {
10-
return connectionState;
9+
void Ble::Connect() {
10+
isConnected = true;
11+
}
12+
13+
void Ble::Disconnect() {
14+
isConnected = false;
15+
}
16+
17+
bool Ble::IsRadioEnabled() const {
18+
return isRadioEnabled;
19+
}
20+
21+
void Ble::EnableRadio() {
22+
isRadioEnabled = true;
23+
}
24+
25+
void Ble::DisableRadio() {
26+
isRadioEnabled = false;
1127
}
1228

1329
void Ble::StartFirmwareUpdate() {

src/components/ble/BleController.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ namespace Pinetime {
1010
using BleAddress = std::array<uint8_t, 6>;
1111
enum class FirmwareUpdateStates { Idle, Running, Validated, Error };
1212
enum class AddressTypes { Public, Random, RPA_Public, RPA_Random };
13-
enum class ConnectStates { Disconnected, Connected, Airplane };
1413

1514
Ble() = default;
16-
bool IsConnected() const {
17-
return (connectionState == ConnectStates::Connected);
18-
}
19-
void SetConnectState(ConnectStates newState);
20-
ConnectStates GetConnectState() const;
15+
bool IsConnected() const;
16+
void Connect();
17+
void Disconnect();
18+
19+
bool IsRadioEnabled() const;
20+
void EnableRadio();
21+
void DisableRadio();
2122

2223
void StartFirmwareUpdate();
2324
void StopFirmwareUpdate();
@@ -57,7 +58,8 @@ namespace Pinetime {
5758
}
5859

5960
private:
60-
ConnectStates connectionState = ConnectStates::Disconnected;
61+
bool isConnected = false;
62+
bool isRadioEnabled = true;
6163
bool isFirmwareUpdating = false;
6264
uint32_t firmwareUpdateTotalBytes = 0;
6365
uint32_t firmwareUpdateCurrentBytes = 0;

src/components/ble/NimbleController.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
184184
case BLE_GAP_EVENT_ADV_COMPLETE:
185185
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
186186
NRF_LOG_INFO("reason=%d; status=%0X", event->adv_complete.reason, event->connect.status);
187-
if (bleController.GetConnectState() == Ble::ConnectStates::Disconnected) {
187+
if (bleController.IsRadioEnabled() && !bleController.IsConnected()) {
188188
StartAdvertising();
189189
}
190190
break;
@@ -199,12 +199,12 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
199199
currentTimeClient.Reset();
200200
alertNotificationClient.Reset();
201201
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
202-
bleController.SetConnectState(Ble::ConnectStates::Disconnected);
202+
bleController.Disconnect();
203203
fastAdvCount = 0;
204204
StartAdvertising();
205205
} else {
206206
connectionHandle = event->connect.conn_handle;
207-
bleController.SetConnectState(Ble::ConnectStates::Connected);
207+
bleController.Connect();
208208
systemTask.PushMessage(Pinetime::System::Messages::BleConnected);
209209
// Service discovery is deferred via systemtask
210210
}
@@ -222,8 +222,8 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
222222
currentTimeClient.Reset();
223223
alertNotificationClient.Reset();
224224
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
225-
if (bleController.GetConnectState() == Ble::ConnectStates::Connected) {
226-
bleController.SetConnectState(Ble::ConnectStates::Disconnected);
225+
if(bleController.IsConnected()) {
226+
bleController.Disconnect();
227227
fastAdvCount = 0;
228228
StartAdvertising();
229229
}
@@ -401,19 +401,20 @@ void NimbleController::NotifyBatteryLevel(uint8_t level) {
401401
}
402402
}
403403

404-
void NimbleController::SwitchAirplaneMode(bool enabled) {
405-
if (enabled) {
406-
if (bleController.IsConnected()) {
407-
bleController.SetConnectState(Ble::ConnectStates::Airplane);
408-
ble_gap_terminate(connectionHandle, BLE_ERR_REM_USER_CONN_TERM);
409-
} else {
410-
bleController.SetConnectState(Ble::ConnectStates::Airplane);
411-
ble_gap_adv_stop();
412-
}
404+
void NimbleController::EnableRadio() {
405+
bleController.EnableRadio();
406+
bleController.Disconnect();
407+
fastAdvCount = 0;
408+
StartAdvertising();
409+
}
410+
411+
void NimbleController::DisableRadio() {
412+
bleController.DisableRadio();
413+
if (bleController.IsConnected()) {
414+
ble_gap_terminate(connectionHandle, BLE_ERR_REM_USER_CONN_TERM);
415+
bleController.Disconnect();
413416
} else {
414-
bleController.SetConnectState(Ble::ConnectStates::Disconnected);
415-
fastAdvCount = 0;
416-
StartAdvertising();
417+
ble_gap_adv_stop();
417418
}
418419
}
419420

src/components/ble/NimbleController.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ namespace Pinetime {
5353
void Init();
5454
void StartAdvertising();
5555
int OnGAPEvent(ble_gap_event* event);
56-
57-
/* these are not implemented yet
58-
int OnDiscoveryEvent(uint16_t i, const ble_gatt_error* pError, const ble_gatt_svc* pSvc);
59-
int OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
60-
int OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
61-
int OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error* error, ble_gatt_attr* attribute);
62-
int OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle,
63-
const ble_gatt_error* error,
64-
uint16_t characteristicValueHandle,
65-
const ble_gatt_dsc* descriptor);
66-
*/
67-
6856
void StartDiscovery();
6957

7058
Pinetime::Controllers::MusicService& music() {
@@ -87,7 +75,8 @@ namespace Pinetime {
8775
fastAdvCount = 0;
8876
};
8977

90-
void SwitchAirplaneMode(bool enabled);
78+
void EnableRadio();
79+
void DisableRadio();
9180

9281
private:
9382
void PersistBond(struct ble_gap_conn_desc& desc);

src/components/settings/Settings.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ namespace Pinetime {
202202
return settings.stepsGoal;
203203
};
204204

205-
void SetAirplaneMode(bool mode) {
206-
airplaneMode = mode;
205+
void SetBleRadioEnabled(bool enabled) {
206+
bleRadioEnabled = enabled;
207207
};
208208

209-
bool GetAirplaneMode() const {
210-
return airplaneMode;
209+
bool GetBleRadioEnabled() const {
210+
return bleRadioEnabled;
211211
};
212212

213213
private:
@@ -240,7 +240,7 @@ namespace Pinetime {
240240
/* airplaneMode is intentionally not saved with the other watch settings and initialized
241241
* to off (false) on every boot because we always want ble to be enabled on startup
242242
*/
243-
bool airplaneMode = false;
243+
bool bleRadioEnabled = true;
244244

245245
void LoadSettingsFromFile();
246246
void SaveSettingsToFile();

src/displayapp/DisplayApp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ void DisplayApp::Refresh() {
293293
case Messages::BleFirmwareUpdateStarted:
294294
LoadApp(Apps::FirmwareUpdate, DisplayApp::FullRefreshDirections::Down);
295295
break;
296-
case Messages::AirplaneModeToggle:
297-
PushMessageToSystemTask(System::Messages::AirplaneModeToggle);
296+
case Messages::BleRadioEnableToggle:
297+
PushMessageToSystemTask(System::Messages::BleRadioEnableToggle);
298298
break;
299299
case Messages::UpdateDateTime:
300300
// Added to remove warning

src/displayapp/Messages.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Pinetime {
2222
ShowPairingKey,
2323
AlarmTriggered,
2424
Clock,
25-
AirplaneModeToggle
25+
BleRadioEnableToggle
2626
};
2727
}
2828
}

src/displayapp/screens/BleIcon.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
#include "displayapp/screens/Symbols.h"
33
using namespace Pinetime::Applications::Screens;
44

5-
const char* BleIcon::GetIcon(Pinetime::Controllers::Ble::ConnectStates state) {
6-
if (state == Pinetime::Controllers::Ble::ConnectStates::Connected)
7-
return Symbols::bluetooth;
8-
else if (state == Pinetime::Controllers::Ble::ConnectStates::Airplane)
5+
const char* BleIcon::GetIcon(bool isRadioEnabled, bool isConnected) {
6+
if(!isRadioEnabled) {
97
return Symbols::airplane;
10-
else
11-
return Symbols::none;
8+
}
9+
10+
if (isConnected) {
11+
return Symbols::bluetooth;
12+
}
13+
14+
return Symbols::none;
1215
}

src/displayapp/screens/BleIcon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Pinetime {
77
namespace Screens {
88
class BleIcon {
99
public:
10-
static const char* GetIcon(Pinetime::Controllers::Ble::ConnectStates state);
10+
static const char* GetIcon(bool isRadioEnabled, bool isConnected);
1111
};
1212
}
1313
}

src/displayapp/screens/PineTimeStyle.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ namespace {
4242
auto* screen = static_cast<PineTimeStyle*>(obj->user_data);
4343
screen->UpdateSelected(obj, event);
4444
}
45+
46+
bool IsBleIconVisible(bool isRadioEnabled, bool isConnected) {
47+
if(!isRadioEnabled) {
48+
return true;
49+
}
50+
return isConnected;
51+
}
4552
}
4653

4754
PineTimeStyle::PineTimeStyle(DisplayApp* app,
@@ -336,11 +343,13 @@ void PineTimeStyle::SetBatteryIcon() {
336343
lv_label_set_text_static(batteryIcon, BatteryIcon::GetBatteryIcon(batteryPercent));
337344
}
338345

346+
339347
void PineTimeStyle::AlignIcons() {
340-
if (notificationState.Get() && bleState.Get() != Pinetime::Controllers::Ble::ConnectStates::Disconnected) {
348+
bool isBleIconVisible = IsBleIconVisible(bleRadioEnabled.Get(), bleState.Get());
349+
if (notificationState.Get() && isBleIconVisible) {
341350
lv_obj_align(bleIcon, sidebar, LV_ALIGN_IN_TOP_MID, 8, 25);
342351
lv_obj_align(notificationIcon, sidebar, LV_ALIGN_IN_TOP_MID, -8, 25);
343-
} else if (notificationState.Get() && bleState.Get() == Pinetime::Controllers::Ble::ConnectStates::Disconnected) {
352+
} else if (notificationState.Get() && !isBleIconVisible) {
344353
lv_obj_align(notificationIcon, sidebar, LV_ALIGN_IN_TOP_MID, 0, 25);
345354
} else {
346355
lv_obj_align(bleIcon, sidebar, LV_ALIGN_IN_TOP_MID, 0, 25);
@@ -363,9 +372,10 @@ void PineTimeStyle::Refresh() {
363372
}
364373
}
365374

366-
bleState = bleController.GetConnectState();
367-
if (bleState.IsUpdated()) {
368-
lv_label_set_text_static(bleIcon, BleIcon::GetIcon(bleState.Get()));
375+
bleState = bleController.IsConnected();
376+
bleRadioEnabled = bleController.IsRadioEnabled();
377+
if (bleState.IsUpdated() || bleRadioEnabled.IsUpdated()) {
378+
lv_label_set_text(bleIcon, BleIcon::GetIcon(bleRadioEnabled.Get(), bleState.Get()));
369379
AlignIcons();
370380
}
371381

0 commit comments

Comments
 (0)