-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
executable file
·143 lines (112 loc) · 4.16 KB
/
display.py
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
#!/usr/bin/python3
from typing import Any, List
import argparse
import libconf
import zmq
import sys
import re
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from multiprocessing import Process, Value, Array
from pprint import pprint
# import matplotlib as mpl
# mpl.rcParams['toolbar'] = 'None'
index_to_freq: List[str] = ["0.8Hz", "1Hz", "1.25Hz", "1.6Hz", "2Hz", "2.5Hz", "3.15Hz", "4Hz", "5Hz", "6.3Hz", "8Hz", "10Hz", "12.5Hz", "16Hz", "20Hz", "25Hz", "31.5Hz", "40Hz", "50Hz", "63Hz", "80Hz", "100Hz", "125Hz", "160Hz", "200Hz", "250Hz", "315Hz", "400Hz", "500Hz", "630Hz", "800Hz", "1kHz", "1.25kHz", "1.6kHz", "2kHz", "2.5kHz", "3.15kHz", "4kHz", "5kHz", "6.3kHz", "8kHz", "10Hz", "12.5kHz", "16kHz", "20kHz"]
parser = argparse.ArgumentParser(description="Display for the SonoMKR Project")
parser.add_argument("-c", "--conf", dest="conf", default="./display.conf", help="The location of the configuration file")
parser.add_argument("--display-conf", dest="display_conf", action="store_true", help="Set this flag to display current config and return")
args = parser.parse_args()
class Channel:
zmq_address: str
zmq_topic: str
freqs: Array
values: Array
size: Value
def __init__(self, config):
self.freqs = Array('I', len(index_to_freq))
self.values = Array('d', len(index_to_freq))
self.size = Value('I', 0)
if not config.zmqAddress:
raise Exception("Channel config misses the 'zmqAddress' parameter")
self.zmq_address = config.zmqAddress
if not config.zmqTopic:
raise Exception("Channel config misses the 'zmqTopic' parameter")
self.zmq_topic = config.zmqTopic
try:
with open(args.conf, 'r') as cf:
conf = libconf.load(cf)
except OSError as err:
sys.stderr.write(f"[ERROR] display config file {args.conf} open error : {err}\n")
exit(1)
if (args.display_conf):
print(libconf.dumps(conf))
exit(0)
zmq_context = zmq.Context()
channels = []
for channel_cfg in conf.channels:
if channel_cfg.active is False:
break
try:
channel = Channel(channel_cfg)
except Exception as err:
sys.stderr.write(f"[ERROR] channel config error :\n{libconf.dumps(channel_cfg)}\nError message : {err}\n")
break
channels.append(channel)
# p = Process(target=man.start_listening)
# p.start()
# man.start_listening()
def listen(channel, zmq_context):
zmq_socket = zmq_context.socket(zmq.SUB)
zmq_socket.connect(channel.zmq_address)
zmq_socket.subscribe(channel.zmq_topic)
while True:
msg = zmq_socket.recv_multipart()
msg = msg[1]
data_matches = re.finditer(r"(\d{1,2}):(-?\d{1,3}.\d{1,2});", msg.decode("utf-8"))
i = 0
for match in data_matches:
channel.freqs[i] = int(match.group(1))
channel.values[i] = float(match.group(2))
i += 1
channel.size.value = i
zmq_context = zmq.Context()
procs = []
for channel in channels:
proc = Process(target=listen, args=(channel, zmq_context))
proc.start()
procs.append(proc)
fig, ax = plt.subplots()
# bars = ax.bar(index_to_freq, list(range(len(index_to_freq))))
def init():
ax.set_xticks(list(range(len(index_to_freq))))
ax.set_xticklabels(index_to_freq, [])
ax.set_ylim(0, 100)
# return bars
def update(frame):
for channel in channels:
size = int(channel.size.value)
bar = ax.bar(list(range(size)), list(channel.values[:size]), color='blue')
ax.set_xticks(list(range(size)))
ax.set_xticklabels(index_to_freq[channel.freqs[0]:channel.freqs[0]+size], rotation=45)
ax.set_ylim(0, 100)
return bar
ani = animation.FuncAnimation(fig, update, init_func=None, blit=True, interval=999)
plt.rcParams['toolbar'] = 'None' # Remove tool bar (upper bar)
fig.canvas.window().statusBar().setVisible(False) # Remove status bar (bottom bar)
plt.tight_layout(pad=0)
plt.subplots_adjust(bottom=0.1)
mng = plt.get_current_fig_manager()
try:
mng.frame.Maximize(True)
except:
pass
try:
mng.window.showMaximized()
except:
pass
plt.show()
for proc in procs:
proc.terminate()
exit(0)