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();
24 let file_conf_path
= if cfg!(debug_assertions
) {
25 String
::from(consts
::FILE_CONF
)
30 .join(consts
::SERVICE_NAME
)
31 .join(consts
::FILE_CONF
)
37 let settings
= settings
::Settings
::read(&file_conf_path
).expect("Cannot load settings");
38 println!("Settings: {settings:?}");
40 let mut machine
: Box
<dyn machine
::Machine
> = match settings
.machine_name
{
41 settings
::MachineName
::Jiji
=> {
42 Box
::new(machine
::MachineJiji
::new().expect("Unable to create MachineJiji"))
44 settings
::MachineName
::LyssMetal
=> {
45 Box
::new(machine
::MachineLyssMetal
::new().expect("Unable to create MachineLyssMetal"))
49 let mut kernel
= [0f32; consts
::KERNEL_SIZE_SAMPLES
];
50 let mut current_pos
= 0usize
;
53 let period
= Duration
::from_micros(1_000_000u64 / consts
::FREQ_TEMP_POLLING
as u64);
56 if completed
.load(Ordering
::Relaxed
) {
60 let time_beginning_loop
= time
::Instant
::now();
62 let temp
= (machine
.get_cpu_tmp() + machine
.get_gpu_tmp()) / 2f32;
63 kernel
[current_pos
] = temp
;
64 current_pos
= (current_pos
+ 1) % consts
::KERNEL_SIZE_SAMPLES
;
70 s
/ kernel
.len() as f32
73 let normalized_temp
= num
::clamp(
74 (mean_temp
- settings
.cold_temperature
)
75 / (settings
.hot_temperature
- settings
.cold_temperature
),
78 ); // Between 0 (cold) and 1 (hot).
81 rgb
::linear_interpolation(settings
.cold_color_1
, settings
.hot_color_1
, normalized_temp
);
83 let color_2
= match (settings
.cold_color_2
, settings
.hot_color_2
) {
84 (Some(cold_color
), Some(hot_color
)) => Some(rgb
::linear_interpolation(
92 // println!("normalized_temp: {normalized_temp}");
94 if tick
% (consts
::FREQ_TEMP_POLLING
/ consts
::FREQ_REFRESHING_RGB
) as i64 == 0 {
95 println!("Update RGB: {color_1:?}/{color_2:?}, temp: {mean_temp}");
96 machine
.set_color_1(&color_1
);
97 if color_2
.is_some() {
98 machine
.set_color_2(&color_2
.unwrap());
100 machine
.set_color_2(&color_1
);
104 let elapsed
= time
::Instant
::now() - time_beginning_loop
;
105 if elapsed
< period
{
106 let to_wait
= period
- elapsed
;
112 // println!("Press any key to continue...");
113 // std::io::stdin().read_line(&mut String::new()).unwrap();