add random?
This commit is contained in:
parent
66d5dba006
commit
6cdc0d7d15
4 changed files with 79 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -203,6 +203,7 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
|||
name = "leds"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
"rs_ws281x",
|
||||
"tungstenite",
|
||||
]
|
||||
|
|
|
@ -7,4 +7,5 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
rs_ws281x = "0.4.2"
|
||||
tungstenite = "0.16.0"
|
||||
tungstenite = "0.16.0"
|
||||
rand = "0.8.4"
|
|
@ -18,6 +18,7 @@ impl TryFrom<Vec<String>> for Box<dyn Pattern> {
|
|||
match s[0].as_str() {
|
||||
"unit" => Ok(Box::new(Unit::parse(s).unwrap()) as Box<dyn Pattern>),
|
||||
"val" => Ok(Box::new(Value::parse(s).unwrap()) as Box<dyn Pattern>),
|
||||
"rand" => Ok(Box::new(Random::parse(s).unwrap()) as Box<dyn Pattern>),
|
||||
_ => Err("No Match".into())
|
||||
}
|
||||
}
|
||||
|
@ -120,6 +121,80 @@ impl Pattern for Value {
|
|||
}
|
||||
}
|
||||
|
||||
struct Random {
|
||||
r: bool,
|
||||
g: bool,
|
||||
b: bool,
|
||||
min: u8,
|
||||
max: u8
|
||||
}
|
||||
|
||||
impl Pattern for Random {
|
||||
fn execute(&self, values: &mut RwLockWriteGuard<'_, [RawColor; crate::LED_SIZE]>, _ticks: u64) {
|
||||
use rand::prelude::*;
|
||||
let mut rng = rand::thread_rng();
|
||||
for x in 0..values.len() {
|
||||
let mut color = (*values)[x];
|
||||
if self.r {
|
||||
let y: f64 = rng.gen();
|
||||
color[2] = ((y * (max - min))+min) as u8;
|
||||
}
|
||||
if self.g {
|
||||
let y: f64 = rng.gen();
|
||||
color[1] = ((y * (max - min))+min) as u8;
|
||||
}
|
||||
if self.b {
|
||||
let y: f64 = rng.gen();
|
||||
color[0] = ((y * (max - min))+min) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
|
||||
let end = args.len() - 1;
|
||||
let e = args[end].parse::<u8>();
|
||||
let en = args[end-1].parse::<u8>();
|
||||
let mut r = false;
|
||||
let mut g = false;
|
||||
let mut b = false;
|
||||
let (min, max) = match en {
|
||||
Ok(i) => {
|
||||
match e {
|
||||
Ok(j) => (i, j),
|
||||
Err(_) => (0, 255)
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
match e {
|
||||
Ok(i) => (0, i),
|
||||
Err(_) => (0, 255)
|
||||
}
|
||||
}
|
||||
};
|
||||
for x in 1..end {
|
||||
match args[x].as_str() {
|
||||
"r" => {r = true;},
|
||||
"g" => {g = true;},
|
||||
"b" => {b = true;},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(Random {
|
||||
r: r,
|
||||
g: g,
|
||||
b: b,
|
||||
min: min,
|
||||
max: max
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct Rainbow {
|
||||
offset_r: Option<usize>,
|
||||
offset_g: Option<usize>,
|
||||
offset_b: Option<usize>
|
||||
}
|
||||
// todo
|
||||
|
||||
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern>, ParseError> {
|
||||
println!("{:?}", v);
|
||||
v.try_into()
|
||||
|
|
|
@ -31,7 +31,7 @@ pub fn websocket(pattern: &Arc<Mutex<Vec<Box<dyn Pattern>>>>) {
|
|||
}
|
||||
else {
|
||||
let msg = tungstenite::Message::from("Not text");
|
||||
websocket.write_message(msg).unwrap();
|
||||
websocket.write_message(msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue