Sometimes its the little things..
[qzeecontrol] / btconnector.cpp
index 48e7959..8f5f97e 100644 (file)
 
 BtConnector::BtConnector(QObject *parent)
     : QObject(parent){
+    _threshold = 50;
+
+    _up = false;
+    _down = false;
+    _left = false;
+    _right = false;
+
     _a = false;
     _b = false;
     _c = false;
@@ -49,6 +56,24 @@ void BtConnector::connect(QString address, int port){
     QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
 }
 
+void BtConnector::disconnect(){
+    if(!socket)
+        return;
+
+    if(socket->isOpen())
+        socket->close();
+
+    delete socket;
+    socket = 0;
+
+    /*
+     * Explicitly set D to false in case the remote was shut off
+     * using the power key, which equals the 'D' key.
+     * Well, good intention but doesn't seem to work.
+     */
+    setD(false);
+}
+
 void BtConnector::readData(){
 //        qDebug("readData...");
     QByteArray data = socket->readAll();
@@ -66,16 +91,48 @@ void BtConnector::readData(){
      * first seems to suffice.
      */
     if(data.at(0) == 5){
-        // Joystick movement
-        _x = (int)(signed char) data.at(4);
-        _y = (int)(signed char) data.at(5);
+        /*
+         * Joystick movement
+         *
+         * X-Axis: positive values -> right, negative values -> left
+         * Y-Axis: positive values -> down, negative values -> up
+         */
+
+        setX((int)(signed char) data.at(4));
+        setY((int)(signed char) data.at(5));
 
-        emit(xChanged(_x));
-        emit(yChanged(_y));
         emit(stickMoved(_x, _y));
-    }else if(data.at(0) == 8){
-        // Button press
+
+        /*
+         * Emulate a digital joystick.
+         */
+        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 presses
+         *
          * A -> 0, B -> 1, C -> 2, D ->3
          * At index 3 to 6 (inclusive)
          */
@@ -98,20 +155,16 @@ void BtConnector::readData(){
                 bool val = (buttonMap & (1 << i)) > 0;
                 switch (i){
                 case 0:
-                    _a = val;
-                    emit (aChanged(val));
+                    setA(val);
                     break;
                 case 1:
-                    _b = val;
-                    emit (bChanged(val));
+                    setB(val);
                     break;
                 case 2:
-                    _c = val;
-                    emit (cChanged(val));
+                    setC(val);
                     break;
                 case 3:
-                    _d = val;
-                    emit (dChanged(val));
+                    setD(val);
                     break;
                 }
             }