From 11aa5e3d880af978dc5c337357c3802355c799eb Mon Sep 17 00:00:00 2001 From: JF Date: Fri, 27 Dec 2019 17:05:09 +0100 Subject: [PATCH] Replace pin numbers by constants --- src/Components/Battery/BatteryController.cpp | 11 ++++++----- src/Components/Battery/BatteryController.h | 3 +++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Components/Battery/BatteryController.cpp b/src/Components/Battery/BatteryController.cpp index 2d0ad520..7719bcbb 100644 --- a/src/Components/Battery/BatteryController.cpp +++ b/src/Components/Battery/BatteryController.cpp @@ -6,8 +6,8 @@ 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); + nrf_gpio_cfg_input(chargingPin, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup); + nrf_gpio_cfg_input(powerPresentPin, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup); nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG; nrfx_saadc_init(&adcConfig, SaadcEventHandler); @@ -19,19 +19,20 @@ void Battery::Init() { .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_p = batteryVoltageAdcInput, .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); + isCharging = !nrf_gpio_pin_read(chargingPin); + isPowerPresent = !nrf_gpio_pin_read(powerPresentPin); nrf_saadc_value_t value = 0; nrfx_saadc_sample_convert(0, &value); + // see https://forum.pine64.org/showthread.php?tid=8147 voltage = (value * 2.0f) / (1024/3.0f); percentRemaining = ((voltage - 3.55)*100)*3.9; diff --git a/src/Components/Battery/BatteryController.h b/src/Components/Battery/BatteryController.h index 5c631dc2..f07648a9 100644 --- a/src/Components/Battery/BatteryController.h +++ b/src/Components/Battery/BatteryController.h @@ -14,6 +14,9 @@ namespace Pinetime { bool IsPowerPresent() const { return isPowerPresent; } private: + static constexpr uint32_t chargingPin = 12; + static constexpr uint32_t powerPresentPin = 19; + static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7; static void SaadcEventHandler(nrfx_saadc_evt_t const * p_event); float percentRemaining = 0.0f; float voltage = 0.0f;