Skip to content

Commit

Permalink
Battery updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mark9064 committed May 21, 2023
1 parent 89d3db4 commit 7254df6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/components/battery/BatteryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Battery* Battery::instance = nullptr;
Battery::Battery() {
instance = this;
nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
SaadcInit();
}

void Battery::ReadPowerState() {
Expand All @@ -34,7 +35,6 @@ void Battery::MeasureVoltage() {
}
// Non blocking read
isReading = true;
SaadcInit();

nrfx_saadc_sample();
}
Expand All @@ -44,7 +44,11 @@ void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) {
}

void Battery::SaadcInit() {
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
nrfx_saadc_config_t adcConfig;
adcConfig.low_power_mode = true;
adcConfig.resolution = NRF_SAADC_RESOLUTION_14BIT;
adcConfig.oversample = NRF_SAADC_OVERSAMPLE_256X;
adcConfig.interrupt_priority = APP_IRQ_PRIORITY_LOW;
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic));

nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
Expand Down Expand Up @@ -72,22 +76,18 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
// ADC gain is 1/4
// thus adc_voltage = battery_voltage / 2 * gain = battery_voltage / 8
// reference_voltage is 600mV
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * (2^(sampling res) )
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 16384;

uint8_t newPercent = 100;
if (!isFull) {
// max. voltage while charging is higher than when discharging
newPercent = std::min(approx.GetValue(voltage), isCharging ? uint8_t {99} : uint8_t {100});
}

if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) {
firstMeasurement = false;
percentRemaining = newPercent;
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);
}
percentRemaining = newPercent;
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);

nrfx_saadc_uninit();
isReading = false;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/components/battery/BatteryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace Pinetime {
bool isFull = false;
bool isCharging = false;
bool isPowerPresent = false;
bool firstMeasurement = true;

void SaadcInit();

Expand Down
2 changes: 1 addition & 1 deletion src/systemtask/SystemTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace Pinetime {
void GoToRunning();
void UpdateMotion();
bool stepCounterMustBeReset = false;
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(60 * 1000);

SystemMonitor monitor;
};
Expand Down

0 comments on commit 7254df6

Please sign in to comment.