|
| 1 | +use hidapi::{HidApi, HidDevice, HidError}; |
| 2 | + |
| 3 | +pub const ILI_VID: u16 = 0x222A; |
| 4 | +pub const ILI_PID: u16 = 0x5539; |
| 5 | +pub const USI_BITMAP: u8 = 1 << 1; |
| 6 | +pub const MPP_BITMAP: u8 = 1 << 2; |
| 7 | + |
| 8 | +fn send_message(device: &HidDevice, message_id: u8, read_len: usize) -> Result<Vec<u8>, HidError> { |
| 9 | + let report_id = 0x03; |
| 10 | + let write_len = 0x01; |
| 11 | + let mut msg = vec![report_id, 0xA3, write_len, read_len as u8, message_id]; |
| 12 | + device.send_feature_report(&msg)?; |
| 13 | + |
| 14 | + msg.pop(); |
| 15 | + let mut buf: [u8; 255] = [0; 255]; |
| 16 | + device.read(&mut buf[..read_len + msg.len()])?; |
| 17 | + Ok(buf[msg.len()..msg.len() + read_len].to_vec()) |
| 18 | +} |
| 19 | + |
| 20 | +fn check_fw_version(device: &HidDevice) -> Result<(), HidError> { |
| 21 | + let res = send_message(device, 0x40, 8)?; |
| 22 | + let ver = res |
| 23 | + .iter() |
| 24 | + .skip(1) |
| 25 | + .fold(res[0].to_string(), |acc, &x| acc + "." + &x.to_string()); |
| 26 | + println!(" Firmware Version: v{}", ver); |
| 27 | + |
| 28 | + let res = send_message(device, 0x20, 16)?; |
| 29 | + println!(" USI Protocol: {:?}", (res[15] & USI_BITMAP) > 0); |
| 30 | + println!(" MPP Protocol: {:?}", (res[15] & MPP_BITMAP) > 0); |
| 31 | + |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | + |
| 35 | +pub fn print_touchscreen_fw_ver() -> Result<(), HidError> { |
| 36 | + debug!("Looking for touchscreen HID device"); |
| 37 | + match HidApi::new() { |
| 38 | + Ok(api) => { |
| 39 | + for dev_info in api.device_list() { |
| 40 | + let vid = dev_info.vendor_id(); |
| 41 | + let pid = dev_info.product_id(); |
| 42 | + let usage_page = dev_info.usage_page(); |
| 43 | + |
| 44 | + debug!(" Found {:04X}:{:04X} (Usage Page {:04X})", vid, pid, usage_page); |
| 45 | + if vid != ILI_VID { |
| 46 | + debug!(" Skipping VID:PID. Expected {:04X}:*", ILI_VID); |
| 47 | + continue; |
| 48 | + } |
| 49 | + if usage_page != 0xFF00 { |
| 50 | + debug!(" Skipping usage page. Expected {:04X}", 0xFF00); |
| 51 | + continue; |
| 52 | + } |
| 53 | + if pid != ILI_PID { |
| 54 | + debug!(" Warning: PID is {:04X}, expected {:04X}", pid, ILI_PID); |
| 55 | + } |
| 56 | + |
| 57 | + debug!(" Found matching touchscreen HID device"); |
| 58 | + println!("Touchscreen"); |
| 59 | + println!(" IC Type: {:04X}", pid); |
| 60 | + |
| 61 | + // Unwrapping because if we can enumerate it, we should be able to open it |
| 62 | + let device = dev_info.open_device(&api).unwrap(); |
| 63 | + if let Err(e) = check_fw_version(&device) { |
| 64 | + error!("Failed to read touchscreen firmware version {:?}", e); |
| 65 | + continue; |
| 66 | + }; |
| 67 | + } |
| 68 | + } |
| 69 | + Err(e) => { |
| 70 | + eprintln!("Failed to open hidapi. Error: {e}"); |
| 71 | + return Err(e); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + Ok(()) |
| 76 | +} |
0 commit comments