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.Controls 2.2
Dialog {
Popup {
id: root
x: startButt.x
y: startButt.y
width: startButt.width
@ -20,6 +20,13 @@ Dialog {
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 {
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 {
anchors.fill: parent
id: settings_col
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: parent.right
ItemDelegate {
id: connect_del
anchors.fill: parent
text: "connect"
onClicked: {
// connect_del.enabled = false
// connect_del.text = _cppBuzzerConn.get("offset")
// _cppBuzzerConn.connect()
// connect_del.enabled = true
connect_del.text = _cppBuzzerConn.test()
width: parent.width
text: _cppBuzzerConn.get("connected")===1 ? "connected to buzzer":"connect to buzzer"
Timer {
running: connect_del.scale === 1
repeat: true
interval: 10
onTriggered: {
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:glEsVersion="0x00020000" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

View file

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

View file

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

View file

@ -16,6 +16,7 @@ Window {
Page {
id:root
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 {
id: running_refresh_timer
running: root.state === "RUNNING"
@ -56,8 +67,7 @@ Window {
root.last_button_pressed = _cppBuzzerConn.get("lastpressed")
console.log(root.startTime);
console.log(root.buzzer_offset);
//root.stoppedTime = ( ( (root.last_button_pressed + root.buzzer_offset) - root.startTime ) / 2 ) / 10
root.stoppedTime = new Date().getTime() - root.startTime
root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime
time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec"
console.log("STOPPED: "+root.stoppedTime)
root.state = "STOPPED"

View file

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