def test_addr(self): self.clear_log() dump_mem(b'0123456789ABCDEFG', addr=0x1234, log=self.log) self.assertEqual(self.log_lines, [ '1234: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 0123456789ABCDEF', '1244: 47 G' ])
def test_no_prefix(self): self.clear_log() dump_mem(b'0123456789ABCDEFG', log=self.log) self.assertEqual(self.log_lines, [ '0000: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 0123456789ABCDEF', '0010: 47 G' ])
def test_no_prefix_no_addr(self): self.clear_log() dump_mem(b'0123456789ABCDEFG', show_addr=False, log=self.log) self.assertEqual(self.log_lines, [ '30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 0123456789ABCDEF', '47 G' ])
def test_a_bit_more_than_a_line(self): self.clear_log() dump_mem(b'0123456789ABCDEFG', prefix=PREFIX, log=self.log) self.assertEqual(self.log_lines, [ ' Prefix: 0000: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 0123456789ABCDEF', ' Prefix: 0010: 47 G' ])
def test_exactly_one_line_no_ascii(self): self.clear_log() dump_mem(b'0123456789ABCDEF', prefix=PREFIX, show_ascii=False, log=self.log) self.assertEqual(self.log_lines, [ ' Prefix: 0000: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46' ])
def fill_and_write_packet(self, dev_id, cmd, data=None): """Allocates and fills a packet. data should be a bytearray of data to include in the packet, or None if no data should be included. """ packet_len = 6 if data is not None: packet_len += len(data) pkt_bytes = bytearray(packet_len) pkt_bytes[0] = 0xff pkt_bytes[1] = 0xff pkt_bytes[2] = dev_id pkt_bytes[3] = 2 # for len and cmd pkt_bytes[4] = cmd if data is not None: pkt_bytes[3] += len(data) pkt_bytes[5:packet_len - 1] = data pkt_bytes[-1] = ~sum(pkt_bytes[2:-1]) & 0xff if self.show & Bus.SHOW_PACKETS: dump_mem(pkt_bytes, prefix=' W', show_ascii=True, log=log) self.serial_port.write_packet(pkt_bytes)
def read_status_packet(self): """Reads a status packet and returns it. Rasises a bioloid.bus.BusError if an error occurs. """ pkt = packet.Packet(status_packet=True) while True: # start = pyb.micros() byte = self.serial_port.read_byte() if byte is None: if self.show & Bus.SHOW_COMMANDS: log('TIMEOUT') if self.show & Bus.SHOW_PACKETS: dump_mem(pkt.pkt_bytes, prefix=' R', show_ascii=True, log=log) raise BusError(packet.ErrorCode.TIMEOUT) err = pkt.process_byte(byte) if err != packet.ErrorCode.NOT_DONE: break if err != packet.ErrorCode.NONE: err_ex = BusError(err) if self.show & Bus.SHOW_COMMANDS: log(err_ex) if self.show & Bus.SHOW_PACKETS: dump_mem(pkt.pkt_bytes, prefix=' R', show_ascii=True, log=log) raise err_ex err = pkt.error_code() if self.show & Bus.SHOW_COMMANDS: log('Rcvd Status: {} from ID: {}'.format(packet.ErrorCode(err), pkt.dev_id)) if self.show & Bus.SHOW_PACKETS: dump_mem(pkt.pkt_bytes, prefix=' R', show_ascii=True, log=log) if err != packet.ErrorCode.NONE: raise BusError(err) return pkt
def test_less_than_one_line_no_ascii(self): self.clear_log() dump_mem(b'0123', prefix=PREFIX, show_ascii=False, log=self.log) self.assertEqual(self.log_lines, [' Prefix: 0000: 30 31 32 33'])
def test_empty_buffer(self): self.clear_log() dump_mem(b'', prefix=PREFIX, log=self.log) self.assertEqual(self.log_lines, [' Prefix:No data'])
def test_non_printable(self): self.clear_log() dump_mem(b'012\x00\x01\x1e\x1f456', log=self.log) self.assertEqual(self.log_lines, [ '0000: 30 31 32 00 01 1e 1f 34 35 36 012....456', ])