made most battery code ready to work

This commit is contained in:
2024-01-22 23:13:52 +01:00
parent b933516062
commit 7ea1486e2c
2 changed files with 174 additions and 134 deletions

View File

@@ -1,3 +1,5 @@
use anyhow::bail;
use bit_field::BitField;
use embedded_hal::blocking::{i2c::{WriteRead, Write, Read}, delay::DelayMs};
use esp_idf_sys::vTaskDelay;
@@ -26,7 +28,8 @@ fn xemics_to_double(x:u32) -> f32 {
}
// Get the exponent, it's 2^(MSbyte - 0x80)
f_exponent = 2.0_f32.powf(v_msbyte.wrapping_sub(128) as f32);
f_exponent = 2.0_f32.powf(((v_msbyte as i16) - 128) as f32);
println!("f_exponent {}", f_exponent);
// Or in 0x80 to the MidHiByte
v_mid_hi_byte = (v_mid_hi_byte | 128) as u8;
// get value out of midhi byte
@@ -79,48 +82,44 @@ fn xemics_to_double(x:u32) -> f32 {
// return -fResult;
}
fn double_to_xemics(mut x:f32) -> u32 {
let i_byte1:i16;
let mut i_byte2:i16;
let i_byte3: i16;
let i_byte4: i16;
let i_exp: i16;
fn float_to_xemics(mut x: f32) -> u32 {
let mut b_negative = false;
let mut f_mantissa: f32;
// Don't blow up with logs of zero
// Vermeidung von Logarithmus von Null
if x == 0.0 {
x = 0.00001;
}
if x < 0.0
{
}
// Überprüfung auf negative Zahl
if x < 0.0 {
b_negative = true;
x = -x;
}
// find the correct exponent
i_exp = (x.log2() + 1.0) as i16;// remember - log of any base is ln(x)/ln(base)
// MS byte is the exponent + 0x80
i_byte1 = i_exp + 128;
// Divide input by this exponent to get mantissa
f_mantissa = x / (2.0_f32.powf(i_exp as f32));
// Scale it up
f_mantissa = f_mantissa / (2.0_f32.powf(-24.0));
// Split the mantissa into 3 bytes
i_byte2 = (f_mantissa / (2.0_f32.powf(16.0))) as i16;
i_byte3 = ((f_mantissa - (i_byte2 as f32 * (2.0_f32.powf(16.0)))) / (2.0_f32.powf(8.0))) as i16;
i_byte4 = (f_mantissa - (i_byte2 as f32 * (2.0_f32.powf(16.0))) - (i_byte3 as f32 * (2.0_f32.powf(8.0)))) as i16;
// subtract the sign bit if number is positive
if b_negative == false
{
i_byte2 = i_byte2 & 0x7F;
}
return (i_byte1 as u8 as u32) << 24 | (i_byte2 as u8 as u32) << 16 | (i_byte3 as u8 as u32) << 8 | i_byte4 as u8 as u32;
// Korrekten Exponenten finden
let i_exp = (x.log2().floor() + 1.0) as i32;
// MS-Byte ist der Exponent + 0x80
let i_byte1 = (i_exp + 128) as u32;
// Eingabe durch diesen Exponenten teilen, um Mantisse zu erhalten
let f_mantissa = x / 2f32.powi(i_exp);
// Skalierung
let scaled_mantissa = f_mantissa * 2f32.powi(24);
// Aufteilung der Mantisse in 3 Bytes
let i_byte2 = ((scaled_mantissa / 65536.0) as u32) & 0xFF;
let i_byte3 = ((scaled_mantissa / 256.0) as u32) & 0xFF;
let i_byte4 = (scaled_mantissa as u32) & 0xFF;
// Subtraktion des Vorzeichenbits, falls die Zahl positiv ist
let i_byte2 = if !b_negative { i_byte2 & 0x7F } else { i_byte2 };
// Zusammenbau des Ergebnisses