import QtQuick 2.9 import QtMultimedia 5.8 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 import "." import "./components" import "./styles" Item { id: control property color color property string text: qsTr("Click start to start") property string elide property int pixelSize: 100 property int scale: 1 property var toppadConn property var startpadConn property var baseConn property double startTime: 0 property double stopTime: 0 property double stoppedTime: 0 property double reactionTime: 0 property double currTime: 0 signal stopped() signal startCanceled(bool falseStart) anchors.fill: parent state: "IDLE" Label { id: time text: parent.text scale: parent.scale anchors.centerIn: parent font.pixelSize: parent.pixelSize elide: parent.elide color: StyleSettings.textColor Behavior on text { enabled: control.state !== "RUNNING" FadeAnimation { target: time } } } function setStarting(){ control.state = "STARTING" } function start(inMilliSeconds){ control.state = "STARTING" control.startTime = new Date().getTime() + inMilliSeconds //set the startime to be 0 after the starttone startTimer.interval = inMilliSeconds startTimer.start() } function stop(type){ //_cppStartpadConn.appendCommand("SET_LED_STARTING"); switch(type){ case "toppad": //the buzzer was pushed control.stopTime = control.toppadConn.lastTriggered + control.toppadConn.offset control.stoppedTime = control.stopTime - control.startTime control.stopped() //time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec" //console.log("STOPPED: "+control.stoppedTime + " started at: " + control.startTime + " offset: "+ control.buzzer_offset + "lastpressed: " + control.last_button_pressed) break case "manual": //the stop button was pressed if(baseConn.state === "connected"){ control.stoppedTime = baseConn.getTime("raw") time.text = (control.stoppedTime / 1000).toFixed(3) + " sec" return } control.stopTime = new Date().getTime() control.stoppedTime = control.stopTime - control.startTime control.stopped() break case "false": //there was a false start control.stoppedTime = -1 startTimer.stop() control.state = "STOPPED" control.startCanceled(true) break case "cancel": //the cancel button was pressed control.stoppedTime = 0 startTimer.stop() control.state = "STOPPED" control.startCanceled(false) break } control.state = "STOPPED" } function reset(){ control.startTime = 0 control.stopTime = 0 control.stoppedTime = 0 if(baseConn.state === "connected"){ var ret = baseConn.sendCommand("CMD_RESET_TIMER") if(ret !== "OK"){ control.state = "IDLE" return } } control.state = "IDLE" } function handleStartpad(){ console.log("startpad triggered") var offset = control.startpadConn.offset var last_pressed = control.startpadConn.lastTriggered var trigger_time = (last_pressed + offset) control.reactionTime = trigger_time - control.startTime if(trigger_time - control.startTime <= 0){ stop("false") } } function handleToppad(){ console.log(lastTriggered) stop("toppad") } Timer { id: startTimer running: false repeat: false onTriggered: { console.log("started") control.state = "RUNNING" } } Timer { //timer that updates the currTime variable running: true repeat: true interval: 1 onTriggered: { control.currTime = new Date().getTime() } } states: [ State { name: "IDLE" //state for the start page PropertyChanges { target: time; text: qsTr("Click start to start");} }, State { name: "STARTING" //state for the start sequence PropertyChanges { target: time; text: control.text;} }, State { name: "RUNNING" //state when the timer is running PropertyChanges { target: time; text: Math.abs( ( ( control.currTime - control.startTime ) / 1000 ) ).toFixed(3) + " sec";} }, State { name: "STOPPED" //state when the meassuring is over PropertyChanges { target: time; text: control.stoppedTime >= 0 ? ( control.stoppedTime / 1000 ).toFixed(3) + " sec":qsTr("false start"); } } ] }