class USB_UART(IO_object): def __init__(self, name): self.uart = UART(1, 9600) # init with given baudrate self.uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters self.buffer = bytearray(8) self.name = name assign_ID(self) # Data acqisition variables self.timer = pyb.Timer(available_timers.pop()) self.timestamp = fw.current_time self.freq = -1.0 self.prev_freq = -1.0 def _timer_ISR(self, t): if self.uart.any() > 0: # no message self.uart.readinto(self.buffer, 2) self.freq = int.from_bytes(self.buffer, 'little') if self.freq != self.prev_freq: self.timestamp = fw.current_time interrupt_queue.put(self.ID) self.prev_freq = self.freq def _initialise(self): self.timer.init(freq=100) # this should be 2*(client frequency) self.timer.callback(self._timer_ISR) def _process_interrupt(self): fw.event_queue.put((self.timestamp, fw.event_typ, fw.events[self.name]))
class cjmcu(object): """docstring for cjmcu""" _CONTINUOUS = const(1) _POLL = const(2) _RATEBASE = 0x11 _BAUD9600 = const(0) _BAUD19200 = const(1) _BAUD38400 = const(2) def __init__(self, aLoc): super(cjmcu, self).__init__() self._uart = UART(aLoc, 9600) self._mode = _POLL self._output = bytearray(4) self._output[0] = 0x66 self._output[1] = 0x66 self._output[2] = self._mode self._output[3] = 0x56 self._input = bytearray(9) self.update() def write(self): '''write output buffer to board.''' self._uart.write(self._output) def read(self): '''read into input buffer from board. Always 9 bytes.''' self._uart.readinto(self._input) def update(self): '''Send command to prompt data output from board, then read data. Note that this only needs to be done if board in in POLL mode.''' self.write() self.read() def setbaud(self, aBaud): '''Set baud rate on board then re-connect with new rate.''' self._output[2] = _BAUDBASE + aBaud self.update() self._output[2] = self._mode self._uart.deinit() self._uart.init(9600 << aBaud) def temps(self): '''Return (ambient, object) temperatures in celcius.''' v1 = (self._input[4] << 8) | self._input[5] v2 = (self._input[6] << 8) | self._input[7] return (v1 / 100.0, v2 / 100.0)
class JYMCU(object): """JY-MCU Bluetooth serial device driver. This is simply a light UART wrapper with addition AT command methods to customize the device.""" def __init__(self, uart, baudrate): """ uart = uart #1-6, baudrate must match what is set on the JY-MCU. Needs to be a #1-C. """ self._uart = UART(uart, baudrate) def __del__(self): self._uart.deinit() def any(self): return self._uart.any() def write(self, astring): return self._uart.write(astring) def writechar(self, achar): self._uart.writechar(achar) def read(self, num=None): return self._uart.read(num) def readline(self): return self._uart.readline() def readchar(self): return self._uart.readchar() def readall(self): return self._uart.readall() def readinto(self, buf, count=None): return self._uart.readinto(buf, count) def _cmd(self, cmd): """ Send AT command, wait a bit then return result string. """ self._uart.write("AT+" + cmd) udelay(500) return self.readline() def baudrate(self, rate): """ Set the baud rate. Needs to be #1-C. """ return self._cmd("BAUD" + str(rate)) def name(self, name): """ Set the name to show up on the connecting device. """ return self._cmd("NAME" + name) def pin(self, pin): """ Set the given 4 digit numeric pin. """ return self._cmd("PIN" + str(pin)) def version(self): return self._cmd("VERSION") def setrepl(self): repl_uart(self._uart)
class JYMCU(object): """JY-MCU Bluetooth serial device driver. This is simply a light UART wrapper with addition AT command methods to customize the device.""" def __init__( self, uart, baudrate ): """ uart = uart #1-6, baudrate must match what is set on the JY-MCU. Needs to be a #1-C. """ self._uart = UART(uart, baudrate) def __del__( self ) : self._uart.deinit() def any( self ) : return self._uart.any() def write( self, astring ) : return self._uart.write(astring) def writechar( self, achar ) : self._uart.writechar(achar) def read( self, num = None ) : return self._uart.read(num) def readline( self ) : return self._uart.readline() def readchar( self ) : return self._uart.readchar() def readall( self ) : return self._uart.readall() def readinto( self, buf, count = None ) : return self._uart.readinto(buf, count) def _cmd( self, cmd ) : """ Send AT command, wait a bit then return result string. """ self._uart.write("AT+" + cmd) udelay(500) return self.readline() def baudrate( self, rate ) : """ Set the baud rate. Needs to be #1-C. """ return self._cmd("BAUD" + str(rate)) def name( self, name ) : """ Set the name to show up on the connecting device. """ return self._cmd("NAME" + name) def pin( self, pin ) : """ Set the given 4 digit numeric pin. """ return self._cmd("PIN" + str(pin)) def version( self ) : return self._cmd("VERSION") def setrepl( self ) : repl_uart(self._uart)
class SBUSReceiver: def __init__(self, uart_port): self.sbus = UART(uart_port, 100000) self.sbus.init(100000, bits=8, parity=0, stop=2, timeout_char=3, read_buf_len=250) # constants self.START_BYTE = b'0f' self.END_BYTE = b'00' self.SBUS_FRAME_LEN = 25 self.SBUS_NUM_CHAN = 18 self.OUT_OF_SYNC_THD = 10 self.SBUS_NUM_CHANNELS = 18 self.SBUS_SIGNAL_OK = 0 self.SBUS_SIGNAL_LOST = 1 self.SBUS_SIGNAL_FAILSAFE = 2 # Stack Variables initialization self.validSbusFrame = 0 self.lostSbusFrame = 0 self.frameIndex = 0 self.resyncEvent = 0 self.outOfSyncCounter = 0 self.sbusBuff = bytearray(1) # single byte used for sync self.sbusFrame = bytearray(25) # single SBUS Frame self.sbusChannels = array.array('H', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # RC Channels self.isSync = False self.startByteFound = False self.failSafeStatus = self.SBUS_SIGNAL_FAILSAFE def get_rx_channels(self): """ Used to retrieve the last SBUS channels values reading :return: an array of 18 unsigned short elements containing 16 standard channel values + 2 digitals (ch 17 and 18) """ return self.sbusChannels def get_rx_channel(self, num_ch): """ Used to retrieve the last SBUS channel value reading for a specific channel :param: num_ch: the channel which to retrieve the value for :return: a short value containing """ return self.sbusChannels[num_ch] def get_failsafe_status(self): """ Used to retrieve the last FAILSAFE status :return: a short value containing """ return self.failSafeStatus def get_rx_report(self): """ Used to retrieve some stats about the frames decoding :return: a dictionary containg three information ('Valid Frames','Lost Frames', 'Resync Events') """ rep = {} rep['Valid Frames'] = self.validSbusFrame rep['Lost Frames'] = self.lostSbusFrame rep['Resync Events'] = self.resyncEvent return rep def decode_frame(self): # TODO: DoubleCheck if it has to be removed for i in range(0, self.SBUS_NUM_CHANNELS - 2): self.sbusChannels[i] = 0 # counters initialization byte_in_sbus = 1 bit_in_sbus = 0 ch = 0 bit_in_channel = 0 for i in range(0, 175): # TODO Generalization if self.sbusFrame[byte_in_sbus] & (1 << bit_in_sbus): self.sbusChannels[ch] |= (1 << bit_in_channel) bit_in_sbus += 1 bit_in_channel += 1 if bit_in_sbus == 8: bit_in_sbus = 0 byte_in_sbus += 1 if bit_in_channel == 11: bit_in_channel = 0 ch += 1 # Decode Digitals Channels # Digital Channel 1 if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 0): self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 1 else: self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 0 # Digital Channel 2 if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 1): self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 1 else: self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 0 # Failsafe self.failSafeStatus = self.SBUS_SIGNAL_OK if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 2): self.failSafeStatus = self.SBUS_SIGNAL_LOST if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 3): self.failSafeStatus = self.SBUS_SIGNAL_FAILSAFE def get_sync(self): if self.sbus.any() > 0: if self.startByteFound: if self.frameIndex == (self.SBUS_FRAME_LEN - 1): self.sbus.readinto(self.sbusBuff, 1) # end of frame byte if self.sbusBuff[0] == 0: # TODO: Change to use constant var value self.startByteFound = False self.isSync = True self.frameIndex = 0 else: self.sbus.readinto(self.sbusBuff, 1) # keep reading 1 byte until the end of frame self.frameIndex += 1 else: self.frameIndex = 0 self.sbus.readinto(self.sbusBuff, 1) # read 1 byte if self.sbusBuff[0] == 15: # TODO: Change to use constant var value self.startByteFound = True self.frameIndex += 1 def get_new_data(self): """ This function must be called periodically according to the specific SBUS implementation in order to update the channels values. For FrSky the period is 300us. """ if self.isSync: if self.sbus.any() >= self.SBUS_FRAME_LEN: self.sbus.readinto(self.sbusFrame, self.SBUS_FRAME_LEN) # read the whole frame if (self.sbusFrame[0] == 15 and self.sbusFrame[ self.SBUS_FRAME_LEN - 1] == 0): # TODO: Change to use constant var value self.validSbusFrame += 1 self.outOfSyncCounter = 0 self.decode_frame() else: self.lostSbusFrame += 1 self.outOfSyncCounter += 1 if self.outOfSyncCounter > self.OUT_OF_SYNC_THD: self.isSync = False self.resyncEvent += 1 else: self.get_sync()
class SBUSReceiver: def __init__(self): self.sbus = UART(3, 100000) self.sbus.init(100000, bits=8, parity=0, stop=2, timeout_char=3, read_buf_len=250) # constants self.START_BYTE = b'0f' self.END_BYTE = b'00' self.SBUS_FRAME_LEN = 25 self.SBUS_NUM_CHAN = 18 self.OUT_OF_SYNC_THD = 10 self.SBUS_NUM_CHANNELS = 18 self.SBUS_SIGNAL_OK = 0 self.SBUS_SIGNAL_LOST = 1 self.SBUS_SIGNAL_FAILSAFE = 2 # Stack Variables initialization self.validSbusFrame = 0 self.lostSbusFrame = 0 self.frameIndex = 0 self.resyncEvent = 0 self.outOfSyncCounter = 0 self.sbusBuff = bytearray(1) # single byte used for sync self.sbusFrame = bytearray(25) # single SBUS Frame self.sbusChannels = array.array('H', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # RC Channels self.isSync = False self.startByteFound = False self.failSafeStatus = self.SBUS_SIGNAL_FAILSAFE # logger.info("SBUS Stack Started") def get_rx_channels(self): return self.sbusChannels def get_rx_channel(self, num_ch): return self.sbusChannels[num_ch] def get_failsafe_status(self): return self.failSafeStatus def get_rx_report(self): rep = {} rep['Valid Frames'] = self.validSbusFrame rep['Lost Frames'] = self.lostSbusFrame rep['Resync Events'] = self.resyncEvent return rep def decode_frame(self): # TODO: DoubleCheck if it has to be removed for i in range(0, self.SBUS_NUM_CHANNELS - 2): self.sbusChannels[i] = 0 # counters initialization byte_in_sbus = 1 bit_in_sbus = 0 ch = 0 bit_in_channel = 0 for i in range(0, 175): # TODO Generalization if self.sbusFrame[byte_in_sbus] & (1 << bit_in_sbus): self.sbusChannels[ch] |= (1 << bit_in_channel) bit_in_sbus += 1 bit_in_channel += 1 if bit_in_sbus == 8: bit_in_sbus = 0 byte_in_sbus += 1 if bit_in_channel == 11: bit_in_channel = 0 ch += 1 # Decode Digitals Channels # Digital Channel 1 if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 0): self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 1 else: self.sbusChannels[self.SBUS_NUM_CHAN - 2] = 0 # Digital Channel 2 if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 1): self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 1 else: self.sbusChannels[self.SBUS_NUM_CHAN - 1] = 0 # Failsafe self.failSafeStatus = self.SBUS_SIGNAL_OK if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 2): self.failSafeStatus = self.SBUS_SIGNAL_LOST if self.sbusFrame[self.SBUS_FRAME_LEN - 2] & (1 << 3): self.failSafeStatus = self.SBUS_SIGNAL_FAILSAFE def get_sync(self): if self.sbus.any() > 0: if self.startByteFound: if self.frameIndex == (self.SBUS_FRAME_LEN - 1): self.sbus.readinto(self.sbusBuff, 1) # end of frame byte if self.sbusBuff[0] == 0: # TODO: Change to use constant var value self.startByteFound = False self.isSync = True self.frameIndex = 0 else: self.sbus.readinto(self.sbusBuff, 1) # keep reading 1 byte until the end of frame self.frameIndex += 1 else: self.frameIndex = 0 self.sbus.readinto(self.sbusBuff, 1) # read 1 byte if self.sbusBuff[0] == 15: # TODO: Change to use constant var value self.startByteFound = True self.frameIndex += 1 def get_new_data(self): if self.isSync: if self.sbus.any() >= self.SBUS_FRAME_LEN: self.sbus.readinto(self.sbusFrame, self.SBUS_FRAME_LEN) # read the whole frame if (self.sbusFrame[0] == 15 and self.sbusFrame[ self.SBUS_FRAME_LEN - 1] == 0): # TODO: Change to use constant var value self.validSbusFrame += 1 self.outOfSyncCounter = 0 self.decode_frame() else: self.lostSbusFrame += 1 self.outOfSyncCounter += 1 if self.outOfSyncCounter > self.OUT_OF_SYNC_THD: self.isSync = False self.resyncEvent += 1 else: self.get_sync()
uart0 = UART(0, 1000000, pins=uart_pins[0][0]) print(uart0) uart1 = UART(1, 1000000, pins=uart_pins[1][0]) print(uart1) print(uart0.write(b'123456') == 6) print(uart1.read() == b'123456') print(uart1.write(b'123') == 3) print(uart0.read(1) == b'1') print(uart0.read(2) == b'23') print(uart0.read() == b'') uart0.write(b'123') buf = bytearray(3) print(uart1.readinto(buf, 1) == 1) print(buf) print(uart1.readinto(buf) == 2) print(buf) # try initializing without the id uart0 = UART(baudrate=1000000, pins=uart_pins[0][0]) uart0.write(b'1234567890') pyb.delay(2) # because of the fifo interrupt levels print(uart1.any() == 10) print(uart1.readline() == b'1234567890') print(uart1.any() == 0) uart0.write(b'1234567890') print(uart1.readall() == b'1234567890')
uart0 = UART(0, 1000000, pins=uart_pins[0][0]) print(uart0) uart1 = UART(1, 1000000, pins=uart_pins[1][0]) print(uart1) print(uart0.write(b'123456') == 6) print(uart1.read() == b'123456') print(uart1.write(b'123') == 3) print(uart0.read(1) == b'1') print(uart0.read(2) == b'23') print(uart0.read() == b'') uart0.write(b'123') buf = bytearray(3) print(uart1.readinto(buf, 1) == 1) print(buf) print(uart1.readinto(buf) == 2) print(buf) uart0.write(b'1234567890') pyb.delay(2) # because of the fifo interrupt levels print(uart1.any() == 10) print(uart1.readline() == b'1234567890') print(uart1.any() == 0) uart0.write(b'1234567890') print(uart1.readall() == b'1234567890') # tx only mode uart0 = UART(0, 1000000, pins=('GP12', None))
#can1.clearfilter(0) """ ## line grid b = [1] * 1024 # 8 horizontal lines # set every 16th byte to 255 # should give 8 vertical lines for i in range(0, 1024, 16): b[i] = 255 #color in the 1st block to see where it is for i in range(0,16): b[i] = 255 """ mfreq = machine.freq() uart1.readinto(buf, 8) while True: #counterUart += 1 #uart1.write(str(counterUart)) #axis_x = accel.x() #axis_y = accel.y() #axis_z = accel.z() axis_x += 1 if axis_x > 128: axis_x = 0 if can1.any(0): recArr = can1.recv(0, timeout=100) recData_au8 = recArr[3]