def open(self, vid, pid): retval = False # Stores an enumeration of all the connected USB HID devices #en = Enumeration(vid=vid, pid=pid) en = Enumeration() # for d in en.find(): # print(d.description()) # return a list of devices based on the search parameters devices = en.find(vid=vid, pid=pid, interface=0) if not devices: Logger.debug("RawHID: No matching device found") return None if len(devices) > 1: Logger.debug( "RawHID: more than one device found: {}".format(devices)) return None # open the device self.hid = devices[0] self.hid.open() Logger.debug("RawHID: Opened: {}".format(self.hid.description())) return True
def __init__(self): en = Enumeration() devices = en.find(vid=1155, pid=22352, serial="EMLTEC0003A1") for dev in devices: print(dev.serial_number) if len(devices) == 0: raise Exception("unable to find the board.") self.dev = devices[0] self.dev.open()
def __init__(self): self._MCU_ADRESS = 65 self._SERVICE_DATA = [0, 123, 16, self._MCU_ADRESS] self._STATIC_MESSAGE = [0, 0, 125] self._COMMAND_INFO = [32, 3, 255] self._PIDS = [0x8008, 0x8009, 0xa292, 0xa293] self._VID = 0x04d9 self._en = Enumeration() self._keeb = None _d = self._detect() if _d[0] == 1: print(_d[1]) exit() else: print(_d[1])
def list_devices(): """Return a list of the supported devices connected Returns: A list of string names of the devices supported which were found. Empty if no supported devices found """ devices = [] hid = Enumeration() all_hids = hid.find() if all_hids: for index, device in enumerate(all_hids): for device_name, spec in device_specs.items(): if (device.vendor_id == spec.hid_id[0] and device.product_id == spec.hid_id[1]): devices.append(device_name) return devices
def writeReport(self, value): # 现在都放到这里来做。打开、写入、关闭。 # 不要一次打开,多次写入。这样在有设备拔插的时候,健壮性很差。 try: en = Enumeration() devices = en.find(vid=VENDOR_ID, pid=PRODUCT_ID) dev = devices[0] dev.open() write_data = bytearray(60) write_data[0] = value len = dev.send_feature_report(write_data, report_id=0x24) # len = dev.write(write_data) # print("write len:", len) time.sleep(0.05) dev.close() except: print("写入HID数据失败") raise
def connectKeyboard(self): connected = Enumeration() devices = connected.find(product=self.KEYBOARD_NAME, interface=self.KEYBOARD_INTERFACE) if len(devices) == 0: raise RuntimeError( "Unable to find keyboard with product_id=%s and interface_id=%d" % (self.KEYBOARD_NAME, self.KEYBOARD_INTERFACE)) self.keyboard_ = devices[0] self.keyboard_.open() print('Keyboard connection established') # On the initial connection write our special sequence # 1st byte - 1 to indicate a new connection # 2nd byte - number of screens available to the keyboard self.keyboard_.write(bytearray([1, len(self.screens_)])) print('Sent connection string') time.sleep(1)
def is_connected(self): """ Checks if the USB device is still connected """ if self._is_open: err = hidapi.hid_read_timeout(self._device, ffi.NULL, 0, 0) if err == -1: return False else: return True else: en = Enumeration(vid=self.vendor_id, pid=self.product_id).find(path=self.path) if len(en) == 0: return False else: return True
print("\t\t(x, y, click)\t"+str(trackpadx_int)+", "+str(trackpady_int)+", "+str(trackpad_click)) print("\tTrigger: ") print("\t\t(pos, click)\t"+str(trigger_pos_int)+", "+str(trig_click)) print("\tBattery: ") print("\t\t(percent)\t"+str(battery_status_int)+" %") print("\tGrip Button: ") print("\t\t(click)\t"+str(grip_btn)) print("\tMenu Button: ") print("\t\t(click)\t"+str(menu_click)) def lsb_shift_hex_to_int(lsbstring): return int(lsbstring[2:4]+lsbstring[0:2],16) # Stores an enumeration of all the connected USB HID devices en = Enumeration() # return a list of devices based on the search parameters vive_wands = en.find(vid=0x28de, pid=0x2012, interface=2) vive_wand_0 = False vive_wand_1 = False if len(vive_wands) == 1: vive_wand_0 = vive_wands[0] if len(vive_wands) == 2: vive_wand_1 = vive_wands[1] if len(vive_wands) > 0: if vive_wand_0: vive_wand_0.open()
def signal_handler(sig, frame): try: # Blank screen on shutdown dev.send_feature_report(bytearray([0x61] + [0x00] * 641)) dev.close() print("\n") sys.exit(0) except: sys.exit(0) # Set up ctrl-c handler signal.signal(signal.SIGINT, signal_handler) # Stores an enumeration of all the connected USB HID devices en = Enumeration() # Return a list of devices based on the search parameters / Hardcoded to Apex 7 devices = en.find(vid=0x1038, pid=0x1612, interface=1) if not devices: devices = en.find(vid=0x1038, pid=0x1618, interface=1) if not devices: print("No devices found, exiting.") sys.exit(0) # Use first device found with vid/pid dev = devices[0] print("Press Ctrl-C to exit.\n") dev.open() while (1):
# else: # if type(serial) == bytes or type(serial) == bytearray: # serial = serial.decode('utf-8') # serial = ffi.new("wchar_t[]", serial) # dev = hidapi.hid_open(vendor_id, product_id, serial) # if dev: # return Device(dev) # else: # None if __name__ == "__main__": # Examples from easyhid import Enumeration # Stores an enumertion of all the connected USB HID devices en = Enumeration() # return a list of devices based on the search parameters devices = en.find(manufacturer="Company", product="Widget", interface=3) # print a description of the devices found for dev in devices: print(dev.description()) # open a device dev.open() # write some bytes to the device dev.write(bytearray([0, 1, 2, 3])) # read some bytes
pad = 'N' elif mam_pad == bytearray(b'\x00'): mam = 'N' pad = 'O' elif mam_pad == bytearray(b'\x02'): mam = 'O' pad = 'O' pulse = int(str(reponse[i * 32 + 48:i * 32 + 51], 'utf-8'), 16) dia = int(int(str(reponse[i * 32 + 51:i * 32 + 54], 'utf-8'), 16) / 4) syst = int(str(reponse[i * 32 + 54:i * 32 + 56], 'utf-8'), 16) map = int(dia + (syst - dia) / 3) print('{} {} {} {} {} {} {} ""'.format(date, pulse, dia, syst, map, mam, pad)) en = Enumeration() devices = en.find(vid=0x04b4, pid=0x5500) if len(devices) != 1: print(u'Dispositif non détecté: absent, panne...') sys.exit() dev = devices[0] dev.open() try: opts, args = getopt.getopt(sys.argv[1:], "rdtngs:") except getopt.GetoptError as err: sys.stderr.write(usage) sys.exit(1) if len(sys.argv) == 1 or len(sys.argv) > 3:
tz, coins, coinIDs, hotkey = getConf() latestVersion = requests.get( "https://api.github.com/repos/KarlHlmgrn/crypto-oled/releases/latest") if latestVersion.status_code == 200: latestVersionJSON = latestVersion.json() if ("v" + str(version)) != latestVersionJSON["tag_name"]: print( "\nThere is a new version of crypto-oled available, get it at https://github.com/KarlHlmgrn/crypto-oled/releases/tag/" + str(latestVersionJSON["tag_name"]) + "\n") else: print("You are running the latest version of crypto-oled!") signal.signal(signal.SIGINT, signal_handler) en = Enumeration() devices = en.find(product="SteelSeries Apex Pro", interface=1) if not devices: devices = en.find(product="SteelSeries Apex Pro TKL", interface=1) if not devices: devices = en.find(product="SteelSeries Apex 7", interface=1) if not devices: devices = en.find(product="SteelSeries Apex 7 TKL", interface=1) if not devices: devices = en.find(product="SteelSeries Apex 5", interface=1) if not devices: print("No devices found, exiting.") sys.exit(0) dev = devices[0]
def open(callback: Callable[[object], None] = None, dof_callback: Callable[[object], None] = None, dof_callback_arr: List[DofCallback] = None, button_callback: Callable[[object, list], None] = None, button_callback_arr: List[ButtonCallback] = None, set_nonblocking_loop=True, device: str = None, DeviceNumber=0) -> Union[None, DeviceSpec]: """ Open a 3D space navigator device. Makes this device the current active device, which enables the module-level read() and close() calls. For multiple devices, use the read() and close() calls on the returned object instead, and don't use the module-level calls. Parameters: callback: If callback is provided, it is called only on DoF state changes with a copy of the current state tuple. dof_callback: If dof_callback is provided, it is called only on DOF state changes with the argument (state). dof_callback_arr: If dof_callback_arr is provided, it is called only on DOF state changes with the argument (state, axis). button_callback: If button_callback is provided, it is called only on button state changes with the argument (state, buttons). button_callback_arr: If button_callbacks_arr is provided, it is called only on specific button state true with the argument (state, buttons, pressed_buttons). set_nonblocking_loop: Disable waiting for input from SpaceMouse. It is required for using callbacks device: name of device to open. Must be one of the values in supported_devices. If None, chooses the first supported device found. DeviceNumber: use the first (DeviceNumber=0) device you find. (for universal wireless receiver) Returns: Device object if the device was opened successfully None if the device could not be opened """ # only used if the module-level functions are used global _active_device # if no device name specified, look for any matching device and choose the first if device == None: all_devices = list_devices() if len(all_devices) > 0: device = all_devices[0] else: raise Exception("No device connected/supported!") found_devices = [] hid = Enumeration() all_hids = hid.find() if all_hids: for index, dev in enumerate(all_hids): spec = device_specs[device] if dev.vendor_id == spec.hid_id[ 0] and dev.product_id == spec.hid_id[1]: found_devices.append({"Spec": spec, "HIDDevice": dev}) print("%s found" % device) else: print("No HID devices detected") return None if len(found_devices) == 0: print("No supported devices found") return None else: if len(found_devices) <= DeviceNumber: DeviceNumber = 0 if len(found_devices) > DeviceNumber: # Check that the input configuration has the correct components # Raise an exception if it encounters incorrect component. check_config(callback, dof_callback, dof_callback_arr, button_callback, button_callback_arr) # create a copy of the device specification spec = found_devices[DeviceNumber]["Spec"] dev = found_devices[DeviceNumber]["HIDDevice"] new_device = copy.deepcopy(spec) new_device.device = dev # set the callbacks new_device.callback = callback new_device.dof_callback = dof_callback new_device.dof_callback_arr = dof_callback_arr new_device.button_callback = button_callback new_device.button_callback_arr = button_callback_arr # open the device new_device.open() # set nonblocking/blocking mode new_device.set_nonblocking_loop = set_nonblocking_loop dev.set_nonblocking(set_nonblocking_loop) _active_device = new_device return new_device print("Unknown error occured.") return None
def showDevices(self): connected = Enumeration() connected.show()
class Keeb(object): def __init__(self): self._MCU_ADRESS = 65 self._SERVICE_DATA = [0, 123, 16, self._MCU_ADRESS] self._STATIC_MESSAGE = [0, 0, 125] self._COMMAND_INFO = [32, 3, 255] self._PIDS = [0x8008, 0x8009, 0xa292, 0xa293] self._VID = 0x04d9 self._en = Enumeration() self._keeb = None _d = self._detect() if _d[0] == 1: print(_d[1]) exit() else: print(_d[1]) def _detect(self): for pid in self._PIDS: try: devices = self._en.find(vid=self._VID, pid=pid, interface=1) self._keeb = devices[0] info = ( f'\nDevice manufacturer: {self._keeb.manufacturer_string}' f'\n\nProduct: {self._keeb.product_string}\n' f'\nSerial Number: {self._keeb.serial_number}\n') return [0, info] except: pass return [1, "\nNo AnnePro2 keeb found\n"] def set_all(self, r: int, g: int, b: int): """ Sets all keys to one same color. It expects 3 positional arguments to a RGB color. Example : set_all(255,255,255) """ try: rgb = [r, g, b] message = self._SERVICE_DATA + \ [16] + [7] + self._STATIC_MESSAGE + \ self._COMMAND_INFO + [1] + rgb self._keeb.open() self._keeb.write(message) self._keeb.close() except: print(self._warning("ERROR: Verify your RGB format\n")) def _generate_multi_color(self, arrayOfRgbValues: list): hid_command = [] real_command_info_length = len(self._COMMAND_INFO) + 1 maxMessageLength = 55 - real_command_info_length messagesToSendAmount = math.ceil( len(arrayOfRgbValues) / maxMessageLength) val_1 = len(arrayOfRgbValues) % maxMessageLength val_2 = maxMessageLength if val_1 == 0 else val_1 arrayOfRgbValuesCopy = arrayOfRgbValues.copy() for i in range(0, messagesToSendAmount): rgb = arrayOfRgbValuesCopy[:maxMessageLength] arrayOfRgbValuesCopy = arrayOfRgbValuesCopy[maxMessageLength:] e = (messagesToSendAmount << 4) + i a = (val_2 + real_command_info_length) if messagesToSendAmount - \ 1 == i else (maxMessageLength + real_command_info_length) message = self._SERVICE_DATA + \ [e] + [a] + self._STATIC_MESSAGE + self._COMMAND_INFO + \ [2] + rgb hid_command.append(message) return hid_command def set_multi(self, rgb: list): """ Receives a bi-dimensional array of sequencial RGB values corresponding to keys left to right. Example: set_multi([ [255,0,0], [0,100,0], ... [100,100,100] ]) The fisrt item being ESC key, second being 1 key, last being right control key. """ black = [0, 0, 0] rgb_matrix = [] for key in ANNE_LAYOUT: if ANNE_LAYOUT.get(key) is None: rgb_matrix += black else: try: rgb_matrix += rgb[ANNE_LAYOUT.get(key)] except: rgb_matrix += black hid_command = self._generate_multi_color(rgb_matrix) self._keeb.open() for command in hid_command: self._keeb.write(command) sleep(0.05) self._keeb.close() def _warning(self, str): return '\033[91m' + str + '\033[0m'