sopping the time with the buzzer and offset calculation is now fully working

This commit is contained in:
Dorian Zedler 2018-07-26 20:51:46 +02:00
parent fb8e80281b
commit 0a4cabc61e
7 changed files with 250 additions and 29 deletions

96
ProgressCircle.qml Normal file
View file

@ -0,0 +1,96 @@
import QtQuick 2.0
import QtQml 2.2
Item {
id: root
width: size
height: size
property int size: 200 // The size of the circle in pixel
property real arcBegin: 0 // start arc angle in degree
property real arcEnd: 270 // end arc angle in degree
property real arcOffset: 0 // rotation
property bool isPie: false // paint a pie instead of an arc
property bool showBackground: false // a full circle as a background of the arc
property real lineWidth: 20 // width of the line
property string colorCircle: "#CC3333"
property string colorBackground: "#779933"
property alias beginAnimation: animationArcBegin.enabled
property alias endAnimation: animationArcEnd.enabled
property int animationDuration: 20
onArcBeginChanged: canvas.requestPaint()
onArcEndChanged: canvas.requestPaint()
Behavior on arcBegin {
id: animationArcBegin
enabled: true
NumberAnimation {
duration: root.animationDuration
easing.type: Easing.InOutCubic
}
}
Behavior on arcEnd {
id: animationArcEnd
enabled: true
NumberAnimation {
duration: root.animationDuration
easing.type: Easing.InOutCubic
}
}
Behavior on opacity {
NumberAnimation {
duration: 100
}
}
Canvas {
id: canvas
anchors.fill: parent
rotation: -90 + parent.arcOffset
onPaint: {
var ctx = getContext("2d")
var x = width / 2
var y = height / 2
var start = Math.PI * (parent.arcBegin / 180)
var end = Math.PI * (parent.arcEnd / 180)
ctx.reset()
if (root.isPie) {
if (root.showBackground) {
ctx.beginPath()
ctx.fillStyle = root.colorBackground
ctx.moveTo(x, y)
ctx.arc(x, y, width / 2, 0, Math.PI * 2, false)
ctx.lineTo(x, y)
ctx.fill()
}
ctx.beginPath()
ctx.fillStyle = root.colorCircle
ctx.moveTo(x, y)
ctx.arc(x, y, width / 2, start, end, false)
ctx.lineTo(x, y)
ctx.fill()
} else {
if (root.showBackground) {
ctx.beginPath();
ctx.arc(x, y, (width / 2) - parent.lineWidth / 2, 0, Math.PI * 2, false)
ctx.lineWidth = root.lineWidth
ctx.strokeStyle = root.colorBackground
ctx.stroke()
}
ctx.beginPath();
ctx.arc(x, y, (width / 2) - parent.lineWidth / 2, start, end, false)
ctx.lineWidth = root.lineWidth
ctx.strokeStyle = root.colorCircle
ctx.stroke()
}
}
}
}

View file

@ -3,8 +3,8 @@ import QtMultimedia 5.8
import QtQuick.Window 2.2 import QtQuick.Window 2.2
import QtQuick.Controls 2.2 import QtQuick.Controls 2.2
Dialog { Popup {
id: root
x: startButt.x x: startButt.x
y: startButt.y y: startButt.y
width: startButt.width width: startButt.width
@ -20,6 +20,13 @@ Dialog {
NumberAnimation { properties: "scale"; from: 1; to: 0; duration: 500; easing.type: Easing.OutCubic } NumberAnimation { properties: "scale"; from: 1; to: 0; duration: 500; easing.type: Easing.OutCubic }
} }
function delay(delayTime, cb) {
timer = new Timer();
timer.interval = delayTime;
timer.repeat = false;
//timer.triggered.connect(cb);
timer.start();
}
background: Rectangle { background: Rectangle {
radius: width * 0.5 radius: width * 0.5
@ -51,20 +58,91 @@ Dialog {
} }
} }
} }
ProgressCircle {
id: prog
property string text: "connecting.."
anchors.centerIn: parent
size: parent.height * 1.03
//colorCircle: "grey"
opacity: 0
lineWidth: 5
arcBegin: 0
arcEnd: 0
Timer {
running: opacity === 1
interval: 1
repeat: true
onTriggered: {
prog.arcEnd = 360 * ( _cppBuzzerConn.get("connection_progress") / 100 )
}
}
Label {
id: content
text: parent.text
anchors.centerIn: parent
font.pixelSize: parent.width * 0.1
}
}
Column { Column {
anchors.fill: parent id: settings_col
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: parent.right
ItemDelegate { ItemDelegate {
id: connect_del id: connect_del
anchors.fill: parent width: parent.width
text: "connect" text: _cppBuzzerConn.get("connected")===1 ? "connected to buzzer":"connect to buzzer"
onClicked: { Timer {
// connect_del.enabled = false running: connect_del.scale === 1
// connect_del.text = _cppBuzzerConn.get("offset") repeat: true
// _cppBuzzerConn.connect() interval: 10
// connect_del.enabled = true onTriggered: {
connect_del.text = _cppBuzzerConn.test() connect_del.text = _cppBuzzerConn.get("connected")===1 ? "connected to buzzer":"connect to buzzer"
}
}
onClicked: {
root.modal = true
root.closePolicy = Popup.NoAutoClose
prog.colorCircle = "grey"
prog.opacity = 1
prog.text = "connecting..."
connect_del.enabled = false
if(_cppBuzzerConn.connect()){
prog.arcEnd = 360
prog.colorCircle = "green"
prog.text = "success!"
}
else {
prog.arcEnd = 360
prog.colorCircle = "red"
prog.colorBackground = "red"
prog.text = "error!"
}
//make a short delay and go back to normal options
shortDelay.start()
}
Timer {
id: shortDelay
running: false
repeat: false
interval: 1000
onTriggered: {
connect_del.enabled = true;
prog.opacity = 0;
root.modal = false;
root.closePolicy = Popup.CloseOnPressOutside;
}
} }
} }
} }

View file

@ -85,4 +85,6 @@
<uses-feature android:name="android.hardware.microphone" android:required="false"/> <uses-feature android:name="android.hardware.microphone" android:required="false"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest> </manifest>

View file

@ -3,6 +3,7 @@
BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent) BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent)
{ {
this->networkManager = new QNetworkAccessManager(); this->networkManager = new QNetworkAccessManager();
this->date = new QDateTime; this->date = new QDateTime;
this->latest_button_pressed = 0; this->latest_button_pressed = 0;
this->connected = false; this->connected = false;
@ -10,14 +11,19 @@ BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent)
bool BuzzerConn::connect() bool BuzzerConn::connect()
{ {
qDebug() << "connecting to buzzer...";
QList<double> times = gettimes(); QList<double> times = gettimes();
qDebug() << times[0];
if(times[0] == 200.0){ if(times[0] == 200.0){
this->connected = true; this->connected = true;
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++){
calcoffset(); this->connection_progress = i;
if(!calcoffset()){
return(false);
} }
}
return(true); return(true);
} }
else{ else{
@ -25,11 +31,12 @@ bool BuzzerConn::connect()
} }
} }
void BuzzerConn::calcoffset() bool BuzzerConn::calcoffset()
{ {
QList<double> times = gettimes(); QList<double> times = gettimes();
if(times[0] == 200.0){ qDebug() << times[0];
double offset = date->currentMSecsSinceEpoch() - times[2]; if(times[0] == 200.0 && this->connected){
double offset = date->currentMSecsSinceEpoch() - times[1];
if(this->latest_offsets.length()>=100){ if(this->latest_offsets.length()>=100){
this->latest_offsets.removeFirst(); this->latest_offsets.removeFirst();
} }
@ -41,10 +48,16 @@ void BuzzerConn::calcoffset()
} }
//this->offset = mem / double(latest_offsets.length()); //this->offset = mem / double(latest_offsets.length());
this->offset = latest_offsets[latest_offsets.length()-1]; this->offset = latest_offsets[latest_offsets.length()-1];
qDebug() << latest_offsets.length(); // qDebug() << latest_offsets.length();
qDebug() << this->latest_offsets; // qDebug() << this->latest_offsets;
qDebug() << latest_offsets[latest_offsets.length()-1]; // qDebug() << latest_offsets[latest_offsets.length()-1];
qDebug() << mem / double(latest_offsets.length()); // qDebug() << mem / double(latest_offsets.length());
qDebug("%20f", this->offset);
return(true);
}
else {
this->connected = false;
return(false);
} }
} }
@ -70,9 +83,12 @@ QList<double> BuzzerConn::gettimes()
bool BuzzerConn::buzzer_triggered() bool BuzzerConn::buzzer_triggered()
{ {
if(!this->connected){
return(false);
}
QList<double> times = this->gettimes(); QList<double> times = this->gettimes();
qDebug() << int(this->get("currtime") - this->starttime); if(times[0] == 200.0){
if(times[0] == 200.0 && this->connected){
if(times[2] > this->latest_button_pressed){ if(times[2] > this->latest_button_pressed){
this->latest_button_pressed = times[2]; this->latest_button_pressed = times[2];
@ -83,6 +99,7 @@ bool BuzzerConn::buzzer_triggered()
} }
} }
else{ else{
this->connected = false;
return(false); return(false);
} }
} }
@ -98,6 +115,7 @@ bool BuzzerConn::start()
return(true); return(true);
} }
else{ else{
this->connected = false;
return(false); return(false);
} }
} }
@ -113,6 +131,15 @@ double BuzzerConn::get(QString key)
else if( key == "currtime") { else if( key == "currtime") {
return(this->date->currentMSecsSinceEpoch()); return(this->date->currentMSecsSinceEpoch());
} }
else if( key == "connection_progress") {
return(this->connection_progress);
}
else if( key == "connected") {
if(this->connected){
return(1);
}
return(0);
}
} }
QString BuzzerConn::test() QString BuzzerConn::test()
@ -135,18 +162,24 @@ ReturnData_t BuzzerConn::senddata(QUrl serviceUrl)
QUrlQuery pdata; QUrlQuery pdata;
QNetworkReply* reply; QNetworkReply* reply;
reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
//wait until the request has finished //wait until the request has finished
QEventLoop loop; QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
timer.start(500);
reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
loop.exec(); loop.exec();
timer.stop();
//get the status code //get the status code
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
ret.status_code = status_code.toInt(); ret.status_code = status_code.toInt();
if(ret.status_code == 0){ //if tehstatus code is zero, the connecion to the server was not possible if(ret.status_code == 0){ //if the statuscode is zero, the connecion to the server was not possible
ret.status_code = 444; ret.status_code = 444;
} }
//get the full text response //get the full text response

View file

@ -20,11 +20,12 @@ class BuzzerConn : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit BuzzerConn(QObject *parent = nullptr); explicit BuzzerConn(QObject *parent = nullptr);
Q_INVOKABLE double offset; double offset;
QList<double> latest_offsets; QList<double> latest_offsets;
double latest_button_pressed; double latest_button_pressed;
double starttime; double starttime;
bool connected; bool connected;
int connection_progress;
private: private:
@ -37,7 +38,7 @@ public slots:
ReturnData_t senddata(QUrl serviceUrl); ReturnData_t senddata(QUrl serviceUrl);
Q_INVOKABLE QList<double> gettimes(); Q_INVOKABLE QList<double> gettimes();
Q_INVOKABLE bool connect(); Q_INVOKABLE bool connect();
Q_INVOKABLE void calcoffset(); Q_INVOKABLE bool calcoffset();
Q_INVOKABLE bool buzzer_triggered(); Q_INVOKABLE bool buzzer_triggered();
Q_INVOKABLE bool start(); Q_INVOKABLE bool start();
Q_INVOKABLE double get(QString key); Q_INVOKABLE double get(QString key);

View file

@ -16,6 +16,7 @@ Window {
Page { Page {
id:root id:root
anchors.fill: parent anchors.fill: parent
@ -40,6 +41,16 @@ Window {
} }
} }
Timer {
//timer that refreshes the connection state to the buzzer
running: false
repeat: true
interval: 500
onTriggered: {
console.log(_cppBuzzerConn.calcoffset())
}
}
Timer { Timer {
id: running_refresh_timer id: running_refresh_timer
running: root.state === "RUNNING" running: root.state === "RUNNING"
@ -56,8 +67,7 @@ Window {
root.last_button_pressed = _cppBuzzerConn.get("lastpressed") root.last_button_pressed = _cppBuzzerConn.get("lastpressed")
console.log(root.startTime); console.log(root.startTime);
console.log(root.buzzer_offset); console.log(root.buzzer_offset);
//root.stoppedTime = ( ( (root.last_button_pressed + root.buzzer_offset) - root.startTime ) / 2 ) / 10 root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime
root.stoppedTime = new Date().getTime() - root.startTime
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)
root.state = "STOPPED" root.state = "STOPPED"

View file

@ -3,5 +3,6 @@
<file>main.qml</file> <file>main.qml</file>
<file>FadeAnimation.qml</file> <file>FadeAnimation.qml</file>
<file>SettingsDialog.qml</file> <file>SettingsDialog.qml</file>
<file>ProgressCircle.qml</file>
</qresource> </qresource>
</RCC> </RCC>