InfiniTime/src/DisplayApp/DisplayApp.cpp

162 lines
4.2 KiB
C++
Raw Normal View History

#include "DisplayApp.h"
#include <FreeRTOS.h>
#include <task.h>
#include <libraries/log/nrf_log.h>
#include <boards.h>
2019-12-07 16:11:50 +00:00
#include <nrf_font.h>
#include <hal/nrf_rtc.h>
2019-12-07 16:11:50 +00:00
#include "Components/Gfx/Gfx.h"
#include <queue.h>
#include <Components/DateTime/DateTimeController.h>
#include <drivers/Cst816s.h>
#include <chrono>
#include <string>
using namespace Pinetime::Applications;
DisplayApp::DisplayApp(Controllers::Battery &batteryController,
Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController) :
spi{},
lcd{new Drivers::St7789(spi, 18)},
gfx{new Components::Gfx(*lcd.get()) },
batteryController{batteryController},
bleController{bleController},
dateTimeController{dateTimeController},
clockScreen{*(gfx.get())}/*,
messageScreen{*(gfx.get())}*/ {
msgQueue = xQueueCreate(queueSize, itemSize);
currentScreen = &clockScreen;
}
void DisplayApp::Start() {
if (pdPASS != xTaskCreate(DisplayApp::Process, "DisplayApp", 256, this, 0, &taskHandle))
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
}
void DisplayApp::Process(void *instance) {
auto *app = static_cast<DisplayApp *>(instance);
NRF_LOG_INFO("DisplayApp task started!");
2019-12-07 16:11:50 +00:00
app->InitHw();
while (1) {
app->Refresh();
}
}
2019-12-07 16:11:50 +00:00
void DisplayApp::InitHw() {
nrf_gpio_cfg_output(14);
nrf_gpio_cfg_output(22);
nrf_gpio_cfg_output(23);
nrf_gpio_pin_clear(14);
2019-12-07 16:11:50 +00:00
nrf_gpio_pin_clear(22);
nrf_gpio_pin_clear(23);
Drivers::SpiMaster::Parameters params;
params.bitOrder = Drivers::SpiMaster::BitOrder::Msb_Lsb;
params.mode = Drivers::SpiMaster::Modes::Mode3;
params.Frequency = Drivers::SpiMaster::Frequencies::Freq8Mhz;
params.pinCSN = 25;
params.pinMISO = 4;
params.pinMOSI = 3;
params.pinSCK = 2;
spi.Init(Drivers::SpiMaster::SpiModule::SPI0, params);
gfx->Init();
currentScreen->Refresh(true);
touchPanel.Init();
}
void DisplayApp::Refresh() {
TickType_t queueTimeout;
switch (state) {
case States::Idle:
IdleState();
queueTimeout = portMAX_DELAY;
break;
case States::Running:
RunningState();
queueTimeout = 1000;
break;
}
Messages msg;
if (xQueueReceive(msgQueue, &msg, queueTimeout)) {
switch (msg) {
case Messages::GoToSleep:
nrf_gpio_pin_set(23);
vTaskDelay(100);
nrf_gpio_pin_set(22);
vTaskDelay(100);
nrf_gpio_pin_set(14);
lcd->DisplayOff();
lcd->Sleep();
touchPanel.Sleep();
state = States::Idle;
break;
case Messages::GoToRunning:
lcd->Wakeup();
touchPanel.Wakeup();
lcd->DisplayOn();
nrf_gpio_pin_clear(23);
nrf_gpio_pin_clear(22);
nrf_gpio_pin_clear(14);
state = States::Running;
break;
case Messages::UpdateDateTime:
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::TouchEvent:
if(state != States::Running) break;
OnTouchEvent();
break;
}
}
}
void DisplayApp::RunningState() {
clockScreen.SetCurrentDateTime(dateTimeController.CurrentDateTime());
if(currentScreen != nullptr) {
currentScreen->Refresh(false);
}
// if(screenState) {
// currentScreen = &clockScreen;
// } else {
// currentScreen = &messageScreen;
// }
// screenState = !screenState;
}
void DisplayApp::IdleState() {
}
void DisplayApp::PushMessage(DisplayApp::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
}
static uint16_t pointColor = 0x07e0;
void DisplayApp::OnTouchEvent() {
auto info = touchPanel.GetTouchInfo();
if(info.isTouch) {
lcd->FillRectangle(info.x-10, info.y-10, 20,20, pointColor);
pointColor+=10;
}
}