A packet that can be sent via the CRTP.
| 54 | |
| 55 | |
| 56 | class CRTPPacket(object): |
| 57 | """ |
| 58 | A packet that can be sent via the CRTP. |
| 59 | """ |
| 60 | |
| 61 | # The max size of a CRTP packet payload |
| 62 | MAX_DATA_SIZE = 30 |
| 63 | |
| 64 | def __init__(self, header=0, data=None): |
| 65 | """ |
| 66 | Create an empty packet with default values. |
| 67 | """ |
| 68 | self.size = 0 |
| 69 | self._data = bytearray() |
| 70 | # The two bits in position 3 and 4 needs to be set for legacy |
| 71 | # support of the bootloader |
| 72 | self.header = header | 0x3 << 2 |
| 73 | self._port = (header & 0xF0) >> 4 |
| 74 | self._channel = header & 0x03 |
| 75 | if data: |
| 76 | self._set_data(data) |
| 77 | |
| 78 | def _get_channel(self): |
| 79 | """Get the packet channel""" |
| 80 | return self._channel |
| 81 | |
| 82 | def _set_channel(self, channel): |
| 83 | """Set the packet channel""" |
| 84 | self._channel = channel |
| 85 | self._update_header() |
| 86 | |
| 87 | def _get_port(self): |
| 88 | """Get the packet port""" |
| 89 | return self._port |
| 90 | |
| 91 | def _set_port(self, port): |
| 92 | """Set the packet port""" |
| 93 | self._port = port |
| 94 | self._update_header() |
| 95 | |
| 96 | def get_header(self): |
| 97 | """Get the header""" |
| 98 | self._update_header() |
| 99 | return self.header |
| 100 | |
| 101 | def set_header(self, port, channel): |
| 102 | """ |
| 103 | Set the port and channel for this packet. |
| 104 | """ |
| 105 | self._port = port |
| 106 | self.channel = channel |
| 107 | self._update_header() |
| 108 | |
| 109 | def _update_header(self): |
| 110 | """Update the header with the port/channel values""" |
| 111 | # The two bits in position 3 and 4 needs to be set for legacy |
| 112 | # support of the bootloader |
| 113 | self.header = ((self._port & 0x0f) << 4 | 3 << 2 | |
no outgoing calls