Replace some unwrap by Result
[temp2RGB.git] / src / a770.rs
1 // use windows::{
2 // core::w,
3 // Win32::{self, Storage::FileSystem},
4 // };
5 // use netcorehost::{nethost, pdcstr};
6
7 // pub fn set_rgb(r: u8, g: u8, b: u8) {
8 // unsafe {
9 // let lib = libloading::Library::new("IntelOCWrapper.dll").unwrap();
10
11 // let fun: libloading::Symbol<unsafe extern fn(u8, u8, u8, u8) -> bool> = lib.get(b"SetLEDColor").unwrap();
12 // let ctlInit: libloading::Symbol<unsafe extern fn(u32) -> std::ffi::c_void> = lib.get(b"ctlInit").unwrap();
13 // let ctlInit: libloading::Symbol<unsafe extern "C++" fn(u32) -> std::ffi::c_void> = lib.get(b"SetLEDColor").unwrap();
14 // println!("ok");
15 // }
16
17 // let hostfxr = nethost::load_hostfxr().unwrap();
18 // let context = hostfxr.initialize_for_dotnet_command_line(pdcstr!("IntelOCWrapper.dll")).unwrap();
19 // let result = context.run_app().value();
20
21 // unsafe {
22 // let handle = FileSystem::CreateFileW(
23 // // w!("\\\\.\\Intel_NF_I2C"),
24 // w!("\\\\.\\VIDEO\\INTC_I2C"),
25 // // w!("\\\\.\\WinRing0_1_2_0"),
26 // 3221225472,
27 // FileSystem::FILE_SHARE_MODE(0),
28 // None,
29 // FileSystem::FILE_CREATION_DISPOSITION(3),
30 // FileSystem::FILE_FLAGS_AND_ATTRIBUTES(1073741824),
31 // Win32::Foundation::HANDLE::default(),
32 // );
33
34 // println!("handle: {:?}", handle);
35 // }
36
37 //"\\\\.\\Intel_NF_I2C"
38 // }
39
40 // internal static \u0024ArrayType\u0024\u0024\u0024BY08E \u003FA0x171ed149\u002E\u003FprevData\u0040\u003F1\u003F\u003FSetLEDBehavior\u0040CVGAAdaptor\u0040\u0040UEAA_NEEEEEEEEE\u0040Z\u00404PAEA;
41 // public static __FnPtr<_ctl_result_t (_ctl_init_args_t*, _ctl_api_handle_t**)> __m2mep\u0040\u003FctlInit\u0040\u0040\u0024\u0024J0YA\u003FAW4_ctl_result_t\u0040\u0040PEAU_ctl_init_args_t\u0040\u0040PEAPEAU_ctl_api_handle_t\u0040\u0040\u0040Z;
42
43 use log::error;
44
45 use std::{
46 io::prelude::*,
47 net::TcpStream,
48 process::{Child, Command},
49 };
50
51 pub struct A770 {
52 process: Child,
53 stream: TcpStream,
54 }
55
56 impl A770 {
57 pub fn new() -> anyhow::Result<Self> {
58 Ok(A770 {
59 process: Command::new(r"IntelOC.exe").spawn()?,
60 stream: TcpStream::connect("127.0.0.1:6577")?,
61 })
62 }
63
64 pub fn set_color(&mut self, r: u8, g: u8, b: u8) -> anyhow::Result<()> {
65 let buffer: [u8; 3] = [r, g, b];
66 self.stream.write(&buffer).map(|_| ())?;
67 Ok(())
68 }
69 }
70
71 impl Drop for A770 {
72 fn drop(&mut self) {
73 if let Err(error) = self.process.kill().and(self.process.try_wait()) {
74 error!("Unable to kill the child process: {:?}", error);
75 }
76 }
77 }