| Total Complexity | 10 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | from libAnt.constants import MESSAGE_TX_SYNC |
||
| 4 | class Message: |
||
| 5 | def __init__(self, type: int, content: bytearray): |
||
| 6 | self._type = type |
||
| 7 | self._content = content |
||
| 8 | |||
| 9 | def __len__(self): |
||
| 10 | return len(self._content) |
||
| 11 | |||
| 12 | def __iter__(self): |
||
| 13 | return self._content |
||
| 14 | |||
| 15 | def __str__(self): |
||
| 16 | return '({:02X}): '.format(self._type) + ' '.join('{:02X}'.format(x) for x in self._content) |
||
| 17 | |||
| 18 | def checksum(self) -> int: |
||
| 19 | chk = MESSAGE_TX_SYNC ^ len(self) ^ self._type |
||
| 20 | for b in self._content: |
||
| 21 | chk ^= b |
||
| 22 | return chk |
||
| 23 | |||
| 24 | def encode(self) -> bytearray: |
||
| 25 | b = bytearray() |
||
| 26 | b.append(MESSAGE_TX_SYNC) |
||
| 27 | b.append(len(self)) |
||
| 28 | b.append(self._type) |
||
| 29 | b.extend(self._content) |
||
| 30 | b.append(self.checksum()) |
||
| 31 | return b |
||
| 32 | |||
| 33 | @property |
||
| 34 | def type(self) -> int: |
||
| 35 | return self._type |
||
| 36 | |||
| 37 | @property |
||
| 38 | def content(self) -> bytearray: |
||
| 39 | return self._content |
||
| 40 |