Class used to handle memory reads that will split up the read in multiple packets in necessary
| 132 | |
| 133 | |
| 134 | class _WriteRequest: |
| 135 | """ |
| 136 | Class used to handle memory reads that will split up the read in multiple |
| 137 | packets in necessary |
| 138 | """ |
| 139 | MAX_DATA_LENGTH = 25 |
| 140 | |
| 141 | def __init__(self, mem, addr, data, cf): |
| 142 | """Initialize the object with good defaults""" |
| 143 | self.mem = mem |
| 144 | self.addr = addr |
| 145 | self._bytes_left = len(data) |
| 146 | self._data = data |
| 147 | self.data = bytearray() |
| 148 | self.cf = cf |
| 149 | |
| 150 | self._current_addr = addr |
| 151 | |
| 152 | self._sent_packet = None |
| 153 | self._sent_reply = None |
| 154 | |
| 155 | self._addr_add = 0 |
| 156 | |
| 157 | def start(self): |
| 158 | """Start the fetching of the data""" |
| 159 | self._write_new_chunk() |
| 160 | |
| 161 | def resend(self): |
| 162 | logger.debug('Sending write again...') |
| 163 | self.cf.send_packet( |
| 164 | self._sent_packet, expected_reply=self._sent_reply, timeout=1) |
| 165 | |
| 166 | def _write_new_chunk(self): |
| 167 | """ |
| 168 | Called to request a new chunk of data to be read from the Crazyflie |
| 169 | """ |
| 170 | # Figure out the length of the next request |
| 171 | new_len = len(self._data) |
| 172 | if new_len > _WriteRequest.MAX_DATA_LENGTH: |
| 173 | new_len = _WriteRequest.MAX_DATA_LENGTH |
| 174 | |
| 175 | logger.debug('Writing new chunk of {}bytes at 0x{:X}'.format( |
| 176 | new_len, self._current_addr)) |
| 177 | |
| 178 | data = self._data[:new_len] |
| 179 | self._data = self._data[new_len:] |
| 180 | |
| 181 | pk = CRTPPacket() |
| 182 | pk.set_header(CRTPPort.MEM, CHAN_WRITE) |
| 183 | pk.data = struct.pack('<BI', self.mem.id, self._current_addr) |
| 184 | # Create a tuple used for matching the reply using id and address |
| 185 | reply = struct.unpack('<BBBBB', pk.data) |
| 186 | self._sent_reply = reply |
| 187 | # Add the data |
| 188 | pk.data += struct.pack('B' * len(data), *data) |
| 189 | self._sent_packet = pk |
| 190 | self.cf.send_packet(pk, expected_reply=reply, timeout=1) |
| 191 |