Exemplo n.º 1
0
    def action(self):
        """Broadcasts an action packet to all of the devices on the bus.
        This causes all of the devices to perform their deferred writes
        at the same time.

        """
        if self.show & Bus.SHOW_COMMANDS:
            log('Broadcasting ACTION')
        self.fill_and_write_packet(packet.Id.BROADCAST, packet.Command.ACTION)
Exemplo n.º 2
0
 def send_read(self, dev_id, offset, num_bytes):
     """Sends a READ request to read data from the device's control
     table.
     """
     if self.show & Bus.SHOW_COMMANDS:
         log('Sending READ to ID {} offset 0x{:02x} len {}'.format(
             dev_id, offset, num_bytes))
     self.fill_and_write_packet(dev_id, packet.Command.READ,
                                bytearray((offset, num_bytes)))
Exemplo n.º 3
0
 def send_reset(self, dev_id):
     """Sends a RESET command to the device, which causes it to reset the
        control table to factory defaults.
     """
     if self.show & Bus.SHOW_COMMANDS:
         if dev_id == packet.Id.BROADCAST:
             log('Broadcasting RESET')
         else:
             log('Sending RESET to ID {}'.format(dev_id))
     self.fill_and_write_packet(dev_id, packet.Command.RESET)
Exemplo n.º 4
0
    def send_write(self, dev_id, offset, data, deferred=False):
        """Sends a WRITE request if deferred is False, or REG_WRITE
        request if deferred is True to write data into the device's
        control table.

        data should be an array of ints, or a bytearray.

        Deferred writes will occur when and ACTION command is broadcast.
        """
        if self.show & Bus.SHOW_COMMANDS:
            cmd_str = 'REG_WRITE' if deferred else 'WRITE'
            if dev_id == packet.Id.BROADCAST:
                log('Broadcasting {} offset 0x{:02x} len {}'.format(
                    cmd_str, offset, len(data)))
            else:
                log('Sending {} to ID {} offset 0x{:02x} len {}'.format(
                    cmd_str, dev_id, offset, len(data)))
        cmd = packet.Command.REG_WRITE if deferred else packet.Command.WRITE
        pkt_data = bytearray(len(data))
        pkt_data[0] = offset
        pkt_data[1:] = data
        self.fill_and_write_packet(dev_id, cmd, pkt_data)
Exemplo n.º 5
0
    def sync_write(self, dev_ids, offset, values):
        """Sets up a synchroous write command.

        dev_ids should be an array of device ids.

        offset should be the offset that the data will be written to.

        values should be an array of bytearrays. There should be one bytearray
        for each dev_id, and each bytearray should be of the same length.

        raises ValueError if the dimensionality of values is incorrect.
        """
        if self.show & Bus.SHOW_COMMANDS:
            ids = ', '.join(['{}'.format(id) for id in dev_ids])
            log('Sending SYNC_WRITE to IDs {} offset 0x{:02x} len {}'.format(
                ids, offset, len(values[0])))
        num_ids = len(dev_ids)
        if num_ids != len(values):
            raise ValueError(
                'len(dev_ids) = {} must match len(values) = {}'.format(
                    num_ids, len(values)))
        bytes_per_id = len(values[0])
        param_len = num_ids * (bytes_per_id + 1) + 2
        data = bytearray(param_len)
        data[0] = offset
        data[1] = bytes_per_id
        data_idx = 2
        for id_idx in range(num_ids):
            if len(values[id_idx]) != bytes_per_id:
                raise ValueError('len(values[{}]) not equal {}'.format(
                    id_idx, bytes_per_id))
            data[data_idx] = dev_ids[id_idx]
            data_idx += 1
            data[data_idx:data_idx + bytes_per_id] = values[id_idx]
            data_idx += bytes_per_id

        self.fill_and_write_packet(packet.Id.BROADCAST,
                                   packet.Command.SYNC_WRITE, data)
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
 def send_ping(self, dev_id):
     """Sends a ping to a device."""
     if self.show & Bus.SHOW_COMMANDS:
         log('Sending PING to ID {}'.format(dev_id))
     self.fill_and_write_packet(dev_id, packet.Command.PING)