summaryrefslogtreecommitdiff
path: root/src/components/battery
diff options
context:
space:
mode:
authorAvamander <avamander@gmail.com>2020-10-02 22:16:48 +0300
committerAvamander <avamander@gmail.com>2020-10-02 22:16:48 +0300
commit6c86d1d9d706706fcb6f214aba8259e61ed68755 (patch)
tree2f4137a9916869cee18fd01449aa83ee586b9604 /src/components/battery
parent4daab2692692d47af24a9384eb0f402821527882 (diff)
Fixed all the includes that were broken due to the renames
Diffstat (limited to 'src/components/battery')
-rw-r--r--src/components/battery/BatteryController.cpp48
-rw-r--r--src/components/battery/BatteryController.h27
2 files changed, 75 insertions, 0 deletions
diff --git a/src/components/battery/BatteryController.cpp b/src/components/battery/BatteryController.cpp
new file mode 100644
index 00000000..571efae6
--- /dev/null
+++ b/src/components/battery/BatteryController.cpp
@@ -0,0 +1,48 @@
+#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;
+
+void Battery::Init() {
+ 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);
+ 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 = batteryVoltageAdcInput,
+ .pin_n = NRF_SAADC_INPUT_DISABLED
+ };
+ nrfx_saadc_channel_init(0, &adcChannelConfig);
+}
+
+void Battery::Update() {
+ 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.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);
+}
+
+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..f07648a9
--- /dev/null
+++ b/src/components/battery/BatteryController.h
@@ -0,0 +1,27 @@
+#pragma once
+#include <drivers/include/nrfx_saadc.h>
+
+
+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 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;
+ bool isCharging = false;
+ bool isPowerPresent = false;
+ };
+ }
+} \ No newline at end of file