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
import sounddevice as sd
import numpy as np
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.QtCore import QSize
import sys
......@@ -84,16 +84,21 @@ class HearingMplCanvas(FigureCanvas):
class HearingTestThread(QtCore.QThread):
played_sound = QtCore.pyqtSignal(float, float, float)
aborted = QtCore.pyqtSignal()
def __init__(self, test: HearingTest):
QtCore.QThread.__init__(self)
self.test = test
self.abort = False
def __del__(self):
self.wait()
def run(self):
for _ in range(5):
if self.abort:
self.aborted.emit()
break
# TODO convert to finished at end of test
finished, freq, volume, played_time, next_sleep_time = self.test.play_next_sound()
self.played_sound.emit(freq, volume, played_time)
......@@ -135,9 +140,14 @@ class TestWindow(QMainWindow):
def keyPressEvent(self, event: QtGui.QKeyEvent):
if not self.test_running:
if event.key() == Qt.Qt.Key_Escape:
self.close()
else:
self.test_running = True
self.run_test()
else:
if event.key() == Qt.Qt.Key_Escape:
self.testing_thread.abort = True
key_press_time = time.time()
self.result.handle_key_press(key_press_time)
self.record_key_press(event, key_press_time)
......@@ -153,12 +163,16 @@ class TestWindow(QMainWindow):
self.testing_thread = HearingTestThread(self.result)
self.testing_thread.played_sound.connect(self.sound_played)
self.testing_thread.finished.connect(self.test_finished)
self.testing_thread.aborted.connect(self.aborted)
self.testing_thread.start()
# need to prevent another thread starting
def sound_played(self, 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):
self.test_running = False
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