Skip to content

Commit 9342d62

Browse files
authored
Emit event on power-present toggle (#320)
* Emit event on power-present toggle * clang-format on changes * also update battery status on any event * update comments; remove double battery update * Fix formatting * Vibrate shortly on charging event * debounce charge event
1 parent 5b2472c commit 9342d62

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Battery::Battery() {
1515

1616
void Battery::Init() {
1717
nrf_gpio_cfg_input(chargingPin, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup);
18-
nrf_gpio_cfg_input(powerPresentPin, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pullup);
1918
}
2019

2120
void Battery::Update() {

src/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ Pinetime::Drivers::Bma421 motionSensor {twiMaster, motionSensorTwiAddress};
101101
Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress};
102102

103103
TimerHandle_t debounceTimer;
104+
TimerHandle_t debounceChargeTimer;
104105
Pinetime::Controllers::Battery batteryController;
105106
Pinetime::Controllers::Ble bleController;
106107
void ble_manager_set_ble_connection_callback(void (*connection)());
107108
void ble_manager_set_ble_disconnection_callback(void (*disconnection)());
108109
static constexpr uint8_t pinTouchIrq = 28;
110+
static constexpr uint8_t pinPowerPresentIrq = 19;
109111
std::unique_ptr<Pinetime::System::SystemTask> systemTask;
110112

111113
Pinetime::Controllers::Settings settingsController {spiNorFlash};
@@ -119,6 +121,13 @@ void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action
119121
}
120122

121123
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
124+
125+
if (pin == pinPowerPresentIrq and action == NRF_GPIOTE_POLARITY_TOGGLE) {
126+
xTimerStartFromISR(debounceChargeTimer, &xHigherPriorityTaskWoken);
127+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
128+
return;
129+
}
130+
122131
xTimerStartFromISR(debounceTimer, &xHigherPriorityTaskWoken);
123132
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
124133
}
@@ -130,6 +139,11 @@ void vApplicationIdleHook(void) {
130139
}
131140
}
132141

142+
void DebounceTimerChargeCallback(TimerHandle_t xTimer) {
143+
xTimerStop(xTimer, 0);
144+
systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnChargingEvent);
145+
}
146+
133147
void DebounceTimerCallback(TimerHandle_t xTimer) {
134148
xTimerStop(xTimer, 0);
135149
systemTask->OnButtonPushed();
@@ -248,6 +262,7 @@ int main(void) {
248262
nrf_drv_clock_init();
249263

250264
debounceTimer = xTimerCreate("debounceTimer", 200, pdFALSE, (void*) 0, DebounceTimerCallback);
265+
debounceChargeTimer = xTimerCreate("debounceTimerCharge", 200, pdFALSE, (void*) 0, DebounceTimerChargeCallback);
251266

252267
systemTask = std::make_unique<Pinetime::System::SystemTask>(spi,
253268
lcd,

src/systemtask/SystemTask.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ void SystemTask::Work() {
149149

150150
nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
151151

152+
nrf_gpio_cfg_sense_input(pinPowerPresentIrq, (nrf_gpio_pin_pull_t) NRF_GPIO_PIN_NOPULL, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_Low);
153+
154+
pinConfig.sense = NRF_GPIOTE_POLARITY_TOGGLE;
155+
pinConfig.pull = NRF_GPIO_PIN_NOPULL;
156+
pinConfig.is_watcher = false;
157+
pinConfig.hi_accuracy = false;
158+
pinConfig.skip_gpio_setup = true;
159+
160+
nrfx_gpiote_in_init(pinPowerPresentIrq, &pinConfig, nrfx_gpiote_evt_handler);
161+
152162
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut()), pdFALSE, this, IdleTimerCallback);
153163
xTimerStart(idleTimer, 0);
154164

@@ -161,12 +171,9 @@ void SystemTask::Work() {
161171
uint8_t msg;
162172
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
163173

164-
// call the battery controller or use the MSG in DisplayApp to get the battery status ???
165-
// it is necessary to validate which is the most efficient
166174
batteryController.Update();
167-
// displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
168-
// analyze a more efficient way to do this refreshment
169-
// this and the UpdateMotion(); can be called on a timer to be independent of the main process ???
175+
// the battery does not emit events when changing charge levels, so we piggyback
176+
// on any system event to read and update the current values
170177

171178
Messages message = static_cast<Messages>(msg);
172179
switch (message) {
@@ -274,6 +281,11 @@ void SystemTask::Work() {
274281
// Remember we'll have to reset the counter next time we're awake
275282
stepCounterMustBeReset = true;
276283
break;
284+
case Messages::OnChargingEvent:
285+
motorController.SetDuration(15);
286+
// Battery level is updated on every message - there's no need to do anything
287+
break;
288+
277289
default:
278290
break;
279291
}

src/systemtask/SystemTask.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ namespace Pinetime {
5555
OnDisplayTaskSleeping,
5656
EnableSleeping,
5757
DisableSleeping,
58-
OnNewDay
58+
OnNewDay,
59+
OnChargingEvent
5960
};
6061

6162
SystemTask(Drivers::SpiMaster& spi,
@@ -121,6 +122,7 @@ namespace Pinetime {
121122
static constexpr uint8_t pinLcdDataCommand = 18;
122123
static constexpr uint8_t pinButton = 13;
123124
static constexpr uint8_t pinTouchIrq = 28;
125+
static constexpr uint8_t pinPowerPresentIrq = 19;
124126

125127
static void Process(void* instance);
126128
void Work();

0 commit comments

Comments
 (0)