InfiniTime/src/displayapp/DisplayApp.cpp

291 lines
11 KiB
C++
Raw Normal View History

#include "DisplayApp.h"
#include <libraries/log/nrf_log.h>
#include <displayapp/screens/HeartRate.h>
2020-11-15 15:49:36 +00:00
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "components/datetime/DateTimeController.h"
#include "components/ble/NotificationManager.h"
#include "displayapp/screens/ApplicationList.h"
2020-11-15 15:49:36 +00:00
#include "displayapp/screens/Brightness.h"
#include "displayapp/screens/Clock.h"
#include "displayapp/screens/FirmwareUpdate.h"
#include "displayapp/screens/FirmwareValidation.h"
#include "displayapp/screens/InfiniPaint.h"
2020-12-03 14:47:22 +00:00
#include "displayapp/screens/Paddle.h"
#include "displayapp/screens/StopWatch.h"
2020-11-15 15:49:36 +00:00
#include "displayapp/screens/Meter.h"
#include "displayapp/screens/Music.h"
#include "displayapp/screens/Navigation.h"
2020-11-15 15:49:36 +00:00
#include "displayapp/screens/Notifications.h"
#include "displayapp/screens/SystemInfo.h"
#include "displayapp/screens/Tile.h"
#include "displayapp/screens/Twos.h"
2020-11-15 15:49:36 +00:00
#include "drivers/Cst816s.h"
#include "drivers/St7789.h"
#include "drivers/Watchdog.h"
#include "systemtask/SystemTask.h"
using namespace Pinetime::Applications;
using namespace Pinetime::Applications::Display;
DisplayApp::DisplayApp(Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Drivers::Cst816S &touchPanel,
Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Drivers::WatchdogView &watchdog,
System::SystemTask &systemTask,
Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::HeartRateController& heartRateController,
Controllers::Settings &settingsController) :
lcd{lcd},
2020-02-10 20:05:33 +00:00
lvgl{lvgl},
batteryController{batteryController},
bleController{bleController},
dateTimeController{dateTimeController},
watchdog{watchdog},
touchPanel{touchPanel},
currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager, settingsController, heartRateController) },
systemTask{systemTask},
notificationManager{notificationManager},
heartRateController{heartRateController},
settingsController{settingsController} {
msgQueue = xQueueCreate(queueSize, itemSize);
onClockApp = true;
}
void DisplayApp::Start() {
2020-10-02 18:46:41 +00:00
if (pdPASS != xTaskCreate(DisplayApp::Process, "displayapp", 512, this, 0, &taskHandle))
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
}
void DisplayApp::Process(void *instance) {
auto *app = static_cast<DisplayApp *>(instance);
2020-10-02 18:46:41 +00:00
NRF_LOG_INFO("displayapp task started!");
2019-12-07 16:11:50 +00:00
app->InitHw();
// Send a dummy notification to unlock the lvgl display driver for the first iteration
xTaskNotifyGive(xTaskGetCurrentTaskHandle());
while (1) {
2020-02-10 20:05:33 +00:00
app->Refresh();
}
}
2019-12-07 16:11:50 +00:00
void DisplayApp::InitHw() {
brightnessController.Init();
}
uint32_t acc = 0;
uint32_t count = 0;
bool toggle = true;
void DisplayApp::Refresh() {
TickType_t queueTimeout;
switch (state) {
case States::Idle:
IdleState();
queueTimeout = portMAX_DELAY;
break;
case States::Running:
RunningState();
queueTimeout = 20;
break;
default:
queueTimeout = portMAX_DELAY;
break;
}
Messages msg;
if (xQueueReceive(msgQueue, &msg, queueTimeout)) {
switch (msg) {
case Messages::GoToSleep:
brightnessController.Backup();
while(brightnessController.Level() != Controllers::BrightnessController::Levels::Off) {
brightnessController.Lower();
vTaskDelay(100);
}
lcd.DisplayOff();
systemTask.PushMessage(System::SystemTask::Messages::OnDisplayTaskSleeping);
state = States::Idle;
break;
case Messages::GoToRunning:
lcd.DisplayOn();
brightnessController.Restore();
state = States::Running;
break;
case Messages::UpdateBleConnection:
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : Screens::Clock::BleConnectionStates::NotConnected);
break;
case Messages::UpdateBatteryLevel:
// clockScreen.SetBatteryPercentRemaining(batteryController.PercentRemaining());
break;
case Messages::NewNotification: {
if(onClockApp) {
currentScreen.reset(nullptr);
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up);
onClockApp = false;
currentScreen = std::make_unique<Screens::Notifications>(this, notificationManager, systemTask.nimble().alertService(), Screens::Notifications::Modes::Preview);
}
}
break;
case Messages::TouchEvent: {
if (state != States::Running) break;
auto gesture = OnTouchEvent();
if(!currentScreen->OnTouchEvent(gesture)) {
switch (gesture) {
case TouchEvents::SwipeUp:
currentScreen->OnButtonPushed();
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up);
break;
case TouchEvents::SwipeDown:
currentScreen->OnButtonPushed();
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down);
break;
default:
break;
}
}
}
break;
case Messages::ButtonPushed:
if(onClockApp)
systemTask.PushMessage(System::SystemTask::Messages::GoToSleep);
else {
auto buttonUsedByApp = currentScreen->OnButtonPushed();
if (!buttonUsedByApp) {
systemTask.PushMessage(System::SystemTask::Messages::GoToSleep);
} else {
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up);
}
}
// lvgl.SetFullRefresh(components::LittleVgl::FullRefreshDirections::Down);
// currentScreen.reset(nullptr);
// if(toggle) {
// currentScreen = std::make_unique<Screens::Tile>(this);
// toggle = false;
// } else {
// currentScreen = std::make_unique<Screens::Clock>(this, dateTimeController, batteryController, bleController);
// toggle = true;
// }
break;
case Messages::BleFirmwareUpdateStarted:
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down);
currentScreen.reset(nullptr);
currentScreen = std::make_unique<Screens::FirmwareUpdate>(this, bleController);
onClockApp = false;
break;
case Messages::UpdateDateTime:
// Added to remove warning
// What should happen here?
break;
}
}
if(state != States::Idle && touchMode == TouchModes::Polling) {
auto info = touchPanel.GetTouchInfo();
2020-08-21 09:55:59 +00:00
if(info.action == 2) {// 2 = contact
if(!currentScreen->OnTouchEvent(info.x, info.y)) {
lvgl.SetNewTapEvent(info.x, info.y);
}
}
}
}
void DisplayApp::RunningState() {
// clockScreen.SetCurrentDateTime(dateTimeController.CurrentDateTime());
if(!currentScreen->Refresh()) {
currentScreen.reset(nullptr);
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up);
onClockApp = false;
switch(nextApp) {
case Apps::None:
case Apps::Launcher: currentScreen = std::make_unique<Screens::ApplicationList>(this, settingsController); break;
case Apps::Clock:
currentScreen = std::make_unique<Screens::Clock>(this, dateTimeController, batteryController, bleController, notificationManager, settingsController, heartRateController);
onClockApp = true;
break;
case Apps::SysInfo: currentScreen = std::make_unique<Screens::SystemInfo>(this, dateTimeController, batteryController, brightnessController, bleController, watchdog); break;
case Apps::Meter: currentScreen = std::make_unique<Screens::Meter>(this);break;
case Apps::StopWatch: currentScreen = std::make_unique<Screens::StopWatch>(this); break;
case Apps::Twos: currentScreen = std::make_unique<Screens::Twos>(this); break;
case Apps::Paint: currentScreen = std::make_unique<Screens::InfiniPaint>(this, lvgl); break;
case Apps::Paddle: currentScreen = std::make_unique<Screens::Paddle>(this, lvgl); break;
case Apps::Brightness : currentScreen = std::make_unique<Screens::Brightness>(this, brightnessController); break;
case Apps::Music : currentScreen = std::make_unique<Screens::Music>(this, systemTask.nimble().music()); break;
case Apps::Navigation : currentScreen = std::make_unique<Screens::Navigation>(this, systemTask.nimble().navigation()); break;
case Apps::FirmwareValidation: currentScreen = std::make_unique<Screens::FirmwareValidation>(this, validator); break;
case Apps::Notifications: currentScreen = std::make_unique<Screens::Notifications>(this, notificationManager, systemTask.nimble().alertService(), Screens::Notifications::Modes::Normal); break;
case Apps::HeartRate: currentScreen = std::make_unique<Screens::HeartRate>(this, heartRateController); break;
}
nextApp = Apps::None;
2020-01-19 18:47:49 +00:00
}
lv_task_handler();
}
void DisplayApp::IdleState() {
}
void DisplayApp::PushMessage(Messages msg) {
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(msgQueue, &msg, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
/* Actual macro used here is port specific. */
// TODO : should I do something here?
}
2019-12-21 21:31:06 +00:00
}
TouchEvents DisplayApp::OnTouchEvent() {
auto info = touchPanel.GetTouchInfo();
if(info.isTouch) {
switch(info.gesture) {
case Pinetime::Drivers::Cst816S::Gestures::SingleTap:
if(touchMode == TouchModes::Gestures)
lvgl.SetNewTapEvent(info.x, info.y);
return TouchEvents::Tap;
case Pinetime::Drivers::Cst816S::Gestures::LongPress:
return TouchEvents::LongTap;
case Pinetime::Drivers::Cst816S::Gestures::DoubleTap:
return TouchEvents::DoubleTap;
case Pinetime::Drivers::Cst816S::Gestures::SlideRight:
return TouchEvents::SwipeRight;
case Pinetime::Drivers::Cst816S::Gestures::SlideLeft:
return TouchEvents::SwipeLeft;
case Pinetime::Drivers::Cst816S::Gestures::SlideDown:
return TouchEvents::SwipeDown;
case Pinetime::Drivers::Cst816S::Gestures::SlideUp:
return TouchEvents::SwipeUp;
case Pinetime::Drivers::Cst816S::Gestures::None:
default:
return TouchEvents::None;
}
}
return TouchEvents::None;
}
void DisplayApp::StartApp(Apps app) {
nextApp = app;
}
void DisplayApp::SetFullRefresh(DisplayApp::FullRefreshDirections direction) {
switch(direction){
case DisplayApp::FullRefreshDirections::Down:
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Down);
break;
case DisplayApp::FullRefreshDirections::Up:
lvgl.SetFullRefresh(Components::LittleVgl::FullRefreshDirections::Up);
break;
default: break;
}
}
void DisplayApp::SetTouchMode(DisplayApp::TouchModes mode) {
touchMode = mode;
}