(self)
Do not call explicitly, called upon initialization of class or self.start()
            The main emokit loop.
        :param reader: EmotivReader class
        :param crypto: EmotivCrypto class
Do not call explicitly, called upon initialization of class or self.start()
            The main emokit loop.
        :param reader: EmotivReader class
        :param crypto: EmotivCrypto class
Do not call explicitly called upon initialization of class or self . start () The main emokit loop . : param reader : EmotivReader class : param crypto : EmotivCrypto class
def run(self):
        """ Do not call explicitly, called upon initialization of class or self.start()
            The main emokit loop.
        :param reader: EmotivReader class
        :param crypto: EmotivCrypto class
        """
        self.initialize_reader()
        if self.serial_number.startswith("UD2016") and not self.force_epoc_mode:
            self.new_format = True
        self.initialize_writer()
        self.initialize_crypto()
        self.initialize_output()
        if self.output is not None:
            self.output.start()
        self.reader.start()
        if self.crypto is not None:
            self.crypto.start()
        last_packets_received = 0
        tick_time = time()
        stale_rx = 0
        restarting_reader = False
        self.lock.acquire()
        while self.running:
            self.lock.release()
            if not self.reader.data.empty():
                try:
                    reader_task = self.reader.data.get()
                except KeyboardInterrupt:
                    self.quit()
                if self.write and self.write_encrypted and self.input_source == 'emotiv':
                    # Due to some encoding problem to save encrypted data we must first convert it to binary.
                    self.encrypted_writer.data. \
                        put_nowait(EmotivWriterTask(data=reader_task.data, encrypted=True, values=False))

                self.packets_received += 1
                if not self.read_encrypted:
                    if not self.read_values:
                        extra_data = False
                        if self.new_format:
                            extra_data = is_extra_data(reader_task.data)
                            if extra_data:
                                new_packet = EmotivExtraPacket(reader_task.data, verbose=self.verbose)
                            else:
                                new_packet = EmotivNewPacket(reader_task.data, verbose=self.verbose)
                        else:
                            new_packet = EmotivOldPacket(reader_task.data, verbose=self.verbose)
                        if new_packet.battery is not None:
                            self.battery = new_packet.battery
                        self.packets.put_nowait(new_packet)
                        self.packets_processed += 1
                        if self.display_output:
                            if self.new_format:
                                if extra_data:
                                    self.output.tasks.put_nowait(EmotivOutputTask(received=True,
                                                                                  decrypted=True,
                                                                                  data=EmotivExtraPacket(raw_data)))
                                else:
                                    self.output.tasks.put_nowait(EmotivOutputTask(received=True,
                                                                                  decrypted=True,
                                                                                  data=EmotivNewPacket(raw_data)))
                            else:
                                self.output.tasks.put_nowait(EmotivOutputTask(received=True,
                                                                              decrypted=True,
                                                                              data=EmotivOldPacket(raw_data)))
                    else:
                        # TODO: Implement read from values.
                        pass
                else:
                    if self.input_source != 'emotiv' and self.read_encrypted:
                        if len(reader_task.data) == 33:
                            reader_task.data = reader_task.data[1:]
                        if len(reader_task.data) != 32:
                            print("Reader task: {}".format(len(reader_task.data)))
                            self.reader.stop()
                            self.crypto.stop()
                            self.running = False
                            raise ValueError("Reached end of data or corrupted data.")
                        # Decode binary data stored in file.
                        if sys.version_info >= (3, 0):
                            raw_data = [int(bytes(item, encoding='latin-1').decode(), 2) for item in reader_task.data]
                        else:
                            raw_data = [int(item, 2) for item in reader_task.data]

                        raw_data = ''.join(map(chr, raw_data[:]))
                        reader_task.data = raw_data

                    if self.display_output:
                        self.output.tasks.put_nowait(EmotivOutputTask(received=True))
                    self.crypto.add_task(reader_task)
            if self.crypto is not None:
                if self.crypto.data_ready():
                    decrypted_task = self.crypto.get_data()
                    if self.write:
                        if self.write_decrypted:
                            if self.decrypted_writer is not None:
                                self.decrypted_writer.data. \
                                    put_nowait(EmotivWriterTask(decrypted_task.data, values=False,
                                                                timestamp=decrypted_task.timestamp))
                    self.packets_processed += 1
                    extra_data = False
                    if self.new_format:
                        extra_data = is_extra_data(decrypted_task.data)
                        if extra_data:
                            new_packet = EmotivExtraPacket(decrypted_task.data, timestamp=decrypted_task.timestamp)
                        else:
                            if self.force_epoc_mode:
                                new_packet = EmotivOldPacket(decrypted_task.data, timestamp=decrypted_task.timestamp)
                            else:
                                new_packet = EmotivNewPacket(decrypted_task.data, timestamp=decrypted_task.timestamp)
                    else:
                        new_packet = EmotivOldPacket(decrypted_task.data, timestamp=decrypted_task.timestamp)
                    # print(new_packet.counter)
                    # data = []
                    # for c in new_packet.raw_data:
                    #    if c > 0:
                    #        data.append(ord(c))
                    # print(data)

                    # values = [new_packet.sensors[name]['value'] for name in
                    #          'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' ')]
                    # cy = struct.pack('>' + str(len(values)) + 'h', *values)
                    # print(cy)

                    if self.display_output:
                        if self.new_format:
                            if extra_data:
                                self.output.tasks.put_nowait(EmotivOutputTask(decrypted=True,
                                                                              data=EmotivExtraPacket(
                                                                                  decrypted_task.data)))

                            else:
                                self.output.tasks.put_nowait(EmotivOutputTask(decrypted=True,
                                                                              data=EmotivNewPacket(
                                                                                  decrypted_task.data)))
                        else:
                            self.output.tasks.put_nowait(EmotivOutputTask(decrypted=True,
                                                                          data=EmotivOldPacket(decrypted_task.data)))
                    if type(new_packet) == EmotivOldPacket:
                        if new_packet.battery is not None:
                            self.battery = new_packet.battery
                    self.packets.put_nowait(new_packet)
                    if self.write:
                        if self.write_values:
                            if self.value_writer is not None:
                                if not extra_data:
                                    self.value_writer.data.put_nowait(
                                        EmotivWriterTask(
                                            data=new_packet.sensors.copy(),
                                            timestamp=decrypted_task.timestamp
                                        )
                                    )
            tick_diff = time() - tick_time
            if tick_diff >= 1:
                tick_time = time()
                packets_received_since_last_update = self.packets_received - last_packets_received
                if packets_received_since_last_update == 1 or packets_received_since_last_update == 0:
                    stale_rx += 1
                last_packets_received = self.packets_received
                if restarting_reader and self.reader.stopped:
                    print("Restarting Reader")
                    stale_rx = 0
                    self.initialize_reader()
                    self.reader.start()
                    restarting_reader = False
                    print("Reader Thread Restarted")

                if stale_rx > 5 and not restarting_reader:
                    self.reader.stop()
                    restarting_reader = True

            self.lock.acquire()
            if self._stop_signal:
                should_stop = True
                if self.reader.running:
                    should_stop = False
                if self.crypto is not None:
                    if self.crypto.running and self.crypto.data_ready():
                        should_stop = False
                if self.decrypted_writer is not None:
                    if self.decrypted_writer.running:
                        should_stop = False
                if self.encrypted_writer is not None:
                    if self.encrypted_writer.running:
                        should_stop = False
                if self.value_writer is not None:
                    if self.value_writer.running:
                        should_stop = False
                if self.output is not None:
                    if self.output.running:
                        should_stop = False
                if should_stop:
                    self.running = False
def run ( self ) : self . initialize_reader ( ) if self . serial_number . startswith ( "UD2016" ) and not self . force_epoc_mode : self . new_format = True self . initialize_writer ( ) self . initialize_crypto ( ) self . initialize_output ( ) if self . output is not None : self . output . start ( ) self . reader . start ( ) if self . crypto is not None : self . crypto . start ( ) last_packets_received = 0 tick_time = time ( ) stale_rx = 0 restarting_reader = False self . lock . acquire ( ) while self . running : self . lock . release ( ) if not self . reader . data . empty ( ) : try : reader_task = self . reader . data . get ( ) except KeyboardInterrupt : self . quit ( ) if self . write and self . write_encrypted and self . input_source == 'emotiv' : # Due to some encoding problem to save encrypted data we must first convert it to binary. self . encrypted_writer . data . put_nowait ( EmotivWriterTask ( data = reader_task . data , encrypted = True , values = False ) ) self . packets_received += 1 if not self . read_encrypted : if not self . read_values : extra_data = False if self . new_format : extra_data = is_extra_data ( reader_task . data ) if extra_data : new_packet = EmotivExtraPacket ( reader_task . data , verbose = self . verbose ) else : new_packet = EmotivNewPacket ( reader_task . data , verbose = self . verbose ) else : new_packet = EmotivOldPacket ( reader_task . data , verbose = self . verbose ) if new_packet . battery is not None : self . battery = new_packet . battery self . packets . put_nowait ( new_packet ) self . packets_processed += 1 if self . display_output : if self . new_format : if extra_data : self . output . tasks . put_nowait ( EmotivOutputTask ( received = True , decrypted = True , data = EmotivExtraPacket ( raw_data ) ) ) else : self . output . tasks . put_nowait ( EmotivOutputTask ( received = True , decrypted = True , data = EmotivNewPacket ( raw_data ) ) ) else : self . output . tasks . put_nowait ( EmotivOutputTask ( received = True , decrypted = True , data = EmotivOldPacket ( raw_data ) ) ) else : # TODO: Implement read from values. pass else : if self . input_source != 'emotiv' and self . read_encrypted : if len ( reader_task . data ) == 33 : reader_task . data = reader_task . data [ 1 : ] if len ( reader_task . data ) != 32 : print ( "Reader task: {}" . format ( len ( reader_task . data ) ) ) self . reader . stop ( ) self . crypto . stop ( ) self . running = False raise ValueError ( "Reached end of data or corrupted data." ) # Decode binary data stored in file. if sys . version_info >= ( 3 , 0 ) : raw_data = [ int ( bytes ( item , encoding = 'latin-1' ) . decode ( ) , 2 ) for item in reader_task . data ] else : raw_data = [ int ( item , 2 ) for item in reader_task . data ] raw_data = '' . join ( map ( chr , raw_data [ : ] ) ) reader_task . data = raw_data if self . display_output : self . output . tasks . put_nowait ( EmotivOutputTask ( received = True ) ) self . crypto . add_task ( reader_task ) if self . crypto is not None : if self . crypto . data_ready ( ) : decrypted_task = self . crypto . get_data ( ) if self . write : if self . write_decrypted : if self . decrypted_writer is not None : self . decrypted_writer . data . put_nowait ( EmotivWriterTask ( decrypted_task . data , values = False , timestamp = decrypted_task . timestamp ) ) self . packets_processed += 1 extra_data = False if self . new_format : extra_data = is_extra_data ( decrypted_task . data ) if extra_data : new_packet = EmotivExtraPacket ( decrypted_task . data , timestamp = decrypted_task . timestamp ) else : if self . force_epoc_mode : new_packet = EmotivOldPacket ( decrypted_task . data , timestamp = decrypted_task . timestamp ) else : new_packet = EmotivNewPacket ( decrypted_task . data , timestamp = decrypted_task . timestamp ) else : new_packet = EmotivOldPacket ( decrypted_task . data , timestamp = decrypted_task . timestamp ) # print(new_packet.counter) # data = [] # for c in new_packet.raw_data: #    if c > 0: #        data.append(ord(c)) # print(data) # values = [new_packet.sensors[name]['value'] for name in #          'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' ')] # cy = struct.pack('>' + str(len(values)) + 'h', *values) # print(cy) if self . display_output : if self . new_format : if extra_data : self . output . tasks . put_nowait ( EmotivOutputTask ( decrypted = True , data = EmotivExtraPacket ( decrypted_task . data ) ) ) else : self . output . tasks . put_nowait ( EmotivOutputTask ( decrypted = True , data = EmotivNewPacket ( decrypted_task . data ) ) ) else : self . output . tasks . put_nowait ( EmotivOutputTask ( decrypted = True , data = EmotivOldPacket ( decrypted_task . data ) ) ) if type ( new_packet ) == EmotivOldPacket : if new_packet . battery is not None : self . battery = new_packet . battery self . packets . put_nowait ( new_packet ) if self . write : if self . write_values : if self . value_writer is not None : if not extra_data : self . value_writer . data . put_nowait ( EmotivWriterTask ( data = new_packet . sensors . copy ( ) , timestamp = decrypted_task . timestamp ) ) tick_diff = time ( ) - tick_time if tick_diff >= 1 : tick_time = time ( ) packets_received_since_last_update = self . packets_received - last_packets_received if packets_received_since_last_update == 1 or packets_received_since_last_update == 0 : stale_rx += 1 last_packets_received = self . packets_received if restarting_reader and self . reader . stopped : print ( "Restarting Reader" ) stale_rx = 0 self . initialize_reader ( ) self . reader . start ( ) restarting_reader = False print ( "Reader Thread Restarted" ) if stale_rx > 5 and not restarting_reader : self . reader . stop ( ) restarting_reader = True self . lock . acquire ( ) if self . _stop_signal : should_stop = True if self . reader . running : should_stop = False if self . crypto is not None : if self . crypto . running and self . crypto . data_ready ( ) : should_stop = False if self . decrypted_writer is not None : if self . decrypted_writer . running : should_stop = False if self . encrypted_writer is not None : if self . encrypted_writer . running : should_stop = False if self . value_writer is not None : if self . value_writer . running : should_stop = False if self . output is not None : if self . output . running : should_stop = False if should_stop : self . running = False