Remove A770 for MachineJiji
[temp2RGB.git] / src / machine.rs
index 9436f49..ac60e72 100644 (file)
@@ -1,7 +1,16 @@
+use log::error;
+use nvapi::sys::i2c;
+
 use crate::{
-    a770, asus_aura_usb, corsair_lighting_pro, corsair_vengeance, cpu_temperature, intel_arc, rgb,
+    /*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;
@@ -11,22 +20,24 @@ pub trait Machine {
 pub struct MachineJiji {
     ram: Vec<corsair_vengeance::Controller>,
     b650e_device: asus_aura_usb::Device,
-    a770: a770::A770,
-    gpu_devices: intel_arc::Devices,
+    // a770: a770::A770,
+    // gpu_devices: intel_arc::Devices,
+    gpus: Vec<nvapi::PhysicalGpu>,
 }
 
 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(),
-            gpu_devices: unsafe { intel_arc::GetDevices() },
-        }
+            b650e_device: asus_aura_usb::Device::new(&api, asus_aura_usb::Motherboard::Asus650e)?,
+            // a770: a770::A770::new()?,
+            // gpu_devices: unsafe { intel_arc::GetDevices() },
+            gpus: nvapi::PhysicalGpu::enumerate()?,
+        })
     }
 }
 
@@ -36,11 +47,16 @@ 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 {
-        unsafe { intel_arc::GetTemperature(self.gpu_devices, 0) as f32 }
+        // unsafe { intel_arc::GetTemperature(self.gpu_devices, 0) as f32 }
+        self.gpus[0].thermal_settings(None).unwrap()[0]
+            .current_temperature
+            .0 as f32
     }
 
     fn get_cpu_tmp(&self) -> f32 {
@@ -48,13 +64,13 @@ impl Machine for MachineJiji {
     }
 }
 
-impl Drop for MachineJiji {
-    fn drop(&mut self) {
-        unsafe {
-            intel_arc::FreeDevices(self.gpu_devices);
-        }
-    }
-}
+// impl Drop for MachineJiji {
+//     fn drop(&mut self) {
+//         unsafe {
+//             intel_arc::FreeDevices(self.gpu_devices);
+//         }
+//     }
+// }
 
 pub struct MachineLyssMetal {
     crosshair_device: asus_aura_usb::Device,
@@ -63,16 +79,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 +97,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 +161,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 {