From aa1554480c6c5bd6083c067aa7a1571ab4bb14cb Mon Sep 17 00:00:00 2001 From: Ruediger Gad Date: Thu, 12 Apr 2012 21:46:08 +0200 Subject: [PATCH] Add properties for joystick directions. --- btconnector.cpp | 31 ++++++++++++++++++++++++++ btconnector.h | 49 ++++++++++++++++++++++++++++++++++++++++++ qml/QZeeControl/MainPage.qml | 47 ++++++++++++++++++++++------------------ 3 files changed, 106 insertions(+), 21 deletions(-) diff --git a/btconnector.cpp b/btconnector.cpp index 48e7959..17f978f 100644 --- a/btconnector.cpp +++ b/btconnector.cpp @@ -21,6 +21,13 @@ BtConnector::BtConnector(QObject *parent) : QObject(parent){ + _threshold = 50; + + _up = false; + _down = false; + _left = false; + _right = false; + _a = false; _b = false; _c = false; @@ -73,6 +80,30 @@ void BtConnector::readData(){ emit(xChanged(_x)); emit(yChanged(_y)); emit(stickMoved(_x, _y)); + + if(_up && (_y > -threshold())){ + setUp(false); + }else if(!_up && (_y < -threshold())){ + setUp(true); + } + + if(_down && (_y < threshold())){ + setDown(false); + }else if(!_down && (_y > threshold())){ + setDown(true); + } + + if(_left && (_x > -threshold())){ + setLeft(false); + }else if(!_left && (_x < -threshold())){ + setLeft(true); + } + + if(_right && (_x < threshold())){ + setRight(false); + }else if(!_right && (_x > threshold())){ + setRight(true); + } }else if(data.at(0) == 8){ // Button press /* diff --git a/btconnector.h b/btconnector.h index 0c5683d..4d9ecb4 100644 --- a/btconnector.h +++ b/btconnector.h @@ -29,6 +29,13 @@ class BtConnector : public QObject { Q_OBJECT + Q_PROPERTY(int threshold READ threshold NOTIFY thresholdChanged WRITE setThreshold) + + Q_PROPERTY(bool up READ up NOTIFY upChanged) + Q_PROPERTY(bool down READ down NOTIFY downChanged) + Q_PROPERTY(bool left READ left NOTIFY leftChanged) + Q_PROPERTY(bool right READ right NOTIFY rightChanged) + Q_PROPERTY(bool a READ a NOTIFY aChanged) Q_PROPERTY(bool b READ b NOTIFY bChanged) Q_PROPERTY(bool c READ c NOTIFY cChanged) @@ -46,6 +53,34 @@ public: Q_INVOKABLE void connect(QString address, int port); + int threshold(void){return _threshold;} + void setThreshold(int val){ + _threshold = val; + thresholdChanged(_threshold); + } + + void setUp(bool val){ + _up = val; + upChanged(_up); + } + void setDown(bool val){ + _down = val; + downChanged(_down); + } + void setLeft(bool val){ + _left = val; + leftChanged(_left); + } + void setRight(bool val){ + _right = val; + rightChanged(_right); + } + + bool up(){return _up;} + bool down(){return _down;} + bool left(){return _left;} + bool right(){return _right;} + bool a(){return _a;} bool b(){return _b;} bool c(){return _c;} @@ -74,6 +109,13 @@ signals: void stickMoved(int x, int y); void buttonsChanged(bool a, bool b, bool c, bool d); + void thresholdChanged(int val); + + void upChanged(bool val); + void downChanged(bool val); + void leftChanged(bool val); + void rightChanged(bool val); + void aChanged(bool val); void bChanged(bool val); void cChanged(bool val); @@ -88,6 +130,13 @@ private slots: private: QBluetoothSocket *socket; + int _threshold; + + bool _up; + bool _down; + bool _left; + bool _right; + bool _a; bool _b; bool _c; diff --git a/qml/QZeeControl/MainPage.qml b/qml/QZeeControl/MainPage.qml index c086e98..8a7f8c9 100644 --- a/qml/QZeeControl/MainPage.qml +++ b/qml/QZeeControl/MainPage.qml @@ -291,7 +291,7 @@ Page { BtConnector{ id: btConn - property int joystickThreshold: 50 + threshold: 50 onConnected: { disconnectButton.enabled = true @@ -329,27 +329,32 @@ Page { xtstAdapter.sendKey("d", val); } - onXChanged: { - if(val > joystickThreshold){ - xtstAdapter.sendKey("Right", true); - }else if(val < -joystickThreshold){ - xtstAdapter.sendKey("Left", true); - }else{ - xtstAdapter.sendKey("Right", false); - xtstAdapter.sendKey("Left", false); - } - } + onUpChanged: xtstAdapter.sendKey("Up", val) + onDownChanged: xtstAdapter.sendKey("Down", val) + onLeftChanged: xtstAdapter.sendKey("Left", val) + onRightChanged: xtstAdapter.sendKey("Right", val) + +// onXChanged: { +// if(val > joystickThreshold){ +// xtstAdapter.sendKey("Right", true); +// }else if(val < -joystickThreshold){ +// xtstAdapter.sendKey("Left", true); +// }else{ +// xtstAdapter.sendKey("Right", false); +// xtstAdapter.sendKey("Left", false); +// } +// } - onYChanged: { - if(val > joystickThreshold){ - xtstAdapter.sendKey("Down", true); - }else if(val < -joystickThreshold){ - xtstAdapter.sendKey("Up", true); - }else{ - xtstAdapter.sendKey("Down", false); - xtstAdapter.sendKey("Up", false); - } - } +// onYChanged: { +// if(val > joystickThreshold){ +// xtstAdapter.sendKey("Down", true); +// }else if(val < -joystickThreshold){ +// xtstAdapter.sendKey("Up", true); +// }else{ +// xtstAdapter.sendKey("Down", false); +// xtstAdapter.sendKey("Up", false); +// } +// } } XtstAdapter{ -- 1.7.9.5