Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix charge_rate when discharging
Browse files Browse the repository at this point in the history
MAX17048 & MAX17049 report negative `CRATE` values when discharging, so interpret them as i16
instead of u16 before converting to f32.

Resolves #1
bencochran committed Dec 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 90c923a commit b659476
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

...
### Fixed

- MAX17048 & MAX17049 `charge_rate` correctly handles negative values (such as when discharging). See: [PR #2](https://github.com/eldruin/max170xx-rs/pull/2)

## 0.1.0 - 2020-07-19

2 changes: 1 addition & 1 deletion src/max170x8_x9.rs
Original file line number Diff line number Diff line change
@@ -100,7 +100,7 @@ macro_rules! impl_common_48_49 {
/// Get the approximate charge or discharge rate of the battery
/// in percentage / hour
pub fn charge_rate(&mut self) -> Result<f32, Error<E>> {
let rate = self.read_register(Register::CRATE)?;
let rate = self.read_register(Register::CRATE)? as i16;
Ok(f32::from(rate) * 0.208)
}
}
2 changes: 2 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -127,6 +127,7 @@ mod max17048 {
get_float!(get_soc, new_48, destroy_48, soc, SOC, 48, 126, 48.49);
get_float!(voltage, new_48, destroy_48, voltage, VCELL, 0xA4, 0x9F, 3.29);
get_float!(rate, new_48, destroy_48, charge_rate, CRATE, 1, 0x45, 67.6);
get_float!(negative_rate, new_48, destroy_48, charge_rate, CRATE, 0xFE, 0xBB, -67.6);
cmd_test!(reset, new_48, destroy_48, reset, COMMAND, POR_X8_X9);
set_table_test!(set_table, new_48, destroy_48);
}
@@ -144,6 +145,7 @@ mod max17049 {
get_float!(get_soc, new_49, destroy_49, soc, SOC, 48, 126, 48.49);
get_float!(voltage, new_49, destroy_49, voltage, VCELL, 0xA4, 0x9F, 6.58);
get_float!(rate, new_49, destroy_49, charge_rate, CRATE, 1, 0x45, 67.6);
get_float!(negative_rate, new_49, destroy_49, charge_rate, CRATE, 0xFE, 0xBB, -67.6);
cmd_test!(reset, new_49, destroy_49, reset, COMMAND, POR_X8_X9);
set_table_test!(set_table, new_49, destroy_49);
}

0 comments on commit b659476

Please sign in to comment.