summaryrefslogtreecommitdiff
path: root/src/components/battery/BatteryController.cpp
diff options
context:
space:
mode:
authorJean-François Milants <jf@codingfield.com>2021-07-11 16:55:06 +0200
committerJean-François Milants <jf@codingfield.com>2021-07-11 16:55:06 +0200
commite21f6a7f414d1f832e8fddfeaab3b9de05aa3459 (patch)
treea4ebbc3a74ba7daf87aa99c0eafcb0e9e7bc6c35 /src/components/battery/BatteryController.cpp
parent6a91b83b12ef849f68d54f490153b02f0ecf58dc (diff)
Notify battery level every 10 minutes when connected to a BLE host.
Refactor battery percent : only use uint8_t to store the battery % remaining.
Diffstat (limited to 'src/components/battery/BatteryController.cpp')
-rw-r--r--src/components/battery/BatteryController.cpp13
1 files changed, 3 insertions, 10 deletions
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp
index 76ad8cb3..fa476ea3 100644
--- a/src/components/battery/BatteryController.cpp
+++ b/src/components/battery/BatteryController.cpp
@@ -1,9 +1,7 @@
#include "BatteryController.h"
#include <hal/nrf_gpio.h>
#include <nrfx_saadc.h>
-#include <libraries/log/nrf_log.h>
#include <algorithm>
-#include <math.h>
using namespace Pinetime::Controllers;
@@ -18,7 +16,6 @@ void Battery::Init() {
}
void Battery::Update() {
-
isCharging = !nrf_gpio_pin_read(chargingPin);
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
@@ -33,13 +30,13 @@ void Battery::Update() {
nrfx_saadc_sample();
}
-void Battery::adcCallbackStatic(nrfx_saadc_evt_t const* event) {
+void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) {
instance->SaadcEventHandler(event);
}
void Battery::SaadcInit() {
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
- APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
+ APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic));
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
@@ -55,7 +52,6 @@ void Battery::SaadcInit() {
}
void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
-
const uint16_t battery_max = 4180; // maximum voltage of battery ( max charging voltage is 4.21 )
const uint16_t battery_min = 3200; // minimum voltage of battery before shutdown ( depends on the battery )
@@ -69,13 +65,10 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
// reference_voltage is 0.6V
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
voltage = p_event->data.done.p_buffer[0] * 6000 / 1024;
-
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
-
percentRemaining = std::max(percentRemaining, 0);
percentRemaining = std::min(percentRemaining, 100);
-
- percentRemainingBuffer.insert(percentRemaining);
+ percentRemainingBuffer.Insert(percentRemaining);
samples++;
if (samples > percentRemainingSamples) {