1012c00fb613dd47eca25e9e045b46c78e607f0b
[temp2RGB.git] / src / machine.rs
1 use log::error;
2 use nvapi::sys::i2c;
3
4 use crate::{
5 /*a770,*/ asus_aura_usb, corsair_lighting_pro, corsair_vengeance, cpu_temperature,
6 intel_arc, lian_li_sl_infinity, rgb,
7 };
8
9 const RGB_FUSION2_GPU_REG_COLOR: u8 = 0x40;
10 const RGB_FUSION2_GPU_REG_MODE: u8 = 0x88;
11
12 const GIGABYTE_RTX3080TI_VISION_OC_ADDR: u8 = 0x63;
13
14 pub trait Machine {
15 fn set_color(&mut self, color: &rgb::RGB);
16 fn get_gpu_tmp(&self) -> f32;
17 fn get_cpu_tmp(&self) -> f32;
18 }
19
20 pub struct MachineJiji {
21 ram: Vec<corsair_vengeance::Controller>,
22 b650e_device: asus_aura_usb::Device,
23 // a770: a770::A770,
24 // gpu_devices: intel_arc::Devices,
25 gpus: Vec<nvapi::PhysicalGpu>,
26 }
27
28 impl MachineJiji {
29 pub fn new() -> anyhow::Result<Self> {
30 let api = hidapi::HidApi::new().unwrap();
31 Ok(MachineJiji {
32 ram: vec![
33 corsair_vengeance::Controller::new(0x19),
34 corsair_vengeance::Controller::new(0x1B),
35 ],
36 b650e_device: asus_aura_usb::Device::new(&api, asus_aura_usb::Motherboard::Asus650e)?,
37 // a770: a770::A770::new()?,
38 // gpu_devices: unsafe { intel_arc::GetDevices() },
39 gpus: nvapi::PhysicalGpu::enumerate()?,
40 })
41 }
42 }
43
44 impl Machine for MachineJiji {
45 fn set_color(&mut self, color: &rgb::RGB) {
46 for controller in &self.ram {
47 controller.set_color(&color);
48 }
49 self.b650e_device.set_color(&color);
50 // if let Err(error) = self.a770.set_color(color.red, color.green, color.blue) {
51 // error!("Unable to set color: {:?}", error);
52 // }
53 }
54
55 fn get_gpu_tmp(&self) -> f32 {
56 // unsafe { intel_arc::GetTemperature(self.gpu_devices, 0) as f32 }
57 self.gpus[0].thermal_settings(None).unwrap()[0]
58 .current_temperature
59 .0 as f32
60 }
61
62 fn get_cpu_tmp(&self) -> f32 {
63 cpu_temperature::read()
64 }
65 }
66
67 // impl Drop for MachineJiji {
68 // fn drop(&mut self) {
69 // unsafe {
70 // intel_arc::FreeDevices(self.gpu_devices);
71 // }
72 // }
73 // }
74
75 pub struct MachineLyssMetal {
76 crosshair_device: asus_aura_usb::Device,
77 corsair_lignting_pro: corsair_lighting_pro::Device,
78 lian_li_sl_infinity: lian_li_sl_infinity::Device,
79 gpus: Vec<nvapi::PhysicalGpu>,
80 }
81
82 impl MachineLyssMetal {
83 pub fn new() -> anyhow::Result<Self> {
84 let api = hidapi::HidApi::new()?;
85
86 nvapi::initialize().expect("Unable to initialize nvapi (Nvidia API)");
87
88 let machine = MachineLyssMetal {
89 crosshair_device: asus_aura_usb::Device::new(
90 &api,
91 asus_aura_usb::Motherboard::AsusCrosshairVIIIHero,
92 )?,
93 corsair_lignting_pro: corsair_lighting_pro::Device::new(
94 &api,
95 &rgb::RGB {
96 red: 0,
97 green: 255,
98 blue: 40,
99 },
100 ),
101 lian_li_sl_infinity: lian_li_sl_infinity::Device::new(&api),
102 gpus: nvapi::PhysicalGpu::enumerate()?,
103 };
104
105 // machine.set_mode_3080ti();
106 Ok(machine)
107 }
108
109 // Doesn't work: "Error: NotSupported".
110 // From OpenRGB, see the following files:
111 // * Controllers\GigabyteRGBFusion2GPUController\GigabyteRGBFusion2GPUControllerDetect.cpp
112 // * Controllers\GigabyteRGBFusion2GPUController\RGBController_GigabyteRGBFusion2GPU.cpp
113 // * Controllers\GigabyteRGBFusion2GPUController\GigabyteRGBFusion2GPUController.cpp
114 // * i2c_smbus\i2c_smbus_nvapi.cpp
115 // Implementation of nvapi-rs: https://github.com/arcnmx/nvapi-rs/blob/master/src/gpu.rs#L645
116 pub fn test_i2c(&self) {
117 // Test from 'GigabyteRGBFusion2GPUControllerDetect.cpp'
118 let data = [0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
119 self.gpus[0]
120 .i2c_write(
121 0,
122 Some(1),
123 false,
124 GIGABYTE_RTX3080TI_VISION_OC_ADDR,
125 &[],
126 &data,
127 i2c::I2cSpeed::Default,
128 )
129 .expect("Error");
130 }
131
132 fn set_mode_3080ti(&self) {
133 let data = [
134 RGB_FUSION2_GPU_REG_MODE,
135 0x01, // Mode (1: static).
136 0x00, // Speed.
137 0x63, // Brightness max.
138 0x00, // Mistery flag.
139 0x01, // Zone.
140 0x00,
141 0x00,
142 ];
143 self.gpus[0]
144 .i2c_write(
145 0,
146 Some(1),
147 false,
148 GIGABYTE_RTX3080TI_VISION_OC_ADDR,
149 &[],
150 &data,
151 i2c::I2cSpeed::Default,
152 )
153 .expect("Error");
154 }
155
156 fn set_color_3080ti(&self, color: &rgb::RGB) {
157 // TODO.
158 self.test_i2c();
159 }
160 }
161
162 impl Machine for MachineLyssMetal {
163 fn set_color(&mut self, color: &rgb::RGB) {
164 self.crosshair_device.set_color(&color).unwrap();
165 self.corsair_lignting_pro.set_color(&color);
166 self.lian_li_sl_infinity.set_color(&color);
167 // self.set_color_3080ti(&color); // TODO.
168 }
169
170 fn get_gpu_tmp(&self) -> f32 {
171 self.gpus[0].thermal_settings(None).unwrap()[0]
172 .current_temperature
173 .0 as f32
174 }
175
176 fn get_cpu_tmp(&self) -> f32 {
177 cpu_temperature::read()
178 }
179 }