Skip to content

Commit 99ca8e5

Browse files
fixed potential bug in P24 examples regarding creation time of KeyboardInputThread
1 parent 6400140 commit 99ca8e5

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎examples/low_level/example_low_level.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector):
5858
print("Press 1 or 2 to stimulate green or yellow connector")
5959
print("Press + or - to increase or decrease current")
6060
print("Press q to quit")
61-
# create keyboard input thread for non blocking console input
62-
keyboard_input_thread = KeyboardInputThread(input_callback)
6361

6462
# get comport from command line argument
6563
com_port = ExampleUtils.get_comport_from_commandline_argument()
6664

6765
connection = None
66+
# created only once low_level_layer is assigned below, because input_callback
67+
# references it and a key press before that would raise an exception
68+
keyboard_input_thread = None
6869
try:
6970
# create serial port connection
7071
connection = SerialPortConnection(com_port)
@@ -83,6 +84,10 @@ def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector):
8384
# call init low level
8485
await low_level_layer.init(LowLevelMode.NO_MEASUREMENT, LowLevelHighVoltageSource.STANDARD)
8586

87+
# only create the keyboard input thread once low_level_layer is assigned, since
88+
# input_callback references it and a key press before that would raise an exception
89+
keyboard_input_thread = KeyboardInputThread(input_callback)
90+
8691
# now we can start stimulation
8792
while keyboard_input_thread.is_alive():
8893
# get new packets from connection
@@ -102,7 +107,8 @@ def send_channel_config(low_level_layer: LayerLowLevel, connector: Connector):
102107
finally:
103108
# request the keyboard thread to stop even on an exception above, so it
104109
# doesn't keep waiting for input that no longer matters
105-
keyboard_input_thread.stop()
110+
if keyboard_input_thread is not None:
111+
keyboard_input_thread.stop()
106112
# always close the serial port connection, even if an exception occurred above,
107113
# otherwise the COM port stays locked for subsequent runs
108114
if connection is not None:

‎examples/mid_level/example_mid_level_update.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ def input_callback(input_value: str) -> bool:
9898
print("Press * or / to increase or decrease current by 10mA")
9999
print("Press q to quit")
100100

101-
# create keyboard input thread for non blocking console input
102-
keyboard_input_thread = KeyboardInputThread(input_callback)
103-
104101
connection = None
102+
# created only once mid_level is assigned below, because input_callback
103+
# references it and a key press before that would raise an exception
104+
keyboard_input_thread = None
105105
try:
106106
# get comport from command line argument
107107
com_port = ExampleUtils.get_comport_from_commandline_argument()
@@ -124,6 +124,10 @@ def input_callback(input_value: str) -> bool:
124124
# set stimulation pattern, P24 device will now stimulate according this pattern
125125
await mid_level.update(self._channel_config)
126126

127+
# only create the keyboard input thread once mid_level is assigned, since
128+
# input_callback references it and a key press before that would raise an exception
129+
keyboard_input_thread = KeyboardInputThread(input_callback)
130+
127131
possible_errors = [ResultAndError.ELECTRODE_ERROR, ResultAndError.PULSE_TIMEOUT_ERROR, ResultAndError.PULSE_LOW_CURRENT_ERROR]
128132
while keyboard_input_thread.is_alive():
129133
# we have to call get_current_data() every 1.5s to keep stimulation ongoing
@@ -141,7 +145,8 @@ def input_callback(input_value: str) -> bool:
141145
finally:
142146
# request the keyboard thread to stop even on an exception above, so it
143147
# doesn't keep waiting for input that no longer matters
144-
keyboard_input_thread.stop()
148+
if keyboard_input_thread is not None:
149+
keyboard_input_thread.stop()
145150
# always close the serial port connection, even if an exception occurred above,
146151
# otherwise the COM port stays locked for subsequent runs
147152
if connection is not None:

0 commit comments

Comments
 (0)