def __exit__(self, exc_type, exc_value, traceback): self.stop() if self.reader: self.reader.close() self.file.close() if 'eeg_raw' in self.platform and self.hid is not None: self.hid.close() elif 'Windows' not in self.platform and self.hid is not None: hidapi.hid_close(self.hid)
def get_packets(): try: setup(headset_config) for data in read(headset_config): packet = decrypt(data, headset_config) yield packet except: raise finally: if headset_config['device']: hidapi.hid_close(headset_config['device']) hidapi.hid_exit()
def run(self, source=None): """Do not call explicitly, called upon initialization of class""" if self.platform == 'Windows': source.set_raw_data_handler(self.data_handler) source.open() self.lock.acquire() while self.running: self.lock.release() if not self.platform == 'Windows': try: if not self._stop_signal: data = read_platform[self.platform]( source, new_format=self.new_format) self.data.put_nowait( EmotivReaderTask(data=data, timestamp=datetime.now())) except Exception as ex: print("Reader Error: {}".format(ex.message)) # print(ex) # Catching StopIteration for some reason stops at the second record, # even though there are more results. else: time.sleep(0.00001) self.lock.acquire() if self._stop_signal: print("Reader stopping...") self.running = False if self.file is not None: self.file.close() if type(source) != int and type(source) != list: source.close() if self.hid is not None: if type(self.hid) != int: self.hid.close() if system_platform != "Windows": try: hidapi.hid_close(source) except Exception: pass try: hidapi.hid_exit() except Exception: pass print("Reader stopped...") self.stopped = True return
def run(self, source=None): """Do not call explicitly, called upon initialization of class""" if self.platform == "Windows": source.set_raw_data_handler(self.data_handler) source.open() self.lock.acquire() while self.running: self.lock.release() if not self.platform == "Windows": self.lock.acquire() try: if not self._stop_signal: data = read_platform[self.platform](source) self.data.put_nowait(data) except Exception as ex: print(ex.message) # Catching StopIteration for some reason stops at the second record, # even though there are more results. self.lock.release() else: time.sleep(0.0005) self.lock.acquire() if self._stop_signal: print("Reader stopping...") self.running = False self.lock.release() if self.file is not None: self.file.close() if type(source) != int: source.close() if self.hid is not None: if type(self.hid) != int: self.hid.close() if system_platform != "Windows": try: hidapi.hid_close(source) except Exception: pass try: hidapi.hid_exit() except Exception: pass print("Reader stopped...") self.stopped = True return
def shutdown_everything(): #fill in more later? global g_running global g_device global g_system_platform global g_os_decryption #print "Shutting down..." g_running = False if g_system_platform == "Windows" or g_os_decryption: g_device.close() else: hidapi.hid_close(g_device) if g_outfile != 0: try: g_outfile.close() except: pass
def SendCommand(packet_array): packets = chr(0x00) # pre-pad with 0x00 per hid_write documentation for byte in packet_array: packets = packets + chr(int(byte, 16)) hidapi.hid_init() # Connect to the RadioSHARK dev = hidapi.hid_open(0x077d, 0x627a) # Write the packets packets_written = hidapi.hid_write(dev, packets) # Close out hidapi.hid_close(dev) if (args.debug): print "Packet Array: %s" % packet_array print "Number of Packets Sent: %s" % str(1 + len(packet_array)) print "Number of Packets Written: %i" % packets_written
# Queue it! if self.write and self.write_raw: self.write_data(data) tasks.put_nowait(''.join(map(chr, data[1:]))) self.packets_received += 1 gevent.sleep(DEVICE_POLL_INTERVAL) except KeyboardInterrupt: self.running = False except Exception, ex: print("Setup emotiv.py(line=710): %s" % ex.message) self.running = False gevent.sleep(DEVICE_POLL_INTERVAL) if _os_decryption: hidraw.close() else: hidapi.hid_close(device) gevent.kill(crypto, KeyboardInterrupt) gevent.kill(console_updater, KeyboardInterrupt) def setup_crypto(self, sn): """ Performs decryption of packets received. Stores decrypted packets in a Queue for use. """ if is_old_model(sn): self.old_model = True if self.display_output: print(self.old_model) k = ['\0'] * 16 k[0] = sn[-1] k[1] = '\0' k[2] = sn[-2]
def close(self) -> None: """Closes the connection to the RoIP device.""" self.hid_device = hidapi.hid_close(self.hid_device)
#!/usr/bin/env python import hidapi import binascii import time hidapi.hid_init() print 'Loaded hidapi library from: {:s}\n'.format(hidapi.hid_lib_path()) devices = hidapi.hid_enumerate(0x0483, 0x5750) if len(devices) == 0: print "No dev attached" exit(1) device = hidapi.hid_open(0x0483, 0x5750) import random while True: result = hidapi.hid_read(device, 4) state = binascii.hexlify(result) print "#%d: %s" % (len(result), state) hidapi.hid_close(device)
if rect: buf, buf_size = create_teensy_guidance_packet(x, y, width, height) else: buf, buf_size = create_teensy_guidance_packet(0, 0, 0, 0) elif can_chase_debug == 0: # create set_motor packet #print "set motors", dir1, pwm1, dir2, pwm2 buf, buf_size = create_teensy_set_motor_packet(dir1, pwm1, dir2, pwm2) else: print time.time(), rect if not rect: freq = 1 else: freq = rect[0] + rect[2] / 2 # create beep packet buf, buf_size = create_teensy_beep_packet(freq, 1000) # write to teensy write_teensy(dev, buf, buf_size) if save_image: import cv # get cam and set the width and height cap = cv.CaptureFromCAM(-1) width, height = 320, 240 cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH, width); cv.SetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT, height); image = cv.QueryFrame(cap) cv.SaveImage("test.jpg", image) # close hid hidapi.hid_close(dev)
def shutdown(self): hidapi.hid_close(self.handle) hidapi.hid_exit()