First commit.
[temp2RGB.git] / src / corsair_vengeance.rs
1 use std::time::Duration;
2
3 use crate::{piix4_i2c, rgb::RGB, timer};
4
5 // use windows::{*, Win32::{System::LibraryLoader::*, Foundation::HCS_E_CONNECTION_CLOSED, Security::InitializeAcl}, Devices::I2c::*, core::HSTRING};
6
7 use crc::{Algorithm, Crc};
8
9 const CRC8_ALG: Algorithm<u8> = Algorithm {
10 width: 8,
11 poly: 0x7,
12 init: 0x0,
13 refin: false,
14 refout: false,
15 xorout: 0x00,
16 check: 0x00,
17 residue: 0x00,
18 };
19
20 const BUS: i32 = 0;
21 const BUS_ADDRESS: i32 = 0x0B00;
22
23 // Called "device location" in 'CorsairDominatorPlatinumController' class.
24 const ADDRESS_DDR_1: i32 = 0x19;
25 const ADDRESS_DDR_2: i32 = 0x1B;
26
27 const CORSAIR_LED_COUNT: usize = 12;
28
29 pub struct Controller {
30 bus: piix4_i2c::I2c,
31 ddr_address: u8,
32 }
33
34 impl Controller {
35 pub fn new(ddr_address: u8) -> Self {
36 Controller {
37 bus: piix4_i2c::I2c::new(0x0B00),
38 ddr_address,
39 }
40 }
41
42 pub fn test(&self) {
43 self.bus.i2c_smbus_write_quick(self.ddr_address, 0);
44 }
45
46 pub fn set_color(&self, color: &RGB) {
47 let mut data = [0u8; CORSAIR_LED_COUNT * 3 + 2];
48 data[0] = 0xC;
49
50 for i in 0..CORSAIR_LED_COUNT {
51 let offset = i * 3 + 1;
52 data[offset] = color.red;
53 data[offset + 1] = color.green;
54 data[offset + 2] = color.blue;
55 }
56
57 let crc = Crc::<u8>::new(&CRC8_ALG);
58 let mut digest = crc.digest();
59 digest.update(&data[0..data.len() - 1]); // '-1' to not take the last byte.
60 data[data.len() - 1] = digest.finalize();
61
62 let timer = timer::Sleep::new();
63
64 self.bus
65 .write_block_data(self.ddr_address, 0x31, &data[0..piix4_i2c::I2C_BLOCK_MAX]);
66 timer.wait(Duration::from_micros(800));
67
68 self.bus
69 .write_block_data(self.ddr_address, 0x32, &data[piix4_i2c::I2C_BLOCK_MAX..]);
70 timer.wait(Duration::from_micros(200));
71 }
72 }
73
74 // TESTS WITH I2C from winapi:
75
76 // let connection_settings = I2cConnectionSettings::Create(ADDRESS_DDR_1).unwrap();
77
78 // // For A770: "DISPLAY\\INTC_I2C\\7&3255D98A&0&UID26040"
79 // // "PCI\\VEN_1022&DEV_790B&SUBSYS_88771043&REV_71\\3&11583659&0&A0"
80
81 // let selector = I2cDevice::GetDeviceSelector().unwrap();
82 // println!("{:?}", selector);
83
84 // let devices_async = Devices::Enumeration::DeviceInformation::FindAllAsync().unwrap(); // Devices::Enumeration::DeviceInformation::FindAllAsyncAqsFilter(&selector).unwrap();
85 // let devices = devices_async.get().unwrap();
86
87 // // println!("{:?}", devices.Size());
88
89 // for i in 0..devices.Size().unwrap() {
90 // let device = devices.GetAt(i).unwrap();
91 // println!("Device Name: {:?}", device.Name().unwrap());
92 // println!("Device Kind: {:?}", device.Kind().unwrap());
93 // println!("Device ID: {:?}", device.Id().unwrap());
94 // println!("-----------------")
95 // }
96
97 // // let device_id = "PCI\\VEN_1022&DEV_790B";
98
99 // // let async_get_device = I2cDevice::FromIdAsync(&HSTRING::from(device_id), &connection_settings).unwrap();
100 // // let device = async_get_device.get();
101
102 // // println!("{:?}", device);