diff options
author | JF <jf@codingfield.com> | 2020-06-07 14:17:45 +0200 |
---|---|---|
committer | Gitea <gitea@fake.local> | 2020-06-07 14:17:45 +0200 |
commit | a0e73f5c1a1e652aa6270b7e42a73aee3d12ded6 (patch) | |
tree | 6205dfb543bb22245d39a2f6e44d2c26cb381c10 /src/SystemTask | |
parent | 8a94750e30399bfb204cbec59a769d9d1b6b5baa (diff) | |
parent | dbdb26ae1fa45cec88f1b9ea0353b3d0a3c39f56 (diff) |
Merge branch 'develop' of JF/PineTime into master
Diffstat (limited to 'src/SystemTask')
-rw-r--r-- | src/SystemTask/SystemTask.cpp | 100 | ||||
-rw-r--r-- | src/SystemTask/SystemTask.h | 18 |
2 files changed, 101 insertions, 17 deletions
diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index fc37ecb2..b0f58d17 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -10,19 +10,27 @@ #include <nimble/hci_common.h> #include <host/ble_gap.h> #include <host/util/util.h> +#include <drivers/InternalFlash.h> #include "../main.h" using namespace Pinetime::System; -SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, +void IdleTimerCallback(TimerHandle_t xTimer) { + auto sysTask = static_cast<SystemTask *>(pvTimerGetTimerID(xTimer)); + sysTask->OnIdle(); +} + + +SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager) : - spi{spi}, lcd{lcd}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, + spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, - nimbleController(*this, bleController,dateTimeController, notificationManager) { + nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) { systemTaksMsgQueue = xQueueCreate(10, 1); } @@ -43,13 +51,20 @@ void SystemTask::Work() { NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); -/* BLE */ + spi.Init(); + spiNorFlash.Init(); + + // Write the 'image OK' flag if it's not already done + // TODO implement a better verification mecanism for the image (ask for user confirmation via UI/BLE ?) + uint32_t* imageOkPtr = reinterpret_cast<uint32_t *>(0x7BFE8); + uint32_t imageOk = *imageOkPtr; + if(imageOk != 1) + Pinetime::Drivers::InternalFlash::WriteWord(0x7BFE8, 1); + nimbleController.Init(); nimbleController.StartAdvertising(); -/* /BLE*/ - - spi.Init(); lcd.Init(); + touchPanel.Init(); batteryController.Init(); @@ -83,26 +98,70 @@ void SystemTask::Work() { nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler); + idleTimer = xTimerCreate ("idleTimer", idleTime, pdFALSE, this, IdleTimerCallback); + xTimerStart(idleTimer, 0); while(true) { uint8_t msg; if (xQueueReceive(systemTaksMsgQueue, &msg, isSleeping?2500 : 1000)) { Messages message = static_cast<Messages >(msg); switch(message) { - case Messages::GoToRunning: isSleeping = false; break; + case Messages::GoToRunning: + isSleeping = false; + xTimerStart(idleTimer, 0); + nimbleController.StartAdvertising(); + break; case Messages::GoToSleep: NRF_LOG_INFO("[SystemTask] Going to sleep"); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep); - isSleeping = true; break; + isSleeping = true; + break; case Messages::OnNewTime: + xTimerReset(idleTimer, 0); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateDateTime); break; case Messages::OnNewNotification: + xTimerReset(idleTimer, 0); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::NewNotification); break; + case Messages::BleConnected: + xTimerReset(idleTimer, 0); + isBleDiscoveryTimerRunning = true; + bleDiscoveryTimer = 5; + break; + case Messages::BleFirmwareUpdateStarted: + doNotGoToSleep = true; + if(isSleeping) GoToRunning(); + displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateStarted); + break; + case Messages::BleFirmwareUpdateFinished: + doNotGoToSleep = false; + xTimerStart(idleTimer, 0); + displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateFinished); + if(bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated) + NVIC_SystemReset(); + break; + case Messages::OnTouchEvent: + xTimerReset(idleTimer, 0); + break; + case Messages::OnButtonEvent: + xTimerReset(idleTimer, 0); + break; default: break; } } + + if(isBleDiscoveryTimerRunning) { + if(bleDiscoveryTimer == 0) { + isBleDiscoveryTimerRunning = false; + // Services discovery is deffered from 3 seconds to avoid the conflicts between the host communicating with the + // tharget and vice-versa. I'm not sure if this is the right way to handle this... + nimbleController.StartDiscovery(); + } else { + bleDiscoveryTimer--; + } + } + uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); dateTimeController.UpdateTime(systick_counter); batteryController.Update(); @@ -113,22 +172,29 @@ void SystemTask::Work() { } void SystemTask::OnButtonPushed() { - if(!isSleeping) { NRF_LOG_INFO("[SystemTask] Button pushed"); + PushMessage(Messages::OnButtonEvent); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::ButtonPushed); } else { NRF_LOG_INFO("[SystemTask] Button pushed, waking up"); - displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToRunning); - isSleeping = false; - displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel); + GoToRunning(); } } +void SystemTask::GoToRunning() { + PushMessage(Messages::GoToRunning); + displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning); + displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel); +} + void SystemTask::OnTouchEvent() { NRF_LOG_INFO("[SystemTask] Touch event"); - displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent); + if(!isSleeping) { + PushMessage(Messages::OnTouchEvent); + displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent); + } } void SystemTask::PushMessage(SystemTask::Messages msg) { @@ -140,3 +206,9 @@ void SystemTask::PushMessage(SystemTask::Messages msg) { // TODO : should I do something here? } } + +void SystemTask::OnIdle() { + if(doNotGoToSleep) return; + NRF_LOG_INFO("Idle timeout -> Going to sleep") + PushMessage(Messages::GoToSleep); +} diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index 5eba391b..b6ecf7c9 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -9,15 +9,18 @@ #include <DisplayApp/DisplayApp.h> #include <drivers/Watchdog.h> #include <Components/Ble/NimbleController.h> +#include <drivers/SpiNorFlash.h> namespace Pinetime { namespace System { class SystemTask { public: - enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification + enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected, + BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent }; - SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, + SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, @@ -29,11 +32,15 @@ namespace Pinetime { void OnButtonPushed(); void OnTouchEvent(); + + void OnIdle(); + private: TaskHandle_t taskHandle; Pinetime::Drivers::SpiMaster& spi; Pinetime::Drivers::St7789& lcd; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; @@ -58,8 +65,13 @@ namespace Pinetime { static void Process(void* instance); void Work(); + bool isBleDiscoveryTimerRunning = false; + uint8_t bleDiscoveryTimer = 0; + static constexpr uint32_t idleTime = 5000; + TimerHandle_t idleTimer; + bool doNotGoToSleep = false; - + void GoToRunning(); }; } }
\ No newline at end of file |