Skip to content

Commit

Permalink
Add airplane mode feature
Browse files Browse the repository at this point in the history
Implement 'airplane mode' to disable/enable bluetooth
Add airplane mode to settings and increment settingsVersion
Add a setting menu for switching airplane mode on and off
Alphabetize the settings menu options
Style cleanups

Closes InfiniTimeOrg#632
  • Loading branch information
evergreen22 committed Dec 14, 2021
1 parent 6a5946c commit ddee4cb
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/settings/SettingPineTimeStyle.cpp
displayapp/screens/settings/SettingSetDate.cpp
displayapp/screens/settings/SettingSetTime.cpp
displayapp/screens/settings/SettingAirplaneMode.cpp

## Watch faces
displayapp/icons/bg_clock.c
Expand Down
34 changes: 27 additions & 7 deletions src/components/ble/NimbleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@
#include "components/ble/NotificationManager.h"
#include "components/datetime/DateTimeController.h"
#include "components/fs/FS.h"
#include "components/settings/Settings.h"
#include "systemtask/SystemTask.h"

using namespace Pinetime::Controllers;

NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
Pinetime::Controllers::Ble& bleController,
Ble& bleController,
DateTime& dateTimeController,
Pinetime::Controllers::NotificationManager& notificationManager,
Controllers::Battery& batteryController,
NotificationManager& notificationManager,
Battery& batteryController,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Controllers::HeartRateController& heartRateController,
Controllers::MotionController& motionController,
Controllers::FS& fs)
HeartRateController& heartRateController,
MotionController& motionController,
FS& fs,
Settings& settingsController)
: systemTask {systemTask},
bleController {bleController},
dateTimeController {dateTimeController},
Expand All @@ -51,7 +53,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
heartRateService {systemTask, heartRateController},
motionService {systemTask, motionController},
fsService {systemTask, fs},
serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
serviceDiscovery({&currentTimeClient, &alertNotificationClient}),
settingsController {settingsController} {
}

void nimble_on_reset(int reason) {
Expand Down Expand Up @@ -144,6 +147,10 @@ void NimbleController::StartAdvertising() {
struct ble_hs_adv_fields fields;
struct ble_hs_adv_fields rsp_fields;

if (settingsController.GetAirplaneMode()) {
return;
}

memset(&adv_params, 0, sizeof(adv_params));
memset(&fields, 0, sizeof(fields));
memset(&rsp_fields, 0, sizeof(rsp_fields));
Expand Down Expand Up @@ -378,6 +385,19 @@ void NimbleController::NotifyBatteryLevel(uint8_t level) {
}
}

void NimbleController::SwitchAirplaneMode() {
if (settingsController.GetAirplaneMode()) {
if (bleController.IsConnected()) {
ble_gap_terminate(connectionHandle, BLE_ERR_REM_USER_CONN_TERM);
} else {
ble_gap_adv_stop();
}
} else {
fastAdvCount = 0;
StartAdvertising();
}
}

void NimbleController::PersistBond(struct ble_gap_conn_desc& desc) {
union ble_store_key key;
union ble_store_value our_sec, peer_sec, peer_cccd_set[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)] = {0};
Expand Down
31 changes: 19 additions & 12 deletions src/components/ble/NimbleController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "components/ble/CurrentTimeService.h"
#include "components/ble/DeviceInformationService.h"
#include "components/ble/DfuService.h"
#include "components/ble/FSService.h"
#include "components/ble/HeartRateService.h"
#include "components/ble/ImmediateAlertService.h"
#include "components/ble/MusicService.h"
Expand All @@ -22,7 +23,7 @@
#include "components/ble/MotionService.h"
#include "components/ble/weather/WeatherService.h"
#include "components/fs/FS.h"
#include "components/ble/FSService.h"
#include "components/settings/Settings.h"

namespace Pinetime {
namespace Drivers {
Expand All @@ -42,18 +43,20 @@ namespace Pinetime {

public:
NimbleController(Pinetime::System::SystemTask& systemTask,
Pinetime::Controllers::Ble& bleController,
Ble& bleController,
DateTime& dateTimeController,
Pinetime::Controllers::NotificationManager& notificationManager,
Controllers::Battery& batteryController,
NotificationManager& notificationManager,
Battery& batteryController,
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Controllers::HeartRateController& heartRateController,
Controllers::MotionController& motionController,
Pinetime::Controllers::FS& fs);
HeartRateController& heartRateController,
MotionController& motionController,
FS& fs,
Settings& settingsController);
void Init();
void StartAdvertising();
int OnGAPEvent(ble_gap_event* event);

/* these are not implemented yet
int OnDiscoveryEvent(uint16_t i, const ble_gatt_error* pError, const ble_gatt_svc* pSvc);
int OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
int OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
Expand All @@ -62,6 +65,7 @@ namespace Pinetime {
const ble_gatt_error* error,
uint16_t characteristicValueHandle,
const ble_gatt_dsc* descriptor);
*/

void StartDiscovery();

Expand All @@ -83,20 +87,22 @@ namespace Pinetime {

void RestartFastAdv() {
fastAdvCount = 0;
}
};

void SwitchAirplaneMode();

private:
void PersistBond(struct ble_gap_conn_desc& desc);
void RestoreBond();

static constexpr const char* deviceName = "InfiniTime";
Pinetime::System::SystemTask& systemTask;
Pinetime::Controllers::Ble& bleController;
Ble& bleController;
DateTime& dateTimeController;
Pinetime::Controllers::NotificationManager& notificationManager;
NotificationManager& notificationManager;
Pinetime::Drivers::SpiNorFlash& spiNorFlash;
Pinetime::Controllers::FS& fs;
Pinetime::Controllers::DfuService dfuService;
FS& fs;
DfuService dfuService;

DeviceInformationService deviceInformationService;
CurrentTimeClient currentTimeClient;
Expand All @@ -112,6 +118,7 @@ namespace Pinetime {
MotionService motionService;
ServiceDiscovery serviceDiscovery;
FSService fsService;
Settings& settingsController;

uint8_t addrType;
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;
Expand Down
24 changes: 20 additions & 4 deletions src/components/settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,37 @@ namespace Pinetime {
}
settings.brightLevel = level;
};

Controllers::BrightnessController::Levels GetBrightness() const {
return settings.brightLevel;
};

void SetStepsGoal( uint32_t goal ) {
if ( goal != settings.stepsGoal ) {
void SetStepsGoal(uint32_t goal) {
if (goal != settings.stepsGoal) {
settingsChanged = true;
}
settings.stepsGoal = goal;
};

uint32_t GetStepsGoal() const { return settings.stepsGoal; };
uint32_t GetStepsGoal() const {
return settings.stepsGoal;
};

void SetAirplaneMode(bool mode) {
if (mode != settings.airplaneMode) {
settings.airplaneMode = mode;
settingsChanged = true;
}
};

bool GetAirplaneMode() const {
return settings.airplaneMode;
};

private:
Pinetime::Controllers::FS& fs;

static constexpr uint32_t settingsVersion = 0x0002;
static constexpr uint32_t settingsVersion = 0x0003;
struct SettingsData {
uint32_t version = settingsVersion;
uint32_t stepsGoal = 10000;
Expand All @@ -178,6 +192,8 @@ namespace Pinetime {
std::bitset<3> wakeUpMode {0};

Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;

bool airplaneMode = false;
};

SettingsData settings;
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace Pinetime {
SettingPineTimeStyle,
SettingSetDate,
SettingSetTime,
Error,
SettingAirplaneMode,
Error
};
}
}
8 changes: 8 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "displayapp/screens/settings/SettingPineTimeStyle.h"
#include "displayapp/screens/settings/SettingSetDate.h"
#include "displayapp/screens/settings/SettingSetTime.h"
#include "displayapp/screens/settings/SettingAirplaneMode.h"

#include "libs/lv_conf.h"

Expand Down Expand Up @@ -289,6 +290,9 @@ void DisplayApp::Refresh() {
case Messages::BleFirmwareUpdateStarted:
LoadApp(Apps::FirmwareUpdate, DisplayApp::FullRefreshDirections::Down);
break;
case Messages::AirplaneModeToggle:
PushMessageToSystemTask(System::Messages::AirplaneModeToggle);
break;
case Messages::UpdateDateTime:
// Added to remove warning
// What should happen here?
Expand Down Expand Up @@ -420,6 +424,10 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
currentScreen = std::make_unique<Screens::SettingPineTimeStyle>(this, settingsController);
ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
break;
case Apps::SettingAirplaneMode:
currentScreen = std::make_unique<Screens::SettingAirplaneMode>(this, settingsController);
ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
break;
case Apps::BatteryInfo:
currentScreen = std::make_unique<Screens::BatteryInfo>(this, batteryController);
ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown);
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/Messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace Pinetime {
DimScreen,
RestoreBrightness,
ShowPairingKey,
AlarmTriggered
AlarmTriggered,
AirplaneModeToggle
};
}
}
Expand Down
92 changes: 92 additions & 0 deletions src/displayapp/screens/settings/SettingAirplaneMode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "displayapp/screens/settings/SettingAirplaneMode.h"
#include <lvgl/lvgl.h>
#include "displayapp/DisplayApp.h"
#include "displayapp/Messages.h"
#include "displayapp/screens/Styles.h"
#include "displayapp/screens/Screen.h"
#include "displayapp/screens/Symbols.h"

using namespace Pinetime::Applications::Screens;

namespace {
static void event_handler(lv_obj_t* obj, lv_event_t event) {
SettingAirplaneMode* screen = static_cast<SettingAirplaneMode*>(obj->user_data);
screen->UpdateSelected(obj, event);
}
}

constexpr std::array<const char*, 2> SettingAirplaneMode::options;

SettingAirplaneMode::SettingAirplaneMode(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController)
: Screen(app), settingsController {settingsController} {

lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);

lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);

lv_obj_set_pos(container1, 10, 60);
lv_obj_set_width(container1, LV_HOR_RES - 20);
lv_obj_set_height(container1, LV_VER_RES - 50);
lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT);

lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(title, "Airplane mode");
lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 15, 15);

lv_obj_t* icon = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_color(icon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_ORANGE);
lv_label_set_text_static(icon, Symbols::phoneSlash);
lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER);
lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0);

for (unsigned int i = 0; i < options.size(); i++) {
cbOption[i] = lv_checkbox_create(container1, nullptr);
lv_checkbox_set_text(cbOption[i], options[i]);
cbOption[i]->user_data = this;
lv_obj_set_event_cb(cbOption[i], event_handler);
SetRadioButtonStyle(cbOption[i]);
}

if (settingsController.GetAirplaneMode() == false) {
lv_checkbox_set_checked(cbOption[0], true);
priorMode = false;
} else {
lv_checkbox_set_checked(cbOption[1], true);
priorMode = true;
}
}

SettingAirplaneMode::~SettingAirplaneMode() {
lv_obj_clean(lv_scr_act());
/* No need to enclose the call to saveSettings inside a test for changed settings
* since the settings controller already does this. We do it here because we can.
*/
if (priorMode != settingsController.GetAirplaneMode()) {
app->PushMessage(Pinetime::Applications::Display::Messages::AirplaneModeToggle);
settingsController.SaveSettings();
}
}

void SettingAirplaneMode::UpdateSelected(lv_obj_t* object, lv_event_t event) {
if (event == LV_EVENT_VALUE_CHANGED) {
for (unsigned int i = 0; i < options.size(); i++) {
if (object == cbOption[i]) {
lv_checkbox_set_checked(cbOption[i], true);

if (i == 0) {
settingsController.SetAirplaneMode(false);
};
if (i == 1) {
settingsController.SetAirplaneMode(true);
};

} else {
lv_checkbox_set_checked(cbOption[i], false);
}
}
}
}
30 changes: 30 additions & 0 deletions src/displayapp/screens/settings/SettingAirplaneMode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <array>
#include <cstdint>
#include <lvgl/lvgl.h>

#include "components/settings/Settings.h"
#include "displayapp/screens/Screen.h"

namespace Pinetime {

namespace Applications {
namespace Screens {

class SettingAirplaneMode : public Screen {
public:
SettingAirplaneMode(DisplayApp* app, Pinetime::Controllers::Settings& settingsController);
~SettingAirplaneMode() override;

void UpdateSelected(lv_obj_t* object, lv_event_t event);

private:
static constexpr std::array<const char*, 2> options = {" No", " Yes"};
Controllers::Settings& settingsController;
lv_obj_t* cbOption[options.size()];
bool priorMode;
};
}
}
}
Loading

0 comments on commit ddee4cb

Please sign in to comment.