-
Notifications
You must be signed in to change notification settings - Fork 59
/
clockmultiplier.py
executable file
·218 lines (180 loc) · 7.52 KB
/
clockmultiplier.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# This software is part of the EEGsynth project, see <https://github.com/eegsynth/eegsynth>.
#
# Copyright (C) 2018-2020 EEGsynth project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import configparser
import argparse
import os
import redis
import serial
import sys
import threading
import time
if hasattr(sys, 'frozen'):
path = os.path.split(sys.executable)[0]
file = os.path.split(sys.executable)[-1]
name = os.path.splitext(file)[0]
elif __name__=='__main__' and sys.argv[0] != '':
path = os.path.split(sys.argv[0])[0]
file = os.path.split(sys.argv[0])[-1]
name = os.path.splitext(file)[0]
elif __name__=='__main__':
path = os.path.abspath('')
file = os.path.split(path)[-1] + '.py'
name = os.path.splitext(file)[0]
else:
path = os.path.split(__file__)[0]
file = os.path.split(__file__)[-1]
name = os.path.splitext(file)[0]
# eegsynth/lib contains shared modules
sys.path.insert(0, os.path.join(path, '../../lib'))
import EEGsynth
class TriggerThread(threading.Thread):
def __init__(self, redischannel, rate, lrate):
threading.Thread.__init__(self)
self.redischannel = redischannel
self.rate = rate
self.lrate = lrate
self.key = "x%d.%s" % (rate, redischannel)
self.previous = None # keep the time of the previous trigger
self.interval = None # estimate the interval between triggers
self.running = True
self.timer = []
def stop(self):
self.running = False
def run(self):
global count
global interval
pubsub = r.pubsub()
# this message unblocks the Redis listen command
pubsub.subscribe('CLOCKMULTIPLIER_UNBLOCK')
# this message triggers the event
pubsub.subscribe(self.redischannel)
while self.running:
for item in pubsub.listen():
if not self.running or not item['type'] == 'message':
break
if item['channel'] == self.redischannel:
now = time.time()
count += 1 # this is for the total count
# cancel all timers that are still running
for t in self.timer:
t.cancel()
self.timer = []
if self.previous == None:
# it is not yet possible to estimate the interval
self.previous = now
continue
elif self.interval == None:
# this is the first estimate of the interval between triggers
self.interval = now - self.previous
self.previous = now
else:
# update the estimate of the interval between triggers
# the learning rate determines how fast the interval updates (0=never, 1=immediate)
self.interval = (1 - self.lrate) * self.interval + self.lrate * (now - self.previous)
self.previous = now
val = float(item['data'])
# send the first one immediately
patch.setvalue(self.key, val)
# schedule the subsequent ones after some time
for number in range(1, self.rate):
delay = number * (self.interval / self.rate)
t = threading.Timer(delay, patch.setvalue, args=[self.key, val])
t.start()
self.timer.append(t)
def _setup():
'''Initialize the module
This adds a set of global variables
'''
global parser, args, config, r, response, patch
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--inifile", default=os.path.join(path, name + '.ini'), help="name of the configuration file")
args = parser.parse_args()
config = configparser.ConfigParser(inline_comment_prefixes=('#', ';'))
config.read(args.inifile)
try:
r = redis.StrictRedis(host=config.get('redis', 'hostname'), port=config.getint('redis', 'port'), db=0, charset='utf-8', decode_responses=True)
response = r.client_list()
except redis.ConnectionError:
raise RuntimeError("cannot connect to Redis server")
# combine the patching from the configuration file and Redis
patch = EEGsynth.patch(config, r)
# there should not be any local variables in this function, they should all be global
if len(locals()):
print('LOCALS: ' + ', '.join(locals().keys()))
def _start():
'''Start the module
This uses the global variables from setup and adds a set of global variables
'''
global parser, args, config, r, response, patch, name
global monitor, debug, channels, multipliers, lrate, count, triggers, channel, multiplier, thread
# this can be used to show parameters that have changed
monitor = EEGsynth.monitor(name=name, debug=patch.getint('general','debug'))
# get the options from the configuration file
debug = patch.getint('general', 'debug')
channels = patch.getstring('clock', 'channel', multiple=True)
multipliers = patch.getint('clock', 'rate', multiple=True)
lrate = patch.getfloat('clock', 'learning_rate', default=1)
# for keeping track of the number of received triggers
count = 0
triggers = []
for channel in channels:
for multiplier in multipliers:
triggers.append(TriggerThread(channel, multiplier, lrate))
monitor.debug("x%d.%s" % (multiplier, channel))
# start the thread for each of the triggers
for thread in triggers:
thread.start()
# there should not be any local variables in this function, they should all be global
if len(locals()):
print('LOCALS: ' + ', '.join(locals().keys()))
def _loop_once():
'''Run the main loop once
This uses the global variables from setup and start, and adds a set of global variables
'''
global parser, args, config, r, response, patch
global monitor, debug, channels, multipliers, lrate, count, triggers, channel, multiplier, thread
monitor.update("count", count / len(multipliers))
# there should not be any local variables in this function, they should all be global
if len(locals()):
print('LOCALS: ' + ', '.join(locals().keys()))
def _loop_forever():
'''Run the main loop forever
'''
global monitor
while True:
monitor.loop()
_loop_once()
time.sleep(1)
def _stop():
'''Stop and clean up on SystemExit, KeyboardInterrupt
'''
global monitor, triggers, r
monitor.success('Closing threads')
for thread in triggers:
thread.stop()
r.publish('CLOCKMULTIPLIER_UNBLOCK', 1)
for thread in triggers:
thread.join()
sys.exit()
if __name__ == '__main__':
_setup()
_start()
try:
_loop_forever()
except (SystemExit, KeyboardInterrupt, RuntimeError):
_stop()