working with const

This commit is contained in:
Alex Janka 2022-11-14 19:52:25 +11:00
parent a344adc26d
commit 3bcb1209e6

View file

@ -1,6 +1,8 @@
#![allow(incomplete_features)] #![allow(incomplete_features)]
#![allow(dead_code)]
#![feature(adt_const_params)] #![feature(adt_const_params)]
#![feature(generic_arg_infer)] #![feature(generic_arg_infer)]
#![feature(generic_const_exprs)]
use std::{path::Path, process::Command}; use std::{path::Path, process::Command};
@ -90,108 +92,73 @@ struct MpptRam {
#[derive(Debug)] #[derive(Debug)]
struct MpptEeprom { struct MpptEeprom {
ev_absorp: Datapoint::Voltage, ev_absorp: Datapoint<{ Datatype::Voltage }>,
ev_float: Datapoint::Voltage, ev_float: Datapoint<{ Datatype::Voltage }>,
et_absorp: Datapoint::Raw, et_absorp: Datapoint<{ Datatype::Raw }>,
et_absorp_ext: Datapoint::Raw, et_absorp_ext: Datapoint<{ Datatype::Raw }>,
ev_absorp_ext: Datapoint::Voltage, ev_absorp_ext: Datapoint<{ Datatype::Voltage }>,
ev_float_cancel: Datapoint::Voltage, ev_float_cancel: Datapoint<{ Datatype::Voltage }>,
et_float_exit_cum: Datapoint::Raw, et_float_exit_cum: Datapoint<{ Datatype::Raw }>,
ev_eq: Datapoint::Voltage, ev_eq: Datapoint<{ Datatype::Voltage }>,
et_eqcalendar: Datapoint::Raw, et_eqcalendar: Datapoint<{ Datatype::Raw }>,
et_eq_above: Datapoint::Raw, et_eq_above: Datapoint<{ Datatype::Raw }>,
et_eq_reg: Datapoint::Raw, et_eq_reg: Datapoint<{ Datatype::Raw }>,
et_batt_service: Datapoint::Raw, et_batt_service: Datapoint<{ Datatype::Raw }>,
ev_tempcomp: Datapoint::Tempcomp, ev_tempcomp: Datapoint<{ Datatype::Tempcomp }>,
ev_hvd: Datapoint::Voltage, ev_hvd: Datapoint<{ Datatype::Voltage }>,
ev_hvr: Datapoint::Voltage, ev_hvr: Datapoint<{ Datatype::Voltage }>,
evb_ref_lim: Datapoint::Voltage, evb_ref_lim: Datapoint<{ Datatype::Voltage }>,
etb_max: Datapoint::Raw, etb_max: Datapoint<{ Datatype::Raw }>,
etb_min: Datapoint::Raw, etb_min: Datapoint<{ Datatype::Raw }>,
ev_soc_g_gy: Datapoint::Voltage, ev_soc_g_gy: Datapoint<{ Datatype::Voltage }>,
ev_soc_gy_y: Datapoint::Voltage, ev_soc_gy_y: Datapoint<{ Datatype::Voltage }>,
ev_soc_y_yr: Datapoint::Voltage, ev_soc_y_yr: Datapoint<{ Datatype::Voltage }>,
ev_soc_yr_r: Datapoint::Voltage, ev_soc_yr_r: Datapoint<{ Datatype::Voltage }>,
emodbus_id: Datapoint::Raw, emodbus_id: Datapoint<{ Datatype::Raw }>,
emeterbus_id: Datapoint::Raw, emeterbus_id: Datapoint<{ Datatype::Raw }>,
eib_lim: Datapoint::Current, eib_lim: Datapoint<{ Datatype::Current }>,
eva_ref_fixed_init: Datapoint::Voltage, eva_ref_fixed_init: Datapoint<{ Datatype::Voltage }>,
eva_ref_fixed_pct_init: Datapoint::VoltagePercentage, eva_ref_fixed_pct_init: Datapoint<{ Datatype::VoltagePercentage }>,
} }
// struct MpptEeprom {
// ev_absorp: Datatype::Voltage,
// ev_float: Datatype::Voltage,
// et_absorp: Datatype::Raw,
// et_absorp_ext: Datatype::Raw,
// ev_absorp_ext: Datatype::Voltage,
// ev_float_cancel: Datatype::Voltage,
// et_float_exit_cum: Datatype::Raw,
// ev_eq: Datatype::Voltage,
// et_eqcalendar: Datatype::Raw,
// et_eq_above: Datatype::Raw,
// et_eq_reg: Datatype::Raw,
// et_batt_service: Datatype::Raw,
// ev_tempcomp: Datatype::Tempcomp,
// ev_hvd: Datatype::Voltage,
// ev_hvr: Datatype::Voltage,
// evb_ref_lim: Datatype::Voltage,
// etb_max: Datatype::Raw,
// etb_min: Datatype::Raw,
// ev_soc_g_gy: Datatype::Voltage,
// ev_soc_gy_y: Datatype::Voltage,
// ev_soc_y_yr: Datatype::Voltage,
// ev_soc_yr_r: Datatype::Voltage,
// emodbus_id: Datatype::Raw,
// emeterbus_id: Datatype::Raw,
// eib_lim: Datatype::Current,
// eva_ref_fixed_init: Datatype::Voltage,
// eva_ref_fixed_pct_init: Datatype::VoltagePercentage,
// }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
enum Datapoint { enum Datatype {
Voltage { data: u16 }, Voltage,
VoltagePercentage { data: u16 }, VoltagePercentage,
Tempcomp { data: u16 }, Tempcomp,
Current { data: u16 }, Current,
Raw { data: u16 }, Raw,
} }
// #[derive(Debug)] #[derive(Debug)]
// struct Datapoint<const t: Datatype> { struct Datapoint<const T: Datatype> {
// data: u16, data: u16,
// } }
// #[derive(Serialize, Deserialize, Debug)] impl<const T: Datatype> Datapoint<T> {
// struct Datapoint { fn get_type(&self) -> Datatype {
// datatype: Datatype, T
// data: u16, }
// }
impl Datapoint {
// fn get_type(&self) -> Datatype {
// t
// }
fn get_scaled(&self, info: &Info) -> f32 { fn get_scaled(&self, info: &Info) -> f32 {
match self { match T {
Datapoint::Voltage { data } => data as f32 * info.v_scale * f32::powf(2., -15.), Datatype::Voltage => self.data as f32 * info.v_scale * f32::powf(2., -15.),
Datapoint::VoltagePercentage { data } => data as f32 * 100. * f32::powf(2., -16.), Datatype::VoltagePercentage => self.data as f32 * 100. * f32::powf(2., -16.),
Datapoint::Tempcomp { data } => data as f32 * info.v_scale * f32::powf(2., -16.), Datatype::Tempcomp => self.data as f32 * info.v_scale * f32::powf(2., -16.),
Datapoint::Current { data } => data as f32 * info.i_scale * f32::powf(2., -15.), Datatype::Current => self.data as f32 * info.i_scale * f32::powf(2., -15.),
Datapoint::Raw { data } => data as f32, Datatype::Raw => self.data as f32,
} }
} }
// fn get_val(input: f32, info: &Info) -> u16 { fn get_val(dt: Datatype, 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 }
@ -312,26 +279,38 @@ fn main() {
match args.command { match args.command {
Some(Commands::Get { name }) => { Some(Commands::Get { name }) => {
println!( // println!(
"{}: {:#?}", // "{}: {:#?}",
name.to_lowercase(), // name.to_lowercase(),
match_datapoint(name.as_str(), &eeprom_data).get_scaled(&Info { // match_datapoint(name.as_str(), &eeprom_data).get_scaled(&Info {
v_scale: 1., // v_scale: 1.,
i_scale: 1. // i_scale: 1.
}) // })
); // );
return; let t = match_datapoint_type(
} name.as_str(),
Some(Commands::Set { name }) => { &eeprom_data,
println!("set var {}", name);
let c = match_datapoint(name.as_str(), &eeprom_data);
c.new_same(
"2",
&Info { &Info {
v_scale: 1., v_scale: 1.,
i_scale: 1., i_scale: 1.,
}, },
); );
println!("{}: {} - {:?}", name, t.val, t.dt);
return;
}
Some(Commands::Set { name }) => {
println!("set var {}", name);
let t = match_datapoint_type(
name.as_str(),
&eeprom_data,
&Info {
v_scale: 1.,
i_scale: 1.,
},
);
println!("type: {:?}", t);
// let new: Datapoint<{ t }> = Datapoint::from_u16(0x0000);
// let b: Datapoint<_> = Datapoint::<{ t }>::from_u16(0x0000);
return; return;
} }
None => { None => {
@ -460,10 +439,10 @@ fn main() {
match args.command { match args.command {
Some(Commands::Get { name }) => { Some(Commands::Get { name }) => {
println!("get var {}", name); println!("get var {}", name);
println!( // println!(
"pp: {:#?}", // "pp: {:#?}",
match_datapoint(name.as_str(), &eeprom_data).get_scaled(&info) // match_datapoint(name.as_str(), &eeprom_data).get_scaled(&info)
); // );
return; return;
} }
Some(Commands::Set { name }) => { Some(Commands::Set { name }) => {
@ -483,43 +462,48 @@ fn main() {
// .expect("could not set value"); // .expect("could not set value");
} }
fn match_datapoint<const t: Datatype>(name: &str, data: &MpptEeprom) -> Datapoint<{ Datatype }> { // fn match_datapoint(name: &str, data: &MpptEeprom) -> Datapoint<{ t }> {
// let a = data.ev_absorp; // match name.to_lowercase().as_str() {
match name.to_lowercase().as_str() { // "ev_absorp" => data.ev_absorp,
"ev_absorp" => data.ev_absorp, // "ev_float" => data.ev_float,
"ev_float" => data.ev_float, // "et_absorp" => data.et_absorp,
"et_absorp" => data.et_absorp, // "et_absorp_ext" => data.et_absorp_ext,
"et_absorp_ext" => data.et_absorp_ext, // "ev_absorp_ext" => data.ev_absorp_ext,
"ev_absorp_ext" => data.ev_absorp_ext, // "ev_float_cancel" => data.ev_float_cancel,
"ev_float_cancel" => data.ev_float_cancel, // "et_float_exit_cum" => data.et_float_exit_cum,
"et_float_exit_cum" => data.et_float_exit_cum, // "ev_eq" => data.ev_eq,
"ev_eq" => data.ev_eq, // "et_eqcalendar" => data.et_eqcalendar,
"et_eqcalendar" => data.et_eqcalendar, // "et_eq_above" => data.et_eq_above,
"et_eq_above" => data.et_eq_above, // "et_eq_reg" => data.et_eq_reg,
"et_eq_reg" => data.et_eq_reg, // "et_batt_service" => data.et_batt_service,
"et_batt_service" => data.et_batt_service, // "ev_tempcomp" => data.ev_tempcomp,
"ev_tempcomp" => data.ev_tempcomp, // "ev_hvd" => data.ev_hvd,
"ev_hvd" => data.ev_hvd, // "ev_hvr" => data.ev_hvr,
"ev_hvr" => data.ev_hvr, // "evb_ref_lim" => data.evb_ref_lim,
"evb_ref_lim" => data.evb_ref_lim, // "etb_max" => data.etb_max,
"etb_max" => data.etb_max, // "etb_min" => data.etb_min,
"etb_min" => data.etb_min, // "ev_soc_g_gy" => data.ev_soc_g_gy,
"ev_soc_g_gy" => data.ev_soc_g_gy, // "ev_soc_gy_y" => data.ev_soc_gy_y,
"ev_soc_gy_y" => data.ev_soc_gy_y, // "ev_soc_y_yr" => data.ev_soc_y_yr,
"ev_soc_y_yr" => data.ev_soc_y_yr, // "ev_soc_yr_r" => data.ev_soc_yr_r,
"ev_soc_yr_r" => data.ev_soc_yr_r, // "emodbus_id" => data.emodbus_id,
"emodbus_id" => data.emodbus_id, // "emeterbus_id" => data.emeterbus_id,
"emeterbus_id" => data.emeterbus_id, // "eib_lim" => data.eib_lim,
"eib_lim" => data.eib_lim, // "eva_ref_fixed_init" => data.eva_ref_fixed_init,
"eva_ref_fixed_init" => data.eva_ref_fixed_init, // "eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init,
"eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init, // &_ => todo!(),
&_ => todo!(), // }
} // }
#[derive(Debug)]
struct DatapointDisplay {
dt: Datatype,
val: f32,
} }
fn match_datapoint_type(name: &str, data: &MpptEeprom) -> Datatype { fn match_datapoint_type(name: &str, data: &MpptEeprom, info: &Info) -> DatapointDisplay {
// let a = data.ev_absorp; // let a = data.ev_absorp;
match name.to_lowercase().as_str() { let t = match name.to_lowercase().as_str() {
"ev_absorp" => data.ev_absorp.get_type(), "ev_absorp" => data.ev_absorp.get_type(),
"ev_float" => data.ev_float.get_type(), "ev_float" => data.ev_float.get_type(),
"et_absorp" => data.et_absorp.get_type(), "et_absorp" => data.et_absorp.get_type(),
@ -548,5 +532,36 @@ fn match_datapoint_type(name: &str, data: &MpptEeprom) -> Datatype {
"eva_ref_fixed_init" => data.eva_ref_fixed_init.get_type(), "eva_ref_fixed_init" => data.eva_ref_fixed_init.get_type(),
"eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init.get_type(), "eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init.get_type(),
&_ => todo!(), &_ => todo!(),
} };
let v = match name.to_lowercase().as_str() {
"ev_absorp" => data.ev_absorp.get_scaled(info),
"ev_float" => data.ev_float.get_scaled(info),
"et_absorp" => data.et_absorp.get_scaled(info),
"et_absorp_ext" => data.et_absorp_ext.get_scaled(info),
"ev_absorp_ext" => data.ev_absorp_ext.get_scaled(info),
"ev_float_cancel" => data.ev_float_cancel.get_scaled(info),
"et_float_exit_cum" => data.et_float_exit_cum.get_scaled(info),
"ev_eq" => data.ev_eq.get_scaled(info),
"et_eqcalendar" => data.et_eqcalendar.get_scaled(info),
"et_eq_above" => data.et_eq_above.get_scaled(info),
"et_eq_reg" => data.et_eq_reg.get_scaled(info),
"et_batt_service" => data.et_batt_service.get_scaled(info),
"ev_tempcomp" => data.ev_tempcomp.get_scaled(info),
"ev_hvd" => data.ev_hvd.get_scaled(info),
"ev_hvr" => data.ev_hvr.get_scaled(info),
"evb_ref_lim" => data.evb_ref_lim.get_scaled(info),
"etb_max" => data.etb_max.get_scaled(info),
"etb_min" => data.etb_min.get_scaled(info),
"ev_soc_g_gy" => data.ev_soc_g_gy.get_scaled(info),
"ev_soc_gy_y" => data.ev_soc_gy_y.get_scaled(info),
"ev_soc_y_yr" => data.ev_soc_y_yr.get_scaled(info),
"ev_soc_yr_r" => data.ev_soc_yr_r.get_scaled(info),
"emodbus_id" => data.emodbus_id.get_scaled(info),
"emeterbus_id" => data.emeterbus_id.get_scaled(info),
"eib_lim" => data.eib_lim.get_scaled(info),
"eva_ref_fixed_init" => data.eva_ref_fixed_init.get_scaled(info),
"eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init.get_scaled(info),
&_ => todo!(),
};
DatapointDisplay { dt: t, val: v }
} }