|
1
|
|
|
from libAnt.constants import * |
|
2
|
|
|
|
|
3
|
|
|
|
|
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
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class SystemResetMessage(Message): |
|
43
|
|
|
def __init__(self): |
|
44
|
|
|
super().__init__(MESSAGE_SYSTEM_RESET, bytearray(1)) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class SetNetworkKeyMessage(Message): |
|
48
|
|
|
def __init__(self, channel: int, key: bytes): |
|
49
|
|
|
content = bytearray() |
|
50
|
|
|
content.append(channel) |
|
51
|
|
|
content.extend(key) |
|
52
|
|
|
super().__init__(MESSAGE_NETWORK_KEY, content) |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
class AssignChannelMessage(Message): |
|
56
|
|
|
def __init__(self, channel: int, type: int, network: int = 0, extended: int = None): |
|
57
|
|
|
content = bytearray() |
|
58
|
|
|
content.append(channel) |
|
59
|
|
|
content.append(type) |
|
60
|
|
|
content.append(network) |
|
61
|
|
|
if extended is not None: |
|
62
|
|
|
content.append(extended) |
|
63
|
|
|
super().__init__(MESSAGE_CHANNEL_ASSIGN, content) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
class SetChannelIdMessage(Message): |
|
67
|
|
|
def __init__(self, channel: int, deviceNumber: int = 0, deviceType: int = 0, transType: int = 0): |
|
68
|
|
|
content = bytearray() |
|
69
|
|
|
content.append(channel) |
|
70
|
|
|
content.extend(deviceNumber.to_bytes(2, byteorder='big')) |
|
71
|
|
|
content.append(deviceType) |
|
72
|
|
|
content.append(transType) |
|
73
|
|
|
super().__init__(MESSAGE_CHANNEL_ID, content) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
class SetChannelRfFrequencyMessage(Message): |
|
77
|
|
|
def __init__(self, channel: int, frequency: int = 2457): |
|
78
|
|
|
content = bytearray() |
|
79
|
|
|
content.append(channel) |
|
80
|
|
|
content.append(frequency - 2400) |
|
81
|
|
|
super().__init__(MESSAGE_CHANNEL_FREQUENCY, content) |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
class OpenRxScanModeMessage(Message): |
|
85
|
|
|
def __init__(self): |
|
86
|
|
|
super().__init__(OPEN_RX_SCAN_MODE, bytearray(1)) |
|
87
|
|
|
|
|
88
|
|
|
class EnableExtendedMessagesMessage(Message): |
|
89
|
|
|
def __init__(self, enable: bool = True): |
|
90
|
|
|
content = bytearray() |
|
91
|
|
|
content.append(0x00) |
|
92
|
|
|
content.append(int(enable)) |
|
93
|
|
|
super().__init__(MESSAGE_ENABLE_EXT_RX_MESSAGES, content) |