config: add test_encode_decode

This commit is contained in:
Astro 2020-09-24 21:35:15 +02:00
parent 254c1c3d73
commit 3b050347d4
2 changed files with 32 additions and 5 deletions

View File

@ -14,7 +14,7 @@ use crate::{
}; };
/// Just for encoding/decoding, actual state resides in ChannelState /// Just for encoding/decoding, actual state resides in ChannelState
#[derive(Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Config { pub struct Config {
channels: [ChannelConfig; CHANNELS], channels: [ChannelConfig; CHANNELS],
} }
@ -38,7 +38,7 @@ impl Config {
} }
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChannelConfig { pub struct ChannelConfig {
center: CenterPoint, center: CenterPoint,
pid: pid::Parameters, pid: pid::Parameters,
@ -62,7 +62,7 @@ impl ChannelConfig {
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct SteinhartHartConfig { struct SteinhartHartConfig {
t0: f32, t0: f32,
r0: f32, r0: f32,
@ -79,7 +79,7 @@ impl From<&steinhart_hart::Parameters> for SteinhartHartConfig {
} }
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct PwmLimits { struct PwmLimits {
max_v: f32, max_v: f32,
max_i_pos: f32, max_i_pos: f32,
@ -128,4 +128,31 @@ mod test {
let buffer = config.encode(&mut buffer).unwrap(); let buffer = config.encode(&mut buffer).unwrap();
assert!(buffer.len() <= EEPROM_SIZE); assert!(buffer.len() <= EEPROM_SIZE);
} }
#[test]
fn test_encode_decode() {
let channel_config = ChannelConfig {
center: CenterPoint::Override(1.5),
pid: pid::Parameters::default(),
pid_target: 93.7,
sh: (&steinhart_hart::Parameters::default()).into(),
pwm: PwmLimits {
max_v: 1.65,
max_i_pos: 2.1,
max_i_neg: 2.25,
},
};
let config = Config {
channels: [
channel_config.clone(),
channel_config.clone(),
],
};
const EEPROM_SIZE: usize = 0x80;
let mut buffer = [0; EEPROM_SIZE];
config.encode(&mut buffer).unwrap();
let decoded = Config::decode(&buffer).unwrap();
assert_eq!(decoded, config);
}
} }

View File

@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Parameters { pub struct Parameters {
pub kp: f32, pub kp: f32,
pub ki: f32, pub ki: f32,