Example #1
0
 def __init__(self, path=None):
     try:
         self.device = hidapi.hid_open(
             HASSEB_USB_VENDOR, HASSEB_USB_PRODUCT,
             None) if not bool(path) else hidapi.hid_open_path(path)
         self.device_found = 1
     except:
         self.device_found = None
Example #2
0
    def __init__(self):
        self.seq1 = 0x0001
        self.seq2 = 0x0068
        self.handle = None

        # Setup Device
        hidapi.hid_init()
        for dev in hidapi.hid_enumerate(vendor_id=0x03eb, product_id=0x2013):
            self.handle = hidapi.hid_open_path(dev.path)

        self.tx_type1(self.handle, '*IDN?')
        self.info = self.rx(self.handle)
Example #3
0
def setup(headset, is_research=True):
    '''
    `is_research` should be True if EPOC+, try False if you have an EPOC
    '''
    hidapi.hid_init()
    path, serial_number = hid_enumerate()

    if len(path) == 0:
        print("Could not find device.")
        print_hid_enumerate()
        exit()

    headset['device'] = hidapi.hid_open_path(path)

    # Setup crypto
    if is_old_model(serial_number):
        headset['old_model'] = True
    k = ['\0'] * 16
    k[0] = serial_number[-1]
    k[1] = '\0'
    k[2] = serial_number[-2]
    if is_research:
        k[3] = 'H'
        k[4] = serial_number[-1]
        k[5] = '\0'
        k[6] = serial_number[-2]
        k[7] = 'T'
        k[8] = serial_number[-3]
        k[9] = '\x10'
        k[10] = serial_number[-4]
        k[11] = 'B'
    else:
        k[3] = 'T'
        k[4] = serial_number[-3]
        k[5] = '\x10'
        k[6] = serial_number[-4]
        k[7] = 'B'
        k[8] = serial_number[-1]
        k[9] = '\0'
        k[10] = serial_number[-2]
        k[11] = 'H'
    k[12] = serial_number[-3]
    k[13] = '\0'
    k[14] = serial_number[-4]
    k[15] = 'P'
    key = ''.join(k)
    print("Decryption key: " + key)

    backend = default_backend()
    cipher = Cipher(algorithms.AES(key.encode()), modes.ECB(), backend=backend)
    decryptor = cipher.decryptor()
    headset['decryptor'] = decryptor
Example #4
0
 def setup_not_windows(self):
     """
     Setup for headset on a non-windows platform.
     Receives packets from headset and sends them to a Queue to be processed
     by the crypto greenlet.
     """
     _os_decryption = False
     if os.path.exists('/dev/eeg/raw'):
         # The decryption is handled by the Linux epoc daemon. We don't need to handle it.
         _os_decryption = True
         hidraw = open("/dev/eeg/raw")
     path, serial_number = hid_enumerate()
     if len(path) == 0:
         print("Could not find device.")
         print_hid_enumerate()
         sys.exit()
     self.serial_number = serial_number
     device = hidapi.hid_open_path(path)
     crypto = gevent.spawn(self.setup_crypto, self.serial_number)
     gevent.sleep(DEVICE_POLL_INTERVAL)
     console_updater = gevent.spawn(self.update_console)
     while self.running:
         try:
             if _os_decryption:
                 data = hidraw.read(32)
                 if data != "":
                     self.packets.put_nowait(
                         EmotivPacket(data, self.sensors, self.old_model))
             else:
                 # Doesn't seem to matter how big we make the buffer 32 returned every time, 33 for other platforms
                 data = hidapi.hid_read(device, 34)
                 if len(data) == 32:
                     # Most of the time the 0 is truncated? That's ok we'll add it... Could probably not do this...
                     data.insert(0, 0)
                 if data != "":
                     if _os_decryption:
                         self.packets.put_nowait(
                             EmotivPacket(data, self.sensors,
                                          self.old_model))
                     else:
                         # 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)
Example #5
0
def setup(headset, is_research=True):
    '''
    `is_research` should be True if EPOC+, try False if you have an EPOC
    '''
    hidapi.hid_init()
    path, serial_number = hid_enumerate()

    if len(path) == 0:
        print("Could not find device.")
        print_hid_enumerate()
        exit()

    headset['device'] = hidapi.hid_open_path(path)

    # Setup crypto
    if is_old_model(serial_number):
        headset['old_model'] = True
    k = ['\0'] * 16
    k[0] = serial_number[-1]
    k[1] = '\0'
    k[2] = serial_number[-2]
    if is_research:
        k[3] = 'H'
        k[4] = serial_number[-1]
        k[5] = '\0'
        k[6] = serial_number[-2]
        k[7] = 'T'
        k[8] = serial_number[-3]
        k[9] = '\x10'
        k[10] = serial_number[-4]
        k[11] = 'B'
    else:
        k[3] = 'T'
        k[4] = serial_number[-3]
        k[5] = '\x10'
        k[6] = serial_number[-4]
        k[7] = 'B'
        k[8] = serial_number[-1]
        k[9] = '\0'
        k[10] = serial_number[-2]
        k[11] = 'H'
    k[12] = serial_number[-3]
    k[13] = '\0'
    k[14] = serial_number[-4]
    k[15] = 'P'
    key = ''.join(k)
    print("Decryption key: " + key)

    backend = default_backend()
    cipher = Cipher(algorithms.AES(key.encode()), modes.ECB(), backend=backend)
    decryptor = cipher.decryptor()
    headset['decryptor'] = decryptor
Example #6
0
 def setup_not_windows(self):
     """
     Setup for headset on a non-windows platform.
     Receives packets from headset and sends them to a Queue to be processed
     by the crypto greenlet.
     """
     _os_decryption = False
     if os.path.exists('/dev/eeg/raw'):
         # The decryption is handled by the Linux epoc daemon. We don't need to handle it.
         _os_decryption = True
         hidraw = open("/dev/eeg/raw")
     path, serial_number = hid_enumerate()
     if len(path) == 0:
         print("Could not find device.")
         print_hid_enumerate()
         sys.exit()
     self.serial_number = serial_number
     device = hidapi.hid_open_path(path)
     crypto = gevent.spawn(self.setup_crypto, self.serial_number)
     gevent.sleep(DEVICE_POLL_INTERVAL)
     console_updater = gevent.spawn(self.update_console)
     while self.running:
         try:
             if _os_decryption:
                 data = hidraw.read(32)
                 if data != "":
                     self.packets.put_nowait(EmotivPacket(data, self.sensors, self.old_model))
             else:
                 # Doesn't seem to matter how big we make the buffer 32 returned every time, 33 for other platforms
                 data = hidapi.hid_read(device, 34)
                 if len(data) == 32:
                     # Most of the time the 0 is truncated? That's ok we'll add it... Could probably not do this...
                     data.insert(0, 0)
                 if data != "":
                     if _os_decryption:
                         self.packets.put_nowait(EmotivPacket(data, self.sensors, self.old_model))
                     else:
                         # 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)
Example #7
0
def find_init_emotiv_non_windows():
    global g_serial_number
    global g_os_decryption
    global g_device
    global g_old_model

    device_path = ''
    devices = hidapi.hid_enumerate()
    for device in devices:
        is_emotiv = False
        try:
            if "Emotiv" in device.manufacturer_string:
                is_emotiv = True
            elif "Emotiv" in device.product_string:
                is_emotiv = True
            elif "EPOC" in device.product_string:
                is_emotiv = True
            elif "Brain Waves" in device.product_string:
                is_emotiv = True
            elif device.product_string == '00000000000':
                is_emotiv = True
            elif "EEG Signals" in device.product_string:
                is_emotiv = True

            if is_emotiv:
                g_serial_number = device.serial_number
                device_path = device.path
                break
        except:
            pass

    if g_serial_number == '':
        print("Could not find device.")
        shutdown_everything()
        return

    g_old_model = is_old_model(g_serial_number)
    g_os_decryption = os.path.exists('/dev/eeg/raw')
    if g_os_decryption:
        g_device = open("/dev/eeg/raw")
    else:
        try:
            g_device = hidapi.hid_open_path(device_path)
        except Exception, ex:
            print ex.message
            shutdown_everything()
Example #8
0
 def setup_not_windows(self):
     """
     Setup for headset on a non-windows platform.
     Receives packets from headset and sends them to a Queue to be processed
     by the crypto thread.
     """
     if os.path.exists('/dev/eeg/raw'):
         self.hid = open("/dev/eeg/raw")
     if self.hid is not None:
         # The decryption is handled by the Linux epoc daemon. We don't need to handle it.
         self.platform += " raw_eeg"
     else:
         path, serial_number = hid_enumerate(hidapi, self.platform)
         if len(path) == 0:
             print_hid_enumerate(system_platform, hidapi)
             raise Exception("Device not found")
         self.serial_number = serial_number
         self.hid = hidapi.hid_open_path(path)
Example #9
0
 def setup_not_windows(self):
     """
     Setup for headset on a non-windows platform.
     Receives packets from headset and sends them to a Queue to be processed
     by the crypto thread.
     """
     if os.path.exists('/dev/eeg/raw'):
         self.hid = open("/dev/eeg/raw")
     if self.hid is not None:
         # The decryption is handled by the Linux epoc daemon. We don't need to handle it.
         self.platform += " raw_eeg"
     else:
         path, serial_number = hid_enumerate(hidapi, self.platform)
         if len(path) == 0:
             print_hid_enumerate(system_platform, hidapi)
             raise Exception("Device not found")
         self.serial_number = serial_number
         self.hid = hidapi.hid_open_path(path)
Example #10
0
def tx_type2(handle, msg):
    msg = bytes('@', 'utf-8') + seq2.to_bytes(1, byteorder='big') + bytes('{}\n@@@@@@@@@@@@@'.format(msg), 'utf-8')
    hidapi.hid_write(handle, msg)

def rx(handle, numBytes=0x10):
    return hidapi.hid_read_timeout(handle, numBytes, 1000)

def getPpm(handle):
    tx_type2(handle, '*TR')
    return int.from_bytes(rx(handle)[2:4], byteorder='little')


# Setup Device
hidapi.hid_init()
for dev in hidapi.hid_enumerate(vendor_id=0x03eb, product_id=0x2013):
    handle = hidapi.hid_open_path(dev.path) 

print('reading info from device')
tx_type1(handle, '*IDN?')
info = rx(handle) + rx(handle) + rx(handle) + rx(handle) + rx(handle) + rx(handle) + rx(handle)
print('ret:"' + str(info) + '"')


# Setup Chart
data_x = []
data_y = []

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

def fechAndDraw(i):
Example #11
0
            can_chase = True
            can_chase_debug = int(a)
            print "can_chase"
        elif o == '-g':
            can_guide = True
            can_guide_mode = int(a)
            print "can_guide"
        elif o == '-i':
            save_image = True
            print "save_image"

    # open teensy hid
    path = find_teensy()
    if path:
        # open hid
        dev = hidapi.hid_open_path(path)
        if program != None:
            print "set program", program
            # create write program packet
            buf, buf_size = create_teensy_program_packet(program)
            # write to teensy
            write_teensy(dev, buf, buf_size)
        if dir1 != None:
            print "set motors", dir1, pwm1, dir2, pwm2
            # create set_motor packet
            buf, buf_size = create_teensy_set_motor_packet(dir1, pwm1, dir2, pwm2)
            # write to teensy
            write_teensy(dev, buf, buf_size)
        if freq != None:
            print "beep", freq, duration
            # create beep packet