Commit ad4b0e6d authored by Ed Rogers's avatar Ed Rogers
Browse files

Add Escape to abort

parent 748c6cda
...@@ -22,7 +22,7 @@ Display agregated scores in second window ...@@ -22,7 +22,7 @@ Display agregated scores in second window
import sounddevice as sd import sounddevice as sd
import numpy as np import numpy as np
import time import time
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui, Qt
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QTextEdit from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QTextEdit
from PyQt5.QtCore import QSize from PyQt5.QtCore import QSize
import sys import sys
...@@ -84,16 +84,21 @@ class HearingMplCanvas(FigureCanvas): ...@@ -84,16 +84,21 @@ class HearingMplCanvas(FigureCanvas):
class HearingTestThread(QtCore.QThread): class HearingTestThread(QtCore.QThread):
played_sound = QtCore.pyqtSignal(float, float, float) played_sound = QtCore.pyqtSignal(float, float, float)
aborted = QtCore.pyqtSignal()
def __init__(self, test: HearingTest): def __init__(self, test: HearingTest):
QtCore.QThread.__init__(self) QtCore.QThread.__init__(self)
self.test = test self.test = test
self.abort = False
def __del__(self): def __del__(self):
self.wait() self.wait()
def run(self): def run(self):
for _ in range(5): for _ in range(5):
if self.abort:
self.aborted.emit()
break
# TODO convert to finished at end of test # TODO convert to finished at end of test
finished, freq, volume, played_time, next_sleep_time = self.test.play_next_sound() finished, freq, volume, played_time, next_sleep_time = self.test.play_next_sound()
self.played_sound.emit(freq, volume, played_time) self.played_sound.emit(freq, volume, played_time)
...@@ -135,9 +140,14 @@ class TestWindow(QMainWindow): ...@@ -135,9 +140,14 @@ class TestWindow(QMainWindow):
def keyPressEvent(self, event: QtGui.QKeyEvent): def keyPressEvent(self, event: QtGui.QKeyEvent):
if not self.test_running: if not self.test_running:
self.test_running = True if event.key() == Qt.Qt.Key_Escape:
self.run_test() self.close()
else:
self.test_running = True
self.run_test()
else: else:
if event.key() == Qt.Qt.Key_Escape:
self.testing_thread.abort = True
key_press_time = time.time() key_press_time = time.time()
self.result.handle_key_press(key_press_time) self.result.handle_key_press(key_press_time)
self.record_key_press(event, key_press_time) self.record_key_press(event, key_press_time)
...@@ -153,12 +163,16 @@ class TestWindow(QMainWindow): ...@@ -153,12 +163,16 @@ class TestWindow(QMainWindow):
self.testing_thread = HearingTestThread(self.result) self.testing_thread = HearingTestThread(self.result)
self.testing_thread.played_sound.connect(self.sound_played) self.testing_thread.played_sound.connect(self.sound_played)
self.testing_thread.finished.connect(self.test_finished) self.testing_thread.finished.connect(self.test_finished)
self.testing_thread.aborted.connect(self.aborted)
self.testing_thread.start() self.testing_thread.start()
# need to prevent another thread starting # need to prevent another thread starting
def sound_played(self, freq, volume, played_time): def sound_played(self, freq, volume, played_time):
self.log.append('Played freq {}, at volume {} and time {}'.format(freq, volume, played_time)) self.log.append('Played freq {}, at volume {} and time {}'.format(freq, volume, played_time))
def aborted(self):
self.log.append('Test aborted')
def test_finished(self): def test_finished(self):
self.test_running = False self.test_running = False
self.title.setText("Press any key to start...") self.title.setText("Press any key to start...")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment