From fcbd341c1cf0084d50e7d32337013095d45091b9 Mon Sep 17 00:00:00 2001 From: JF Date: Fri, 27 Dec 2019 16:05:35 +0100 Subject: Implement battery measurement in BatteryController. Update battery info on wake up (with button) --- src/Components/Battery/BatteryController.cpp | 44 ++++++++++++++++++++++++++++ src/Components/Battery/BatteryController.h | 24 +++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/Components/Battery/BatteryController.cpp create mode 100644 src/Components/Battery/BatteryController.h (limited to 'src/Components') diff --git a/src/Components/Battery/BatteryController.cpp b/src/Components/Battery/BatteryController.cpp new file mode 100644 index 00000000..2d0ad520 --- /dev/null +++ b/src/Components/Battery/BatteryController.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include "BatteryController.h" + +using namespace Pinetime::Controllers; + +void Battery::Init() { + nrf_gpio_cfg_input(12, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup); + nrf_gpio_cfg_input(19, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup); + + nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG; + nrfx_saadc_init(&adcConfig, SaadcEventHandler); + nrf_saadc_channel_config_t adcChannelConfig = { + .resistor_p = NRF_SAADC_RESISTOR_DISABLED, + .resistor_n = NRF_SAADC_RESISTOR_DISABLED, + .gain = NRF_SAADC_GAIN1_5, + .reference = NRF_SAADC_REFERENCE_INTERNAL, + .acq_time = NRF_SAADC_ACQTIME_3US, + .mode = NRF_SAADC_MODE_SINGLE_ENDED, + .burst = NRF_SAADC_BURST_DISABLED, + .pin_p = (nrf_saadc_input_t)(SAADC_CH_PSELP_PSELP_AnalogInput7), + .pin_n = NRF_SAADC_INPUT_DISABLED + }; + nrfx_saadc_channel_init(0, &adcChannelConfig); +} + +void Battery::Update() { + isCharging = !nrf_gpio_pin_read(12); + isPowerPresent = !nrf_gpio_pin_read(19); + + nrf_saadc_value_t value = 0; + nrfx_saadc_sample_convert(0, &value); + + voltage = (value * 2.0f) / (1024/3.0f); + percentRemaining = ((voltage - 3.55)*100)*3.9; + + 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); +} + +void Battery::SaadcEventHandler(nrfx_saadc_evt_t const * event) { + +} \ No newline at end of file diff --git a/src/Components/Battery/BatteryController.h b/src/Components/Battery/BatteryController.h new file mode 100644 index 00000000..5c631dc2 --- /dev/null +++ b/src/Components/Battery/BatteryController.h @@ -0,0 +1,24 @@ +#pragma once +#include + + +namespace Pinetime { + namespace Controllers { + class Battery { + public: + void Init(); + void Update(); + float PercentRemaining() const { return percentRemaining; } + float Voltage() const { return voltage; } + bool IsCharging() const { return isCharging; } + bool IsPowerPresent() const { return isPowerPresent; } + + private: + static void SaadcEventHandler(nrfx_saadc_evt_t const * p_event); + float percentRemaining = 0.0f; + float voltage = 0.0f; + bool isCharging = false; + bool isPowerPresent = false; + }; + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2