|
| 1 | +use hidapi::{HidApi, HidDevice}; |
| 2 | + |
| 3 | +#[cfg(target_os = "windows")] |
| 4 | +use crate::touchscreen_win; |
| 5 | + |
| 6 | +pub const ILI_VID: u16 = 0x222A; |
| 7 | +pub const ILI_PID: u16 = 0x5539; |
| 8 | +pub const USI_BITMAP: u8 = 1 << 1; |
| 9 | +pub const MPP_BITMAP: u8 = 1 << 2; |
| 10 | + |
| 11 | +struct HidapiTouchScreen { |
| 12 | + device: HidDevice, |
| 13 | +} |
| 14 | + |
| 15 | +impl TouchScreen for HidapiTouchScreen { |
| 16 | + fn open_device() -> Option<HidapiTouchScreen> { |
| 17 | + debug!("Looking for touchscreen HID device"); |
| 18 | + match HidApi::new() { |
| 19 | + Ok(api) => { |
| 20 | + for dev_info in api.device_list() { |
| 21 | + let vid = dev_info.vendor_id(); |
| 22 | + let pid = dev_info.product_id(); |
| 23 | + let usage_page = dev_info.usage_page(); |
| 24 | + if vid != ILI_VID { |
| 25 | + trace!(" Skipping VID:PID. Expected {:04X}:*", ILI_VID); |
| 26 | + continue; |
| 27 | + } |
| 28 | + debug!( |
| 29 | + " Found {:04X}:{:04X} (Usage Page {:04X})", |
| 30 | + vid, pid, usage_page |
| 31 | + ); |
| 32 | + if usage_page != 0xFF00 { |
| 33 | + debug!(" Skipping usage page. Expected {:04X}", 0xFF00); |
| 34 | + continue; |
| 35 | + } |
| 36 | + if pid != ILI_PID { |
| 37 | + debug!(" Warning: PID is {:04X}, expected {:04X}", pid, ILI_PID); |
| 38 | + } |
| 39 | + |
| 40 | + debug!(" Found matching touchscreen HID device"); |
| 41 | + debug!(" Path: {:?}", dev_info.path()); |
| 42 | + debug!(" IC Type: {:04X}", pid); |
| 43 | + |
| 44 | + // Unwrapping because if we can enumerate it, we should be able to open it |
| 45 | + let device = dev_info.open_device(&api).unwrap(); |
| 46 | + debug!(" Opened device."); |
| 47 | + |
| 48 | + return Some(HidapiTouchScreen { device }); |
| 49 | + } |
| 50 | + } |
| 51 | + Err(e) => { |
| 52 | + error!("Failed to open hidapi. Error: {e}"); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + None |
| 57 | + } |
| 58 | + |
| 59 | + fn send_message(&self, message_id: u8, read_len: usize, data: Vec<u8>) -> Option<Vec<u8>> { |
| 60 | + let report_id = 0x03; |
| 61 | + let data_len = data.len(); |
| 62 | + let mut msg = [0u8; 0x40]; |
| 63 | + msg[0] = report_id; |
| 64 | + msg[1] = 0xA3; |
| 65 | + msg[2] = data_len as u8; |
| 66 | + msg[3] = read_len as u8; |
| 67 | + msg[4] = message_id; |
| 68 | + for (i, b) in data.into_iter().enumerate() { |
| 69 | + msg[5 + i] = b; |
| 70 | + } |
| 71 | + |
| 72 | + // Not sure why, but on Windows we just have to write an output report |
| 73 | + // HidApiError { message: "HidD_SetFeature: (0x00000057) The parameter is incorrect." } |
| 74 | + // Still doesn't work on Windows. Need to write a byte more than the buffer is long |
| 75 | + #[cfg(target_os = "windows")] |
| 76 | + let send_feature_report = false; |
| 77 | + #[cfg(not(target_os = "windows"))] |
| 78 | + let send_feature_report = true; |
| 79 | + |
| 80 | + if send_feature_report { |
| 81 | + debug!(" send_feature_report {:X?}", msg); |
| 82 | + self.device.send_feature_report(&msg).ok()?; |
| 83 | + } else { |
| 84 | + debug!(" Writing {:X?}", msg); |
| 85 | + self.device.write(&msg).ok()?; |
| 86 | + }; |
| 87 | + |
| 88 | + if read_len == 0 { |
| 89 | + return Some(vec![]); |
| 90 | + } |
| 91 | + |
| 92 | + let msg_len = 3 + data_len; |
| 93 | + let mut buf: [u8; 0x40] = [0; 0x40]; |
| 94 | + debug!(" Reading"); |
| 95 | + let res = self.device.read(&mut buf); |
| 96 | + debug!(" res: {:?}", res); |
| 97 | + debug!(" Read buf: {:X?}", buf); |
| 98 | + Some(buf[msg_len..msg_len + read_len].to_vec()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pub trait TouchScreen { |
| 103 | + fn open_device() -> Option<Self> |
| 104 | + where |
| 105 | + Self: std::marker::Sized; |
| 106 | + fn send_message(&self, message_id: u8, read_len: usize, data: Vec<u8>) -> Option<Vec<u8>>; |
| 107 | + |
| 108 | + fn check_fw_version(&self) -> Option<()> { |
| 109 | + println!("Touchscreen"); |
| 110 | + let res = self.send_message(0x42, 3, vec![0])?; |
| 111 | + let ver = res |
| 112 | + .iter() |
| 113 | + .skip(1) |
| 114 | + .fold(format!("{:02X}", res[0]), |acc, &x| { |
| 115 | + acc + "." + &format!("{:02X}", x) |
| 116 | + }); |
| 117 | + // Expecting 06.00.0A |
| 118 | + debug!(" Protocol Version: v{}", ver); |
| 119 | + |
| 120 | + let res = self.send_message(0x40, 8, vec![0])?; |
| 121 | + let ver = res |
| 122 | + .iter() |
| 123 | + .skip(1) |
| 124 | + .fold(res[0].to_string(), |acc, &x| acc + "." + &x.to_string()); |
| 125 | + println!(" Firmware Version: v{}", ver); |
| 126 | + |
| 127 | + let res = self.send_message(0x20, 16, vec![0])?; |
| 128 | + println!(" USI Protocol: {:?}", (res[15] & USI_BITMAP) > 0); |
| 129 | + println!(" MPP Protocol: {:?}", (res[15] & MPP_BITMAP) > 0); |
| 130 | + |
| 131 | + Some(()) |
| 132 | + } |
| 133 | + |
| 134 | + fn enable_touch(&self, enable: bool) -> Option<()> { |
| 135 | + self.send_message(0x38, 0, vec![!enable as u8, 0x00])?; |
| 136 | + Some(()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +pub fn print_fw_ver() -> Option<()> { |
| 141 | + #[cfg(target_os = "windows")] |
| 142 | + let device = touchscreen_win::NativeWinTouchScreen::open_device()?; |
| 143 | + #[cfg(not(target_os = "windows"))] |
| 144 | + let device = HidapiTouchScreen::open_device()?; |
| 145 | + |
| 146 | + device.check_fw_version() |
| 147 | +} |
| 148 | + |
| 149 | +pub fn enable_touch(enable: bool) -> Option<()> { |
| 150 | + #[cfg(target_os = "windows")] |
| 151 | + let device = touchscreen_win::NativeWinTouchScreen::open_device()?; |
| 152 | + #[cfg(not(target_os = "windows"))] |
| 153 | + let device = HidapiTouchScreen::open_device()?; |
| 154 | + |
| 155 | + device.enable_touch(enable) |
| 156 | +} |
0 commit comments