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