-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Graph support, no UI Graph, just the the logic (#93)
* init * multi win * finish window multi * cargo update
- Loading branch information
Showing
19 changed files
with
536 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
theme = "Light" | ||
update_delay = 1500 | ||
current_config = "fake" | ||
current_config = "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
Control = [] | ||
Fan = [] | ||
Temp = [] | ||
Flat = [] | ||
Linear = [] | ||
Target = [] | ||
|
||
[[CustomTemp]] | ||
name = "CPU" | ||
kind = "Average" | ||
inputs = [] | ||
inputs = [] | ||
|
||
[[Graph]] | ||
name = "Graph" | ||
|
||
[[Graph.coord]] | ||
temp = 50 | ||
percent = 30 | ||
|
||
[[Graph.coord]] | ||
temp = 50 | ||
percent = 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,239 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::vec; | ||
|
||
use crate::node::IsValid; | ||
use hardware::{Hardware, Value}; | ||
use serde::{Deserialize, Deserializer, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
use crate::{ | ||
app_graph::Nodes, | ||
config::graph::affine::Affine, | ||
id::IdGenerator, | ||
node::{IsValid, Node, NodeType, ToNode}, | ||
update::UpdateError, | ||
}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Eq)] | ||
pub struct Coord { | ||
pub temp: u8, | ||
pub percent: u8, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Default)] | ||
impl TryFrom<(&str, &str)> for Coord { | ||
type Error = Box<dyn std::error::Error>; | ||
|
||
fn try_from((temp, percent): (&str, &str)) -> Result<Self, Self::Error> { | ||
let temp = temp.parse::<u8>()?; | ||
|
||
let percent = percent.parse::<u8>()?; | ||
|
||
if percent > 100 { | ||
return Err("Percent > 100".into()); | ||
} | ||
|
||
Ok(Coord { temp, percent }) | ||
} | ||
} | ||
|
||
impl PartialEq for Coord { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.temp == other.temp | ||
} | ||
} | ||
|
||
impl PartialOrd for Coord { | ||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for Coord { | ||
fn cmp(&self, other: &Self) -> std::cmp::Ordering { | ||
self.temp.cmp(&other.temp) | ||
} | ||
} | ||
|
||
impl Coord { | ||
pub fn exact_same(&self, other: &Self) -> bool { | ||
self.percent == other.percent && self.temp == other.temp | ||
} | ||
} | ||
|
||
// todo: better default + UI | ||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Graph { | ||
pub name: String, | ||
#[serde(rename = "coord")] | ||
pub coords: Vec<Coord>, | ||
pub coords: Coords, | ||
pub input: Option<String>, // Temp or CustomTemp | ||
} | ||
|
||
impl Default for Graph { | ||
fn default() -> Self { | ||
Self { | ||
name: Default::default(), | ||
coords: Coords(vec![ | ||
Coord { | ||
temp: 10, | ||
percent: 10, | ||
}, | ||
Coord { | ||
temp: 70, | ||
percent: 100, | ||
}, | ||
]), | ||
input: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl ToNode for Graph { | ||
fn to_node(self, id_generator: &mut IdGenerator, nodes: &Nodes, _hardware: &Hardware) -> Node { | ||
Node::new(id_generator, NodeType::Graph(self), nodes) | ||
} | ||
} | ||
|
||
impl IsValid for Graph { | ||
fn is_valid(&self) -> bool { | ||
self.input.is_some() //TODO: add conditions on coords | ||
#[derive(PartialEq)] | ||
enum DupState { | ||
Init, | ||
Prev { temp: u8 }, | ||
DuplicateFound, | ||
} | ||
|
||
self.input.is_some() | ||
&& !self.coords.0.is_empty() | ||
&& self | ||
.coords | ||
.0 | ||
.iter() | ||
.fold(DupState::Init, |prev, coord| match prev { | ||
DupState::Init => DupState::Prev { temp: coord.temp }, | ||
DupState::Prev { temp } => { | ||
if temp == coord.temp { | ||
DupState::DuplicateFound | ||
} else { | ||
DupState::Prev { temp: coord.temp } | ||
} | ||
} | ||
DupState::DuplicateFound => DupState::DuplicateFound, | ||
}) | ||
!= DupState::DuplicateFound | ||
&& !self.coords.0.iter().any(|coord| coord.percent > 100) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Debug, Clone, Default)] | ||
pub struct Coords(pub Vec<Coord>); | ||
|
||
impl<'de> serde::Deserialize<'de> for Coords { | ||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> { | ||
let mut s: Vec<Coord> = Vec::deserialize(d)?; | ||
|
||
s.sort(); | ||
|
||
Ok(Coords(s)) | ||
} | ||
} | ||
|
||
impl Graph { | ||
pub fn get_value(&self, value: Value) -> Result<Value, UpdateError> { | ||
let dummy_coord = Coord { | ||
temp: value as u8, | ||
percent: 0, | ||
}; | ||
|
||
let res = match self.coords.0.binary_search(&dummy_coord) { | ||
Ok(index) => self.coords.0[index].percent as Value, | ||
Err(index) => { | ||
if index == 0 { | ||
self.coords.0[index].percent as Value | ||
} else if index == self.coords.0.len() { | ||
self.coords.0[index - 1].percent as Value | ||
} else { | ||
let coord1 = &self.coords.0[index - 1]; | ||
let coord2 = &self.coords.0[index]; | ||
|
||
Affine { | ||
xa: coord1.temp.into(), | ||
ya: coord1.percent.into(), | ||
xb: coord2.temp.into(), | ||
yb: coord2.percent.into(), | ||
} | ||
.calcule(value) as Value | ||
} | ||
} | ||
}; | ||
|
||
Ok(res) | ||
} | ||
} | ||
|
||
// todo: use it in linear | ||
mod affine { | ||
use hardware::Value; | ||
|
||
#[derive(Debug)] | ||
pub struct Affine { | ||
pub xa: f32, | ||
pub ya: f32, | ||
pub xb: f32, | ||
pub yb: f32, | ||
} | ||
|
||
impl Affine { | ||
pub fn calcule(&self, value: Value) -> f32 { | ||
let a = (self.yb - self.ya) / (self.xb - self.xa); | ||
let b = self.ya - a * self.xa; | ||
|
||
a * value as f32 + b | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let coord1 = Coord { | ||
temp: 10, | ||
percent: 10, | ||
}; | ||
|
||
let coord2 = Coord { | ||
temp: 20, | ||
percent: 20, | ||
}; | ||
|
||
let coord3 = Coord { | ||
temp: 30, | ||
percent: 30, | ||
}; | ||
|
||
let coord4 = Coord { | ||
temp: 40, | ||
percent: 40, | ||
}; | ||
|
||
let coords = Coords(vec![coord1, coord2, coord3, coord4]); | ||
|
||
let dummy_coord = Coord { | ||
temp: 50, | ||
percent: 0, | ||
}; | ||
|
||
let res = coords.0.binary_search(&dummy_coord); | ||
|
||
match res { | ||
Ok(index) => { | ||
println!("use {}", index); | ||
} | ||
Err(index) => { | ||
if index == 0 { | ||
println!("use {}", index); | ||
} else if index == coords.0.len() { | ||
println!("use {}", index - 1); | ||
} else { | ||
println!("use {} and {}", index - 1, index); | ||
} | ||
} | ||
} | ||
dbg!(&res); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.