def __init__(self, pid=22352): """ """ # stores an enumeration of all the connected USB HID devices en = easyhid.Enumeration() # return a list of devices based on the search parameters devices = en.find(vid=1155, pid=pid) # print a description of the devices found for dev in devices: print(dev.description()) if len(devices) == 0: raise Exception( 'no device detected, make sure the arm is powered, connected and you have read-write permissions on /dev/hidraw' ) self._device = devices[0] # open a device self._device.open() self._serial_number = self._device.serial_number.encode('ascii') print('connected to xArm controller serial number: ' + self._serial_number) # compute bits_to_rads: Lewansoul bits are 1000 for 240 degree # range, then convert from degrees to radians self.bits_to_si = math.radians((240.0 / 1000.0)) # list of arms self._arms = [] # lock to make sure read/writes are safe self._lock = threading.RLock()
def programFirmwareHex(self, boot_vid, boot_pid, serial_num, file_name): device = None for i in range(1): en = easyhid.Enumeration(vid=boot_vid, pid=boot_pid).find() # Look for devices with matching serial_num number for dev in en: if dev.serial_number == serial_num: device = dev break # if a device was found with matching vid:pid, but it doesn't have # a matching serial_num number, then assume that the bootloader/firmware # doesn't set the serial_num number to the same value, so just program # the first matching device if len(en) != 0: device = en[0] break if device == None: error_msg_box("Couldn't connect to the device's bootloader") return else: if self.tryOpenDevice(device): return self.program_xusb_boot_firmware_hex(device, file_name) self.statusBar().showMessage("Finished updating firmware", timeout=STATUS_BAR_TIMEOUT)
def find_devices(vid=efm8boot.ids.SILICON_LABS_USB_ID, pid=0x0000, mcu=None, path=None): """ Find all EFM8 HID bootloaders that are connected Parameters: vid: USB vendor id to match pid: USB product id to match mcu: microcontroller part name to match """ # Find all Silicon Labs USB IDs en = easyhid.Enumeration(vid=vid, pid=pid) devices = [] for hidDevice in en.find(path=path): if hidDevice.product_id in efm8boot.ids.EFM8UB_HID_DEVICES: boot = efm8boot.EFM8BootloaderHID(hidDevice) # if given, check that the mcu part matches if mcu and boot.info.name != mcu: continue devices.append(boot) return devices
def task(self, args): dev_list = easyhid.Enumeration().find(interface=3) for dev in dev_list: print_hid_info(dev) if args.verbosity != None: dev.open() if args.verbosity >= 1: print_device_info(dev) print_layout_info(dev)
def tryOpenDevicePath(self, device_path): try: device = easyhid.Enumeration().find(path=device_path)[0] device.open() return device except: msg_box( description="Failed to open device! Check it is still present " "and you have permission to write to it.", title="USB Device write error") return None
def tryOpenDevicePath2(self, device_path): try: device = easyhid.Enumeration().find(path=device_path)[0] return KeyplusKeyboard(device) except Exception as err: msg_box( description="Failed to open device! Check it is still present " "and you have permission to write to it. ErrorMsg: {}".format( err), title="USB Device write error") traceback.print_exc(file=sys.stderr) return None
def updateList(self): self.updateCounter += 1 deviceInfoList = list( filter(is_supported_device, easyhid.Enumeration().find())) deleteList = [] deviceIds = [dev.path for dev in deviceInfoList] oldDevices = [] newDevices = [] # look at the list of connected devices and find out which devices are # no longer connected and remove them i = 0 while i < self.layout.count(): devItem = self.layout.itemAt(i).widget() if hasattr(devItem, "device") and (devItem.device.path in deviceIds): oldDevices.append(devItem.device) i += 1 else: self.layout.takeAt(i).widget().deleteLater() # Now find the list of new devices oldDeviceIds = [dev.path for dev in oldDevices] for dev in deviceInfoList: if dev.path in oldDeviceIds: continue else: newDevices.append(dev) for devInfo in newDevices: devWidget = DeviceWidget(devInfo) if devWidget.label: self.deviceWidgets.append(devWidget) self.layout.addWidget(devWidget) devWidget.program.connect(self.programming_handler) devWidget.show_info.connect(self.info_handler) devWidget.reset.connect(self.reset_handler) # if len(self.deviceWidgets) == 0: if len(oldDevices) == 0 and len(newDevices) == 0: n = self.updateCounter % 4 label = QLabel("Scanning for devices" + "." * n + " " * (4 - n)) self.layout.setAlignment(Qt.AlignCenter) self.layout.addWidget(label) self.deviceWidgets = [] else: self.layout.setAlignment(Qt.AlignTop) self.updateCounter = 0
def __init__(self, verbose=False): self.verbose = verbose en = easyhid.Enumeration() devices = en.find(vid=0x0483, pid=0x5750) if self.verbose: for dev in devices: print(dev.description()) if len(devices) < 1: print('ERROR: No robotic arms found, check for terminators') sys.exit(1) self.dev = devices[0] self.dev.open() if self.verbose: print('Connected to xArm device')
def __init__(self, pid=22352): # Stores an enumeration of all the connected USB HID devices en = easyhid.Enumeration() # return a list of devices based on the search parameters devices = en.find(vid=1155, pid=pid) # print a description of the devices found # for dev in devices: # print(dev.description()) assert len(devices) > 0 self.dev = devices[0] # open a device self.dev.open() print('Connected to xArm device')
def find_matching_device(self, args): hid_list = easyhid.Enumeration() target_vid = protocol.DEFAULT_VID target_pid = None if args.vid_pid: matches = args.vid_pid.split(":") if len(matches) == 1: try: target_vid = int(matches[0], base=16) if target_vid > 0xffff: raise Exception except: print("Bad VID/PID pair: " + args.vid_pid, file=sys.stderr) exit(2) elif len(matches) == 2: try: if matches[0] == '': target_vid = None else: target_vid = target_vid = int(matches[0], base=16) if matches[1] == '': target_pid = None else: target_pid = target_pid = int(matches[1], base=16) if target_vid and target_vid > 0xffff: raise Exception if target_pid and target_pid > 0xffff: raise Exception except: print("Bad VID/PID pair: " + args.vid_pid, file=sys.stderr) exit(2) if args.serial != None: args.serial = self.get_similar_serial_number(hid_list, args.serial) matching_devices = hid_list.find(vid=target_vid, pid=target_pid, serial=args.serial, interface=3) if len(matching_devices) == 0: print("Couldn't find a matching device to open", file=sys.stderr) exit(1) return matching_devices[0]
def programFirmwareHex(self, boot_vid, boot_pid, serial_num, file_name): device = None matches = [] en = easyhid.Enumeration().find() # Look for devices with matching serial_num number for dev in en: if serial_num and dev.serial_number == serial_num: device = dev break elif (not serial_num and dev.vendor_id == boot_vid and dev.product_id == boot_pid): # if a device was found with matching vid:pid, but the # original device didn't expose a serial number, then # assume that the bootloader/firmware doesn't set the # serial_num number, so just program the first matching # device device = dev break if device == None: error_msg_box("Couldn't connect to the device's bootloader") return else: if self.tryOpenDevice(device): return elif is_xusb_bootloader_device(device): self.program_xusb_boot_firmware_hex(device, file_name) elif is_kp_boot_device(device): self.program_kp_boot_32u4_firmware_hex(device, file_name) elif is_nrf24lu1p_bootloader_device: error_msg_box("Programming nrf24 is currently unsupported") return elif is_efm8_boot_device(device): self.program_efm8_boot_firmware_hex(device, file_name) self.statusBar().showMessage("Finished updating firmware", timeout=STATUS_BAR_TIMEOUT)
def __init__(self): '''Abstraction of HID device so that the interface is the same across platforms Raises ------ TypeError If operating system is not Darwin, Linux or Windows Attributes ---------- device : obj hid device type : int indicates how to interact with hid device. if 1, send message as bytes and provide size argument to read; if 0, send message as bytearray without leading 0 ''' if platform.system() == 'Linux': import easyhid en = easyhid.Enumeration() devices = en.find(vid=1155, pid=22352) assert len(devices) == 1 self.device = devices[0] self.device.open() self.type = 0 elif platform.system() == 'Windows': import hid self.device = hid.Device(vid=1155, pid=22352) self.type = 1 elif platform.system() == 'Darwin': import hid self.device = hid.device() self.device.open(1155, 22352) self.type = 0 else: raise TypeError('unsupported operating system')
def find_devices(vid=USB_VID, pid=USB_PID, chip_name=None, min_version=None, path=None): hid_devices = easyhid.Enumeration().find(vid=vid, pid=pid) result = [] for hid_dev in hid_devices: try: boot_dev = BootloaderDevice(hid_dev) except: print( "Warning: couldn't open a HID device check permissions and that" " it is not already in use: {}".format(hid_dev.description()), file=sys.stderr) continue if chip_name and boot_dev.chip_name != chip_name: continue if min_version and boot_dev.version < min_version: continue if path and boot_dev.path != path: continue result.append(boot_dev) return result
def find_matching_device(self, args): hid_list = easyhid.Enumeration() target_vid = protocol.DEFAULT_VID target_pid = None if args.vid_pid: matches = args.vid_pid.split(":") if len(matches) == 1: try: target_vid = int(matches[0], base=16) if target_vid > 0xffff: raise Exception except: print("Bad VID/PID pair: " + args.vid_pid, file=sys.stderr) exit(EXIT_MATCH_DEVICE) elif len(matches) == 2: try: if matches[0] == '': target_vid = None else: target_vid = target_vid = int(matches[0], base=16) if matches[1] == '': target_pid = None else: target_pid = target_pid = int(matches[1], base=16) if target_vid and target_vid > 0xffff: raise Exception if target_pid and target_pid > 0xffff: raise Exception except: print("Bad VID/PID pair: " + args.vid_pid, file=sys.stderr) exit(EXIT_MATCH_DEVICE) if args.serial != None: args.serial = self.get_similar_serial_number(hid_list, args.serial) matching_devices = hid_list.find( vid=target_vid, pid=target_pid, serial=args.serial, interface=3 ) if len(matching_devices) == 0: print("Couldn't find a matching device to open", file=sys.stderr) exit(EXIT_MATCH_DEVICE) if args.dev_id != None: matching_id_list = [] for dev in matching_devices: try: dev.open() dev_info = protocol.get_device_info(dev) dev.close() if dev_info.id == args.dev_id: matching_id_list.append(dev) except: print("Warning: couldn't open device: " + str(dev), file=sys.stderr) dev.close() matching_devices = matching_id_list num_matches = len(matching_devices) if num_matches== 0: print("Couldn't find any matching devices.", file=sys.stderr) exit(EXIT_MATCH_DEVICE) elif num_matches == 1: return matching_devices[0] elif num_matches > 1: print("Error: found {} matching devices, select a specifc device or " "disconnect the other devices".format(num_matches), file=sys.stderr) exit(EXIT_MATCH_DEVICE)
def find_devices(name=None, serial_number=None, vid_pid=None, device_id=None, hid_enumeration=None, chip_name=None): """ Returns a list of keyplus keyboards that are currently connected to the computer. The arguments can be used to filter result. Args: name: filter list by the device name serial_number: filter list by the devices serial number. Tries for an exact match, but will accept a partial match if an exact match is not found. vid_pid: filter list by the USB vendor and product id for the device in the format 'VID:PID'. device_id: filter list by device id hid_enumeration: an enumeration of USB devices to test. If this argument is not set, the function will call `easyhid.Enumeration()` itself. """ if chip_name != None: chip_id = get_chip_id_from_name(chip_name) else: chip_id = None if not hid_enumeration: hid_enumeration = easyhid.Enumeration() target_vid = 0 target_pid = 0 if vid_pid: matches = vid_pid.split(":") if len(matches) == 1: try: target_vid = int(matches[0], base=16) except TypeError: KeyplusError("Bad VID/PID pair: " + vid_pid) elif len(matches) == 2: try: if matches[0] == '': target_vid = None else: target_vid = target_vid = int(matches[0], base=16) if matches[1] == '': target_pid = None else: target_pid = target_pid = int(matches[1], base=16) except TypeError: raise KeyplusError("Bad VID/PID pair: " + vid_pid) assert(target_vid <= 0xffff) assert(target_pid <= 0xffff) if serial_number != None: serial_number = _get_similar_serial_number(hid_enumeration, serial_number) matching_devices = hid_enumeration.find( vid=target_vid, pid=target_pid, serial=serial_number, interface=INTERFACE_VENDOR ) matching_dev_list = [] for hid_device in matching_devices: try: if ((target_vid == 0 and target_pid == 0) and not is_keyplus_usb_id(hid_device.vendor_id, hid_device.product_id) ): # Ignore devices that don't use the keyplus vendor IDs continue new_kb = KeyplusKeyboard(hid_device) if device_id != None and device_id != new_kb.device_id: continue if name != None and (name not in new_kb.name): continue if chip_id != None and (chip_id != new_kb.firmware_info.chip_id): continue matching_dev_list.append(new_kb) except (KeyplusError, easyhid.HIDException) as err: # Couldn't open the device. Could be in use by another program or # do not have correct permissions to read from it. print("Warning: couldn't open device: " + str(err), file=sys.stderr) hid_device.close() return matching_dev_list
def has_fw_support_wireless(firmwareInfo): return (firmwareInfo.connectivity_support_flags & SUPPORT_WIRELESS) != 0 def has_fw_support_i2c(firmwareInfo): return (firmwareInfo.connectivity_support_flags & SUPPORT_I2C) != 0 def has_fw_support_unifying(firmwareInfo): return (firmwareInfo.connectivity_support_flags & SUPPORT_UNIFYING) != 0 def has_fw_support_usb(firmwareInfo): return (firmwareInfo.connectivity_support_flags & SUPPORT_USB) != 0 def has_fw_support_bluetooth(firmwareInfo): return (firmwareInfo.connectivity_support_flags & SUPPORT_BT) != 0 if __name__ == "__main__": import easyhid # hid_devs = easyhid.Enumeration(vid=0) hid_list = easyhid.Enumeration() sub_set = hid_list.filter(vid=0x6666, pid=0x1111, interface=3) device = sub_set[0].open() print(get_firmware_info(device)) # print(get_device_info(device)) # print(get_layers(device, 0))
def __init__(self, serial_number=None): '''Abstraction of HID device so that the interface is the same across platforms Raises ------ TypeError If operating system is not Darwin, Linux or Windows Attributes ---------- device : obj hid device type : int indicates how to interact with hid device. if 1, send message as bytes and provide size argument to read; if 0, send message as bytearray without leading 0 serial_number : str serial number of device ''' if platform.system() == 'Linux': import easyhid self.exception = easyhid.easyhid.HIDException en = easyhid.Enumeration() devices = en.find(vid=1155, pid=22352, serial=serial_number) if len(devices) == 0: print( '\n[ERROR] No device found. Ensure that the xarm is connected via ' 'usb cable and the power is on.\n' 'Turn the xarm off and back on again if needed.\n') exit() elif len(devices) > 1: serial_numbers = ', '.join( [f"\t{d.serial_number}" for d in devices]) print( '\n[ERROR] More than 1 xarm device found with the following serial numbers: \n' f' {serial_numbers}\n' ' You must specify the serial number in this case.\n') exit() else: self.device = devices[0] self.serial_number = self.device.serial_number self.device.open() self.type = 0 elif platform.system() == 'Windows': import hid if serial_number is not None: # serial number should be unicode serial_number = str(serial_number) try: self.device = hid.Device(vid=1155, pid=22352, serial=serial_number) except hid.HIDException: print( '\n[ERROR] No device found. Ensure that the xarm is connected via ' 'usb cable and the power is on.\n' 'Turn the xarm off and back on again if needed.\n') exit() self.serial_number = self.device.serial self.type = 1 elif platform.system() == 'Darwin': import hid self.device = hid.device() try: if serial_number is not None: # serial number should be unicode serial_number = str(serial_number) self.device.open(1155, 22352, serial_number) except OSError: print( '\n[ERROR] No device found. Ensure that the xarm is connected via ' 'usb cable and the power is on.\n' 'Turn the xarm off and back on again if needed.\n') exit() self.serial_number = self.device.get_serial_number_string() self.type = 0 else: raise TypeError('unsupported operating system')