selected_var_format_button

This commit is contained in:
Martin Kavík 2024-06-08 23:51:30 +02:00
parent fbdf8090a1
commit a6da2887c9
13 changed files with 191 additions and 87 deletions

View file

@ -10,6 +10,7 @@ publish.workspace = true
[dependencies]
wellen.workspace = true
moonlight.workspace = true
convert-base = "1.1.2"
# @TODO update `futures_util_ext` - add feature `sink`, set exact `futures-util` version
futures-util = { version = "0.3.30", features = ["sink"] }

View file

@ -1,5 +1,8 @@
use moonlight::*;
mod var_format;
pub use var_format::VarFormat;
pub mod wellen_helpers;
#[derive(Serialize, Deserialize, Debug, Default)]

54
shared/src/var_format.rs Normal file
View file

@ -0,0 +1,54 @@
#[derive(Default, Clone, Copy, Debug)]
pub enum VarFormat {
ASCII,
Binary,
BinaryWithGroups,
#[default]
Hexadecimal,
Octal,
Signed,
Unsigned,
}
impl VarFormat {
pub fn as_static_str(&self) -> &'static str {
match self {
VarFormat::ASCII => "Text",
VarFormat::Binary => "Bin",
VarFormat::BinaryWithGroups => "Bins",
VarFormat::Hexadecimal => "Hex",
VarFormat::Octal => "Oct",
VarFormat::Signed => "i32",
VarFormat::Unsigned => "u32",
}
}
pub fn next(&self) -> Self {
match self {
VarFormat::ASCII => VarFormat::Binary,
VarFormat::Binary => VarFormat::BinaryWithGroups,
VarFormat::BinaryWithGroups => VarFormat::Hexadecimal,
VarFormat::Hexadecimal => VarFormat::Octal,
VarFormat::Octal => VarFormat::Signed,
VarFormat::Signed => VarFormat::Unsigned,
VarFormat::Unsigned => VarFormat::ASCII,
}
}
pub fn format(&self, value: wellen::SignalValue) -> String {
// @TODO optimize it by not using `.to_string` if possible
let value = value.to_string();
let ones_and_zeros = value
.chars()
.rev()
.map(|char| char.to_digit(2).unwrap())
.collect::<Vec<_>>();
let mut base = convert_base::Convert::new(2, 16);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value: String = output
.into_iter()
.map(|number| char::from_digit(number, 16).unwrap())
.collect();
value
}
}