From 3bcb1209e6814e54c4fc28c9b6461a68cc66eae6 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 14 Nov 2022 19:52:25 +1100 Subject: [PATCH] working with const --- src/main.rs | 305 +++++++++++++++++++++++++++------------------------- 1 file changed, 160 insertions(+), 145 deletions(-) diff --git a/src/main.rs b/src/main.rs index f77d5e9..25961af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #![allow(incomplete_features)] +#![allow(dead_code)] #![feature(adt_const_params)] #![feature(generic_arg_infer)] +#![feature(generic_const_exprs)] use std::{path::Path, process::Command}; @@ -90,108 +92,73 @@ struct MpptRam { #[derive(Debug)] struct MpptEeprom { - ev_absorp: Datapoint::Voltage, - ev_float: Datapoint::Voltage, - et_absorp: Datapoint::Raw, - et_absorp_ext: Datapoint::Raw, - ev_absorp_ext: Datapoint::Voltage, - ev_float_cancel: Datapoint::Voltage, - et_float_exit_cum: Datapoint::Raw, - ev_eq: Datapoint::Voltage, - et_eqcalendar: Datapoint::Raw, - et_eq_above: Datapoint::Raw, - et_eq_reg: Datapoint::Raw, - et_batt_service: Datapoint::Raw, - ev_tempcomp: Datapoint::Tempcomp, - ev_hvd: Datapoint::Voltage, - ev_hvr: Datapoint::Voltage, - evb_ref_lim: Datapoint::Voltage, - etb_max: Datapoint::Raw, - etb_min: Datapoint::Raw, - ev_soc_g_gy: Datapoint::Voltage, - ev_soc_gy_y: Datapoint::Voltage, - ev_soc_y_yr: Datapoint::Voltage, - ev_soc_yr_r: Datapoint::Voltage, - emodbus_id: Datapoint::Raw, - emeterbus_id: Datapoint::Raw, - eib_lim: Datapoint::Current, - eva_ref_fixed_init: Datapoint::Voltage, - eva_ref_fixed_pct_init: Datapoint::VoltagePercentage, + ev_absorp: Datapoint<{ Datatype::Voltage }>, + ev_float: Datapoint<{ Datatype::Voltage }>, + et_absorp: Datapoint<{ Datatype::Raw }>, + et_absorp_ext: Datapoint<{ Datatype::Raw }>, + ev_absorp_ext: Datapoint<{ Datatype::Voltage }>, + ev_float_cancel: Datapoint<{ Datatype::Voltage }>, + et_float_exit_cum: Datapoint<{ Datatype::Raw }>, + ev_eq: Datapoint<{ Datatype::Voltage }>, + et_eqcalendar: Datapoint<{ Datatype::Raw }>, + et_eq_above: Datapoint<{ Datatype::Raw }>, + et_eq_reg: Datapoint<{ Datatype::Raw }>, + et_batt_service: Datapoint<{ Datatype::Raw }>, + ev_tempcomp: Datapoint<{ Datatype::Tempcomp }>, + ev_hvd: Datapoint<{ Datatype::Voltage }>, + ev_hvr: Datapoint<{ Datatype::Voltage }>, + evb_ref_lim: Datapoint<{ Datatype::Voltage }>, + etb_max: Datapoint<{ Datatype::Raw }>, + etb_min: Datapoint<{ Datatype::Raw }>, + ev_soc_g_gy: Datapoint<{ Datatype::Voltage }>, + ev_soc_gy_y: Datapoint<{ Datatype::Voltage }>, + ev_soc_y_yr: Datapoint<{ Datatype::Voltage }>, + ev_soc_yr_r: Datapoint<{ Datatype::Voltage }>, + emodbus_id: Datapoint<{ Datatype::Raw }>, + emeterbus_id: Datapoint<{ Datatype::Raw }>, + eib_lim: Datapoint<{ Datatype::Current }>, + eva_ref_fixed_init: Datapoint<{ Datatype::Voltage }>, + 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)] -enum Datapoint { - Voltage { data: u16 }, - VoltagePercentage { data: u16 }, - Tempcomp { data: u16 }, - Current { data: u16 }, - Raw { data: u16 }, +enum Datatype { + Voltage, + VoltagePercentage, + Tempcomp, + Current, + Raw, } -// #[derive(Debug)] -// struct Datapoint { -// data: u16, -// } +#[derive(Debug)] +struct Datapoint { + data: u16, +} -// #[derive(Serialize, Deserialize, Debug)] -// struct Datapoint { -// datatype: Datatype, -// data: u16, -// } - -impl Datapoint { - // fn get_type(&self) -> Datatype { - // t - // } +impl Datapoint { + fn get_type(&self) -> Datatype { + T + } fn get_scaled(&self, info: &Info) -> f32 { - match self { - Datapoint::Voltage { data } => data as f32 * info.v_scale * f32::powf(2., -15.), - Datapoint::VoltagePercentage { data } => data as f32 * 100. * f32::powf(2., -16.), - Datapoint::Tempcomp { data } => data as f32 * info.v_scale * f32::powf(2., -16.), - Datapoint::Current { data } => data as f32 * info.i_scale * f32::powf(2., -15.), - Datapoint::Raw { data } => data as f32, + match T { + Datatype::Voltage => self.data as f32 * info.v_scale * f32::powf(2., -15.), + Datatype::VoltagePercentage => self.data as f32 * 100. * f32::powf(2., -16.), + Datatype::Tempcomp => self.data as f32 * info.v_scale * f32::powf(2., -16.), + Datatype::Current => self.data as f32 * info.i_scale * f32::powf(2., -15.), + Datatype::Raw => self.data as f32, } } - // fn get_val(input: f32, info: &Info) -> u16 { - // match dt { - // Datatype::Voltage => ((input / f32::powf(2., -15.)) / info.v_scale) 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::Current => ((input / f32::powf(2., -15.)) / info.i_scale) as u16, - // Datatype::Raw => input as u16, - // } - // } + fn get_val(dt: Datatype, input: f32, info: &Info) -> u16 { + match dt { + Datatype::Voltage => ((input / f32::powf(2., -15.)) / info.v_scale) 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::Current => ((input / f32::powf(2., -15.)) / info.i_scale) as u16, + Datatype::Raw => input as u16, + } + } fn from_u16(d: u16) -> Self { Self { data: d } @@ -312,26 +279,38 @@ fn main() { match args.command { Some(Commands::Get { name }) => { - println!( - "{}: {:#?}", - name.to_lowercase(), - match_datapoint(name.as_str(), &eeprom_data).get_scaled(&Info { - v_scale: 1., - i_scale: 1. - }) - ); - return; - } - Some(Commands::Set { name }) => { - println!("set var {}", name); - let c = match_datapoint(name.as_str(), &eeprom_data); - c.new_same( - "2", + // println!( + // "{}: {:#?}", + // name.to_lowercase(), + // match_datapoint(name.as_str(), &eeprom_data).get_scaled(&Info { + // v_scale: 1., + // i_scale: 1. + // }) + // ); + let t = match_datapoint_type( + name.as_str(), + &eeprom_data, &Info { v_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; } None => { @@ -460,10 +439,10 @@ fn main() { match args.command { Some(Commands::Get { name }) => { println!("get var {}", name); - println!( - "pp: {:#?}", - match_datapoint(name.as_str(), &eeprom_data).get_scaled(&info) - ); + // println!( + // "pp: {:#?}", + // match_datapoint(name.as_str(), &eeprom_data).get_scaled(&info) + // ); return; } Some(Commands::Set { name }) => { @@ -483,43 +462,48 @@ fn main() { // .expect("could not set value"); } -fn match_datapoint(name: &str, data: &MpptEeprom) -> Datapoint<{ Datatype }> { - // let a = data.ev_absorp; - match name.to_lowercase().as_str() { - "ev_absorp" => data.ev_absorp, - "ev_float" => data.ev_float, - "et_absorp" => data.et_absorp, - "et_absorp_ext" => data.et_absorp_ext, - "ev_absorp_ext" => data.ev_absorp_ext, - "ev_float_cancel" => data.ev_float_cancel, - "et_float_exit_cum" => data.et_float_exit_cum, - "ev_eq" => data.ev_eq, - "et_eqcalendar" => data.et_eqcalendar, - "et_eq_above" => data.et_eq_above, - "et_eq_reg" => data.et_eq_reg, - "et_batt_service" => data.et_batt_service, - "ev_tempcomp" => data.ev_tempcomp, - "ev_hvd" => data.ev_hvd, - "ev_hvr" => data.ev_hvr, - "evb_ref_lim" => data.evb_ref_lim, - "etb_max" => data.etb_max, - "etb_min" => data.etb_min, - "ev_soc_g_gy" => data.ev_soc_g_gy, - "ev_soc_gy_y" => data.ev_soc_gy_y, - "ev_soc_y_yr" => data.ev_soc_y_yr, - "ev_soc_yr_r" => data.ev_soc_yr_r, - "emodbus_id" => data.emodbus_id, - "emeterbus_id" => data.emeterbus_id, - "eib_lim" => data.eib_lim, - "eva_ref_fixed_init" => data.eva_ref_fixed_init, - "eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init, - &_ => todo!(), - } +// fn match_datapoint(name: &str, data: &MpptEeprom) -> Datapoint<{ t }> { +// match name.to_lowercase().as_str() { +// "ev_absorp" => data.ev_absorp, +// "ev_float" => data.ev_float, +// "et_absorp" => data.et_absorp, +// "et_absorp_ext" => data.et_absorp_ext, +// "ev_absorp_ext" => data.ev_absorp_ext, +// "ev_float_cancel" => data.ev_float_cancel, +// "et_float_exit_cum" => data.et_float_exit_cum, +// "ev_eq" => data.ev_eq, +// "et_eqcalendar" => data.et_eqcalendar, +// "et_eq_above" => data.et_eq_above, +// "et_eq_reg" => data.et_eq_reg, +// "et_batt_service" => data.et_batt_service, +// "ev_tempcomp" => data.ev_tempcomp, +// "ev_hvd" => data.ev_hvd, +// "ev_hvr" => data.ev_hvr, +// "evb_ref_lim" => data.evb_ref_lim, +// "etb_max" => data.etb_max, +// "etb_min" => data.etb_min, +// "ev_soc_g_gy" => data.ev_soc_g_gy, +// "ev_soc_gy_y" => data.ev_soc_gy_y, +// "ev_soc_y_yr" => data.ev_soc_y_yr, +// "ev_soc_yr_r" => data.ev_soc_yr_r, +// "emodbus_id" => data.emodbus_id, +// "emeterbus_id" => data.emeterbus_id, +// "eib_lim" => data.eib_lim, +// "eva_ref_fixed_init" => data.eva_ref_fixed_init, +// "eva_ref_fixed_pct_init" => data.eva_ref_fixed_pct_init, +// &_ => 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; - match name.to_lowercase().as_str() { + let t = match name.to_lowercase().as_str() { "ev_absorp" => data.ev_absorp.get_type(), "ev_float" => data.ev_float.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_pct_init" => data.eva_ref_fixed_pct_init.get_type(), &_ => 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 } }