Add DateTimeController to manage the time.

Use messages in message queues to refresh the UI
This commit is contained in:
JF 2019-12-28 14:34:50 +01:00
parent 92985eb98b
commit b744b96622
8 changed files with 179 additions and 74 deletions

View File

@ -320,8 +320,8 @@ void ble_manager_start_advertising(void *p_erase_bonds) {
} }
} }
void (*OnNewTimeCallback)(uint8_t, uint8_t); void (*OnNewTimeCallback)(current_time_char_t*);
void ble_manager_set_callback(void (*OnNewTime)(uint8_t, uint8_t)) { void ble_manager_set_callback(void (*OnNewTime)(current_time_char_t*)) {
OnNewTimeCallback = OnNewTime; OnNewTimeCallback = OnNewTime;
} }
@ -485,8 +485,7 @@ void ble_manager_cts_print_time(ble_cts_c_evt_t *p_evt) {
NRF_LOG_INFO("\tManual update %x", NRF_LOG_INFO("\tManual update %x",
p_evt->params.current_time.adjust_reason.manual_time_update); p_evt->params.current_time.adjust_reason.manual_time_update);
OnNewTimeCallback(p_evt->params.current_time.exact_time_256.day_date_time.date_time.minutes, OnNewTimeCallback(&p_evt->params.current_time);
p_evt->params.current_time.exact_time_256.day_date_time.date_time.hours);
} }
void ble_manager_init_connection_params() { void ble_manager_init_connection_params() {

View File

@ -35,7 +35,7 @@ void ble_manager_init();
void ble_manager_start_advertising(void *p_erase_bonds); void ble_manager_start_advertising(void *p_erase_bonds);
// TODO use signals from RTOS to notify new time // TODO use signals from RTOS to notify new time
void ble_manager_set_callback(void (*OnNewTime)(uint8_t minutes, uint8_t hours)); void ble_manager_set_callback(void (*OnNewTime)(current_time_char_t* currentTime));

View File

@ -40,6 +40,8 @@ list(APPEND SOURCE_FILES
BLE/BleManager.c BLE/BleManager.c
Components/Battery/BatteryController.cpp Components/Battery/BatteryController.cpp
Components/Ble/BleController.cpp Components/Ble/BleController.cpp
Components/Pinetime/PinetimeController.cpp
Components/DateTime/DateTimeController.cpp
) )
set(INCLUDE_FILES set(INCLUDE_FILES
@ -55,6 +57,8 @@ set(INCLUDE_FILES
BLE/BleManager.h BLE/BleManager.h
Components/Battery/BatteryController.h Components/Battery/BatteryController.h
Components/Ble/BleController.h Components/Ble/BleController.h
Components/Pinetime/PinetimeController.h
Components/DateTime/DateTimeController.h
) )
nRF5x_addExecutable(pinetime-app "${SOURCE_FILES}") nRF5x_addExecutable(pinetime-app "${SOURCE_FILES}")

View File

@ -0,0 +1,16 @@
#include "DateTimeController.h"
using namespace Pinetime::Controllers;
void DateTime::UpdateTime(uint16_t year, Months month, uint8_t day, Days dayOfWeek, uint8_t hour, uint8_t minute,
uint8_t second) {
this->year = year;
this->month = month;
this->dayOfWeek = dayOfWeek;
this->day = day;
this->hour = hour;
this->minute = minute;
this->second = second;
}

View File

@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
namespace Pinetime {
namespace Controllers {
class DateTime {
public:
enum class Days : uint8_t {Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
enum class Months : uint8_t {Unknown, January, February, March, April, May, June, July, August, September, October, November, December};
void UpdateTime(uint16_t year, Months month, uint8_t day, Days dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second);
uint16_t Year() const { return year; }
Months Month() const { return month; }
uint8_t Day() const { return day; }
Days DayOfWeek() const { return dayOfWeek; }
uint8_t Hours() const { return hour; }
uint8_t Minutes() const { return minute; }
uint8_t Seconds() const { return second; }
private:
uint16_t year = 0;
Months month = Months::Unknown;
uint8_t day = 0;
Days dayOfWeek = Days::Unknown;
uint8_t hour = 0;
uint8_t minute = 0;
uint8_t second = 0;
};
}
}

View File

@ -7,12 +7,43 @@
#include <hal/nrf_rtc.h> #include <hal/nrf_rtc.h>
#include "Components/Gfx/Gfx.h" #include "Components/Gfx/Gfx.h"
#include <queue.h> #include <queue.h>
#include <Components/DateTime/DateTimeController.h>
using namespace Pinetime::Applications; using namespace Pinetime::Applications;
DisplayApp::DisplayApp(Pinetime::Controllers::Battery &batteryController, Pinetime::Controllers::Ble &bleController) : char const *DisplayApp::DaysString[] = {
"",
"MONDAY",
"TUESDAY",
"WEDNESDAY",
"THURSDAY",
"FRIDAY",
"SATURDAY",
"SUNDAY"
};
char const *DisplayApp::MonthsString[] = {
"",
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"
};
DisplayApp::DisplayApp(Controllers::Battery &batteryController,
Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController) :
batteryController{batteryController}, batteryController{batteryController},
bleController{bleController} { bleController{bleController},
dateTimeController{dateTimeController} {
msgQueue = xQueueCreate(queueSize, itemSize); msgQueue = xQueueCreate(queueSize, itemSize);
} }
@ -22,7 +53,7 @@ void DisplayApp::Start() {
} }
void DisplayApp::Process(void *instance) { void DisplayApp::Process(void *instance) {
auto* app = static_cast<DisplayApp*>(instance); auto *app = static_cast<DisplayApp *>(instance);
NRF_LOG_INFO("DisplayApp task started!"); NRF_LOG_INFO("DisplayApp task started!");
app->InitHw(); app->InitHw();
@ -54,7 +85,7 @@ void DisplayApp::InitHw() {
gfx->ClearScreen(); gfx->ClearScreen();
uint8_t x = 7; uint8_t x = 7;
gfx->DrawChar(&largeFont , '0', &x, 78, 0xffff); gfx->DrawChar(&largeFont, '0', &x, 78, 0xffff);
x = 61; x = 61;
gfx->DrawChar(&largeFont, '0', &x, 78, 0xffff); gfx->DrawChar(&largeFont, '0', &x, 78, 0xffff);
@ -69,12 +100,12 @@ void DisplayApp::InitHw() {
gfx->DrawChar(&largeFont, '0', &x, 78, 0xffff); gfx->DrawChar(&largeFont, '0', &x, 78, 0xffff);
gfx->DrawString(10, 0, 0x0000, "BLE", &smallFont, false); gfx->DrawString(10, 0, 0x0000, "BLE", &smallFont, false);
gfx->DrawString(20, 180, 0xffff, "FRIDAY 27 DEC 2019", &smallFont, false); gfx->DrawString(20, 180, 0xffff, "", &smallFont, false);
} }
void DisplayApp::Refresh() { void DisplayApp::Refresh() {
TickType_t queueTimeout; TickType_t queueTimeout;
switch(state) { switch (state) {
case States::Idle: case States::Idle:
IdleState(); IdleState();
queueTimeout = portMAX_DELAY; queueTimeout = portMAX_DELAY;
@ -86,8 +117,8 @@ void DisplayApp::Refresh() {
} }
Messages msg; Messages msg;
if(xQueueReceive( msgQueue, &msg, queueTimeout)) { if (xQueueReceive(msgQueue, &msg, queueTimeout)) {
switch(msg) { switch (msg) {
case Messages::GoToSleep: case Messages::GoToSleep:
nrf_gpio_pin_set(23); nrf_gpio_pin_set(23);
vTaskDelay(100); vTaskDelay(100);
@ -102,66 +133,53 @@ void DisplayApp::Refresh() {
nrf_gpio_pin_clear(14); nrf_gpio_pin_clear(14);
state = States::Running; state = States::Running;
break; break;
} case Messages::UpdateDateTime:
}
}
void DisplayApp::Minutes(uint8_t m) {
// TODO yeah, I know, race condition...
minutes = m;
}
void DisplayApp::Hours(uint8_t h) {
// TODO yeah, I know, race condition too...
hours = h;
}
void DisplayApp::SetTime(uint8_t minutes, uint8_t hours) {
deltaSeconds = nrf_rtc_counter_get(portNRF_RTC_REG) / 1000; deltaSeconds = nrf_rtc_counter_get(portNRF_RTC_REG) / 1000;
this->minutes = minutes; this->seconds = dateTimeController.Seconds();
this->hours = hours; this->minutes = dateTimeController.Minutes();
this->hours = dateTimeController.Hours();
dateUpdated = true;
break;
case Messages::UpdateBleConnection:
bleConnectionUpdated = true;
break;
case Messages::UpdateBatteryLevel:
batteryLevelUpdated = true;
break;
}
}
} }
void DisplayApp::RunningState() { void DisplayApp::RunningState() {
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
if (batteryLevelUpdated) {
char batteryChar[11]; char batteryChar[11];
uint16_t newBatteryValue = batteryController.PercentRemaining(); uint16_t newBatteryValue = batteryController.PercentRemaining();
newBatteryValue = (newBatteryValue>100) ? 100 : newBatteryValue; newBatteryValue = (newBatteryValue > 100) ? 100 : newBatteryValue;
newBatteryValue = (newBatteryValue<0) ? 0 : newBatteryValue; newBatteryValue = (newBatteryValue < 0) ? 0 : newBatteryValue;
if(newBatteryValue != battery) {
battery = newBatteryValue; batteryLevelUpdated = false;
sprintf(batteryChar, "BAT: %d%%", battery); sprintf(batteryChar, "BAT: %d%%", newBatteryValue);
gfx->DrawString((240-108), 0, 0xffff, batteryChar, &smallFont, false); gfx->DrawString((240 - 108), 0, 0xffff, batteryChar, &smallFont, false);
} }
bool newIsBleConnected = bleController.IsConnected(); if (bleConnectionUpdated) {
if(newIsBleConnected != bleConnected) { bleConnectionUpdated = false;
bleConnected = newIsBleConnected; uint16_t color = (bleController.IsConnected()) ? 0xffff : 0x0000;
uint16_t color = (bleConnected) ? 0xffff : 0x0000;
gfx->DrawString(10, 0, color, "BLE", &smallFont, false); gfx->DrawString(10, 0, color, "BLE", &smallFont, false);
} }
auto raw = systick_counter / 1000; auto raw = systick_counter / 1000;
auto currentDeltaSeconds = raw - deltaSeconds; auto currentDeltaSeconds = raw - deltaSeconds;
auto deltaMinutes = (currentDeltaSeconds / 60); auto deltaMinutes = (currentDeltaSeconds / 60);
auto currentMinutes = minutes + deltaMinutes; auto currentMinutes = minutes + deltaMinutes;
auto deltaHours = currentMinutes / 60; auto deltaHours = currentMinutes / 60;
currentMinutes -= (deltaHours * 60); currentMinutes -= (deltaHours * 60);
//
// TODO make this better!
// minutes = raw / 60;
// seconds = raw - (minutes*60);
auto currentHours = hours + deltaHours; auto currentHours = hours + deltaHours;
char minutesChar[3]; char minutesChar[3];
sprintf(minutesChar, "%02d", currentMinutes); sprintf(minutesChar, "%02d", currentMinutes);
@ -169,30 +187,51 @@ void DisplayApp::RunningState() {
sprintf(hoursChar, "%02d", currentHours); sprintf(hoursChar, "%02d", currentHours);
uint8_t x = 7; uint8_t x = 7;
if(hoursChar[0] != currentChar[0]) { if (hoursChar[0] != currentChar[0]) {
gfx->DrawChar(&largeFont, hoursChar[0], &x, 78, 0xffff); gfx->DrawChar(&largeFont, hoursChar[0], &x, 78, 0xffff);
currentChar[0] = hoursChar[0]; currentChar[0] = hoursChar[0];
} }
x = 61; x = 61;
if(hoursChar[1] != currentChar[1]) { if (hoursChar[1] != currentChar[1]) {
gfx->DrawChar(&largeFont, hoursChar[1], &x, 78, 0xffff); gfx->DrawChar(&largeFont, hoursChar[1], &x, 78, 0xffff);
currentChar[1] = hoursChar[1]; currentChar[1] = hoursChar[1];
} }
x = 127; x = 127;
if(minutesChar[0] != currentChar[2]) { if (minutesChar[0] != currentChar[2]) {
gfx->DrawChar(&largeFont, minutesChar[0], &x, 78, 0xffff); gfx->DrawChar(&largeFont, minutesChar[0], &x, 78, 0xffff);
currentChar[2] = minutesChar[0]; currentChar[2] = minutesChar[0];
} }
x = 181; x = 181;
if(minutesChar[1] != currentChar[3]) { if (minutesChar[1] != currentChar[3]) {
gfx->DrawChar(&largeFont, minutesChar[1], &x, 78, 0xffff); gfx->DrawChar(&largeFont, minutesChar[1], &x, 78, 0xffff);
currentChar[3] = minutesChar[1]; currentChar[3] = minutesChar[1];
} }
if (dateUpdated) {
auto year = dateTimeController.Year();
auto month = dateTimeController.Month();
auto day = dateTimeController.Day();
auto dayOfWeek = dateTimeController.DayOfWeek();
char dateStr[22];
sprintf(dateStr, "%s %d %s %d", DayOfWeekToString(dayOfWeek), day, MonthToString(month), year);
gfx->DrawString(10, 180, 0xffff, dateStr, &smallFont, false);
dateUpdated = false;
}
} }
const char *DisplayApp::MonthToString(Pinetime::Controllers::DateTime::Months month) {
return DisplayApp::MonthsString[static_cast<uint8_t>(month)];
}
const char *DisplayApp::DayOfWeekToString(Pinetime::Controllers::DateTime::Days dayOfWeek) {
return DisplayApp::DaysString[static_cast<uint8_t>(dayOfWeek)];
}
void DisplayApp::IdleState() { void DisplayApp::IdleState() {
} }
@ -200,10 +239,8 @@ void DisplayApp::IdleState() {
void DisplayApp::PushMessage(DisplayApp::Messages msg) { void DisplayApp::PushMessage(DisplayApp::Messages msg) {
BaseType_t xHigherPriorityTaskWoken; BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE; xHigherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR( msgQueue, &msg, &xHigherPriorityTaskWoken ); xQueueSendFromISR(msgQueue, &msg, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
/* Now the buffer is empty we can switch context if necessary. */
if(xHigherPriorityTaskWoken) {
/* Actual macro used here is port specific. */ /* Actual macro used here is port specific. */
// TODO : should I do something here? // TODO : should I do something here?
} }

View File

@ -8,6 +8,7 @@
#include <queue.h> #include <queue.h>
#include <Components/Battery/BatteryController.h> #include <Components/Battery/BatteryController.h>
#include <Components/Ble/BleController.h> #include <Components/Ble/BleController.h>
#include <Components/DateTime/DateTimeController.h>
#include "lcdfont14.h" #include "lcdfont14.h"
extern const FONT_INFO lCD_70ptFontInfo; extern const FONT_INFO lCD_70ptFontInfo;
@ -17,15 +18,11 @@ namespace Pinetime {
class DisplayApp { class DisplayApp {
public: public:
enum class States {Idle, Running}; enum class States {Idle, Running};
enum class Messages : uint8_t {GoToSleep, GoToRunning} ; enum class Messages : uint8_t {GoToSleep, GoToRunning, UpdateDateTime, UpdateBleConnection, UpdateBatteryLevel} ;
DisplayApp(Pinetime::Controllers::Battery& batteryController, Pinetime::Controllers::Ble& bleController); DisplayApp(Controllers::Battery &batteryController,
Controllers::Ble &bleController,
Controllers::DateTime& dateTimeController);
void Start(); void Start();
void Minutes(uint8_t m);
void Hours(uint8_t h);
void SetTime(uint8_t minutes, uint8_t hours);
void PushMessage(Messages msg); void PushMessage(Messages msg);
private: private:
@ -39,6 +36,8 @@ namespace Pinetime {
const FONT_INFO smallFont {lCD_14ptFontInfo.height, lCD_14ptFontInfo.startChar, lCD_14ptFontInfo.endChar, lCD_14ptFontInfo.spacePixels, lCD_14ptFontInfo.charInfo, lCD_14ptFontInfo.data}; const FONT_INFO smallFont {lCD_14ptFontInfo.height, lCD_14ptFontInfo.startChar, lCD_14ptFontInfo.endChar, lCD_14ptFontInfo.spacePixels, lCD_14ptFontInfo.charInfo, lCD_14ptFontInfo.data};
void Refresh(); void Refresh();
static const char* MonthToString(Pinetime::Controllers::DateTime::Months month);
static const char* DayOfWeekToString(Pinetime::Controllers::DateTime::Days dayOfWeek);
uint8_t seconds = 0; uint8_t seconds = 0;
uint8_t minutes = 0; uint8_t minutes = 0;
uint8_t hours = 0; uint8_t hours = 0;
@ -55,8 +54,15 @@ namespace Pinetime {
Pinetime::Controllers::Battery &batteryController; Pinetime::Controllers::Battery &batteryController;
Pinetime::Controllers::Ble &bleController; Pinetime::Controllers::Ble &bleController;
uint16_t battery = 0; Pinetime::Controllers::DateTime& dateTimeController;
bool bleConnected = false; bool bleConnectionUpdated = false;
bool batteryLevelUpdated = false;
static char const *DaysString[];
static char const *MonthsString[];
bool dateUpdated = false;
}; };
} }

View File

@ -10,6 +10,8 @@
#include <hal/nrf_rtc.h> #include <hal/nrf_rtc.h>
#include <timers.h> #include <timers.h>
#include <libraries/log/nrf_log.h> #include <libraries/log/nrf_log.h>
#include <ble/ble_services/ble_cts_c/ble_cts_c.h>
#include <Components/DateTime/DateTimeController.h>
#include "BLE/BleManager.h" #include "BLE/BleManager.h"
#include "Components/Battery/BatteryController.h" #include "Components/Battery/BatteryController.h"
#include "Components/Ble/BleController.h" #include "Components/Ble/BleController.h"
@ -28,6 +30,7 @@ bool isSleeping = false;
TimerHandle_t debounceTimer; TimerHandle_t debounceTimer;
Pinetime::Controllers::Battery batteryController; Pinetime::Controllers::Battery batteryController;
Pinetime::Controllers::Ble bleController; Pinetime::Controllers::Ble bleController;
Pinetime::Controllers::DateTime dateTimeController;
extern "C" { extern "C" {
void vApplicationIdleHook() { void vApplicationIdleHook() {
@ -51,6 +54,7 @@ void DebounceTimerCallback(TimerHandle_t xTimer) {
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToRunning); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToRunning);
isSleeping = false; isSleeping = false;
batteryController.Update(); batteryController.Update();
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
} }
else { else {
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep);
@ -66,6 +70,7 @@ void SystemTask(void *) {
batteryController.Init(); batteryController.Init();
batteryController.Update(); batteryController.Update();
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback);
@ -81,18 +86,26 @@ void SystemTask(void *) {
pinConfig.pull = (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown; pinConfig.pull = (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown;
nrfx_gpiote_in_init(13, &pinConfig, nrfx_gpiote_evt_handler); nrfx_gpiote_in_init(13, &pinConfig, nrfx_gpiote_evt_handler);
vTaskSuspend(nullptr); vTaskSuspend(nullptr);
} }
void OnNewTime(uint8_t minutes, uint8_t hours) { void OnNewTime(current_time_char_t* currentTime) {
bleController.Connect(); bleController.Connect();
displayApp->SetTime(minutes, hours); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBleConnection);
auto dayOfWeek = currentTime->exact_time_256.day_date_time.day_of_week;
auto year = currentTime->exact_time_256.day_date_time.date_time.year;
auto month = currentTime->exact_time_256.day_date_time.date_time.month;
auto day = currentTime->exact_time_256.day_date_time.date_time.day;
auto hour = currentTime->exact_time_256.day_date_time.date_time.hours;
auto minute = currentTime->exact_time_256.day_date_time.date_time.minutes;
auto second = currentTime->exact_time_256.day_date_time.date_time.seconds;
dateTimeController.UpdateTime(year, static_cast<Pinetime::Controllers::DateTime::Months >(month), day, static_cast<Pinetime::Controllers::DateTime::Days>(dayOfWeek), hour, minute, second);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateDateTime);
} }
int main(void) { int main(void) {
displayApp.reset(new Pinetime::Applications::DisplayApp(batteryController, bleController)); displayApp.reset(new Pinetime::Applications::DisplayApp(batteryController, bleController, dateTimeController));
logger.Init(); logger.Init();
nrf_drv_clock_init(); nrf_drv_clock_init();