move datatypes to mppt-common

This commit is contained in:
Alex Janka 2022-12-18 15:59:20 +11:00
parent 74bbbefbf0
commit cfb6a76b5b
6 changed files with 16 additions and 231 deletions

8
Cargo.lock generated
View file

@ -421,6 +421,13 @@ dependencies = [
"adler",
]
[[package]]
name = "mppt-common"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]]
name = "mppt-control"
version = "1.0.0"
@ -428,6 +435,7 @@ dependencies = [
"bincode",
"clap 4.0.29",
"libmodbus-rs",
"mppt-common",
"serde",
"serde_json",
]

View file

@ -15,3 +15,4 @@ clap = { version = "4.0", features = ["derive"] }
bincode = "1.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
mppt-common = { path = "./mppt-common" }

@ -1 +1 @@
Subproject commit e19ce9d25d25840844510b111543d535bba7b349
Subproject commit d738d53c4ec4c3caae0a18855d34298e4cc9e6bb

View file

@ -1,205 +0,0 @@
use crate::INFO_SCALE;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug};
pub trait DataPoint {
fn get_scaled(&self) -> f32;
fn get_scaled_from(&self, data: u16) -> f32;
fn get_raw(&self) -> u16;
fn to_string(&self) -> String;
fn to_string_v(&self) -> String {
format!("{}, raw: {}", self.to_string(), self.get_raw())
}
fn get_type(&self) -> String;
fn u16_from_f32(&self, input: f32) -> u16;
fn from_u16(data: u16) -> Self
where
Self: Sized;
}
impl fmt::Debug for dyn DataPoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{}", self.to_string_v())
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Voltage {
data: u16,
}
impl DataPoint for Voltage {
fn get_scaled(&self) -> f32 {
self.get_scaled_from(self.data)
}
fn get_scaled_from(&self, data: u16) -> f32 {
let info = INFO_SCALE.lock().expect("Can't lock info");
data as f32 * info.v_scale * f32::powf(2., -15.)
}
fn from_u16(data: u16) -> Self {
Self { data }
}
fn get_raw(&self) -> u16 {
self.data
}
fn to_string(&self) -> String {
format!("{}V", self.get_scaled())
}
fn get_type(&self) -> String {
String::from("Voltage")
}
fn u16_from_f32(&self, input: f32) -> u16 {
let info = INFO_SCALE.lock().expect("Can't lock info");
((input / f32::powf(2., -15.)) / info.v_scale) as u16
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Current {
data: u16,
}
impl DataPoint for Current {
fn get_scaled(&self) -> f32 {
self.get_scaled_from(self.data)
}
fn get_scaled_from(&self, data: u16) -> f32 {
let info = INFO_SCALE.lock().expect("Can't lock info");
data as f32 * info.i_scale * f32::powf(2., -15.)
}
fn from_u16(data: u16) -> Self {
Self { data }
}
fn get_raw(&self) -> u16 {
self.data
}
fn to_string(&self) -> String {
format!("{}A", self.get_scaled())
}
fn get_type(&self) -> String {
String::from("Current")
}
fn u16_from_f32(&self, input: f32) -> u16 {
let info = INFO_SCALE.lock().expect("Can't lock info");
((input / f32::powf(2., -15.)) / info.i_scale) as u16
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct VoltagePercentage {
data: u16,
}
impl DataPoint for VoltagePercentage {
fn get_scaled(&self) -> f32 {
self.get_scaled_from(self.data)
}
fn get_scaled_from(&self, data: u16) -> f32 {
data as f32 * 100. * f32::powf(2., -16.)
}
fn from_u16(data: u16) -> Self {
Self { data }
}
fn get_raw(&self) -> u16 {
self.data
}
fn to_string(&self) -> String {
format!("{}%", self.get_scaled())
}
fn get_type(&self) -> String {
String::from("Voltage Percentage")
}
fn u16_from_f32(&self, input: f32) -> u16 {
((input / f32::powf(2., -16.)) / 100.) as u16
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Tempcomp {
data: u16,
}
impl DataPoint for Tempcomp {
fn get_scaled(&self) -> f32 {
self.get_scaled_from(self.data)
}
fn get_scaled_from(&self, data: u16) -> f32 {
let info = INFO_SCALE.lock().expect("Can't lock info");
data as f32 * info.v_scale * f32::powf(2., -16.)
}
fn from_u16(data: u16) -> Self {
Self { data }
}
fn get_raw(&self) -> u16 {
self.data
}
fn to_string(&self) -> String {
format!("{} - temperature compensation value", self.get_scaled())
}
fn get_type(&self) -> String {
String::from("Temperature Compensation")
}
fn u16_from_f32(&self, input: f32) -> u16 {
let info = INFO_SCALE.lock().expect("Can't lock info");
((input / f32::powf(2., -16.)) / info.v_scale) as u16
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Raw {
data: u16,
}
impl DataPoint for Raw {
fn get_scaled(&self) -> f32 {
self.get_scaled_from(self.data)
}
fn get_scaled_from(&self, data: u16) -> f32 {
data as f32
}
fn from_u16(data: u16) -> Self {
Self { data }
}
fn get_raw(&self) -> u16 {
self.data
}
fn to_string(&self) -> String {
String::from("raw value")
}
fn get_type(&self) -> String {
String::from("Raw Value")
}
fn u16_from_f32(&self, input: f32) -> u16 {
input as u16
}
}

View file

@ -1,14 +1,15 @@
#![allow(dead_code)]
mod datatypes;
mod mppt_structs;
mod offsets;
use crate::datatypes::*;
use crate::mppt_structs::{MpptData, MpptEeprom, MpptRam};
use crate::offsets::{OffsetsEeprom, OffsetsRam};
use clap::{Parser, Subcommand};
use libmodbus_rs::{Modbus, ModbusClient, ModbusRTU};
use std::{fmt::Debug, path::Path, process::Command, sync::Mutex};
use mppt_common::datatypes::*;
use mppt_common::Info;
use mppt_common::INFO_SCALE;
use std::{path::Path, process::Command};
const DEVICE_ID: u8 = 0x01;
const RAM_DATA_SIZE: u16 = 0x005B;
@ -23,11 +24,6 @@ const DEFAULT_SERIAL: &str = if cfg!(target_os = "linux") {
"unknown"
};
static INFO_SCALE: Mutex<Info> = Mutex::new(Info {
v_scale: 1.,
i_scale: 1.,
});
#[derive(Parser)]
#[clap(disable_help_subcommand = true)]
#[clap(author, about, long_about = None)]
@ -63,21 +59,6 @@ enum Commands {
PrintJSON,
}
#[derive(Debug, Clone)]
struct Info {
v_scale: f32,
i_scale: f32,
}
impl Info {
pub fn from(data: &[u16]) -> Self {
Self {
v_scale: data[0] as f32 + (data[1] as f32 / f32::powf(2., 16.)),
i_scale: data[2] as f32 + (data[3] as f32 / f32::powf(2., 16.)),
}
}
}
fn main() {
let args = Args::parse();
if args.get_serial_ports {

View file

@ -1,4 +1,4 @@
use crate::datatypes::*;
use mppt_common::datatypes::*;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display};