diff options
author | Joel Spadin <joelspadin@gmail.com> | 2021-04-03 01:00:51 -0500 |
---|---|---|
committer | Nick Winans <nick@winans.codes> | 2021-07-24 23:39:13 -0500 |
commit | a6de43e665ded4831d79159495660ac3a7d48e99 (patch) | |
tree | e2a3c4d4b79dd2e18723a564c29f7eb07b169daf /app/drivers/sensor/battery/battery_common.c | |
parent | 824d605c2218ff01ece4391711d69f623fcc75c0 (diff) |
feat: Add nrf VDDH battery driver
Added a driver which uses the nRF52's ADC channel on the VDDH pin to
read the battery voltage when using high voltage mode.
Diffstat (limited to 'app/drivers/sensor/battery/battery_common.c')
-rw-r--r-- | app/drivers/sensor/battery/battery_common.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/drivers/sensor/battery/battery_common.c b/app/drivers/sensor/battery/battery_common.c new file mode 100644 index 0000000..36e98af --- /dev/null +++ b/app/drivers/sensor/battery/battery_common.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include <errno.h> +#include <drivers/sensor.h> + +#include "battery_common.h" + +int battery_channel_get(const struct battery_value *value, enum sensor_channel chan, + struct sensor_value *val_out) { + switch (chan) { + case SENSOR_CHAN_GAUGE_VOLTAGE: + val_out->val1 = value->millivolts / 1000; + val_out->val2 = (value->millivolts % 1000) * 1000U; + break; + + case SENSOR_CHAN_GAUGE_STATE_OF_CHARGE: + val_out->val1 = value->state_of_charge; + val_out->val2 = 0; + break; + + default: + return -ENOTSUP; + } + + return 0; +} + +uint8_t lithium_ion_mv_to_pct(int16_t bat_mv) { + // Simple linear approximation of a battery based off adafruit's discharge graph: + // https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages + + if (bat_mv >= 4200) { + return 100; + } else if (bat_mv <= 3450) { + return 0; + } + + return bat_mv * 2 / 15 - 459; +}
\ No newline at end of file |