Automatically go to sleep after 5 sec of inactivity.

Advertise for  3 minutes at startup and on wake-up. Disable automatic continuous StartAdvertising.
This commit is contained in:
JF 2020-06-01 20:40:11 +02:00
parent de822cc3a2
commit e7723598a6
3 changed files with 58 additions and 11 deletions

View File

@ -33,8 +33,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
spiNorFlash{spiNorFlash}, spiNorFlash{spiNorFlash},
dfuService{systemTask, bleController, spiNorFlash}, dfuService{systemTask, bleController, spiNorFlash},
currentTimeClient{dateTimeController}, currentTimeClient{dateTimeController},
alertNotificationClient{systemTask, notificationManager},
anService{systemTask, notificationManager}, anService{systemTask, notificationManager},
alertNotificationClient{systemTask, notificationManager},
currentTimeService{dateTimeController} { currentTimeService{dateTimeController} {
} }
@ -97,6 +97,8 @@ void NimbleController::Init() {
} }
void NimbleController::StartAdvertising() { void NimbleController::StartAdvertising() {
if(ble_gap_adv_active()) return;
ble_svc_gap_device_name_set("Pinetime-JF"); ble_svc_gap_device_name_set("Pinetime-JF");
/* set adv parameters */ /* set adv parameters */
@ -136,7 +138,7 @@ void NimbleController::StartAdvertising() {
res = ble_gap_adv_rsp_set_fields(&rsp_fields); res = ble_gap_adv_rsp_set_fields(&rsp_fields);
// ASSERT(res == 0); // ASSERT(res == 0);
res = ble_gap_adv_start(addrType, NULL, 10000, res = ble_gap_adv_start(addrType, NULL, 180000,
&adv_params, GAPEventCallback, this); &adv_params, GAPEventCallback, this);
// ASSERT(res == 0);// TODO I've disabled these ASSERT as they sometime asserts and reset the mcu. // ASSERT(res == 0);// TODO I've disabled these ASSERT as they sometime asserts and reset the mcu.
// For now, the advertising is restarted as soon as it ends. There may be a race condition // For now, the advertising is restarted as soon as it ends. There may be a race condition
@ -160,7 +162,6 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
case BLE_GAP_EVENT_ADV_COMPLETE: case BLE_GAP_EVENT_ADV_COMPLETE:
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE"); NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
NRF_LOG_INFO("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status); NRF_LOG_INFO("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status);
StartAdvertising();
break; break;
case BLE_GAP_EVENT_CONNECT: { case BLE_GAP_EVENT_CONNECT: {
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT"); NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT");

View File

@ -15,6 +15,12 @@
using namespace Pinetime::System; using namespace Pinetime::System;
void IdleTimerCallback(TimerHandle_t xTimer) {
auto sysTask = static_cast<SystemTask *>(pvTimerGetTimerID(xTimer));
sysTask->OnIdle();
}
SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel,
Components::LittleVgl &lvgl, Components::LittleVgl &lvgl,
@ -92,35 +98,55 @@ void SystemTask::Work() {
nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler); nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
idleTimer = xTimerCreate ("idleTimer", idleTime, pdFALSE, this, IdleTimerCallback);
xTimerStart(idleTimer, 0);
while(true) { while(true) {
uint8_t msg; uint8_t msg;
if (xQueueReceive(systemTaksMsgQueue, &msg, isSleeping?2500 : 1000)) { if (xQueueReceive(systemTaksMsgQueue, &msg, isSleeping?2500 : 1000)) {
Messages message = static_cast<Messages >(msg); Messages message = static_cast<Messages >(msg);
switch(message) { switch(message) {
case Messages::GoToRunning: isSleeping = false; break; case Messages::GoToRunning:
isSleeping = false;
xTimerStart(idleTimer, 0);
nimbleController.StartAdvertising();
break;
case Messages::GoToSleep: case Messages::GoToSleep:
NRF_LOG_INFO("[SystemTask] Going to sleep"); NRF_LOG_INFO("[SystemTask] Going to sleep");
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep);
isSleeping = true; break; isSleeping = true;
break;
case Messages::OnNewTime: case Messages::OnNewTime:
xTimerReset(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateDateTime); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateDateTime);
break; break;
case Messages::OnNewNotification: case Messages::OnNewNotification:
xTimerReset(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::NewNotification); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::NewNotification);
break; break;
case Messages::BleConnected: case Messages::BleConnected:
xTimerReset(idleTimer, 0);
isBleDiscoveryTimerRunning = true; isBleDiscoveryTimerRunning = true;
bleDiscoveryTimer = 5; bleDiscoveryTimer = 5;
break; break;
case Messages::BleFirmwareUpdateStarted: case Messages::BleFirmwareUpdateStarted:
doNotGoToSleep = true;
if(isSleeping) GoToRunning();
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateStarted); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateStarted);
break; break;
case Messages::BleFirmwareUpdateFinished: case Messages::BleFirmwareUpdateFinished:
doNotGoToSleep = false;
xTimerStart(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateFinished); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateFinished);
if(bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated) if(bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated)
NVIC_SystemReset(); NVIC_SystemReset();
break; break;
case Messages::OnTouchEvent:
xTimerReset(idleTimer, 0);
break;
case Messages::OnButtonEvent:
xTimerReset(idleTimer, 0);
break;
default: break; default: break;
} }
} }
@ -146,23 +172,30 @@ void SystemTask::Work() {
} }
void SystemTask::OnButtonPushed() { void SystemTask::OnButtonPushed() {
if(!isSleeping) { if(!isSleeping) {
NRF_LOG_INFO("[SystemTask] Button pushed"); NRF_LOG_INFO("[SystemTask] Button pushed");
PushMessage(Messages::OnButtonEvent);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::ButtonPushed); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::ButtonPushed);
} }
else { else {
NRF_LOG_INFO("[SystemTask] Button pushed, waking up"); NRF_LOG_INFO("[SystemTask] Button pushed, waking up");
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToRunning); GoToRunning();
isSleeping = false;
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
} }
} }
void SystemTask::GoToRunning() {
PushMessage(Messages::GoToRunning);
displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning);
displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel);
}
void SystemTask::OnTouchEvent() { void SystemTask::OnTouchEvent() {
NRF_LOG_INFO("[SystemTask] Touch event"); NRF_LOG_INFO("[SystemTask] Touch event");
if(!isSleeping) {
PushMessage(Messages::OnTouchEvent);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent);
} }
}
void SystemTask::PushMessage(SystemTask::Messages msg) { void SystemTask::PushMessage(SystemTask::Messages msg) {
BaseType_t xHigherPriorityTaskWoken; BaseType_t xHigherPriorityTaskWoken;
@ -173,3 +206,9 @@ void SystemTask::PushMessage(SystemTask::Messages msg) {
// TODO : should I do something here? // TODO : should I do something here?
} }
} }
void SystemTask::OnIdle() {
if(doNotGoToSleep) return;
NRF_LOG_INFO("Idle timeout -> Going to sleep")
PushMessage(Messages::GoToSleep);
}

View File

@ -16,7 +16,7 @@ namespace Pinetime {
class SystemTask { class SystemTask {
public: public:
enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected, enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected,
BleFirmwareUpdateStarted, BleFirmwareUpdateFinished BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent
}; };
SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
@ -32,6 +32,9 @@ namespace Pinetime {
void OnButtonPushed(); void OnButtonPushed();
void OnTouchEvent(); void OnTouchEvent();
void OnIdle();
private: private:
TaskHandle_t taskHandle; TaskHandle_t taskHandle;
@ -64,7 +67,11 @@ namespace Pinetime {
void Work(); void Work();
bool isBleDiscoveryTimerRunning = false; bool isBleDiscoveryTimerRunning = false;
uint8_t bleDiscoveryTimer = 0; uint8_t bleDiscoveryTimer = 0;
static constexpr uint32_t idleTime = 5000;
TimerHandle_t idleTimer;
bool doNotGoToSleep = false;
void GoToRunning();
}; };
} }
} }