Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved battery level accuracy with updated voltage-to-percentage calculation #7929

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,20 @@ export function getKey<T>(object: {[s: string]: T} | {[s: number]: T}, value: T,

export function batteryVoltageToPercentage(voltage: number, option: BatteryNonLinearVoltage | BatteryLinearVoltage): number {
if (option === '3V_2100') {
let percentage: number = 100; // >= 3000

if (voltage < 2100) {
let percentage: number;

if (voltage >= 3200) {
percentage = 100;
} else if (voltage >= 3000) {
percentage = 90 + ((voltage - 3000) * (100 - 90)) / (3200 - 3000);
} else if (voltage >= 2900) {
percentage = 70 + ((voltage - 2900) * (90 - 70)) / (3000 - 2900);
} else if (voltage >= 2800) {
percentage = 40 + ((voltage - 2800) * (70 - 40)) / (2900 - 2800);
} else if (voltage >= 2700) {
percentage = 10 + ((voltage - 2700) * (40 - 10)) / (2800 - 2700);
} else {
percentage = 0;
} else if (voltage < 2440) {
percentage = 6 - ((2440 - voltage) * 6) / 340;
} else if (voltage < 2740) {
percentage = 18 - ((2740 - voltage) * 12) / 300;
} else if (voltage < 2900) {
percentage = 42 - ((2900 - voltage) * 24) / 160;
} else if (voltage < 3000) {
percentage = 100 - ((3000 - voltage) * 58) / 100;
}

return Math.round(percentage);
Expand Down
8 changes: 6 additions & 2 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ describe('lib/utils.js', () => {
});

it('gets non-linear', () => {
expect(utils.batteryVoltageToPercentage(2300, '3V_2100')).toStrictEqual(4);
expect(utils.batteryVoltageToPercentage(3000, '3V_2100')).toStrictEqual(100);
expect(utils.batteryVoltageToPercentage(2600, '3V_2100')).toStrictEqual(0);
expect(utils.batteryVoltageToPercentage(2700, '3V_2100')).toStrictEqual(10);
expect(utils.batteryVoltageToPercentage(2800, '3V_2100')).toStrictEqual(40);
expect(utils.batteryVoltageToPercentage(2900, '3V_2100')).toStrictEqual(70);
expect(utils.batteryVoltageToPercentage(3000, '3V_2100')).toStrictEqual(90);
expect(utils.batteryVoltageToPercentage(3200, '3V_2100')).toStrictEqual(100);
expect(utils.batteryVoltageToPercentage(2000, '3V_2100')).toStrictEqual(0);

expect(utils.batteryVoltageToPercentage(2300, '3V_1500_2800')).toStrictEqual(74);
Expand Down
Loading