add inputs struct & simple tempo tapping
This commit is contained in:
parent
17705e2288
commit
13d58a7ac5
2 changed files with 76 additions and 26 deletions
100
src/main.rs
100
src/main.rs
|
@ -27,47 +27,62 @@ use crate::{
|
||||||
util::get_angle
|
util::get_angle
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Breathing {
|
|
||||||
t: Arc<RwLock<Duration>>
|
#[derive(Clone)]
|
||||||
|
struct Inputs {
|
||||||
|
t: Duration,
|
||||||
|
|
||||||
|
intensity: f32,
|
||||||
|
cycle_len: Duration,
|
||||||
|
|
||||||
|
wave_peak: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_angle(p: &Vector2<f32>) -> f32 {
|
|
||||||
let pi=3.1415926;
|
|
||||||
let pi2 = 2.0*pi;
|
|
||||||
|
|
||||||
if p.x < 0.0 {
|
|
||||||
(p.y / p.x).atan() / pi2 + 0.75
|
|
||||||
} else if p.x == 0.0 && p.y == 0.0 {
|
|
||||||
0.0
|
|
||||||
} else {
|
|
||||||
if p.y < 0.0 {
|
|
||||||
(-p.x / p.y).atan() / pi2
|
|
||||||
} else {
|
|
||||||
(p.y / p.x).atan() / pi2 + 0.25
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
struct Breathing { inputs: Arc<RwLock< Inputs >> }
|
||||||
impl ColorGrid for Breathing {
|
impl ColorGrid for Breathing {
|
||||||
fn get(&self, p: &Vector2<f32>) -> Rgb<f32> {
|
fn get(&self, p: &Vector2<f32>) -> Rgb<f32> {
|
||||||
let millis = self.t.read().unwrap().as_millis();
|
let inputs = self.inputs.read().unwrap().clone();
|
||||||
|
|
||||||
|
let millis = inputs.t.as_millis();
|
||||||
|
|
||||||
let p1 = p + Vector2::new(0.0,0.5);
|
let p1 = p + Vector2::new(0.0,0.5);
|
||||||
let r2 = p1.x*p1.x + p1.y*p1.y;
|
let r2 = p1.x*p1.x + p1.y*p1.y;
|
||||||
let v = if r2 > 1.0 { 1.0 } else { r2 };
|
let v = if r2 > 1.0 { 1.0 } else { r2 };
|
||||||
let phi = ( get_angle(&p1) );
|
let phi = ( get_angle(&p1) );
|
||||||
let mirrorphi = if phi < 0.5 { phi } else { 1.0-phi };
|
let mirrorphi = if phi < 0.5 { phi } else { 1.0-phi };
|
||||||
let gamma = (
|
let gamma =
|
||||||
|
(
|
||||||
(30.0) *
|
(30.0) *
|
||||||
mirrorphi *
|
mirrorphi *
|
||||||
( 0.5+ 0.5*f32::sin(millis as f32 / 4000.0) )
|
(
|
||||||
|
0.5+ 0.5*f32::sin(
|
||||||
|
inputs.t.as_millis() as f32
|
||||||
|
/ inputs.cycle_len.as_millis() as f32
|
||||||
|
)
|
||||||
|
)
|
||||||
) % 1.0;
|
) % 1.0;
|
||||||
|
|
||||||
Rgb::from_color(
|
Rgb::from_color(
|
||||||
&Hsv::<f32, Turns<f32>>::new(
|
&Hsv::<f32, Turns<f32>>::new(
|
||||||
Turns( 0.25+0.25*f32::sin(millis as f32/8000.0) + gamma*0.5 ),
|
Turns( 0.25+0.25*f32::sin(millis as f32/8000.0) + gamma*0.5 ),
|
||||||
0.5 + r2 * 0.5,
|
0.5 + r2 * 0.5,
|
||||||
mirrorphi,
|
inputs.wave_peak,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Anim2 { inputs: Arc<RwLock< Inputs >> }
|
||||||
|
impl ColorGrid for Anim2 {
|
||||||
|
fn get(&self, p: &Vector2<f32>) -> Rgb<f32> {
|
||||||
|
let inputs = self.inputs.read().unwrap().clone();
|
||||||
|
// let millis = self
|
||||||
|
Rgb::from_color(
|
||||||
|
&Hsv::<f32, Turns<f32>>::new(
|
||||||
|
Turns( 0.0 ),
|
||||||
|
0.5,
|
||||||
|
( inputs.t.as_millis() as f32 / inputs.cycle_len.as_millis() as f32 ) % 1.0
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -86,7 +101,15 @@ async fn main() {
|
||||||
socket.write().unwrap().set_read_timeout(Some(std::time::Duration::from_millis(500)));
|
socket.write().unwrap().set_read_timeout(Some(std::time::Duration::from_millis(500)));
|
||||||
socket.write().unwrap().set_write_timeout(Some(std::time::Duration::from_millis(50)));
|
socket.write().unwrap().set_write_timeout(Some(std::time::Duration::from_millis(50)));
|
||||||
|
|
||||||
let t = Arc::new(RwLock::new(Duration::from_millis(0)));
|
let inputs = Arc::new(RwLock::new(
|
||||||
|
Inputs {
|
||||||
|
t: Duration::from_millis(0),
|
||||||
|
|
||||||
|
intensity: 0.5,
|
||||||
|
cycle_len: Duration::from_millis(300),
|
||||||
|
wave_peak: 0.5,
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
let mut lighting_setup = LightingSetup::new(
|
let mut lighting_setup = LightingSetup::new(
|
||||||
vec![
|
vec![
|
||||||
|
@ -111,22 +134,26 @@ async fn main() {
|
||||||
],
|
],
|
||||||
|
|
||||||
Box::new(
|
Box::new(
|
||||||
Breathing{ t: t.clone() }
|
Anim2{ inputs: inputs.clone() }
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
let tbegin = std::time::Instant::now();
|
let tbegin = std::time::Instant::now();
|
||||||
|
let mut last_tap = std::time::Instant::now();
|
||||||
|
|
||||||
event_loop.run(move |event, elwt| {
|
event_loop.run(move |event, elwt| {
|
||||||
let tcur = std::time::Instant::now();
|
let tcur = std::time::Instant::now();
|
||||||
elwt.set_control_flow(ControlFlow::WaitUntil(
|
elwt.set_control_flow(ControlFlow::WaitUntil(
|
||||||
tcur + Duration::from_millis(10)
|
tcur + Duration::from_millis(10)
|
||||||
));
|
));
|
||||||
|
|
||||||
*t.write().unwrap() = tcur - tbegin;
|
inputs.write().unwrap().t = tcur - tbegin;
|
||||||
lighting_setup.update_buffers();
|
lighting_setup.update_buffers();
|
||||||
//lighting_setup.update_outputs();
|
//lighting_setup.update_outputs();
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
|
//Event::
|
||||||
|
|
||||||
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {
|
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {
|
||||||
let (width, height) = {
|
let (width, height) = {
|
||||||
let size = window.inner_size();
|
let size = window.inner_size();
|
||||||
|
@ -141,6 +168,29 @@ async fn main() {
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
elwt.exit();
|
elwt.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
winit::event::Event::WindowEvent{
|
||||||
|
window_id: _,
|
||||||
|
event: winit::event::WindowEvent::KeyboardInput{ device_id, event, is_synthetic }
|
||||||
|
} => {
|
||||||
|
if event.state == winit::event::ElementState::Pressed {
|
||||||
|
match event.logical_key {
|
||||||
|
winit::keyboard::Key::Character(c) => {
|
||||||
|
eprintln!("pressed {}", c);
|
||||||
|
|
||||||
|
if c == "x" {
|
||||||
|
// tap tempo
|
||||||
|
let old_tap = last_tap;
|
||||||
|
last_tap = std::time::Instant::now();
|
||||||
|
|
||||||
|
inputs.write().unwrap().cycle_len = last_tap - old_tap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,7 @@ impl LightingSetup {
|
||||||
let green = (color.green() * 32.0) as u32;
|
let green = (color.green() * 32.0) as u32;
|
||||||
let blue = (color.blue() * 32.0) as u32;
|
let blue = (color.blue() * 32.0) as u32;
|
||||||
|
|
||||||
buffer[index as usize] = 0;//blue | (green << 8) | (red << 16);
|
buffer[index as usize] = blue | (green << 8) | (red << 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
for fixture in self.fixtures.iter() {
|
for fixture in self.fixtures.iter() {
|
||||||
|
|
Loading…
Reference in a new issue