Threaded, full featured

git-svn-id: http://moon:8086/svn/projects/HendiControl@199 fda53097-d464-4ada-af97-ba876c37ca34
This commit is contained in:
2019-03-31 07:47:48 +00:00
parent e6930b7920
commit 1c065bd397
+41 -3
View File
@@ -1,8 +1,11 @@
import time import time
import numpy as np import numpy as np
from matplotlib.pyplot import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show, subplot from matplotlib.pyplot import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show, subplot
import signal
import threading
from tempSensorSim import TempSensorSim from tempSensorSim import TempSensorSim
#from max31865 import Max31865
class TempLogger(): class TempLogger():
def __init__(self, sensor): def __init__(self, sensor):
@@ -11,7 +14,24 @@ class TempLogger():
self.times = np.empty(0) self.times = np.empty(0)
self.count = 0 self.count = 0
def run(self, dt, duration): self.abort = False
self.thread = None
self.sema = threading.Semaphore(0)
def start(self, dt, duration):
if self.thread == None:
self.thread = threading.Thread(target=self.run, args=(dt, duration,))
self.thread.start()
def stop(self):
if self.thread:
self.abort = True
self.thread.join()
self.thread = None
def run(self, *args):
dt = args[0]
duration = args[1]
for t in range(0, duration): for t in range(0, duration):
temp = self.sensor.temperature() temp = self.sensor.temperature()
@@ -20,17 +40,35 @@ class TempLogger():
self.temps = np.append(self.temps, temp) self.temps = np.append(self.temps, temp)
self.count += 1 self.count += 1
time.sleep(dt) time.sleep(dt)
if self.abort:
break
return self.result() self.sema.release()
def wait(self):
if self.thread:
self.sema.acquire()
pass
def result(self): def result(self):
return self.times, self.temps return self.times, self.temps
if __name__ == '__main__': if __name__ == '__main__':
def handler(signum, frame):
print('Signal handler called with signal', signum)
if signum == signal.SIGINT:
logger.stop()
signal.signal(signal.SIGINT, handler)
# sensor = Max31865()
sensor = TempSensorSim("FakeTemp") sensor = TempSensorSim("FakeTemp")
logger = TempLogger(sensor) logger = TempLogger(sensor)
result = logger.run(1, 10) logger.start(1.0, 10)
logger.wait()
result = logger.result()
figure(1) figure(1)
plot(result[0]/60, result[1], 'b-', linewidth=1) plot(result[0]/60, result[1], 'b-', linewidth=1)