aboutsummaryrefslogtreecommitdiffstats
path: root/src/Components/Battery/BatteryController.cpp
diff options
context:
space:
mode:
authorJF <jf@codingfield.com>2020-09-27 20:02:47 +0200
committerJF <jf@codingfield.com>2020-09-27 20:02:47 +0200
commit5d3a9744938b38c064379c3f78ace5a7dd87926f (patch)
treed0e73cf9c19260328a729eb3ccc398ce8536b329 /src/Components/Battery/BatteryController.cpp
parent079d437016e2bdbd2161fb6b3f11f9d9e346709a (diff)
Add BatteryInformationService to expose the battery level to BLE host.
Diffstat (limited to 'src/Components/Battery/BatteryController.cpp')
-rw-r--r--src/Components/Battery/BatteryController.cpp5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/Components/Battery/BatteryController.cpp b/src/Components/Battery/BatteryController.cpp
index 198ce5aa..571efae6 100644
--- a/src/Components/Battery/BatteryController.cpp
+++ b/src/Components/Battery/BatteryController.cpp
@@ -1,6 +1,7 @@
#include <drivers/include/nrfx_saadc.h>
#include <hal/nrf_gpio.h>
#include <libraries/log/nrf_log.h>
+#include <algorithm>
#include "BatteryController.h"
using namespace Pinetime::Controllers;
@@ -34,7 +35,9 @@ void Battery::Update() {
// see https://forum.pine64.org/showthread.php?tid=8147
voltage = (value * 2.0f) / (1024/3.0f);
- percentRemaining = ((voltage - 3.55)*100)*3.9;
+ percentRemaining = ((voltage - 3.55f)*100.0f)*3.9f;
+ percentRemaining = std::max(percentRemaining, 0.0f);
+ percentRemaining = std::min(percentRemaining, 100.0f);
// NRF_LOG_INFO("BATTERY " NRF_LOG_FLOAT_MARKER " %% - " NRF_LOG_FLOAT_MARKER " v", NRF_LOG_FLOAT(percentRemaining), NRF_LOG_FLOAT(voltage));
// NRF_LOG_INFO("POWER Charging : %d - Power : %d", isCharging, isPowerPresent);