3 atomic
::{AtomicBool
, Ordering
},
6 time
::{self, Duration
},
9 use crate::{consts
, machine
, rgb
, settings
, timer
, winring0
};
11 pub fn main_loop(completed
: Arc
<AtomicBool
>) {
12 if consts
::FREQ_REFRESHING_RGB
> consts
::FREQ_TEMP_POLLING
{
13 panic!("Polling frequency must be greater or equal than RGB refresh frequency");
16 if consts
::FREQ_TEMP_POLLING
% consts
::FREQ_REFRESHING_RGB
!= 0 {
17 panic!("Polling frequency must be a multiple of RGB refresh frequency");
22 let sleep
= timer
::Sleep
::new();
23 let settings
= settings
::Settings
::read(consts
::FILE_CONF
).expect("Cannot load settings");
24 println!("Settings: {settings:?}");
26 let mut machine
: Box
<dyn machine
::Machine
> = match settings
.machine_name
{
27 settings
::MachineName
::Jiji
=> Box
::new(machine
::MachineJiji
::new()),
28 settings
::MachineName
::LyssMetal
=> Box
::new(machine
::MachineLyssMetal
::new()),
31 let mut kernel
= [0f32; consts
::KERNEL_SIZE_SAMPLES
];
32 let mut current_pos
= 0usize
;
35 let period
= Duration
::from_micros(1_000_000u64 / consts
::FREQ_TEMP_POLLING
as u64);
38 if completed
.load(Ordering
::Relaxed
) {
42 let time_beginning_loop
= time
::Instant
::now();
44 let temp
= (machine
.get_cpu_tmp() + machine
.get_gpu_tmp()) / 2f32;
45 kernel
[current_pos
] = temp
;
46 current_pos
= (current_pos
+ 1) % consts
::KERNEL_SIZE_SAMPLES
;
52 s
/ kernel
.len() as f32
55 let normalized_temp
= num
::clamp(
56 (mean_temp
- settings
.cold_temperature
)
57 / (settings
.hot_temperature
- settings
.cold_temperature
),
60 ); // Between 0 (cold) and 1 (hot).
63 rgb
::linear_interpolation(settings
.cold_color
, settings
.hot_color
, normalized_temp
);
65 // println!("normalized_temp: {normalized_temp}");
67 if tick
% (consts
::FREQ_TEMP_POLLING
/ consts
::FREQ_REFRESHING_RGB
) as i64 == 0 {
68 println!("Update RGB: {color:?}, temp: {mean_temp}");
69 machine
.set_color(&color
);
72 let elapsed
= time
::Instant
::now() - time_beginning_loop
;
74 let to_wait
= period
- elapsed
;
80 // println!("Press any key to continue...");
81 // std::io::stdin().read_line(&mut String::new()).unwrap();