1
0
Fork 0

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},
dfuService{systemTask, bleController, spiNorFlash},
currentTimeClient{dateTimeController},
alertNotificationClient{systemTask, notificationManager},
anService{systemTask, notificationManager},
alertNotificationClient{systemTask, notificationManager},
currentTimeService{dateTimeController} {
}
@ -97,6 +97,8 @@ void NimbleController::Init() {
}
void NimbleController::StartAdvertising() {
if(ble_gap_adv_active()) return;
ble_svc_gap_device_name_set("Pinetime-JF");
/* set adv parameters */
@ -136,7 +138,7 @@ void NimbleController::StartAdvertising() {
res = ble_gap_adv_rsp_set_fields(&rsp_fields);
// ASSERT(res == 0);
res = ble_gap_adv_start(addrType, NULL, 10000,
res = ble_gap_adv_start(addrType, NULL, 180000,
&adv_params, GAPEventCallback, this);
// 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
@ -160,7 +162,6 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
case 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);
StartAdvertising();
break;
case BLE_GAP_EVENT_CONNECT: {
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT");

View file

@ -15,6 +15,12 @@
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,
Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel,
Components::LittleVgl &lvgl,
@ -92,35 +98,55 @@ void SystemTask::Work() {
nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
idleTimer = xTimerCreate ("idleTimer", idleTime, pdFALSE, this, IdleTimerCallback);
xTimerStart(idleTimer, 0);
while(true) {
uint8_t msg;
if (xQueueReceive(systemTaksMsgQueue, &msg, isSleeping?2500 : 1000)) {
Messages message = static_cast<Messages >(msg);
switch(message) {
case Messages::GoToRunning: isSleeping = false; break;
case Messages::GoToRunning:
isSleeping = false;
xTimerStart(idleTimer, 0);
nimbleController.StartAdvertising();
break;
case Messages::GoToSleep:
NRF_LOG_INFO("[SystemTask] Going to sleep");
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep);
isSleeping = true; break;
isSleeping = true;
break;
case Messages::OnNewTime:
xTimerReset(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateDateTime);
break;
case Messages::OnNewNotification:
xTimerReset(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::NewNotification);
break;
case Messages::BleConnected:
xTimerReset(idleTimer, 0);
isBleDiscoveryTimerRunning = true;
bleDiscoveryTimer = 5;
break;
case Messages::BleFirmwareUpdateStarted:
doNotGoToSleep = true;
if(isSleeping) GoToRunning();
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateStarted);
break;
case Messages::BleFirmwareUpdateFinished:
doNotGoToSleep = false;
xTimerStart(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateFinished);
if(bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated)
NVIC_SystemReset();
break;
case Messages::OnTouchEvent:
xTimerReset(idleTimer, 0);
break;
case Messages::OnButtonEvent:
xTimerReset(idleTimer, 0);
break;
default: break;
}
}
@ -146,22 +172,29 @@ void SystemTask::Work() {
}
void SystemTask::OnButtonPushed() {
if(!isSleeping) {
NRF_LOG_INFO("[SystemTask] Button pushed");
PushMessage(Messages::OnButtonEvent);
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;
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
GoToRunning();
}
}
void SystemTask::GoToRunning() {
PushMessage(Messages::GoToRunning);
displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning);
displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel);
}
void SystemTask::OnTouchEvent() {
NRF_LOG_INFO("[SystemTask] Touch event");
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent);
if(!isSleeping) {
PushMessage(Messages::OnTouchEvent);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent);
}
}
void SystemTask::PushMessage(SystemTask::Messages msg) {
@ -173,3 +206,9 @@ void SystemTask::PushMessage(SystemTask::Messages msg) {
// 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 {
public:
enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected,
BleFirmwareUpdateStarted, BleFirmwareUpdateFinished
BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent
};
SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
@ -32,6 +32,9 @@ namespace Pinetime {
void OnButtonPushed();
void OnTouchEvent();
void OnIdle();
private:
TaskHandle_t taskHandle;
@ -64,7 +67,11 @@ namespace Pinetime {
void Work();
bool isBleDiscoveryTimerRunning = false;
uint8_t bleDiscoveryTimer = 0;
static constexpr uint32_t idleTime = 5000;
TimerHandle_t idleTimer;
bool doNotGoToSleep = false;
void GoToRunning();
};
}
}