Пример #1
0
    def _handle_async(self):
        """
        Handles async (usually sensor) messages form sphero,
        It calls the a parser function which will call any regstered
        callback for each type of data
        """
        id_code = ord(self.bluetooth.receive(1))
        length_msb = ord(self.bluetooth.receive(1))
        length_lsb = ord(self.bluetooth.receive(1))
        length = (length_msb << 8) + length_lsb

        array = self._read(length, offset=3)

        array[0] = id_code
        array[1] = length_msb
        array[2] = length_lsb
        checksum = check_sum(array[0:-1])
        if array[-1] != checksum:
            eprint("Malfromed Packet, recieved: {0} expected: {1}".format(
                array[-1], checksum))
            return
        else:
            data = array[3:-1]
            tocall = self._asyn_func.get(id_code, nothing)
            thread = threading.Thread(target=tocall, args=(data, ))
            thread.start()
            return
Пример #2
0
    def _handle_acknowledge(self):
        """
        Parses response packets from sphero. Response added to a response dictionary
        as a Response tuple where tuple[0] indicates success and tuple[1] is the data,
        the thread waiting on the response is alerted to it via an event registered
        in the response_event_lookup dictionary, and is responsible for parsing the data.
        all access should be done with the response_lock.
        """
        msrp = ord(self.bluetooth.receive(1))
        seq = ord(self.bluetooth.receive(1))
        length = ord(self.bluetooth.receive(1))
        if length == 0xff:
            pass
            # raise Exception("NOt Implemented _MSRP: {0}".format(_msrp))
            # TODO cover oxff cases
        array = self._read(length, offset=3)

        array[0] = msrp
        array[1] = seq
        array[2] = length
        checksum = check_sum(array[0:-1])
        if array[-1] != checksum:
            eprint("Malfromed Packet, recieved: {0} expected: {1}".format(
                array[-1], checksum))
            return
        else:
            event = None
            with self._response_lock:
                if seq in self._response_event_lookup:
                    event = self._response_event_lookup[seq]
                    del self._response_event_lookup[seq]
                else:
                    return
            if msrp == 0:
                self._responses[seq] = Response(True, array[3:length + 3 - 1])
            else:
                self._responses[seq] = Response(
                    False, _MSRP.get(msrp, "UNKNOWN ERROR"))
            event.set()
Пример #3
0
    def _send(self, did, cid, data, response):
        """
        sends data to sphero `did` is the device id, `cid` is the virtual core id
        for more information view sphero Docs.

        `data` is the data to send

        `response` indicates if a response is expected or not if it is it handles
        working with the event system, and blocks until response is recieved,
        or `self.response_time_out` elapses
        """
        event = None
        seq_number = 0
        data_length = len(data)
        if response:
            with self._response_lock:
                seq_number = self._seq
                event = threading.Event()
                self._response_event_lookup[seq_number] = event

        with self._msg_lock:
            self._msg[1] = _ANSWER if response else _NO_ANSWER
            self._msg[2] = did
            self._msg[3] = cid
            self._msg[4] = seq_number
            self._msg[5] = data_length + 1
            self._msg[6:6 + data_length] = data
            checksum = check_sum(self._msg[2:6 + data_length])
            self._msg[6 + data_length] = checksum
            self.bluetooth.send(buffer(self._msg[0:6 + data_length + 1]))

        if response:
            if event.wait(self._response_time_out):
                with self._response_lock:
                    return self._responses[seq_number]
        return Response(True, '')