diff options
| author | Riku Isokoski <riksu9000@gmail.com> | 2021-07-13 19:11:46 (GMT) |
|---|---|---|
| committer | Riku Isokoski <riksu9000@gmail.com> | 2021-07-13 19:11:46 (GMT) |
| commit | b5eabf6604734e40caa4a3ccbbd8aa9e9dfa7ced (patch) | |
| tree | 2cf14c70a8b28dd1691a7583c3b38405682f1c61 /src/components/battery | |
| parent | 7efe2b7c51e1d21305154edad6ddf1b608784451 (diff) | |
| parent | a07b6382aef3c30d8ea8f28fcc3cc880d7afd747 (diff) | |
Merge branch 'develop' into fix_adc
Diffstat (limited to 'src/components/battery')
| -rw-r--r-- | src/components/battery/BatteryController.cpp | 19 | ||||
| -rw-r--r-- | src/components/battery/BatteryController.h | 7 |
2 files changed, 13 insertions, 13 deletions
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp index 41b4c48..54b9ffd 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); @@ -32,13 +29,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, @@ -54,7 +51,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,10 +65,13 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) { // 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); + if (voltage > battery_max) { + percentRemaining = 100; + } else if (voltage < battery_min) { + percentRemaining = 0; + } else { + percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min); + } nrfx_saadc_uninit(); isReading = false; diff --git a/src/components/battery/BatteryController.h b/src/components/battery/BatteryController.h index 84d488e..6f09b73 100644 --- a/src/components/battery/BatteryController.h +++ b/src/components/battery/BatteryController.h @@ -14,7 +14,7 @@ namespace Pinetime { void Init(); void Update(); - int PercentRemaining() const { + uint8_t PercentRemaining() const { return percentRemaining; } @@ -25,6 +25,7 @@ namespace Pinetime { bool IsCharging() const { return isCharging; } + bool IsPowerPresent() const { return isPowerPresent; } @@ -37,7 +38,7 @@ namespace Pinetime { static constexpr uint32_t powerPresentPin = 19; static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; uint16_t voltage = 0; - int percentRemaining = -1; + uint8_t percentRemaining = 0; bool isCharging = false; bool isPowerPresent = false; @@ -45,7 +46,7 @@ namespace Pinetime { void SaadcInit(); void SaadcEventHandler(nrfx_saadc_evt_t const* p_event); - static void adcCallbackStatic(nrfx_saadc_evt_t const* event); + static void AdcCallbackStatic(nrfx_saadc_evt_t const* event); bool isReading = false; }; |
