pub const MYBMM_PACK_NAME_LEN: usize = 32;
pub const MAX_TEMPS: usize = 8;
pub const MAX_CELLS: usize = 32;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProtocolVersion {
#[default]
Jk02_24S,
Jk02_32S,
Jk04,
}
impl ProtocolVersion {
pub fn from_model(model: &str) -> Self {
if model.starts_with("JK_PB2") || model.starts_with("JK-BD") || model.starts_with("JK_HY") {
ProtocolVersion::Jk02_32S
} else {
ProtocolVersion::Jk02_24S
}
}
pub fn is_jk02(&self) -> bool {
matches!(self, ProtocolVersion::Jk02_24S | ProtocolVersion::Jk02_32S)
}
pub fn is_jk04(&self) -> bool {
matches!(self, ProtocolVersion::Jk04)
}
pub fn cell_offset(&self) -> usize {
match self {
ProtocolVersion::Jk02_32S => 16,
_ => 0,
}
}
pub fn max_frame_cells(&self) -> usize {
match self {
ProtocolVersion::Jk02_24S => 24,
ProtocolVersion::Jk02_32S => 32,
ProtocolVersion::Jk04 => 24,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct JkSettings {
pub smart_sleep_voltage: f32,
pub cell_uvp: f32, pub cell_uvpr: f32, pub cell_ovp: f32, pub cell_ovpr: f32, pub balance_trigger_voltage: f32,
pub cell_soc100_voltage: f32,
pub cell_soc0_voltage: f32,
pub cell_request_charge_voltage: f32, pub cell_request_float_voltage: f32, pub power_off_voltage: f32,
pub balance_starting_voltage: f32,
pub max_charge_current: f32,
pub max_discharge_current: f32,
pub max_balance_current: f32,
pub charge_ocp_delay: f32, pub charge_ocp_recovery: f32, pub discharge_ocp_delay: f32,
pub discharge_ocp_recovery: f32,
pub scp_recovery: f32, pub scp_delay: f32,
pub charge_otp: f32, pub charge_otp_recovery: f32,
pub discharge_otp: f32,
pub discharge_otp_recovery: f32,
pub charge_utp: f32, pub charge_utp_recovery: f32,
pub power_tube_otp: f32, pub power_tube_otp_recovery: f32,
pub discharge_utp: f32,
pub discharge_utp_recovery: f32,
pub heating_start_temperature: f32,
pub heating_stop_temperature: f32,
pub cell_count: u8,
pub total_battery_capacity: f32,
pub charging_switch: bool,
pub discharging_switch: bool,
pub balancer_switch: bool,
pub heating_switch: bool,
pub disable_temp_sensors: bool,
pub display_always_on: bool,
pub smart_sleep_switch: bool,
pub disable_pcl_module: bool,
pub timed_stored_data: bool,
pub charging_float_mode: bool,
pub discharge_precharge_time: u8, pub cell_request_charge_voltage_time: f32, pub cell_request_float_voltage_time: f32, pub re_bulk_soc: f32,
pub voltage_calibration: f32,
pub current_calibration: f32,
pub wire_resistance: [f32; 32],
pub raw_frame: Vec<u8>,
}
impl JkSettings {
pub fn new() -> Self {
Self::default()
}
}
#[derive(Debug, Clone)]
pub struct MybmmPack {
pub name: String,
pub uuid: String,
pub r#type: String,
pub transport: String,
pub target: String,
pub opts: String,
pub state: u16,
pub failed: i32,
pub error: i32,
pub errmsg: String,
pub capacity: f32,
pub voltage: f32,
pub current: f32,
pub power: f32,
pub charging_power: f32,
pub discharging_power: f32,
pub status: i32,
pub ntemps: i32,
pub temps: [f32; MAX_TEMPS],
pub power_tube_temp: f32,
pub cells: i32,
pub cellvolt: [f32; MAX_CELLS],
pub cellres: [f32; MAX_CELLS],
pub cell_min: f32,
pub cell_max: f32,
pub cell_diff: f32,
pub cell_avg: f32,
pub balancebits: u32,
pub capabilities: u16,
pub protocol_version: ProtocolVersion,
pub model: String,
pub hwvers: String,
pub swvers: String,
pub error_bitmask: u16,
pub balancing_current: f32,
pub balancing: bool,
pub soc: f32,
pub capacity_remaining: f32,
pub total_battery_capacity: f32,
pub charging_cycles: u32,
pub total_charging_cycle_capacity: f32,
pub soh: f32,
pub total_runtime: u32,
pub charging: bool,
pub discharging: bool,
pub precharging: bool,
pub heating: bool,
pub enabled_cells_bitmask: u32,
pub settings: Option<JkSettings>,
}
impl MybmmPack {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
uuid: String::new(),
r#type: String::new(),
transport: String::new(),
target: String::new(),
opts: String::new(),
state: 0,
failed: 0,
error: 0,
errmsg: String::new(),
capacity: 0.0,
voltage: 0.0,
current: 0.0,
power: 0.0,
charging_power: 0.0,
discharging_power: 0.0,
status: 0,
ntemps: 0,
temps: [0.0; MAX_TEMPS],
power_tube_temp: 0.0,
cells: 0,
cellvolt: [0.0; MAX_CELLS],
cellres: [0.0; MAX_CELLS],
cell_min: 0.0,
cell_max: 0.0,
cell_diff: 0.0,
cell_avg: 0.0,
balancebits: 0,
capabilities: 0,
protocol_version: ProtocolVersion::default(),
model: String::new(),
hwvers: String::new(),
swvers: String::new(),
error_bitmask: 0,
balancing_current: 0.0,
balancing: false,
soc: 0.0,
capacity_remaining: 0.0,
total_battery_capacity: 0.0,
charging_cycles: 0,
total_charging_cycle_capacity: 0.0,
soh: 0.0,
total_runtime: 0,
charging: false,
discharging: false,
precharging: false,
heating: false,
enabled_cells_bitmask: 0,
settings: None,
}
}
pub fn is_pb2(&self) -> bool {
self.protocol_version == ProtocolVersion::Jk02_32S
}
}