Skip to content

Commit c6f73eb

Browse files
author
Marc Hofmann
committed
improved examples
1 parent 5e85f3a commit c6f73eb

File tree

8 files changed

+60
-15
lines changed

8 files changed

+60
-15
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"type": "debugpy",
1010
"request": "launch",
1111
// "program": "src/__main__.py",
12-
"module": "examples.low_level.example_low_level_plot",
12+
"module": "examples.dyscom.example_dyscom_fastplotlib",
1313
"justMyCode": false,
1414
// "args": ["COM3"],
1515
"console": "integratedTerminal"

examples/dyscom/example_dyscom_fastplotlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ async def device_communication() -> int:
6262
await dyscom.start()
6363

6464
# loop for some time
65-
for x in range(1000):
65+
for x in range(5000):
6666
# check if we closed window
6767
if not is_window_open:
6868
break
6969

7070
# check operation mode from time to time, this function is not waiting for response
7171
# so we have to handle it by ourself later
72-
if x % 100 == 0:
72+
if x % 500 == 0:
7373
dyscom.send_get_operation_mode()
7474

7575
live_data_counter = 0

examples/dyscom/example_dyscom_pyplot.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ async def main() -> int:
4949
# start dyscom measurement
5050
await dyscom.start()
5151

52-
for x in range(1000):
52+
# loop for some time
53+
for x in range(5000):
5354
# check operation mode from time to time
54-
if x % 100 == 0:
55+
if x % 500 == 0:
5556
dyscom.send_get_operation_mode()
5657

5758
live_data_counter = 0
@@ -70,7 +71,7 @@ async def main() -> int:
7071
break
7172

7273
# reduce framerate further
73-
if sld.number % 60 == 0:
74+
if sld.number % 10 == 0:
7475
plot_helper.append_value(0, sld.samples[0].value)
7576
plot_helper.update()
7677

@@ -88,6 +89,8 @@ async def main() -> int:
8889
# close serial port connection
8990
connection.close()
9091

92+
print("Close plot window to quit")
93+
plot_helper.loop()
9194
return 0
9295

9396

examples/dyscom/example_dyscom_write_csv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ async def device_communication() -> int:
6464
total_count = 0
6565

6666
# loop for some time
67-
for x in range(1000):
67+
for x in range(5000):
6868
# check operation mode from time to time, this function is not waiting for response
6969
# so we have to handle it by ourself later
70-
if x % 100 == 0:
70+
if x % 500 == 0:
7171
dyscom.send_get_operation_mode()
7272

7373
live_data_counter = 0

examples/low_level/example_low_level_plot.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_channel_color(channel: Channel) -> str:
3737
"""Retrieves color from channel"""
3838
color = channel.name
3939
if color == "WHITE":
40-
color = "PINK"
40+
color = "PURPLE"
4141
return color
4242

4343

@@ -48,7 +48,7 @@ async def main() -> int:
4848
for connector in Connector:
4949
for channel in Channel:
5050
plots_info[calc_plot_index(connector, channel)] = f"Connector {connector.name}, channel {channel.name}", get_channel_color(channel)
51-
plot_helper = PyPlotHelper(plots_info, 2500)
51+
plot_helper = PyPlotHelper(plots_info, 500)
5252

5353
# get comport from command line argument
5454
com_port = ExampleUtils.get_comport_from_commandline_argument()
@@ -74,27 +74,31 @@ async def main() -> int:
7474
send_channel_config(low_level_layer)
7575

7676
# wait for stimulation to happen
77-
await asyncio.sleep(0.25)
77+
await asyncio.sleep(1.0)
7878

7979
# process all acknowledges and append values to plot data
8080
while True:
8181
ack = low_level_layer.packet_buffer.get_packet_from_buffer()
8282
if ack:
8383
if ack.command == Commands.LOW_LEVEL_CHANNEL_CONFIG_ACK:
8484
ll_config_ack: PacketLowLevelChannelConfigAck = ack
85+
# update plot with measured values
8586
plot_helper.append_values(calc_plot_index(ll_config_ack.connector, ll_config_ack.channel),
8687
ll_config_ack.measurement_samples)
88+
plot_helper.update()
8789
else:
8890
break
8991

92+
await asyncio.sleep(0.1)
93+
9094
# call stop low level
9195
await low_level_layer.stop()
92-
# update plot with measured values
93-
plot_helper.update()
9496

9597
# close serial port connection
9698
connection.close()
9799

100+
print("Close plot window to quit")
101+
plot_helper.loop()
98102
return 0
99103

100104

examples/utils/example_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import threading
55
import os
6+
import asyncio
67
from typing import Callable
78
from getch import getch
89

@@ -56,3 +57,21 @@ def get_comport_from_commandline_argument() -> str:
5657

5758
com_port = sys.argv[1]
5859
return com_port
60+
61+
62+
@staticmethod
63+
async def wait_for_any_key_pressed(cb: Callable[[], None] | None):
64+
# keyboard func
65+
def input_callback(input_value: str) -> bool:
66+
"""Callback call from keyboard input thread"""
67+
# quit on any key
68+
return True
69+
70+
# create keyboard input thread for non blocking console input
71+
keyboard_input_thread = KeyboardInputThread(input_callback)
72+
73+
# now we can start stimulation
74+
while keyboard_input_thread.is_alive():
75+
if cb is not None:
76+
cb()
77+
await asyncio.sleep(0.1)

examples/utils/plot_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def update(self):
6565
"""Update plot"""
6666

6767

68+
def loop(self):
69+
"""Run event loop until plot window closed"""
70+
pass
71+
72+
6873
def _calc_layout_dimension(self, channel_count: int) -> tuple[int, int]:
6974
"""Calculates layout for a specific number of channels, tries to grow
7075
equal in both directions"""

examples/utils/pyplot_utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,39 @@ class PyPlotHelper(PlotHelper):
7171
def __init__(self, channels: dict[int, tuple[str, str]], max_value_count: int):
7272
super().__init__()
7373

74+
self._window_closed = False
7475
x_dimension, y_dimension = self._calc_layout_dimension(len(channels))
7576
self._figure, self._axes = plt.subplots(y_dimension, x_dimension, constrained_layout=True, squeeze=False)
77+
self._figure.canvas.mpl_connect("close_event", self._on_close)
7678

7779
for (key, value), sub_plot in zip(channels.items(), self._axes.flat):
7880
sub_plot.set(xlabel="Samples", ylabel=value[0], title=value[0])
7981
self._data[key] = PyPlotValueChannel(sub_plot, max_value_count, value[1])
8082

8183
# interactive mode and show plot
82-
self._animation_result = animation.FuncAnimation(self._figure, self._animation, interval=100)
83-
# plt.ion()
84+
self._animation_result = animation.FuncAnimation(self._figure, self._animation,
85+
interval=100, save_count=max_value_count)
86+
plt.ion()
8487
plt.show(block=False)
88+
self.update()
8589

8690

8791
def update(self):
8892
"""Wait for a short time to process events"""
8993
plt.pause(0.0001)
9094

9195

96+
def loop(self):
97+
"""Run event loop until plot window closed"""
98+
while not self._window_closed:
99+
self.update()
100+
101+
92102
def _animation(self, frame: int, *fargs: tuple): # pylint:disable=unused-argument
93103
"""This function is call in context of main thread"""
94104
for x in self._data.values():
95105
x.update_plot()
106+
107+
108+
def _on_close(self, event):
109+
self._window_closed = True

0 commit comments

Comments
 (0)