Replace some unwrap by Result
[temp2RGB.git] / src / machine.rs
index 9436f49..9d150db 100644 (file)
@@ -1,7 +1,15 @@
+use log::error;
+use nvapi::sys::i2c;
+
 use crate::{
     a770, asus_aura_usb, corsair_lighting_pro, corsair_vengeance, cpu_temperature, intel_arc, rgb,
 };
 
+const RGB_FUSION2_GPU_REG_COLOR: u8 = 0x40;
+const RGB_FUSION2_GPU_REG_MODE: u8 = 0x88;
+
+const GIGABYTE_RTX3080TI_VISION_OC_ADDR: u8 = 0x63;
+
 pub trait Machine {
     fn set_color(&mut self, color: &rgb::RGB);
     fn get_gpu_tmp(&self) -> f32;
@@ -16,17 +24,17 @@ pub struct MachineJiji {
 }
 
 impl MachineJiji {
-    pub fn new() -> Self {
+    pub fn new() -> anyhow::Result<Self> {
         let api = hidapi::HidApi::new().unwrap();
-        MachineJiji {
+        Ok(MachineJiji {
             ram: vec![
                 corsair_vengeance::Controller::new(0x19),
                 corsair_vengeance::Controller::new(0x1B),
             ],
-            b650e_device: asus_aura_usb::Device::new(&api, asus_aura_usb::Motherboard::Asus650e),
-            a770: a770::A770::new(),
+            b650e_device: asus_aura_usb::Device::new(&api, asus_aura_usb::Motherboard::Asus650e)?,
+            a770: a770::A770::new()?,
             gpu_devices: unsafe { intel_arc::GetDevices() },
-        }
+        })
     }
 }
 
@@ -36,7 +44,9 @@ impl Machine for MachineJiji {
             controller.set_color(&color);
         }
         self.b650e_device.set_color(&color);
-        self.a770.set_color(color.red, color.green, color.blue);
+        if let Err(error) = self.a770.set_color(color.red, color.green, color.blue) {
+            error!("Unable to set color: {:?}", error);
+        }
     }
 
     fn get_gpu_tmp(&self) -> f32 {
@@ -63,16 +73,16 @@ pub struct MachineLyssMetal {
 }
 
 impl MachineLyssMetal {
-    pub fn new() -> Self {
-        let api = hidapi::HidApi::new().unwrap();
+    pub fn new() -> anyhow::Result<Self> {
+        let api = hidapi::HidApi::new()?;
 
         nvapi::initialize().expect("Unable to initialize nvapi (Nvidia API)");
 
-        MachineLyssMetal {
+        let machine = MachineLyssMetal {
             crosshair_device: asus_aura_usb::Device::new(
                 &api,
                 asus_aura_usb::Motherboard::AsusCrosshairVIIIHero,
-            ),
+            )?,
             corsair_lignting_pro: corsair_lighting_pro::Device::new(
                 &api,
                 &rgb::RGB {
@@ -81,8 +91,63 @@ impl MachineLyssMetal {
                     blue: 40,
                 },
             ),
-            gpus: nvapi::PhysicalGpu::enumerate().unwrap(),
-        }
+            gpus: nvapi::PhysicalGpu::enumerate()?,
+        };
+
+        // machine.set_mode_3080ti();
+        Ok(machine)
+    }
+
+    // Doesn't work: "Error: NotSupported".
+    // From OpenRGB, see the following files:
+    //  * Controllers\GigabyteRGBFusion2GPUController\GigabyteRGBFusion2GPUControllerDetect.cpp
+    //  * Controllers\GigabyteRGBFusion2GPUController\RGBController_GigabyteRGBFusion2GPU.cpp
+    //  * Controllers\GigabyteRGBFusion2GPUController\GigabyteRGBFusion2GPUController.cpp
+    //  * i2c_smbus\i2c_smbus_nvapi.cpp
+    // Implementation of nvapi-rs: https://github.com/arcnmx/nvapi-rs/blob/master/src/gpu.rs#L645
+    pub fn test_i2c(&self) {
+        // Test from 'GigabyteRGBFusion2GPUControllerDetect.cpp'
+        let data = [0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
+        self.gpus[0]
+            .i2c_write(
+                0,
+                Some(1),
+                false,
+                GIGABYTE_RTX3080TI_VISION_OC_ADDR,
+                &[],
+                &data,
+                i2c::I2cSpeed::Default,
+            )
+            .expect("Error");
+    }
+
+    fn set_mode_3080ti(&self) {
+        let data = [
+            RGB_FUSION2_GPU_REG_MODE,
+            0x01, // Mode (1: static).
+            0x00, // Speed.
+            0x63, // Brightness max.
+            0x00, // Mistery flag.
+            0x01, // Zone.
+            0x00,
+            0x00,
+        ];
+        self.gpus[0]
+            .i2c_write(
+                0,
+                Some(1),
+                false,
+                GIGABYTE_RTX3080TI_VISION_OC_ADDR,
+                &[],
+                &data,
+                i2c::I2cSpeed::Default,
+            )
+            .expect("Error");
+    }
+
+    fn set_color_3080ti(&self, color: &rgb::RGB) {
+        // TODO.
+        self.test_i2c();
     }
 }
 
@@ -90,6 +155,7 @@ impl Machine for MachineLyssMetal {
     fn set_color(&mut self, color: &rgb::RGB) {
         self.crosshair_device.set_color(&color);
         self.corsair_lignting_pro.set_color(&color);
+        // self.set_color_3080ti(&color); // TODO.
     }
 
     fn get_gpu_tmp(&self) -> f32 {