ad7172: perform data sign checks

This commit is contained in:
Astro 2019-10-02 21:27:35 +02:00
parent 12c2be0a03
commit 83117db8c5
2 changed files with 11 additions and 5 deletions

View File

@ -114,7 +114,7 @@ impl<SPI: Transfer<u8>, NSS: OutputPin> Adc<SPI, NSS> {
} }
/// Get data /// Get data
pub fn read_data(&mut self) -> Result<u32, AdcError<SPI::Error>> { pub fn read_data(&mut self) -> Result<i32, AdcError<SPI::Error>> {
self.read_reg(&regs::Data) self.read_reg(&regs::Data)
.map(|data| data.data()) .map(|data| data.data())
} }

View File

@ -145,10 +145,16 @@ impl if_mode::Data {
def_reg!(Data, data, 0x04, 3); def_reg!(Data, data, 0x04, 3);
impl data::Data { impl data::Data {
pub fn data(&self) -> u32 { pub fn data(&self) -> i32 {
(u32::from(self.0[0]) << 16) | let raw =
(u32::from(self.0[1]) << 8) | (u32::from(self.0[0]) << 16) |
u32::from(self.0[2]) (u32::from(self.0[1]) << 8) |
u32::from(self.0[2]);
if raw & 0x80_0000 != 0 {
((raw & 0x7F_FFFF) | 0x8000_0000) as i32
} else {
raw as i32
}
} }
} }