Class used to handle memory reads that will split up the read in multiple packets if necessary
| 68 | |
| 69 | |
| 70 | class _ReadRequest: |
| 71 | """ |
| 72 | Class used to handle memory reads that will split up the read in multiple |
| 73 | packets if necessary |
| 74 | """ |
| 75 | MAX_DATA_LENGTH = 20 |
| 76 | |
| 77 | def __init__(self, mem, addr, length, cf): |
| 78 | """Initialize the object with good defaults""" |
| 79 | self.mem = mem |
| 80 | self.addr = addr |
| 81 | self._bytes_left = length |
| 82 | self.data = bytearray() |
| 83 | self.cf = cf |
| 84 | |
| 85 | self._current_addr = addr |
| 86 | |
| 87 | def start(self): |
| 88 | """Start the fetching of the data""" |
| 89 | self._request_new_chunk() |
| 90 | |
| 91 | def resend(self): |
| 92 | logger.debug('Sending write again...') |
| 93 | self._request_new_chunk() |
| 94 | |
| 95 | def _request_new_chunk(self): |
| 96 | """ |
| 97 | Called to request a new chunk of data to be read from the Crazyflie |
| 98 | """ |
| 99 | # Figure out the length of the next request |
| 100 | new_len = self._bytes_left |
| 101 | if new_len > _ReadRequest.MAX_DATA_LENGTH: |
| 102 | new_len = _ReadRequest.MAX_DATA_LENGTH |
| 103 | |
| 104 | logger.debug('Requesting new chunk of {}bytes at 0x{:X}'.format( |
| 105 | new_len, self._current_addr)) |
| 106 | |
| 107 | # Request the data for the next address |
| 108 | pk = CRTPPacket() |
| 109 | pk.set_header(CRTPPort.MEM, CHAN_READ) |
| 110 | pk.data = struct.pack('<BIB', self.mem.id, self._current_addr, new_len) |
| 111 | reply = struct.unpack('<BBBBB', pk.data[:-1]) |
| 112 | self.cf.send_packet(pk, expected_reply=reply, timeout=1) |
| 113 | |
| 114 | def add_data(self, addr, data): |
| 115 | """Callback when data is received from the Crazyflie""" |
| 116 | data_len = len(data) |
| 117 | if not addr == self._current_addr: |
| 118 | logger.warning( |
| 119 | 'Address did not match when adding data to read request!') |
| 120 | return |
| 121 | |
| 122 | # Add the data and calculate the next address to fetch |
| 123 | self.data += data |
| 124 | self._bytes_left -= data_len |
| 125 | self._current_addr += data_len |
| 126 | |
| 127 | if self._bytes_left > 0: |