use moving average and not difference

This commit is contained in:
PVV Sanctuary
2026-03-19 19:29:08 +01:00
parent 36e4e14dbf
commit 3c8565e605

View File

@@ -2,7 +2,6 @@ use reqwest::Client;
use serde::Serialize;
use std::time::{Duration, Instant};
use okmain::rgb;
use win_screenshot::prelude::*;
const URL: &str = "https://homeassistant.pvv.ntnu.no:8123/api/services/light/turn_on";
@@ -23,8 +22,10 @@ async fn main() {
brightness: 200,
rgb_color: [0, 0, 0],
};
let logging = true;
let time_between_requests = Duration::from_millis(1000);
let logging = false;
let time_between_requests = Duration::from_millis(100);
let mut moving_average = [0u8; 3];
let beta = 0.9f64;
loop {
let frame_start = Instant::now();
let v = loop {
@@ -33,26 +34,24 @@ async fn main() {
let now = Instant::now();
let v =
get_next_rgb_value_for_asynchronously_setting_mood_lights_in_koserommet_at_pvv_by_using_a_mean(buf);
let diff = color_difference_is_great_enough_for_making_a_request_to_change_light_colors_using_euclidian_distance(
payload.rgb_color,
v);
let elapsed = now.elapsed();
if diff > 0.1 {
if logging {
println!(
"calculated average pixel value in: {} milliseconds",
elapsed.as_millis()
);
println!("new_rgb_color: {:?}", v);
println!("rgb_color: {:?}", payload.rgb_color);
println!("Found difference of: {}", diff);
}
payload.rgb_color = v;
break v;
if logging {
println!(
"calculated average pixel value in: {} milliseconds",
elapsed.as_millis()
);
println!("new_rgb_color: {:?}", v);
println!("rgb_color: {:?}", payload.rgb_color);
}
std::thread::sleep(Duration::from_millis(33));
if frame_start.elapsed() > time_between_requests {
break v;
for i in 0..3 {
moving_average[i] =
(moving_average[i] as f64 * beta + v[i] as f64 * (1.0 - beta)) as u8;
}
payload.rgb_color = moving_average;
break moving_average;
}
};
payload.rgb_color = v;
@@ -109,7 +108,7 @@ fn get_next_rgb_value_for_asynchronously_setting_mood_lights_in_koserommet_at_pv
[a.r, a.g, a.b]
}
fn color_difference_is_great_enough_for_making_a_request_to_change_light_colors_using_euclidian_distance(
fn _color_difference_is_great_enough_for_making_a_request_to_change_light_colors_using_euclidian_distance(
a: [u8; 3],
b: [u8; 3],
) -> f64 {