changing types

This commit is contained in:
Alex Janka 2022-09-09 16:32:21 +10:00
parent 9a1d1c962f
commit 1a8bf2e57c
3 changed files with 136 additions and 105 deletions

7
Cargo.lock generated
View file

@ -389,7 +389,6 @@ dependencies = [
"clap 3.2.20", "clap 3.2.20",
"libmodbus-rs", "libmodbus-rs",
"serde", "serde",
"sum_type",
] ]
[[package]] [[package]]
@ -579,12 +578,6 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "sum_type"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da5b4a0c9f3c7c8e891e445a7c776627e208e8bba23ab680798066dd283e6a15"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.99" version = "1.0.99"

View file

@ -9,5 +9,4 @@ edition = "2021"
libmodbus-rs = "0.8.3" libmodbus-rs = "0.8.3"
clap = { version = "3.2.20", features = ["derive"] } clap = { version = "3.2.20", features = ["derive"] }
bincode = "1.3" bincode = "1.3"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
sum_type = "0.2"

View file

@ -3,7 +3,6 @@ use std::{path::Path, process::Command};
use clap::{AppSettings, Parser, Subcommand}; use clap::{AppSettings, Parser, Subcommand};
use libmodbus_rs::{Modbus, ModbusClient, ModbusRTU}; use libmodbus_rs::{Modbus, ModbusClient, ModbusRTU};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sum_type::sum_type;
mod offsets; mod offsets;
use crate::offsets::{OffsetsEeprom, OffsetsRam}; use crate::offsets::{OffsetsEeprom, OffsetsRam};
@ -88,56 +87,51 @@ struct MpptRam {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct MpptEeprom { struct MpptEeprom {
EV_absorp: UnitValues, EV_absorp: Voltage,
EV_float: UnitValues, EV_float: Voltage,
Et_absorp: UnitValues, Et_absorp: Raw,
Et_absorp_ext: UnitValues, Et_absorp_ext: Raw,
EV_absorp_ext: UnitValues, EV_absorp_ext: Voltage,
EV_float_cancel: UnitValues, EV_float_cancel: Voltage,
Et_float_exit_cum: UnitValues, Et_float_exit_cum: Raw,
EV_eq: UnitValues, EV_eq: Voltage,
Et_eqcalendar: UnitValues, Et_eqcalendar: Raw,
Et_eq_above: UnitValues, Et_eq_above: Raw,
Et_eq_reg: UnitValues, Et_eq_reg: Raw,
Et_batt_service: UnitValues, Et_batt_service: Raw,
EV_tempcomp: UnitValues, EV_tempcomp: Tempcomp,
EV_hvd: UnitValues, EV_hvd: Voltage,
EV_hvr: UnitValues, EV_hvr: Voltage,
Evb_ref_lim: UnitValues, Evb_ref_lim: Voltage,
ETb_max: UnitValues, ETb_max: Raw,
ETb_min: UnitValues, ETb_min: Raw,
EV_soc_g_gy: UnitValues, EV_soc_g_gy: Voltage,
EV_soc_gy_y: UnitValues, EV_soc_gy_y: Voltage,
EV_soc_y_yr: UnitValues, EV_soc_y_yr: Voltage,
EV_soc_yr_r: UnitValues, EV_soc_yr_r: Voltage,
Emodbus_id: UnitValues, Emodbus_id: Raw,
Emeterbus_id: UnitValues, Emeterbus_id: Raw,
EIb_lim: UnitValues, EIb_lim: Current,
EVa_ref_fixed_init: UnitValues, EVa_ref_fixed_init: Voltage,
EVa_ref_fixed_pct_init: UnitValues, EVa_ref_fixed_pct_init: VoltagePercentage,
} }
sum_type! { trait Scaled {
#[derive(Serialize, Deserialize, Debug)] fn get_scaled(&self, info: &Info) -> f32;
enum UnitValues {
Raw(u16),
ScaledCurrent(ScaledCurrent),
ScaledVoltage(ScaledVoltage),
ScaledVoltagePercentage(ScaledVoltagePercentage),
ScaledTempcomp(ScaledTempcomp),
}
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct ScaledTempcomp { struct Tempcomp {
data: u16, data: u16,
} }
impl ScaledTempcomp { impl Scaled for Tempcomp {
pub fn get(&self, info: &Info) -> f32 { fn get_scaled(&self, info: &Info) -> f32 {
self.data as f32 * info.v_scale * f32::powf(2., -16.) self.data as f32 * info.v_scale * f32::powf(2., -16.)
} }
}
impl Tempcomp {
pub fn new(voltage: f32, info: &Info) -> Self { pub fn new(voltage: f32, info: &Info) -> Self {
Self { Self {
data: ((voltage / f32::powf(2., -16.)) / info.v_scale) as u16, data: ((voltage / f32::powf(2., -16.)) / info.v_scale) as u16,
@ -146,16 +140,18 @@ impl ScaledTempcomp {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct ScaledVoltage { struct Voltage {
data: u16, data: u16,
} }
impl ScaledVoltage { impl Scaled for Voltage {
pub fn get(&self, info: &Info) -> f32 { fn get_scaled(&self, info: &Info) -> f32 {
self.data as f32 * info.v_scale * f32::powf(2., -15.) self.data as f32 * info.v_scale * f32::powf(2., -15.)
} }
}
pub fn new(voltage: f32, info: &Info) -> Self { impl Voltage {
fn new(voltage: f32, info: &Info) -> Self {
Self { Self {
data: ((voltage / f32::powf(2., -15.)) / info.v_scale) as u16, data: ((voltage / f32::powf(2., -15.)) / info.v_scale) as u16,
} }
@ -163,16 +159,18 @@ impl ScaledVoltage {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct ScaledVoltagePercentage { struct VoltagePercentage {
data: u16, data: u16,
} }
impl ScaledVoltagePercentage { impl Scaled for VoltagePercentage {
pub fn get(&self, info: &Info) -> f32 { fn get_scaled(&self, info: &Info) -> f32 {
self.data as f32 * 100. * f32::powf(2., -16.) self.data as f32 * 100. * f32::powf(2., -16.)
} }
}
pub fn new(voltage: f32, info: &Info) -> Self { impl VoltagePercentage {
fn new(voltage: f32, info: &Info) -> Self {
Self { Self {
data: ((voltage / f32::powf(2., -16.)) / 100.) as u16, data: ((voltage / f32::powf(2., -16.)) / 100.) as u16,
} }
@ -180,22 +178,41 @@ impl ScaledVoltagePercentage {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct ScaledCurrent { struct Current {
data: u16, data: u16,
} }
impl ScaledCurrent { impl Scaled for Current {
pub fn get(&self, info: &Info) -> f32 { fn get_scaled(&self, info: &Info) -> f32 {
self.data as f32 * info.i_scale * f32::powf(2., -15.) self.data as f32 * info.i_scale * f32::powf(2., -15.)
} }
}
pub fn new(voltage: f32, info: &Info) -> Self { impl Current {
fn new(current: f32, info: &Info) -> Self {
Self { Self {
data: ((voltage / f32::powf(2., -15.)) / info.i_scale) as u16, data: ((current / f32::powf(2., -15.)) / info.i_scale) as u16,
} }
} }
} }
#[derive(Serialize, Deserialize, Debug)]
struct Raw {
data: u16,
}
impl Scaled for Raw {
fn get_scaled(&self, info: &Info) -> f32 {
self.data as f32
}
}
impl Raw {
fn new(input: u16, info: &Info) -> Self {
Self { data: input }
}
}
#[derive(Parser)] #[derive(Parser)]
#[clap(global_setting = AppSettings::DisableHelpSubcommand)] #[clap(global_setting = AppSettings::DisableHelpSubcommand)]
#[clap(author, about, long_about = None)] #[clap(author, about, long_about = None)]
@ -380,65 +397,87 @@ fn main() {
.expect("could not get eeprom data"); .expect("could not get eeprom data");
let eeprom_data = MpptEeprom { let eeprom_data = MpptEeprom {
EV_absorp: UnitValues::ScaledVoltage(ScaledVoltage { EV_absorp: Voltage {
data: data_in[OffsetsEeprom::EV_absorp], data: data_in[OffsetsEeprom::EV_absorp],
}), },
EV_float: UnitValues::ScaledVoltage(ScaledVoltage { EV_float: Voltage {
data: data_in[OffsetsEeprom::EV_float], data: data_in[OffsetsEeprom::EV_float],
}), },
Et_absorp: UnitValues::Raw(data_in[OffsetsEeprom::Et_absorp]), Et_absorp: Raw {
Et_absorp_ext: UnitValues::Raw(data_in[OffsetsEeprom::Et_absorp_ext]), data: data_in[OffsetsEeprom::Et_absorp],
EV_absorp_ext: UnitValues::ScaledVoltage(ScaledVoltage { },
Et_absorp_ext: Raw {
data: data_in[OffsetsEeprom::Et_absorp_ext],
},
EV_absorp_ext: Voltage {
data: data_in[OffsetsEeprom::EV_absorp_ext], data: data_in[OffsetsEeprom::EV_absorp_ext],
}), },
EV_float_cancel: UnitValues::ScaledVoltage(ScaledVoltage { EV_float_cancel: Voltage {
data: data_in[OffsetsEeprom::EV_float_cancel], data: data_in[OffsetsEeprom::EV_float_cancel],
}), },
Et_float_exit_cum: UnitValues::Raw(data_in[OffsetsEeprom::Et_float_exit_cum]), Et_float_exit_cum: Raw {
EV_eq: UnitValues::ScaledVoltage(ScaledVoltage { data: data_in[OffsetsEeprom::Et_float_exit_cum],
},
EV_eq: Voltage {
data: data_in[OffsetsEeprom::EV_eq], data: data_in[OffsetsEeprom::EV_eq],
}), },
Et_eqcalendar: UnitValues::Raw(data_in[OffsetsEeprom::Et_eqcalendar]), Et_eqcalendar: Raw {
Et_eq_above: UnitValues::Raw(data_in[OffsetsEeprom::Et_eq_above]), data: data_in[OffsetsEeprom::Et_eqcalendar],
Et_eq_reg: UnitValues::Raw(data_in[OffsetsEeprom::Et_eq_reg]), },
Et_batt_service: UnitValues::Raw(data_in[OffsetsEeprom::Et_batt_service]), Et_eq_above: Raw {
EV_tempcomp: UnitValues::ScaledTempcomp(ScaledTempcomp { data: data_in[OffsetsEeprom::Et_eq_above],
},
Et_eq_reg: Raw {
data: data_in[OffsetsEeprom::Et_eq_reg],
},
Et_batt_service: Raw {
data: data_in[OffsetsEeprom::Et_batt_service],
},
EV_tempcomp: Tempcomp {
data: data_in[OffsetsEeprom::EV_tempcomp], data: data_in[OffsetsEeprom::EV_tempcomp],
}), },
EV_hvd: UnitValues::ScaledVoltage(ScaledVoltage { EV_hvd: Voltage {
data: data_in[OffsetsEeprom::EV_hvd], data: data_in[OffsetsEeprom::EV_hvd],
}), },
EV_hvr: UnitValues::ScaledVoltage(ScaledVoltage { EV_hvr: Voltage {
data: data_in[OffsetsEeprom::EV_hvr], data: data_in[OffsetsEeprom::EV_hvr],
}), },
Evb_ref_lim: UnitValues::ScaledVoltage(ScaledVoltage { Evb_ref_lim: Voltage {
data: data_in[OffsetsEeprom::Evb_ref_lim], data: data_in[OffsetsEeprom::Evb_ref_lim],
}), },
ETb_max: UnitValues::Raw(data_in[OffsetsEeprom::ETb_max]), ETb_max: Raw {
ETb_min: UnitValues::Raw(data_in[OffsetsEeprom::ETb_min]), data: data_in[OffsetsEeprom::ETb_max],
EV_soc_g_gy: UnitValues::ScaledVoltage(ScaledVoltage { },
ETb_min: Raw {
data: data_in[OffsetsEeprom::ETb_min],
},
EV_soc_g_gy: Voltage {
data: data_in[OffsetsEeprom::EV_soc_g_gy], data: data_in[OffsetsEeprom::EV_soc_g_gy],
}), },
EV_soc_gy_y: UnitValues::ScaledVoltage(ScaledVoltage { EV_soc_gy_y: Voltage {
data: data_in[OffsetsEeprom::EV_soc_gy_y], data: data_in[OffsetsEeprom::EV_soc_gy_y],
}), },
EV_soc_y_yr: UnitValues::ScaledVoltage(ScaledVoltage { EV_soc_y_yr: Voltage {
data: data_in[OffsetsEeprom::EV_soc_y_yr], data: data_in[OffsetsEeprom::EV_soc_y_yr],
}), },
EV_soc_yr_r: UnitValues::ScaledVoltage(ScaledVoltage { EV_soc_yr_r: Voltage {
data: data_in[OffsetsEeprom::EV_soc_yr_r], data: data_in[OffsetsEeprom::EV_soc_yr_r],
}), },
Emodbus_id: UnitValues::Raw(data_in[OffsetsEeprom::Emodbus_id]), Emodbus_id: Raw {
Emeterbus_id: UnitValues::Raw(data_in[OffsetsEeprom::Emeterbus_id]), data: data_in[OffsetsEeprom::Emodbus_id],
EIb_lim: UnitValues::ScaledCurrent(ScaledCurrent { },
Emeterbus_id: Raw {
data: data_in[OffsetsEeprom::Emeterbus_id],
},
EIb_lim: Current {
data: data_in[OffsetsEeprom::EIb_lim], data: data_in[OffsetsEeprom::EIb_lim],
}), },
EVa_ref_fixed_init: UnitValues::ScaledVoltage(ScaledVoltage { EVa_ref_fixed_init: Voltage {
data: data_in[OffsetsEeprom::EVa_ref_fixed_init], data: data_in[OffsetsEeprom::EVa_ref_fixed_init],
}), },
EVa_ref_fixed_pct_init: UnitValues::ScaledVoltagePercentage(ScaledVoltagePercentage { EVa_ref_fixed_pct_init: VoltagePercentage {
data: data_in[OffsetsEeprom::EVa_ref_fixed_pct_init], data: data_in[OffsetsEeprom::EVa_ref_fixed_pct_init],
}), },
}; };
println!("eeprom: {:#?}", eeprom_data); println!("eeprom: {:#?}", eeprom_data);