summaryrefslogtreecommitdiff
path: root/src/SystemTask
diff options
context:
space:
mode:
authorJF <jf@codingfield.com>2020-02-23 12:44:39 (GMT)
committerJF <jf@codingfield.com>2020-02-23 12:44:39 (GMT)
commit02772b996fb26146cf38fc6deccff7f43a49dfd6 (patch)
treef2ff90c577f68ccb44b6470621bf87e68a49aed8 /src/SystemTask
parent2bdff7ed2b490cb8ce5599341e12d707c0ba7fd0 (diff)
Do not compile GFX and older fonts anymore.
Refactor SystemTask in its own class. Refactor Screen to be able to close current screen and open a new one. Re-enable sleep/wake up and propagate button event to Screens.
Diffstat (limited to 'src/SystemTask')
-rw-r--r--src/SystemTask/SystemTask.cpp115
-rw-r--r--src/SystemTask/SystemTask.h60
2 files changed, 175 insertions, 0 deletions
diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp
new file mode 100644
index 0000000..642e36a
--- /dev/null
+++ b/src/SystemTask/SystemTask.cpp
@@ -0,0 +1,115 @@
+#include <libraries/log/nrf_log.h>
+#include <libraries/gpiote/app_gpiote.h>
+#include <drivers/Cst816s.h>
+#include <DisplayApp/LittleVgl.h>
+#include <hal/nrf_rtc.h>
+#include "SystemTask.h"
+#include "../main.h"
+using namespace Pinetime::System;
+
+SystemTask::SystemTask(Pinetime::Drivers::SpiMaster &spi, Pinetime::Drivers::St7789 &lcd,
+ Pinetime::Drivers::Cst816S &touchPanel, Pinetime::Components::LittleVgl &lvgl,
+ Pinetime::Controllers::Battery &batteryController, Pinetime::Controllers::Ble &bleController,
+ Pinetime::Controllers::DateTime& dateTimeController) :
+ spi{spi}, lcd{lcd}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController} {
+ systemTaksMsgQueue = xQueueCreate(10, 1);
+}
+
+void SystemTask::Start() {
+ if (pdPASS != xTaskCreate(SystemTask::Process, "MAIN", 256, this, 0, &taskHandle))
+ APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
+}
+
+void SystemTask::Process(void *instance) {
+ auto *app = static_cast<SystemTask *>(instance);
+ NRF_LOG_INFO("SystemTask task started!");
+ app->Work();
+}
+
+void SystemTask::Work() {
+ APP_GPIOTE_INIT(2);
+ bool erase_bonds=false;
+// nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds);
+
+ spi.Init();
+ lcd.Init();
+ touchPanel.Init();
+ batteryController.Init();
+
+ displayApp.reset(new Pinetime::Applications::DisplayApp(lcd, lvgl, touchPanel, batteryController, bleController, dateTimeController, *this));
+ displayApp->Start();
+
+ batteryController.Update();
+ displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
+
+ nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_High);
+ nrf_gpio_cfg_output(15);
+ nrf_gpio_pin_set(15);
+
+ nrfx_gpiote_in_config_t pinConfig;
+ pinConfig.skip_gpio_setup = true;
+ pinConfig.hi_accuracy = false;
+ pinConfig.is_watcher = false;
+ pinConfig.sense = (nrf_gpiote_polarity_t)NRF_GPIOTE_POLARITY_HITOLO;
+ pinConfig.pull = (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown;
+
+ nrfx_gpiote_in_init(pinButton, &pinConfig, nrfx_gpiote_evt_handler);
+
+ nrf_gpio_cfg_sense_input(pinTouchIrq, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_Low);
+
+ pinConfig.skip_gpio_setup = true;
+ pinConfig.hi_accuracy = false;
+ pinConfig.is_watcher = false;
+ pinConfig.sense = (nrf_gpiote_polarity_t)NRF_GPIOTE_POLARITY_HITOLO;
+ pinConfig.pull = (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup;
+
+ nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
+
+
+ while(true) {
+ uint8_t msg;
+ if (xQueueReceive(systemTaksMsgQueue, &msg, isSleeping?3600000 : 1000)) {
+ Messages message = static_cast<Messages >(msg);
+ switch(message) {
+ case Messages::GoToRunning: isSleeping = false; break;
+ case Messages::GoToSleep:
+ NRF_LOG_INFO("[SystemTask] Going to sleep");
+ displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep);
+ isSleeping = true; break;
+ default: break;
+ }
+ }
+ uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
+ dateTimeController.UpdateTime(systick_counter);
+ }
+}
+
+void SystemTask::OnButtonPushed() {
+
+ if(!isSleeping) {
+ NRF_LOG_INFO("[SystemTask] Button pushed");
+ 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;
+ batteryController.Update();
+ displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
+ }
+}
+
+void SystemTask::OnTouchEvent() {
+ NRF_LOG_INFO("[SystemTask] Touch event");
+ displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent);
+}
+
+void SystemTask::PushMessage(SystemTask::Messages msg) {
+ BaseType_t xHigherPriorityTaskWoken;
+ xHigherPriorityTaskWoken = pdFALSE;
+ xQueueSendFromISR(systemTaksMsgQueue, &msg, &xHigherPriorityTaskWoken);
+ if (xHigherPriorityTaskWoken) {
+ /* Actual macro used here is port specific. */
+ // TODO : should I do something here?
+ }
+}
diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h
new file mode 100644
index 0000000..cb91354
--- /dev/null
+++ b/src/SystemTask/SystemTask.h
@@ -0,0 +1,60 @@
+#pragma once
+
+#include <FreeRTOS.h>
+#include <task.h>
+#include <memory>
+#include <drivers/SpiMaster.h>
+#include <drivers/St7789.h>
+#include <Components/Battery/BatteryController.h>
+#include <DisplayApp/DisplayApp.h>
+
+namespace Pinetime {
+ namespace System {
+ class SystemTask {
+ public:
+ enum class Messages {GoToSleep, GoToRunning};
+
+ SystemTask(Pinetime::Drivers::SpiMaster& spi,
+ Pinetime::Drivers::St7789& lcd,
+ Pinetime::Drivers::Cst816S& touchPanel,
+ Pinetime::Components::LittleVgl& lvgl,
+ Pinetime::Controllers::Battery& batteryController,
+ Pinetime::Controllers::Ble& bleController,
+ Pinetime::Controllers::DateTime& dateTimeController);
+
+
+ void Start();
+ void PushMessage(Messages msg);
+
+ void OnButtonPushed();
+ void OnTouchEvent();
+ private:
+ TaskHandle_t taskHandle;
+
+ Pinetime::Drivers::SpiMaster& spi;
+ Pinetime::Drivers::St7789& lcd;
+ Pinetime::Drivers::Cst816S& touchPanel;
+ Pinetime::Components::LittleVgl& lvgl;
+ Pinetime::Controllers::Battery& batteryController;
+ std::unique_ptr<Pinetime::Applications::DisplayApp> displayApp;
+ Pinetime::Controllers::Ble& bleController;
+ Pinetime::Controllers::DateTime& dateTimeController;
+ QueueHandle_t systemTaksMsgQueue;
+ bool isSleeping = false;
+
+
+ static constexpr uint8_t pinSpiSck = 2;
+ static constexpr uint8_t pinSpiMosi = 3;
+ static constexpr uint8_t pinSpiMiso = 4;
+ static constexpr uint8_t pinSpiCsn = 25;
+ static constexpr uint8_t pinLcdDataCommand = 18;
+ static constexpr uint8_t pinButton = 13;
+ static constexpr uint8_t pinTouchIrq = 28;
+
+ static void Process(void* instance);
+ void Work();
+
+
+ };
+ }
+} \ No newline at end of file