added buzzer connectivity
This commit is contained in:
parent
db94ba449d
commit
fb8e80281b
4 changed files with 113 additions and 58 deletions
|
@ -59,31 +59,12 @@ Dialog {
|
|||
anchors.fill: parent
|
||||
text: "connect"
|
||||
onClicked: {
|
||||
var i;
|
||||
if(i === 100){
|
||||
i = 0
|
||||
running = false
|
||||
}
|
||||
// connect_del.enabled = false
|
||||
// connect_del.text = _cppBuzzerConn.get("offset")
|
||||
// _cppBuzzerConn.connect()
|
||||
// connect_del.enabled = true
|
||||
connect_del.text = _cppBuzzerConn.test()
|
||||
|
||||
var theUrl = "http://192.168.4.1";
|
||||
var xmlHttp = null;
|
||||
|
||||
xmlHttp = new XMLHttpRequest();
|
||||
xmlHttp.open( "GET", theUrl, false );
|
||||
xmlHttp.setRequestHeader("Content-type", "text/plain;charset=UTF-8");
|
||||
xmlHttp.send( null );
|
||||
var response = xmlHttp.responseText
|
||||
var response_status = xmlHttp.status
|
||||
connect_del.text = response
|
||||
var res = response.split("<br>");
|
||||
var time_now = new Date().getTime()
|
||||
console.log(time_now)
|
||||
console.log(res[1])
|
||||
console.log(time_now-res[0])
|
||||
root.buzzer_offset = time_now-res[0]
|
||||
root.last_button_pressed = res[1]
|
||||
console.log("buzzer_offset: "+root.buzzer_offset)
|
||||
connect_del.text = response
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@
|
|||
BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent)
|
||||
{
|
||||
this->networkManager = new QNetworkAccessManager();
|
||||
connect();
|
||||
this->date = new QDateTime;
|
||||
this->latest_button_pressed = 0;
|
||||
this->connected = false;
|
||||
}
|
||||
|
||||
bool BuzzerConn::connect()
|
||||
|
@ -12,9 +14,10 @@ bool BuzzerConn::connect()
|
|||
|
||||
if(times[0] == 200.0){
|
||||
this->connected = true;
|
||||
this->latest_button_pressed = times[2];
|
||||
for(int i=0;i<100;i++){
|
||||
calcoffset();
|
||||
calcoffset();
|
||||
calcoffset();
|
||||
}
|
||||
return(true);
|
||||
}
|
||||
else{
|
||||
|
@ -25,15 +28,24 @@ bool BuzzerConn::connect()
|
|||
void BuzzerConn::calcoffset()
|
||||
{
|
||||
QList<double> times = gettimes();
|
||||
QDateTime * date = new QDateTime;
|
||||
if(times[0] == 200.0){
|
||||
double offset = date->currentMSecsSinceEpoch() - times[2];
|
||||
if(this->latest_offsets.length()>=100){
|
||||
this->latest_offsets.removeFirst();
|
||||
}
|
||||
this->latest_offsets.append(offset);
|
||||
|
||||
double mem = 0;
|
||||
for(int i=0;i<latest_offsets.length();i++){
|
||||
qDebug()<<latest_offsets[i];
|
||||
mem += latest_offsets[i];
|
||||
}
|
||||
this->offset = mem / double(latest_offsets.length());
|
||||
//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());
|
||||
}
|
||||
}
|
||||
|
||||
QList<double> BuzzerConn::gettimes()
|
||||
|
@ -43,7 +55,6 @@ QList<double> BuzzerConn::gettimes()
|
|||
times.append(double(ret.status_code));
|
||||
|
||||
if(ret.status_code == 200){
|
||||
qDebug()<<ret.text;
|
||||
ret.text.replace("\n","");
|
||||
ret.text.replace("\r","");
|
||||
QStringList times_cache = ret.text.split("<br>");
|
||||
|
@ -57,6 +68,59 @@ QList<double> BuzzerConn::gettimes()
|
|||
}
|
||||
}
|
||||
|
||||
bool BuzzerConn::buzzer_triggered()
|
||||
{
|
||||
QList<double> times = this->gettimes();
|
||||
qDebug() << int(this->get("currtime") - this->starttime);
|
||||
if(times[0] == 200.0 && this->connected){
|
||||
if(times[2] > this->latest_button_pressed){
|
||||
this->latest_button_pressed = times[2];
|
||||
|
||||
return(true);
|
||||
}
|
||||
else {
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
else{
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
bool BuzzerConn::start()
|
||||
{
|
||||
if(!this->connected){
|
||||
return(false);
|
||||
}
|
||||
QList<double> times = this->gettimes();
|
||||
if(times[0] == 200.0 && this->connected){
|
||||
this->latest_button_pressed = times[2];
|
||||
return(true);
|
||||
}
|
||||
else{
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
double BuzzerConn::get(QString key)
|
||||
{
|
||||
if(key == "offset"){
|
||||
return(this->offset);
|
||||
}
|
||||
else if (key == "lastpressed") {
|
||||
return(this->latest_button_pressed);
|
||||
}
|
||||
else if( key == "currtime") {
|
||||
return(this->date->currentMSecsSinceEpoch());
|
||||
}
|
||||
}
|
||||
|
||||
QString BuzzerConn::test()
|
||||
{
|
||||
ReturnData_t ret = this->senddata(QUrl("http://www.google.de"));
|
||||
return(ret.text);
|
||||
}
|
||||
|
||||
ReturnData_t BuzzerConn::senddata(QUrl serviceUrl)
|
||||
{
|
||||
|
||||
|
@ -67,7 +131,6 @@ ReturnData_t BuzzerConn::senddata(QUrl serviceUrl)
|
|||
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
||||
"application/x-www-form-urlencoded");
|
||||
|
||||
//set ssl configuration
|
||||
//send a POST request with the given url and data to the server
|
||||
QUrlQuery pdata;
|
||||
QNetworkReply* reply;
|
||||
|
|
16
buzzerconn.h
16
buzzerconn.h
|
@ -20,20 +20,28 @@ class BuzzerConn : public QObject
|
|||
Q_OBJECT
|
||||
public:
|
||||
explicit BuzzerConn(QObject *parent = nullptr);
|
||||
double offset;
|
||||
Q_INVOKABLE double offset;
|
||||
QList<double> latest_offsets;
|
||||
double latest_button_pressed;
|
||||
double starttime;
|
||||
bool connected;
|
||||
|
||||
|
||||
private:
|
||||
QNetworkAccessManager *networkManager;
|
||||
QDateTime *date;
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
ReturnData_t senddata(QUrl serviceUrl);
|
||||
QList<double> gettimes();
|
||||
bool connect();
|
||||
void calcoffset();
|
||||
Q_INVOKABLE QList<double> gettimes();
|
||||
Q_INVOKABLE bool connect();
|
||||
Q_INVOKABLE void calcoffset();
|
||||
Q_INVOKABLE bool buzzer_triggered();
|
||||
Q_INVOKABLE bool start();
|
||||
Q_INVOKABLE double get(QString key);
|
||||
Q_INVOKABLE QString test();
|
||||
};
|
||||
|
||||
#endif // BUZZERCONN_H
|
||||
|
|
35
main.qml
35
main.qml
|
@ -21,7 +21,7 @@ Window {
|
|||
|
||||
property double startTime: 0
|
||||
property double stoppedTime: 0
|
||||
property double currTime: new Date().getTime()
|
||||
property double currTime
|
||||
|
||||
property double buzzer_offset
|
||||
property double last_button_pressed
|
||||
|
@ -43,26 +43,28 @@ Window {
|
|||
Timer {
|
||||
id: running_refresh_timer
|
||||
running: root.state === "RUNNING"
|
||||
repeat: true
|
||||
repeat: false
|
||||
interval: 1
|
||||
|
||||
onTriggered: {
|
||||
var theUrl = "http://192.168.4.1";
|
||||
var xmlHttp = null;
|
||||
//root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime
|
||||
//console.log("STOPPED: "+root.stoppedTime)
|
||||
//time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec"
|
||||
if(_cppBuzzerConn.buzzer_triggered()){
|
||||
|
||||
xmlHttp = new XMLHttpRequest();
|
||||
xmlHttp.open( "GET", theUrl, false );
|
||||
xmlHttp.send( null );
|
||||
var response = xmlHttp.responseText
|
||||
var res = response.split("<br>");
|
||||
startButt.text = res[0]
|
||||
if(res[1]>root.last_button_pressed){
|
||||
root.last_button_pressed = res[1]
|
||||
root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime
|
||||
console.log("STOPPED: "+root.stoppedTime)
|
||||
root.buzzer_offset = _cppBuzzerConn.get("offset")
|
||||
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
|
||||
time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec"
|
||||
console.log("STOPPED: "+root.stoppedTime)
|
||||
root.state = "STOPPED"
|
||||
}
|
||||
if(root.state === "RUNNING"){
|
||||
running_refresh_timer.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,8 +75,9 @@ Window {
|
|||
|
||||
onPlayingChanged: {
|
||||
if(!playing){
|
||||
root.startTime = new Date().getTime()
|
||||
root.currTime = new Date().getTime()
|
||||
root.startTime = _cppBuzzerConn.get("currtime")
|
||||
_cppBuzzerConn.start()
|
||||
root.currTime = _cppBuzzerConn.get("currtime")
|
||||
time.text = ( ( root.currTime - root.startTime ) / 1000 ).toFixed(3) + " sec"
|
||||
root.state = "RUNNING"
|
||||
}
|
||||
|
|
Reference in a new issue