Skip to content

Commit

Permalink
Various performance tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Dec 4, 2023
1 parent d6d5096 commit 9d5f3d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const SERVICE_TYPE: &str = "_nanoleafapi._tcp.local.";
fn update_lights(panels: NanoleafLayoutResponse, nanoleaf: NanoleafClient, buffer_manager: Arc<RwLock<BufferManager>>, color_channel: Receiver<Vec<Hsl>>, intensity: f32) {
// Needs to be over a sliding window.
let mut window = SlidingWindow::new(64);
let mut color_set = Vec::new();
let mut color_set: Vec<Hsl> = Vec::new();
let mut sorted_panels = panels.position_data.to_vec();
sorted_panels.sort_by(|a,b| {
let v = a.x as i32 - b.x as i32;
Expand All @@ -49,9 +49,11 @@ fn update_lights(panels: NanoleafLayoutResponse, nanoleaf: NanoleafClient, buffe
loop {
let process_start = Instant::now();
{
color_set = color_channel.recv_timeout(Duration::from_millis(30)).unwrap_or( color_set);
if let Ok(v) = color_channel.try_recv() {
color_set = v;
}

if let Some(data) = buffer_manager.write().unwrap().fft_interval::<10>(LIGHT_INTERVAL) {
if let Some(data) = buffer_manager.write().unwrap().fft_interval(LIGHT_INTERVAL, panels.num_panels) {
let mut effect = NanoleafEffectPayload::new(panels.num_panels);
for (panel_index, panel) in sorted_panels.iter().enumerate() {
if let Some(color) = color_set.get(panel_index) {
Expand All @@ -74,7 +76,7 @@ fn update_lights(panels: NanoleafLayoutResponse, nanoleaf: NanoleafClient, buffe
if LIGHT_INTERVAL.ge(&process_start.elapsed()) {
let sleep_duration = LIGHT_INTERVAL.sub(process_start.elapsed());
if sleep_duration.ge(&Duration::ZERO) {
thread::sleep(LIGHT_INTERVAL);
thread::sleep(sleep_duration);
}
}
}
Expand Down Expand Up @@ -158,6 +160,7 @@ fn configure_display(pause_duration:time::Duration, panel_count: usize, output_n
let mut last_value = 0.0f32;
let mut heatmap = vec![vec![vec![vec![0u32; 21]; 21]; 37]; panel_count];
loop {
let start = Instant::now();
let frame_copy = backend::capture_output_frame(
&globals,
&conn,
Expand All @@ -167,11 +170,16 @@ fn configure_display(pause_duration:time::Duration, panel_count: usize, output_n
let hsl = visual::prominent_color::determine_prominent_color(frame_copy, &mut heatmap);
let value_hash: f32 = hsl.iter().map(|f| f.get_hue() + f.get_lightness() + f.get_saturation()).sum();
if value_hash != last_value {
log::debug!("Sending new hsl {:?}", hsl);
tx.send(hsl).unwrap();
last_value = value_hash;
}
thread::sleep(pause_duration);
if pause_duration.ge(&start.elapsed()) {
let sleep_duration = LIGHT_INTERVAL.sub(start.elapsed());
if sleep_duration.ge(&Duration::ZERO) {
thread::sleep(sleep_duration);
}
}

}
});
rx
Expand Down
7 changes: 4 additions & 3 deletions src/vis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ impl BufferManager {

// TODO: would be nice to have constant_q and/or variable_q intervals

pub fn fft_interval<const T: usize>(
pub fn fft_interval(
&mut self,
interval: Duration,
) -> Option<Box<[f32; T]>> {
out_size: usize,
) -> Option<Box<[f32]>> {
let BufferSlice { values, rate } = self.take_next(interval);

if values.len() < 2 {
Expand Down Expand Up @@ -147,7 +148,7 @@ impl BufferManager {
//.normalized()
.build()
.unwrap()
.take(T)
.take(out_size)
.map(|Complex { re, im }| {
let power = f32::sqrt(re * re + im * im);
let value = power / fft.scaling_factor;
Expand Down

0 comments on commit 9d5f3d1

Please sign in to comment.