diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 809544c92f..ae8eece843 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 3bf1ec80c6..49e44e08e7 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -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}, @@ -51,7 +53,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, heartRateService {systemTask, heartRateController}, motionService {systemTask, motionController}, fsService {systemTask, fs}, - serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { + serviceDiscovery({¤tTimeClient, &alertNotificationClient}), + settingsController {settingsController} { } void nimble_on_reset(int reason) { @@ -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)); @@ -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}; diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 7a3870373e..43400ba6cd 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -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" @@ -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 { @@ -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); @@ -62,6 +65,7 @@ namespace Pinetime { const ble_gatt_error* error, uint16_t characteristicValueHandle, const ble_gatt_dsc* descriptor); + */ void StartDiscovery(); @@ -83,7 +87,9 @@ namespace Pinetime { void RestartFastAdv() { fastAdvCount = 0; - } + }; + + void SwitchAirplaneMode(); private: void PersistBond(struct ble_gap_conn_desc& desc); @@ -91,12 +97,12 @@ namespace Pinetime { 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; @@ -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; diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 2d7973d8f0..a56baf3380 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -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; @@ -178,6 +192,8 @@ namespace Pinetime { std::bitset<3> wakeUpMode {0}; Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; + + bool airplaneMode = false; }; SettingsData settings; diff --git a/src/displayapp/Apps.h b/src/displayapp/Apps.h index b2f55ccdc8..971c36b76e 100644 --- a/src/displayapp/Apps.h +++ b/src/displayapp/Apps.h @@ -37,7 +37,8 @@ namespace Pinetime { SettingPineTimeStyle, SettingSetDate, SettingSetTime, - Error, + SettingAirplaneMode, + Error }; } } diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 233f433dd2..d3896b18ea 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -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" @@ -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? @@ -420,6 +424,10 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) currentScreen = std::make_unique(this, settingsController); ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown); break; + case Apps::SettingAirplaneMode: + currentScreen = std::make_unique(this, settingsController); + ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown); + break; case Apps::BatteryInfo: currentScreen = std::make_unique(this, batteryController); ReturnApp(Apps::Settings, FullRefreshDirections::Down, TouchEvents::SwipeDown); diff --git a/src/displayapp/Messages.h b/src/displayapp/Messages.h index b22d6c3cf9..4d52df2055 100644 --- a/src/displayapp/Messages.h +++ b/src/displayapp/Messages.h @@ -20,7 +20,8 @@ namespace Pinetime { DimScreen, RestoreBrightness, ShowPairingKey, - AlarmTriggered + AlarmTriggered, + AirplaneModeToggle }; } } diff --git a/src/displayapp/screens/settings/SettingAirplaneMode.cpp b/src/displayapp/screens/settings/SettingAirplaneMode.cpp new file mode 100644 index 0000000000..b8378e0085 --- /dev/null +++ b/src/displayapp/screens/settings/SettingAirplaneMode.cpp @@ -0,0 +1,92 @@ +#include "displayapp/screens/settings/SettingAirplaneMode.h" +#include +#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(obj->user_data); + screen->UpdateSelected(obj, event); + } +} + +constexpr std::array 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); + } + } + } +} diff --git a/src/displayapp/screens/settings/SettingAirplaneMode.h b/src/displayapp/screens/settings/SettingAirplaneMode.h new file mode 100644 index 0000000000..fcc0222865 --- /dev/null +++ b/src/displayapp/screens/settings/SettingAirplaneMode.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include + +#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 options = {" No", " Yes"}; + Controllers::Settings& settingsController; + lv_obj_t* cbOption[options.size()]; + bool priorMode; + }; + } + } +} diff --git a/src/displayapp/screens/settings/Settings.cpp b/src/displayapp/screens/settings/Settings.cpp index 392c12e052..16cca2a3a7 100644 --- a/src/displayapp/screens/settings/Settings.cpp +++ b/src/displayapp/screens/settings/Settings.cpp @@ -36,10 +36,10 @@ bool Settings::OnTouchEvent(Pinetime::Applications::TouchEvents event) { std::unique_ptr Settings::CreateScreen1() { std::array applications {{ - {Symbols::sun, "Display", Apps::SettingDisplay}, - {Symbols::eye, "Wake Up", Apps::SettingWakeUp}, - {Symbols::clock, "Time format", Apps::SettingTimeFormat}, - {Symbols::home, "Watch face", Apps::SettingWatchFace}, + {Symbols::list, "About", Apps::SysInfo}, + {Symbols::phoneSlash, "Airplane mode", Apps::SettingAirplaneMode}, + {Symbols::batteryHalf, "Battery", Apps::BatteryInfo}, + {Symbols::sun, "Display", Apps::SettingDisplay} }}; return std::make_unique(0, 3, app, settingsController, applications); @@ -48,10 +48,10 @@ std::unique_ptr Settings::CreateScreen1() { std::unique_ptr Settings::CreateScreen2() { std::array applications {{ - {Symbols::shoe, "Steps", Apps::SettingSteps}, + {Symbols::check, "Firmware", Apps::FirmwareValidation}, + {Symbols::paintbrush, "PTS colors", Apps::SettingPineTimeStyle}, {Symbols::clock, "Set date", Apps::SettingSetDate}, - {Symbols::clock, "Set time", Apps::SettingSetTime}, - {Symbols::batteryHalf, "Battery", Apps::BatteryInfo} + {Symbols::clock, "Set time", Apps::SettingSetTime} }}; return std::make_unique(1, 3, app, settingsController, applications); @@ -60,10 +60,10 @@ std::unique_ptr Settings::CreateScreen2() { std::unique_ptr Settings::CreateScreen3() { std::array applications {{ - {Symbols::paintbrush, "PTS Colors", Apps::SettingPineTimeStyle}, - {Symbols::check, "Firmware", Apps::FirmwareValidation}, - {Symbols::list, "About", Apps::SysInfo}, - {Symbols::none, "None", Apps::None}, + {Symbols::shoe, "Steps", Apps::SettingSteps}, + {Symbols::clock, "Time format", Apps::SettingTimeFormat}, + {Symbols::eye, "Wake Up", Apps::SettingWakeUp}, + {Symbols::home, "Watch face", Apps::SettingWatchFace}, }}; return std::make_unique(2, 3, app, settingsController, applications); diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h index cc30fdc682..886505b536 100644 --- a/src/systemtask/Messages.h +++ b/src/systemtask/Messages.h @@ -29,6 +29,7 @@ namespace Pinetime { BatteryPercentageUpdated, StartFileTransfer, StopFileTransfer, + AirplaneModeToggle }; } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index a95d479dec..281be29e2d 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -110,7 +110,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, spiNorFlash, heartRateController, motionController, - fs) { + fs, + settingsController) { } void SystemTask::Start() { @@ -424,7 +425,9 @@ void SystemTask::Work() { motorController.RunForDuration(35); displayApp.PushMessage(Pinetime::Applications::Display::Messages::ShowPairingKey); break; - + case Messages::AirplaneModeToggle: + nimbleController.SwitchAirplaneMode(); + break; default: break; }