def open_dev(): for desc in hid.enumerate(): if desc["vendor_id"] == 0x16d0 and desc[ "product_id"] == 0x106d and desc["interface_number"] == 1: dev = hid.device() dev.open_path(desc["path"]) return dev
def open(self): device = self.devices[0] try: self.dev = hidraw.device() self.dev.open_path(device.encode()) except Exception as e: self.log.error(e)
def __enter__(self): dev = hidraw.device() for d in hidraw.enumerate(): logging.debug("Looking at v=%s pr=%s sn=%s if=%s p=%s", d['vendor_id'], d['product_id'], d['serial_number'], d['interface_number'], d['path']) if d['vendor_id'] != self.vend: continue if d['product_id'] != self.prod: continue if d['interface_number'] != self.iface: continue if self.sn is not None and d['serial_number'] != self.sn: continue dev.open_path(d['path']) logging.debug("Checking version") r = dev.write([0, ord('V')] + [0] * 7) if r != 9: logging.debug("Device handshake failed to send: %d", r) continue r = dev.read(8) if r[0:3] != [ord('V'), 0x00, 0xFF]: logging.debug("Device handshake mismatch: %r", r) dev.close() continue logging.debug("Device handshake OK: %r", r) self.dev = dev return dev else: raise ValueError("Device not found")
def __init__(self, serial=None): btns = dict(((ser,(path,rls)) for (ser,rls,path) in self.list_connected())) if not btns: raise VerySeriousButtonNotFound("No VerySeriousButtons connected") if not serial: path, rls = btns.values()[0] else: if serial not in btns: raise VerySeriousButtonNotFound("Couldn't find VerySeriousButton with serial number '%s'" % (serial,)) path, rls = btns[serial] self.release_number = rls self.serial_number = serial self.hid_dev = hid.device() self.hid_dev.open_path(path) self.hid_dev.set_nonblocking(False) info = self.get_device_info() self.keyseq_page_size = info["keyseq_pagesize"] self.keyseq_nkeys = info["keyseq_nkeys"] self.num_keyseq_pages = info["keyseq_npages"] self.singlekey_nkeys = info["singlekey_nkeys"]
def __init__(self, serial=None): btns = dict( ((ser, (path, rls)) for (ser, rls, path) in self.list_connected())) if not btns: raise VerySeriousButtonNotFound("No VerySeriousButtons connected") if not serial: path, rls = btns.values()[0] else: if serial not in btns: raise VerySeriousButtonNotFound( "Couldn't find VerySeriousButton with serial number '%s'" % (serial, )) path, rls = btns[serial] self.release_number = rls self.serial_number = serial self.hid_dev = hid.device() self.hid_dev.open_path(path) self.hid_dev.set_nonblocking(False) info = self.get_device_info() self.keyseq_page_size = info["keyseq_pagesize"] self.keyseq_nkeys = info["keyseq_nkeys"] self.num_keyseq_pages = info["keyseq_npages"] self.singlekey_nkeys = info["singlekey_nkeys"]
def open(self): self.handle = hid.device() self.handle.open_path(self.path) self.handle.set_nonblocking(True) self.init()
def main(): """ Main function: Entry point if running this module from the command line. Prints messages to stdout. """ import argparse from inspect import cleandoc parser = argparse.ArgumentParser(description=cleandoc(__doc__)) parser.add_argument('-v', '--verbose', action='store_true', help='Increase verbosity') parser.add_argument('-d', '--debug', action='store_true', help='Enable debug mode') args = parser.parse_args() try: try: import hidraw as hid except ImportError: import hid except ImportError: parser.error("You need to install cython-hidapi first!") import logging loglevel = logging.WARNING if args.verbose: loglevel = logging.INFO if args.debug: loglevel = logging.DEBUG logging.basicConfig(format='%(message)s', level=loglevel) try: logging.info("Enumerating Devices") devices = hid.enumerate(0x1a86, 0xe008) if len(devices) == 0: raise NameError('No device found. Check your USB connection.') logging.info("Found {} devices: ".format(len(devices))) for dev in devices: name = dev['manufacturer_string'] + " " + dev['product_string'] path = dev['path'].decode('ascii') logging.info("* {} [{}]".format(name, path)) logging.info("Opening device") h = hid.device() try: h.open(0x1a86, 0xe008) except IOError as ex: raise NameError('Cannot open the device. Please check permissions.') buf = [0]*6 buf[0] = 0x0 # report ID buf[1] = BPS & 0xFF buf[2] = ( BPS >> 8 ) & 0xFF buf[3] = ( BPS >> 16 ) & 0xFF buf[4] = ( BPS >> 24 ) & 0xFF buf[5] = 0x03 # 3 = enable? fr = h.send_feature_report(buf) if fr == -1: raise NameError("Sending Feature Report Failed") logging.debug("Feature Report Sent") try: logging.debug("Start Reading Messages") while True: #answer = h.read(256) answer = h.read(256, timeout_ms=1000) if len(answer) < 1: continue nbytes = answer[0] & 0x7 if nbytes > 0: if len(answer) < nbytes+1: raise NameError("More bytes announced then sent") payload = answer[1:nbytes+1] data = [b & ( ~(1<<7) ) for b in payload] data = [chr(b) for b in data] data = ''.join(data) sys.stdout.write(data) sys.stdout.flush() except KeyboardInterrupt: logging.info("You pressed CTRL-C, stopping...") logging.debug("Closing device") h.close() except IOError as ex: logging.error(ex) except Exception as ex: logging.error(ex)
#!/usr/bin/env python2 import hidraw import hid import os import struct import time import serial import math import sys dlist = hid.enumerate(0x0461, 0x0021) devs = [] for dv in dlist: d = hidraw.device() d.open_path(dv["path"]) devs.append(d) buf=bytearray(64) sernums = [] serh = serial.Serial (sys.argv[1], 38400, timeout=0) serh.write("GO\r") junk=serh.read(100) latitude=float(sys.argv[3]) desired=float(sys.argv[2]) + (90.0-latitude) print "Desired elevation is %f for declination %f" % (desired, float(sys.argv[2])) for d in devs: buf[0] = 0x00 buf[1] = 0x00 buf[2] = 0x01
def main(): """ Main function: Entry point if running this module from the command line. Prints messages to stdout. """ import argparse from inspect import cleandoc parser = argparse.ArgumentParser(description=cleandoc(__doc__)) parser.add_argument('-v', '--verbose', action='store_true', help='Increase verbosity') parser.add_argument('-d', '--debug', action='store_true', help='Enable debug mode') args = parser.parse_args() try: try: import hidraw as hid except ImportError: import hid except ImportError: parser.error("You need to install cython-hidapi first!") import logging loglevel = logging.WARNING if args.verbose: loglevel = logging.INFO if args.debug: loglevel = logging.DEBUG logging.basicConfig(format='%(message)s', level=loglevel) try: logging.info("Enumerating Devices") devices = hid.enumerate(0x1a86, 0xe008) if len(devices) == 0: raise NameError('No device found. Check your USB connection.') logging.info("Found {} devices: ".format(len(devices))) for dev in devices: name = dev['manufacturer_string'] + " " + dev['product_string'] path = dev['path'].decode('ascii') logging.info("* {} [{}]".format(name, path)) logging.info("Opening device") h = hid.device() try: h.open(0x1a86, 0xe008) except IOError as ex: raise NameError( 'Cannot open the device. Please check permissions.') buf = [0] * 6 buf[0] = 0x0 # report ID buf[1] = BPS & 0xFF buf[2] = (BPS >> 8) & 0xFF buf[3] = (BPS >> 16) & 0xFF buf[4] = (BPS >> 24) & 0xFF buf[5] = 0x03 # 3 = enable? fr = h.send_feature_report(buf) if fr == -1: raise NameError("Sending Feature Report Failed") logging.debug("Feature Report Sent") try: logging.debug("Start Reading Messages") while True: #answer = h.read(256) answer = h.read(256, timeout_ms=1000) if len(answer) < 1: continue nbytes = answer[0] & 0x7 if nbytes > 0: if len(answer) < nbytes + 1: raise NameError("More bytes announced then sent") payload = answer[1:nbytes + 1] data = [b & (~(1 << 7)) for b in payload] data = [chr(b) for b in data] data = ''.join(data) sys.stdout.write(data) sys.stdout.flush() except KeyboardInterrupt: logging.info("You pressed CTRL-C, stopping...") logging.debug("Closing device") h.close() except IOError as ex: logging.error(ex) except Exception as ex: logging.error(ex)
def readFromUsb(self): import argparse,sys from inspect import cleandoc BPS = 19200 try: try: import hidraw as hid except ImportError: import hid except ImportError: parser.error("You need to install cython-hidapi first!") import logging loglevel = logging.ERROR logging.basicConfig(format='%(message)s', level=loglevel) #try: if True: logging.info("Enumerating Devices") devices = hid.enumerate(0, 0) print(devices) if len(devices) == 0: raise NameError('No device found. Check your USB connection.') logging.info("Found {} devices: ".format(len(devices))) for dev in devices: name = dev['manufacturer_string'] + " " + dev['product_string'] path = dev['path'].decode('ascii') logging.info("* {} [{}]".format(name, path)) logging.info("Opening device") h = hid.device() try: h.open(0x1a86, 0xe008) except IOError as ex: raise NameError('Cannot open the device. Please check permissions.') buf = [0]*6 buf[0] = 0x0 # report ID buf[1] = BPS & 0xFF buf[2] = ( BPS >> 8 ) & 0xFF buf[3] = ( BPS >> 16 ) & 0xFF buf[4] = ( BPS >> 24 ) & 0xFF buf[5] = 0x03 # 3 = enable? fr = h.send_feature_report(buf) if fr == -1: raise NameError("Sending Feature Report Failed") logging.debug("Feature Report Sent") try: logging.debug("Start Reading Messages") while True: #answer = h.read(256) answer = h.read(256, timeout_ms=1000) if len(answer) < 1: continue nbytes = answer[0] & 0x7 if nbytes > 0: if len(answer) < nbytes+1: raise NameError("More bytes announced then sent") payload = answer[1:nbytes+1] data = [b & ( ~(1<<7) ) for b in payload] data = [chr(b) for b in data] data = ''.join(data) if not data.endswith("\r\n"): self.lastvalue = data else: self.lastvalue = self.lastvalue + data self.parseValue(self.lastvalue) #sys.stdout.write(data) #sys.stdout.flush() except KeyboardInterrupt: logging.info("You pressed CTRL-C, stopping...") logging.debug("Closing device") h.close()
def open(self): self.device = hid.device() self.device.open_path(self.path) self.device.set_nonblocking(True) self.opened = True
def flash(self): desc = find_device() dev = hid.device() dev.open_path(desc["path"]) hid_send(dev, b"\x0B", retries=20) os.system("vibl-flash {}".format(FIRMWARE))