changed the integration of the c++ classes into the qml code to make things cleaner,

everything seems to be working again
added false start sound
This commit is contained in:
Dorian Zedler 2018-09-17 23:06:34 +02:00
parent cd3e485bf2
commit 24f9dcbc6f
9 changed files with 157 additions and 235 deletions

View file

@ -19,6 +19,12 @@ typedef struct strReturnData{
class BuzzerConn : public QObject class BuzzerConn : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(double lastTriggered READ getLastTriggered NOTIFY triggered)
Q_PROPERTY(QString ipAdress WRITE setIP READ getIP)
Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
Q_PROPERTY(int progress READ getProgress NOTIFY progressChanged)
Q_PROPERTY(double offset READ getOffset NOTIFY offsetChanged)
public: public:
explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80); explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80);
double offset; double offset;
@ -31,6 +37,7 @@ public:
int port; int port;
int errors; int errors;
int errors_until_disconnect = 4; int errors_until_disconnect = 4;
QString state;
@ -42,6 +49,13 @@ private:
QStringList pending_commands; QStringList pending_commands;
//QSemaphore dataPipe(1); //QSemaphore dataPipe(1);
signals: signals:
void triggered();
void stateChanged();
void progressChanged();
void offsetChanged();
public slots: public slots:
ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout); ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout);
@ -73,6 +87,17 @@ public slots:
//refreshed the connection to the buzzer //refreshed the connection to the buzzer
Q_INVOKABLE void appendCommand(QString command); Q_INVOKABLE void appendCommand(QString command);
void setIP(const QString &ipAdress);
QString getIP() const;
QString getState() const;
void setState(QString newState);
int getProgress() const;
double getOffset() const;
double getLastTriggered() const;
}; };

View file

@ -1,96 +0,0 @@
import QtQuick 2.0
Item {
id: buzzerConn
height: 0
width: 0
opacity: 0
visible: false
signal pushed()
property bool connected: false
property var status: {'status': buzzerConn.state, 'progress': get_progress()}
Timer {
//timer that refreshes the connection state to the buzzer
id: connectionRefreshTimer
running: root.state !== "RUNNING" && root.state !== "STARTING" && buzzerConn.status !== "connecting"
repeat: false
interval: 1000
onTriggered: {
_cppBuzzerConn.refresh()
if(!_cppBuzzerConn.get("connected") && buzzerConn.state !== "connecting"){
buzzerConn.state = "disconnected"
}
connectionRefreshTimer.start()
}
}
Timer {
//timer that checks, if the button has been pushed
id: running_refresh_timer
running: root.state === "RUNNING"
repeat: false
interval: 1
onTriggered: {
if(_cppBuzzerConn.buzzer_triggered()){
buzzerConn.pushed()
}
if(root.state === "RUNNING"){
running_refresh_timer.start()
}
}
}
Timer {
id: prog_refresh
running: buzzerConn.state === "connecting"
interval: 1
repeat: true
onTriggered: {
buzzerConn.status.progress = get_progress()
}
}
states: [
State {
name: "disconnected"
PropertyChanges {
target: buzzerConn
}
},
State {
name: "connecting"
PropertyChanges {
target: buzzerConn
}
},
State {
name: "connected"
PropertyChanges {
target: buzzerConn
}
}
]
function connect(){
buzzerConn.state = "connecting"
if(_cppBuzzerConn.connect()){
buzzerConn.state = "connected"
return(true)
}
else {
buzzerConn.state = "disconnected"
return(false)
}
}
function get_progress(){
return(_cppBuzzerConn.get("connection_progress"))
}
}

View file

@ -1,97 +0,0 @@
import QtQuick 2.0
Item {
id: startpadConn
height: 0
width: 0
opacity: 0
visible: false
signal pushed()
property bool connected: false
property var status: {'status': startpadConn.state, 'progress': get_progress()}
property bool active
Timer {
//timer that refreshes the connection state to the buzzer
id: connectionRefreshTimer
running: root.state !== "RUNNING" && root.state !== "STARTING" && startpadConn.status !== "connecting"
repeat: false
interval: 1000
onTriggered: {
_cppStartpadConn.refresh()
if(!_cppStartpadConn.get("connected") && startpadConn.state !== "connecting"){
startpadConn.state = "disconnected"
}
connectionRefreshTimer.start()
}
}
Timer {
//timer that checks, if the button has been pushed
id: running_refresh_timer
running: (root.state === "RUNNING" || root.state === "STARTING") && active
repeat: false
interval: 1
onTriggered: {
if(_cppStartpadConn.buzzer_triggered()){
startpadConn.pushed()
}
if(root.state === "RUNNING" || root.state === "STARTING" && active){
running_refresh_timer.start()
}
}
}
Timer {
id: prog_refresh
running: startpadConn.state === "connecting"
interval: 1
repeat: true
onTriggered: {
startpadConn.status.progress = get_progress()
}
}
states: [
State {
name: "disconnected"
PropertyChanges {
target: startpadConn
}
},
State {
name: "connecting"
PropertyChanges {
target: startpadConn
}
},
State {
name: "connected"
PropertyChanges {
target: startpadConn
}
}
]
function connect(){
startpadConn.state = "connecting"
if(_cppStartpadConn.connect()){
startpadConn.state = "connected"
return(true)
}
else {
startpadConn.state = "disconnected"
return(false)
}
}
function get_progress(){
return(_cppStartpadConn.get("connection_progress"))
}
}

View file

@ -21,7 +21,6 @@ import QtQuick.Window 2.2
import QtQuick.Controls 2.2 import QtQuick.Controls 2.2
import "." import "."
import "./components" import "./components"
import "./connections"
import "./styles" import "./styles"
//import QtQuick.Layouts 1.11 //import QtQuick.Layouts 1.11
@ -47,8 +46,8 @@ Window {
property double stoppedTime: 0 property double stoppedTime: 0
property double currTime property double currTime
property double buzzer_offset property double buzzer_offset: buzzerConn.offset
property double last_button_pressed property double last_button_pressed: buzzerConn.lastTriggered
property var last_run : { property var last_run : {
'stop_type': "", 'time': 0, 'react_time': 0 'stop_type': "", 'time': 0, 'react_time': 0
@ -69,6 +68,62 @@ Window {
color: StyleSettings.backgroundColor color: StyleSettings.backgroundColor
} }
BuzzerConn {
id: buzzerConn
ipAdress: "192.168.4.10"
property var status: {'status': buzzerConn.state, 'progress': buzzerConn.progress}
onLastTriggeredChanged: {
console.log(lastTriggered)
root.stop("buzzer")
}
}
Timer {
id: buzzerRefreshTimer
running: buzzerConn.state === "connected"
interval: root.state === "RUNNING" ? 1:1000
repeat: false
onTriggered: {
buzzerConn.refresh()
this.start()
}
}
StartpadConn {
id: startpadConn
ipAdress: "192.168.4.11"
property var status: {'status': startpadConn.state, 'progress': startpadConn.progress}
property string color: root.state === "RUNNING" ? "SET_LED_RUNNING":"SET_LED_STARTING"
onColorChanged: {
appendCommand(color)
}
onLastTriggeredChanged: {
console.log("startpad triggered")
var offset = startpadConn.offset
var last_pressed = startpadConn.lastTriggered
var trigger_time = (last_pressed + offset)
root.last_run.react_time = trigger_time - root.startTime
if(trigger_time - root.startTime <= 0){
root.stop("false")
}
}
}
Timer {
id: startpadRefreshTimer
running: startpadConn.state === "connected"
interval: root.state === "RUNNING" || root.state === "STARTING" ? 1:1000
repeat: false
onTriggered: {
startpadConn.refresh()
this.start()
}
}
/*
BuzzerConn { BuzzerConn {
id: buzzerConn id: buzzerConn
onPushed: { onPushed: {
@ -96,7 +151,7 @@ Window {
} }
} }
} }
*/
Timer { Timer {
//timer that updates the currTime variable //timer that updates the currTime variable
running: true running: true
@ -183,20 +238,24 @@ Window {
if(!playing && root.state==="STARTING"){ if(!playing && root.state==="STARTING"){
console.log(root.startTime) console.log(root.startTime)
_cppStartpadConn.appendCommand("SET_LED_RUNNING")
root.currTime = root.startTime root.currTime = root.startTime
time.text = ( ( root.currTime - root.startTime ) / 1000 ).toFixed(3) + " sec" time.text = ( ( root.currTime - root.startTime ) / 1000 ).toFixed(3) + " sec"
root.state = "RUNNING" root.state = "RUNNING"
} }
else if(playing) { else if(playing) {
console.log("start sound started") console.log("start sound started")
root.startTime = _cppBuzzerConn.get("currtime") + 3100 //set the startime to be 0 after the starttone root.startTime = new Date().getTime() + 3100 //set the startime to be 0 after the starttone
startpadConn.active = true
} }
} }
} }
SoundEffect {
//start sound
id: falseSound
source: "qrc:/sounds/false.wav"
}
/*------------------------ /*------------------------
Timer text an upper line Timer text an upper line
------------------------*/ ------------------------*/
@ -687,16 +746,16 @@ Window {
} }
function stop(type){ function stop(type){
_cppStartpadConn.appendCommand("SET_LED_STARTING"); //_cppStartpadConn.appendCommand("SET_LED_STARTING");
switch(type){ switch(type){
case "buzzer": case "buzzer":
//the buzzer was pushed //the buzzer was pushed
root.buzzer_offset = _cppBuzzerConn.get("offset") root.buzzer_offset = buzzerConn.offset
root.last_button_pressed = _cppBuzzerConn.get("lastpressed") root.last_button_pressed = buzzerConn.lastTriggered
root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime
root.state = "STOPPED" root.state = "STOPPED"
//time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec" //time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec"
console.log("STOPPED: "+root.stoppedTime) console.log("STOPPED: "+root.stoppedTime + " started at: " + root.startTime + " offset: "+ root.buzzer_offset + "lastpressed: " + root.last_button_pressed)
break break
case "manual": case "manual":
//the stop button was pressed //the stop button was pressed
@ -713,9 +772,10 @@ Window {
at_marksSound.stop() at_marksSound.stop()
readySound.stop() readySound.stop()
startSound.stop() startSound.stop()
falseSound.play()
break break
} }
startpadConn.active = true //tartpadConn.active = true
} }
function reset(){ function reset(){

View file

@ -7,8 +7,6 @@
<file>components/SimpleIndicator.qml</file> <file>components/SimpleIndicator.qml</file>
<file>components/ConnectionDelegate.qml</file> <file>components/ConnectionDelegate.qml</file>
<file>components/FadeAnimation.qml</file> <file>components/FadeAnimation.qml</file>
<file>connections/BuzzerConn.qml</file>
<file>connections/StartpadConn.qml</file>
<file>styles/StyleSettings.qml</file> <file>styles/StyleSettings.qml</file>
<file>styles/qmldir</file> <file>styles/qmldir</file>
<file>styles/Dark.js</file> <file>styles/Dark.js</file>

View file

@ -20,5 +20,6 @@
<file>translations/de_DE.ts</file> <file>translations/de_DE.ts</file>
<file>graphics/icons/settings_black.png</file> <file>graphics/icons/settings_black.png</file>
<file>graphics/icons/startpad_black.png</file> <file>graphics/icons/startpad_black.png</file>
<file>sounds/false.wav</file>
</qresource> </qresource>
</RCC> </RCC>

BIN
sounds/false.wav Normal file

Binary file not shown.

View file

@ -30,6 +30,8 @@ BuzzerConn::BuzzerConn(QObject *parent, QString ip, int port) : QObject(parent)
this->ip = ip; this->ip = ip;
this->port = port; this->port = port;
this->setState("disconnected");
// "http://192.168.4.1" // "http://192.168.4.1"
} }
@ -37,6 +39,8 @@ bool BuzzerConn::connect()
{ {
qDebug() << "connecting..."; qDebug() << "connecting...";
setState("connecting");
//wait until the request has finished //wait until the request has finished
QEventLoop loop; QEventLoop loop;
QTimer timer; QTimer timer;
@ -62,17 +66,21 @@ bool BuzzerConn::connect()
this->latest_button_pressed = times[2]; this->latest_button_pressed = times[2];
for(int i=0;i<=100;i++){ for(int i=0;i<=100;i++){
this->connection_progress = i; this->connection_progress = i;
emit this->progressChanged();
if(!calcoffset(this->gettimes(1000))){ if(!calcoffset(this->gettimes(1000))){
this->connection_progress = 100; this->connection_progress = 100;
this->connected = false; this->connected = false;
setState("disconnected");
return(false); return(false);
} }
} }
this->connected = true; this->connected = true;
setState("connected");
return(true); return(true);
} }
else{ else{
this->connected = false; this->connected = false;
setState("disconnected");
return(false); return(false);
} }
} }
@ -95,7 +103,8 @@ bool BuzzerConn::calcoffset(QList<double> times)
mem += latest_offsets[i]; mem += latest_offsets[i];
} }
this->offset = mem / double(latest_offsets.length()); this->offset = mem / double(latest_offsets.length());
qDebug("%20f", this->offset); offsetChanged();
//qDebug("%20f", this->offset);
return(true); return(true);
} }
else { else {
@ -215,6 +224,41 @@ double BuzzerConn::get(QString key)
} }
return(0); return(0);
} }
return(0);
}
void BuzzerConn::setIP(const QString &ipAdress){
this->ip = ipAdress;
}
QString BuzzerConn::getIP() const
{
return(this->ip);
}
QString BuzzerConn::getState() const
{
return(this->state);
}
void BuzzerConn::setState(QString newState){
this->state = newState;
emit stateChanged();
}
int BuzzerConn::getProgress() const
{
return(connection_progress);
}
double BuzzerConn::getOffset() const
{
return(this->offset);
}
double BuzzerConn::getLastTriggered() const
{
return(this->latest_button_pressed);
} }
QString BuzzerConn::test() QString BuzzerConn::test()
@ -228,36 +272,23 @@ bool BuzzerConn::refresh()
if(!this->connected){ if(!this->connected){
return(false); return(false);
} }
// QList<double> times;
// ReturnData_t ret = senddata(this->reloadNetworkManager, QUrl(this->ip), 1000);
// times.append(double(ret.status_code));
// if(ret.status_code == 200){
// ret.text.replace("\n","");
// ret.text.replace("\r","");
// QStringList times_cache = ret.text.split("<br>");
// times.append(times_cache[0].toDouble());
// times.append(times_cache[1].toDouble());
// calcoffset(times);
// return(true);
// }
// else{
// //this->connected = false;
// return(false);
// }
if(pending_commands.length() > 0){ if(pending_commands.length() > 0){
QString command = this->pending_commands.first(); QString command = this->pending_commands.first();
signed long retval = this->sendCommand(command, 800); signed long retval = this->sendCommand(command, 2000);
if(retval > 0){ if(retval > 0){
this->pending_commands.removeFirst(); this->pending_commands.removeFirst();
} }
} }
//refresh the times //refresh the times
QList<double> ret = this->gettimes(800); QList<double> ret = this->gettimes(2000);
if(ret[0] >= 0){ if(ret[0] >= 0){
if(ret[2] > this->latest_button_pressed){
this->latest_button_pressed = ret[2];
emit triggered();
}
this->errors = 0; this->errors = 0;
return(this->calcoffset(ret)); return(this->calcoffset(ret));
} }
@ -356,8 +387,8 @@ signed long BuzzerConn::sendCommand(QString command, int timeout){
long data = 0; long data = 0;
this->socket->read((char*)&data,4); this->socket->read((char*)&data,4);
qDebug() << data; //qDebug() << data;
qDebug() << this->socket->bytesAvailable(); //qDebug() << this->socket->bytesAvailable();
return data; return data;
} }

View file

@ -105,14 +105,18 @@ int main(int argc, char *argv[])
#endif #endif
connectToDatabase(); connectToDatabase();
BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, "192.168.4.10", 80); //BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, "192.168.4.10", 80);
BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, "192.168.4.11", 80); //BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, "192.168.4.11", 80);
AppSettings * pAppSettings = new AppSettings(); AppSettings * pAppSettings = new AppSettings();
//setup the sql storage model as a qml model //setup the sql storage model as a qml model
qmlRegisterType<SqlProfileModel>("com.itsblue.speedclimbingstopwatch", 1, 0, "SqlProfileModel"); qmlRegisterType<SqlProfileModel>("com.itsblue.speedclimbingstopwatch", 1, 0, "SqlProfileModel");
qmlRegisterType<SqlStorageModel>("com.itsblue.speedclimbingstopwatch", 1, 0, "SqlStorageModel"); qmlRegisterType<SqlStorageModel>("com.itsblue.speedclimbingstopwatch", 1, 0, "SqlStorageModel");
//setup the startpad and buzzer conn qml objects
qmlRegisterType<BuzzerConn>("com.itsblue.speedclimbingstopwatch", 1, 0, "BuzzerConn");
qmlRegisterType<BuzzerConn>("com.itsblue.speedclimbingstopwatch", 1, 0, "StartpadConn");
//setup translation engine //setup translation engine
//to the language of the system //to the language of the system
//if the system language is not found the language is set to english //if the system language is not found the language is set to english
@ -125,16 +129,12 @@ int main(int argc, char *argv[])
if (engine.rootObjects().isEmpty()) if (engine.rootObjects().isEmpty())
return -1; return -1;
engine.rootContext()->setContextProperty("_cppBuzzerConn", pBuzzerConn);
engine.rootContext()->setContextProperty("_cppStartpadConn", pStartpadConn);
engine.rootContext()->setContextProperty("_cppAppSettings", pAppSettings); engine.rootContext()->setContextProperty("_cppAppSettings", pAppSettings);
int iRet = 0; int iRet = 0;
iRet = app.exec(); iRet = app.exec();
//call the destructors of all objects //call the destructors of all objects
delete pBuzzerConn;
delete pStartpadConn;
delete pAppSettings; delete pAppSettings;
return iRet; return iRet;