Update version info.
[qzeecontrol] / btconnector.cpp
index 17f978f..8f5f97e 100644 (file)
@@ -56,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();
@@ -73,14 +91,21 @@ 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));
 
+        /*
+         * Emulate a digital joystick.
+         */
         if(_up && (_y > -threshold())){
             setUp(false);
         }else if(!_up && (_y < -threshold())){
@@ -104,9 +129,10 @@ void BtConnector::readData(){
         }else if(!_right && (_x > threshold())){
             setRight(true);
         }
-    }else if(data.at(0) == 8){
-        // Button press
+    }else if(data.at(0) == 8){ 
         /*
+         * Button presses
+         *
          * A -> 0, B -> 1, C -> 2, D ->3
          * At index 3 to 6 (inclusive)
          */
@@ -129,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;
                 }
             }