Skip to content

Commit 92189fa

Browse files
committed
track the internal state of the device
1 parent 3908200 commit 92189fa

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

canalystii/device.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def __init__(
4242
if active_config is None or active_config.bConfigurationValue != 1:
4343
self._dev.set_configuration(1)
4444

45+
self._initialized = [False, False]
46+
self._started = [False, False]
4547

4648
# Check this looks like the firmware we expect: as this is an unofficial driver,
4749
# we don't know if other versions might are out there.
@@ -147,17 +149,28 @@ def init(self, channel, bitrate=None, timing0=None, timing1=None, start=True):
147149
unknown2=0x1,
148150
) # placeholder
149151
self.send_command(channel, init_packet)
152+
self._initialized[channel] = True
150153
self.start(channel)
151154

152155
def stop(self, channel):
153156
"""Stop this channel. Data won't be sent or received on this channel until it is started again."""
157+
if not self._initialized[channel]:
158+
raise RuntimeError(f"Channel {channel} is not initialized.")
154159
self.send_command(channel, protocol.SimpleCommand(protocol.COMMAND_STOP))
160+
self._started[channel] = False
155161

156162
def start(self, channel):
157163
"""Start this channel."""
164+
if not self._initialized[channel]:
165+
raise RuntimeError(f"Channel {channel} is not initialized.")
158166
self.send_command(channel, protocol.SimpleCommand(protocol.COMMAND_START))
167+
self._started[channel] = True
159168

160169
def receive(self, channel):
170+
if not self._initialized[channel]:
171+
raise RuntimeError(f"Channel {channel} is not initialized.")
172+
if not self._started[channel]:
173+
raise RuntimeError(f"Channel {channel} is stopped, can't receive messages.")
161174
status = self.send_command(
162175
channel, self.COMMAND_MESSAGE_STATUS, protocol.MessageStatusResponse
163176
)
@@ -192,6 +205,10 @@ def receive(self, channel):
192205
return result
193206

194207
def send(self, channel, messages, flush_timeout=None):
208+
if not self._initialized[channel]:
209+
raise RuntimeError(f"Channel {channel} is not initialized.")
210+
if not self._started[channel]:
211+
raise RuntimeError(f"Channel {channel} is stopped, can't send messages.")
195212
if isinstance(messages, protocol.Message):
196213
messages = [messages]
197214
tx_buffer_num = (len(messages) + 2) // 3
@@ -207,10 +224,12 @@ def send(self, channel, messages, flush_timeout=None):
207224
if flush_timeout is not None:
208225
return self.flush_tx_buffer(channel, flush_timeout)
209226

210-
def get_can_status(self):
227+
def get_can_status(self, channel):
211228
"""Return some internal CAN-related values. The actual meaning of these is currently unknown."""
229+
if not self._initialized[channel]:
230+
logger.warning(f"Channel {channel} is not initialized, CAN status may be invalid.")
212231
return self.send_command(
213-
0,
232+
channel,
214233
protocol.SimpleCommand(protocol.COMMAND_CAN_STATUS),
215234
protocol.CANStatusResponse,
216235
)

0 commit comments

Comments
 (0)