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
=> {
28 Box
::new(machine
::MachineJiji
::new().expect("Unable to create MachineJiji"))
30 settings
::MachineName
::LyssMetal
=> {
31 Box
::new(machine
::MachineLyssMetal
::new().expect("Unable to create MachineLyssMetal"))
35 let mut kernel
= [0f32; consts
::KERNEL_SIZE_SAMPLES
];
36 let mut current_pos
= 0usize
;
39 let period
= Duration
::from_micros(1_000_000u64 / consts
::FREQ_TEMP_POLLING
as u64);
42 if completed
.load(Ordering
::Relaxed
) {
46 let time_beginning_loop
= time
::Instant
::now();
48 let temp
= (machine
.get_cpu_tmp() + machine
.get_gpu_tmp()) / 2f32;
49 kernel
[current_pos
] = temp
;
50 current_pos
= (current_pos
+ 1) % consts
::KERNEL_SIZE_SAMPLES
;
56 s
/ kernel
.len() as f32
59 let normalized_temp
= num
::clamp(
60 (mean_temp
- settings
.cold_temperature
)
61 / (settings
.hot_temperature
- settings
.cold_temperature
),
64 ); // Between 0 (cold) and 1 (hot).
67 rgb
::linear_interpolation(settings
.cold_color
, settings
.hot_color
, normalized_temp
);
69 // println!("normalized_temp: {normalized_temp}");
71 if tick
% (consts
::FREQ_TEMP_POLLING
/ consts
::FREQ_REFRESHING_RGB
) as i64 == 0 {
72 println!("Update RGB: {color:?}, temp: {mean_temp}");
73 machine
.set_color(&color
);
76 let elapsed
= time
::Instant
::now() - time_beginning_loop
;
78 let to_wait
= period
- elapsed
;
84 // println!("Press any key to continue...");
85 // std::io::stdin().read_line(&mut String::new()).unwrap();