Skip to content

Commit

Permalink
test // enum works
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2seb committed Nov 12, 2023
1 parent 8a12be7 commit 66015d3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 16 deletions.
40 changes: 38 additions & 2 deletions firmware/litex-fw/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions firmware/litex-fw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ aligned-array = "1.0.1"
irq = "0.2.3"
litex-interrupt = { path = "../deps/litex-interrupt" }
critical-section = "1.1.2"
strum_macros = "0.25.3"
strum = {version = "0.25.0", features = ["derive"], default-features=false}

[profile.release]
lto = true
Expand Down
89 changes: 75 additions & 14 deletions firmware/litex-fw/src/opt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use heapless::String;

use strum_macros::{EnumIter, IntoStaticStr};

pub type OptionString = String<32>;

pub trait OptionTrait {
Expand All @@ -10,65 +12,84 @@ pub trait OptionTrait {
}

#[derive(Clone)]
pub struct Option<T> {
pub struct NumOption<T> {
pub name: OptionString,
pub value: T,
step: T,
min: T,
max: T,
}

#[derive(Clone)]
pub struct EnumOption<T> {
pub name: OptionString,
pub value: T,
}

#[derive(Clone, Copy, PartialEq, EnumIter, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum EnumTest {
ValueA,
ValueB,
ValueC
}

#[derive(Clone)]
pub struct Options {
pub modify: bool,
pub selected: usize,
pub attack_ms: Option<u32>,
pub decay_ms: Option<u32>,
pub release_ms: Option<u32>,
pub resonance: Option<i16>,
pub delay_len: Option<u32>,
pub attack_ms: NumOption<u32>,
pub decay_ms: NumOption<u32>,
pub release_ms: NumOption<u32>,
pub resonance: NumOption<i16>,
pub delay_len: NumOption<u32>,
pub enum_test: EnumOption<EnumTest>,
}

impl Options {
pub fn new() -> Options {
Options {
modify: false,
selected: 0,
delay_len: Option {
delay_len: NumOption{
name: "delayln".into(),
value: 511,
step: 1,
min: 128,
max: 511,
},
attack_ms: Option {
attack_ms: NumOption{
name: "attack".into(),
value: 100,
step: 50,
min: 0,
max: 5000,
},
decay_ms: Option {
decay_ms: NumOption{
name: "decay".into(),
value: 100,
step: 50,
min: 0,
max: 5000,
},
release_ms: Option {
release_ms: NumOption{
name: "release".into(),
value: 300,
step: 50,
min: 0,
max: 5000,
},
resonance: Option {
resonance: NumOption{
name: "reso".into(),
value: 10000,
step: 1000,
min: 0,
max: 20000,
},
enum_test: EnumOption{
name: "enumt".into(),
value: EnumTest::ValueA,
},
}
}

Expand All @@ -95,8 +116,9 @@ impl Options {
}

#[allow(dead_code)]
pub fn view(&self) -> [& dyn OptionTrait; 5] {
pub fn view(&self) -> [& dyn OptionTrait; 6] {
[
&self.enum_test,
&self.attack_ms,
&self.decay_ms,
&self.release_ms,
Expand All @@ -106,8 +128,9 @@ impl Options {
}

#[allow(dead_code)]
pub fn view_mut(&mut self) -> [&mut dyn OptionTrait; 5] {
pub fn view_mut(&mut self) -> [&mut dyn OptionTrait; 6] {
[
&mut self.enum_test,
&mut self.attack_ms,
&mut self.decay_ms,
&mut self.release_ms,
Expand All @@ -122,7 +145,7 @@ impl<T: Copy +
core::ops::Sub<Output = T> +
core::cmp::PartialOrd +
ufmt::uDisplay>
OptionTrait for Option<T> {
OptionTrait for NumOption<T> {

fn name(&self) -> &OptionString {
&self.name
Expand Down Expand Up @@ -158,3 +181,41 @@ impl<T: Copy +
}
}
}

impl<T: Copy + strum::IntoEnumIterator + PartialEq + Into<&'static str>>
OptionTrait for EnumOption<T> {

fn name(&self) -> &OptionString {
&self.name
}

fn value(&self) -> OptionString {
String::from(self.value.into())
}

fn tick_up(&mut self) {
let mut it = T::iter();
for v in it.by_ref() {
if v == self.value {
break;
}
}
if let Some(v) = it.next() {
self.value = v;
}
}

fn tick_down(&mut self) {
let it = T::iter();
let mut last_value: Option<T> = None;
for v in it {
if v == self.value {
if let Some(lv) = last_value {
self.value = lv;
return;
}
}
last_value = Some(v);
}
}
}

0 comments on commit 66015d3

Please sign in to comment.