def SetupArduino(self, selectFunc=None, **flags): success = True # check debug flag # it's also possible to set self.debugMode directly self._debugMode = flags.get('debugMode', self._debugMode) # empty stream sendStream = [] ePairs = self.SelectElectrodePairs(selectFunc, **flags) if len(ePairs) == 0: success = False if success: # generate byte stream for all electrode pairs for ePair in ePairs: stream = self.GenerateSendStream(ePair['ePair'], ePair['int']) # check for valid stream # otherwise stop loop if len(stream) != 0: sendStream.append(stream) else: success = False break if success: # enable/disable debug for Arduino if self._debugMode: success = self.SendMessage('debug 1') else: success = self.SendMessage('debug 0') # check if something is in stream if success and len(sendStream) != 0: # send byte stream for setting electrode pair setup # Arduino will just call the setups one by one # according to the defined timings sendStream = ''.join(sendStream) self.logger.debug( 'Sending %s bytes to Arduino... %s' % (len(sendStream), coreUtils.GetTextFromByteStream(sendStream))) success = self.SendMessage('setelectrodes %s %s' % (len(ePairs), sendStream)) if success: self.logger.info('Arduino setup was updated.') else: self.logger.error('Failed.') return success
def WriteStream(self, b): success = True if self.comPortStatus: if self.SafeWriteToComPort(b, leaveOpen=True): sleep(50e-3) self.logger.debug( 'Sent %s to tilter' % coreUtils.GetTextFromByteStream(b) ) else: success = False return success
def GenerateSendStream(self, activeElectrodePair, residenceTime=0): '''converts chosen chamber and electrode pair to bytes for sending via serial interface NOTE: all switches are updated at once (8 bytes + 1 byte for chamber and electrode encoding) ''' sendBytes = bytes() if self.chipConfigStatus and self.switchConfigStatus: # support the two debugging switches on current PCB (v4.0 Ketki) # one with a resister (1k) # and the other with a short # NOTE: set DIO lines and residence time to zero if isinstance(activeElectrodePair, str): activeSwitches = self.GetActiveSwitchIndices( activeElectrodePair) electrodeCoding = (0x00).to_bytes(1, 'big') elif isinstance(activeElectrodePair, int): # index in PcbConfig file, starting from 0 independent from routing activeSwitches = self.GetActiveSwitchIndices( activeElectrodePair) # chamber and electrode coding electrodeCoding = (activeElectrodePair & 0x1F).to_bytes( 1, 'big') # first switch bytes # use switch index and convert to bytes for sw in activeSwitches: sendBytes += sw.to_bytes(1, 'big') # then chamber number, electrode pair as MSB (NOTE: only five bits are used) # append residence time encoded in four bytes sendBytes += electrodeCoding sendBytes += residenceTime.to_bytes(4, 'big') self.logger.debug('Active electrode pair: %s' % activeElectrodePair) self.logger.debug('Active switches (abs): %s' % activeSwitches) self.logger.debug( 'Active switches: %s on device: %s' % ([i % 8 for i in activeSwitches], [i // 8 for i in activeSwitches])) # wrap the text generated from sendBytes every two half-bytes and print it self.logger.debug('Prepare %s bytes for storing on Arduino: %s' % (len(activeSwitches) + 5, coreUtils.GetTextFromByteStream(sendBytes))) return sendBytes.decode('latin-1')