Add a color (2 colors can be now defined for a machine).
[temp2RGB.git] / src / main_loop.rs
index 6e7dd74..a11c36b 100644 (file)
@@ -17,13 +17,34 @@ pub fn main_loop(completed: Arc<AtomicBool>) {
         panic!("Polling frequency must be a multiple of RGB refresh frequency");
     }
 
-    init_winring0();
+    winring0::init();
 
     let sleep = timer::Sleep::new();
-    let settings = settings::Settings::read(consts::FILE_CONF).expect("Cannot load settings");
+
+    let file_conf_path = if cfg!(debug_assertions) {
+        String::from(consts::FILE_CONF)
+    } else {
+        String::from(
+            dirs::config_dir()
+                .unwrap()
+                .join(consts::SERVICE_NAME)
+                .join(consts::FILE_CONF)
+                .to_str()
+                .unwrap(),
+        )
+    };
+
+    let settings = settings::Settings::read(&file_conf_path).expect("Cannot load settings");
     println!("Settings: {settings:?}");
 
-    let machine: &mut dyn machine::Machine = &mut machine::MachineJiji::new();
+    let mut machine: Box<dyn machine::Machine> = match settings.machine_name {
+        settings::MachineName::Jiji => {
+            Box::new(machine::MachineJiji::new().expect("Unable to create MachineJiji"))
+        }
+        settings::MachineName::LyssMetal => {
+            Box::new(machine::MachineLyssMetal::new().expect("Unable to create MachineLyssMetal"))
+        }
+    };
 
     let mut kernel = [0f32; consts::KERNEL_SIZE_SAMPLES];
     let mut current_pos = 0usize;
@@ -56,14 +77,28 @@ pub fn main_loop(completed: Arc<AtomicBool>) {
             1f32,
         ); // Between 0 (cold) and 1 (hot).
 
-        let color =
-            rgb::linear_interpolation(settings.cold_color, settings.hot_color, normalized_temp);
+        let color_1 =
+            rgb::linear_interpolation(settings.cold_color_1, settings.hot_color_1, normalized_temp);
+
+        let color_2 = match (settings.cold_color_2, settings.hot_color_2) {
+            (Some(cold_color), Some(hot_color)) => Some(rgb::linear_interpolation(
+                cold_color,
+                hot_color,
+                normalized_temp,
+            )),
+            _ => None,
+        };
 
         // println!("normalized_temp: {normalized_temp}");
 
         if tick % (consts::FREQ_TEMP_POLLING / consts::FREQ_REFRESHING_RGB) as i64 == 0 {
-            println!("Update RGB: {color:?}, temp: {mean_temp}");
-            machine.set_color(&color);
+            println!("Update RGB: {color_1:?}/{color_2:?}, temp: {mean_temp}");
+            machine.set_color_1(&color_1);
+            if color_2.is_some() {
+                machine.set_color_2(&color_2.unwrap());
+            } else {
+                machine.set_color_2(&color_1);
+            }
         }
 
         let elapsed = time::Instant::now() - time_beginning_loop;
@@ -77,20 +112,5 @@ pub fn main_loop(completed: Arc<AtomicBool>) {
     // println!("Press any key to continue...");
     // std::io::stdin().read_line(&mut String::new()).unwrap();
 
-    unsafe {
-        winring0::DeinitializeOls();
-    }
-}
-
-fn init_winring0() {
-    unsafe {
-        let ols_ok = winring0::InitializeOls() != 0;
-        if !ols_ok {
-            panic!("Unable to initalize WingRing0");
-        }
-        let dll_status = winring0::GetDllStatus();
-        if dll_status != 0 {
-            panic!("WingRing0 DLL status error: {}", dll_status);
-        }
-    }
+    winring0::deinit();
 }