Repository navigation
Expand file tree
/
Copy pathserial_channel_device.py
More file actions
200 lines (158 loc) · 6.98 KB
/
Copy pathserial_channel_device.py
File metadata and controls
200 lines (158 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'''
Useful for interacting with serial devices which use channels.
'''
import struct
ACK_BYTE = bytes((0xCB,))
CMD_GET = 0
CMD_SET = 1
CMD_GET_BULK = 2
CMD_SET_BULK = 3
CMD_AND = 4
CMD_OR = 5
CMD_GET_BUFFER= 6
CMD_SET_BUFFER = 7
class SerialChannelDevice():
"""A serial device with single-byte channels and several-byte
buffers.
Channels can be GET/SET individually or in bulk.
Buffers can be GET/SET in bulk only.
Consult device documentation for channel/buffer implementation.
+------------------------------------------------------------------+
| Serial Channel Device |
| |
| Channels (1B) Buffers (XB) |
| +-----------+ +-----------+-----------+-----------+-----------+ |
| | ch0 | | buf0-0 | buf0-1 | ... | buf0-N | |
| +-----------+ +-----------+-----------+-----------+-----------+ |
| | ch1 | +-----------+-----------+-----------+-----------+ |
| +-----------+ | buf1-0 | buf1-1 | ... | buf1-N | |
| | ... | +-----------+-----------+-----------+-----------+ |
| +-----------+ |
| | chN | |
| +-----------+ |
+------------------------------------------------------------------+
"""
def __init__(self, serial_port):
self.serial_port = serial_port
def get(self, channel_index):
"""Returns GetPacket as bytes.
GET packet is for retreiving channel values.
+--------+---------------+
| cmd id | channel index |
+--------+---------------+
| 3 bits | 5 bits |
+--------+---------------+
"""
self.transaction(
struct.pack('B', (CMD_GET << 5) | (channel_index & 0x1f)))
# Serial port will now contain channel data
return self.serial_port.read(1)
def set(self, channel_index, value):
"""Returns SetPacket as bytes.
SET packet for setting channel values.
+--------+---------------+--------+
| cmd id | channel index | value |
+--------+---------------+--------+
| 3 bits | 5 bits | 1 byte |
+--------+---------------+--------+
"""
self.transaction(
struct.pack('BB',
(CMD_SET << 5 | channel_index & 0x1f),
value))
def get_bulk(self, channel_index, length):
"""GET BULK packet for retrieving multiple adjacent channel
values in one go.
+--------+---------------------+--------+
| cmd id | start channel index | length |
+--------+---------------------+--------+
| 3 bits | 5 bits | 1 byte |
+--------+---------------------+--------+
"""
self.transaction(
struct.pack('BB',
(CMD_GET_BULK << 5 | channel_index & 0x1f),
length))
# Serial port will now contain channel data
return self.serial_port.read(length)
def set_bulk(self, channel_index, value_bytes):
"""SET BULK packet for setting multiple adjacent channel values
in one go.
+--------+-----------------+-----+------------+
| cmd id | start ch. index | len | values |
+--------+-----------------+-----+------------+
| 3 bits | 5 bits | 1B | 1+ byte(s) |
+--------+-----------------+-----+------------+
"""
self.transaction(
struct.pack('BB',
(CMD_SET_BULK << 5 | channel_index & 0x1f),
len(value_bytes)) + value_bytes)
def and_mask(self, channel_index, mask):
"""Returns AndPacket as bytes.
AND packet for ANDing channel values.
+--------+---------------+-----------+
| cmd id | channel index | AND mask |
+--------+---------------+-----------+
| 3 bits | 5 bits | 1 byte |
+--------+---------------+-----------+
"""
self.transaction(
struct.pack('B', (CMD_AND << 5 | channel_index & 0x1f)) + \
bytes((mask,)))
def or_mask(self, channel_index, mask):
"""Returns OrPacket as bytes.
OR packet for ORing channel values.
+--------+---------------+----------+
| cmd id | channel index | OR mask |
+--------+---------------+----------+
| 3 bits | 5 bits | 1 byte |
+--------+---------------+----------+
"""
self.transaction(
struct.pack('B', (CMD_OR << 5 | channel_index & 0x1f)) + \
bytes((mask,)))
def set_bit(self, channel_index, bit_index, state):
"""Sets a bit in a channel to state."""
if state:
self.or_mask(channel_index, 1 << bit_index)
else:
self.and_mask(channel_index, 0xff ^ (1 << bit_index))
def get_bit(self, channel_index, bit_index):
"""Returns a bit from a channel."""
value = struct.unpack('B', self.get(channel_index))[0]
return (value >> bit_index) & 0x1
def get_buffer(self, buffer_index, length, offset=0):
"""GET BUFFER packet for reading whole buffers.
+--------+--------------+--------+--------+
| cmd id | buffer index | offset | length |
+--------+--------------+--------+--------+
| 3 bits | 5 bits | 1 byte | 1 byte |
+--------+--------------+--------+--------+
"""
self.transaction(
struct.pack('BBB',
(CMD_GET_BUFFER << 5 | buffer_index & 0x1f),
offset,
length))
# Serial port will now contain buffer data
return self.serial_port.read(length)
def set_buffer(self, buffer_index, value_bytes, offset=0):
"""SET BUFFER packet for setting whole buffers.
+--------+--------------+--------+--------+------------+
| cmd id | buffer index | offset | length | values |
+--------+--------------+--------+--------+------------+
| 3 bits | 5 bits | 1 byte | 1 byte | 1+ byte(s) |
+--------+--------------+--------+--------+------------+
"""
self.transaction(
struct.pack('BBB',
(CMD_SET_BUFFER << 5 | buffer_index & 0x1f),
offset,
len(value_bytes)) + value_bytes)
def transaction(self, tx_bytes):
"""Sends a packet and waits for a ACK response."""
# Send the packet
self.serial_port.write(tx_bytes)
# CodeBug will always return an ACK byte
assert self.serial_port.read(1) == ACK_BYTE