epic fail

This commit is contained in:
Alex Janka 2022-11-14 17:50:52 +11:00
parent 58f64c1501
commit a344adc26d

View file

@ -90,33 +90,33 @@ struct MpptRam {
#[derive(Debug)] #[derive(Debug)]
struct MpptEeprom { struct MpptEeprom {
ev_absorp: Datapoint<{ Datatype::Voltage }>, ev_absorp: Datapoint::Voltage,
ev_float: Datapoint<{ Datatype::Voltage }>, ev_float: Datapoint::Voltage,
et_absorp: Datapoint<{ Datatype::Raw }>, et_absorp: Datapoint::Raw,
et_absorp_ext: Datapoint<{ Datatype::Raw }>, et_absorp_ext: Datapoint::Raw,
ev_absorp_ext: Datapoint<{ Datatype::Voltage }>, ev_absorp_ext: Datapoint::Voltage,
ev_float_cancel: Datapoint<{ Datatype::Voltage }>, ev_float_cancel: Datapoint::Voltage,
et_float_exit_cum: Datapoint<{ Datatype::Raw }>, et_float_exit_cum: Datapoint::Raw,
ev_eq: Datapoint<{ Datatype::Voltage }>, ev_eq: Datapoint::Voltage,
et_eqcalendar: Datapoint<{ Datatype::Raw }>, et_eqcalendar: Datapoint::Raw,
et_eq_above: Datapoint<{ Datatype::Raw }>, et_eq_above: Datapoint::Raw,
et_eq_reg: Datapoint<{ Datatype::Raw }>, et_eq_reg: Datapoint::Raw,
et_batt_service: Datapoint<{ Datatype::Raw }>, et_batt_service: Datapoint::Raw,
ev_tempcomp: Datapoint<{ Datatype::Tempcomp }>, ev_tempcomp: Datapoint::Tempcomp,
ev_hvd: Datapoint<{ Datatype::Voltage }>, ev_hvd: Datapoint::Voltage,
ev_hvr: Datapoint<{ Datatype::Voltage }>, ev_hvr: Datapoint::Voltage,
evb_ref_lim: Datapoint<{ Datatype::Voltage }>, evb_ref_lim: Datapoint::Voltage,
etb_max: Datapoint<{ Datatype::Raw }>, etb_max: Datapoint::Raw,
etb_min: Datapoint<{ Datatype::Raw }>, etb_min: Datapoint::Raw,
ev_soc_g_gy: Datapoint<{ Datatype::Voltage }>, ev_soc_g_gy: Datapoint::Voltage,
ev_soc_gy_y: Datapoint<{ Datatype::Voltage }>, ev_soc_gy_y: Datapoint::Voltage,
ev_soc_y_yr: Datapoint<{ Datatype::Voltage }>, ev_soc_y_yr: Datapoint::Voltage,
ev_soc_yr_r: Datapoint<{ Datatype::Voltage }>, ev_soc_yr_r: Datapoint::Voltage,
emodbus_id: Datapoint<{ Datatype::Raw }>, emodbus_id: Datapoint::Raw,
emeterbus_id: Datapoint<{ Datatype::Raw }>, emeterbus_id: Datapoint::Raw,
eib_lim: Datapoint<{ Datatype::Current }>, eib_lim: Datapoint::Current,
eva_ref_fixed_init: Datapoint<{ Datatype::Voltage }>, eva_ref_fixed_init: Datapoint::Voltage,
eva_ref_fixed_pct_init: Datapoint<{ Datatype::VoltagePercentage }>, eva_ref_fixed_pct_init: Datapoint::VoltagePercentage,
} }
// struct MpptEeprom { // struct MpptEeprom {
// ev_absorp: Datatype::Voltage, // ev_absorp: Datatype::Voltage,
@ -149,18 +149,18 @@ struct MpptEeprom {
// } // }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
enum Datatype { enum Datapoint {
Voltage, Voltage { data: u16 },
VoltagePercentage, VoltagePercentage { data: u16 },
Tempcomp, Tempcomp { data: u16 },
Current, Current { data: u16 },
Raw, Raw { data: u16 },
} }
#[derive(Debug)] // #[derive(Debug)]
struct Datapoint<const t: Datatype> { // struct Datapoint<const t: Datatype> {
data: u16, // data: u16,
} // }
// #[derive(Serialize, Deserialize, Debug)] // #[derive(Serialize, Deserialize, Debug)]
// struct Datapoint { // struct Datapoint {
@ -168,30 +168,30 @@ struct Datapoint<const t: Datatype> {
// data: u16, // data: u16,
// } // }
impl<const t: Datatype> Datapoint<t> { impl Datapoint {
fn get_type(&self) -> Datatype { // fn get_type(&self) -> Datatype {
t // t
} // }
fn get_scaled(&self, info: &Info) -> f32 { fn get_scaled(&self, info: &Info) -> f32 {
match t { match self {
Datatype::Voltage => self.data as f32 * info.v_scale * f32::powf(2., -15.), Datapoint::Voltage { data } => data as f32 * info.v_scale * f32::powf(2., -15.),
Datatype::VoltagePercentage => self.data as f32 * 100. * f32::powf(2., -16.), Datapoint::VoltagePercentage { data } => data as f32 * 100. * f32::powf(2., -16.),
Datatype::Tempcomp => self.data as f32 * info.v_scale * f32::powf(2., -16.), Datapoint::Tempcomp { data } => data as f32 * info.v_scale * f32::powf(2., -16.),
Datatype::Current => self.data as f32 * info.i_scale * f32::powf(2., -15.), Datapoint::Current { data } => data as f32 * info.i_scale * f32::powf(2., -15.),
Datatype::Raw => self.data as f32, Datapoint::Raw { data } => data as f32,
} }
} }
fn get_val(dt: Datatype, input: f32, info: &Info) -> u16 { // fn get_val(input: f32, info: &Info) -> u16 {
match dt { // match dt {
Datatype::Voltage => ((input / f32::powf(2., -15.)) / info.v_scale) as u16, // Datatype::Voltage => ((input / f32::powf(2., -15.)) / info.v_scale) as u16,
Datatype::VoltagePercentage => ((input / f32::powf(2., -16.)) / 100.) as u16, // Datatype::VoltagePercentage => ((input / f32::powf(2., -16.)) / 100.) as u16,
Datatype::Tempcomp => ((input / f32::powf(2., -16.)) / info.v_scale) as u16, // Datatype::Tempcomp => ((input / f32::powf(2., -16.)) / info.v_scale) as u16,
Datatype::Current => ((input / f32::powf(2., -15.)) / info.i_scale) as u16, // Datatype::Current => ((input / f32::powf(2., -15.)) / info.i_scale) as u16,
Datatype::Raw => input as u16, // Datatype::Raw => input as u16,
} // }
} // }
fn from_u16(d: u16) -> Self { fn from_u16(d: u16) -> Self {
Self { data: d } Self { data: d }