You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
/* |
|
* 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; |
|
} |