Intermediate commit.
authorRuediger Gad <rgad@fb2.fh-frankfurt.de>
Thu, 12 Apr 2012 07:04:58 +0000 (09:04 +0200)
committerRuediger Gad <rgad@fb2.fh-frankfurt.de>
Thu, 12 Apr 2012 07:04:58 +0000 (09:04 +0200)
btconnector.h
qml/QZeeControl/MainPage.qml

index aaeac76..7ea6e63 100644 (file)
@@ -47,16 +47,48 @@ signals:
     void error(QBluetoothSocket::SocketError errorCode);
 
     void stickMoved(int x, int y);
+    void buttonsChanged(bool a, bool b, bool c, bool d);
 
 private slots:
     void readData(){
-        qDebug("readData...");
+//        qDebug("readData...");
         QByteArray data = socket->readAll();
-        qDebug("read %d bytes.", data.size());
+//        qDebug("read %d bytes.", data.size());
 
+/*
         for(int i=0; i < data.size(); i++){
             qDebug("%d: %d", i, ((signed char)data.at(i)));
         }
+*/
+
+        /*
+         * Actually it seems like that the first three bytes are used for
+         * identifying the "type" of data sent. However, for now using the
+         * first seems to suffice.
+         */
+        if(data.at(0) == 5){
+            // Joystick movement
+            emit(stickMoved((int)(signed char) data.at(4), (int)(signed char) data.at(5)));
+        }else if(data.at(0) == 8){
+            // Button press
+            /*
+             * A -> 0, B -> 1, C -> 2, D ->3
+             * At index 3 to 6 (inclusive)
+             */
+
+            char buttonMap = 0;
+
+            for(int i = 3; i <= 6; i++){
+                for(int b = 0; b <= 3; b++){
+                    if(data.at(i) == b){
+                        buttonMap ^= (1 << b);
+                    }
+                }
+            }
+
+            qDebug("Button map: %d", buttonMap);
+            emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
+        }
     }
 
 private:
index d624df6..8198d51 100644 (file)
@@ -9,11 +9,11 @@ Page {
     Label {
         id: label
         anchors.centerIn: parent
-        text: "Click to Scan"
+        text: "Press to connect."
     }
 
     Button{
-        id: scanButton
+        id: connectButton
 
         anchors {
             horizontalCenter: parent.horizontalCenter
@@ -28,6 +28,22 @@ Page {
         }
     }
 
+    Button{
+        id: disconnectButton
+
+        anchors {
+            horizontalCenter: parent.horizontalCenter
+            top: connectButton.bottom
+            topMargin: 10
+        }
+
+        text: "Disconnect"
+
+        onClicked: {
+            btConn.disconnect()
+        }
+    }
+
     BluetoothDiscoveryModel{
         id: btDiscovery
 
@@ -37,10 +53,11 @@ Page {
         onDiscoveryChanged: {
             if(discovery){
                 label.text = "Scanning for devices..."
-                scanButton.enabled = false
+                connectButton.enabled = false
+                disconnectButton.enabled = false
             }else{
-                label.text = "Scan finished."
-                scanButton.enabled = true
+                connectButton.enabled = true
+                disconnectButton.enabled = false
             }
         }
 
@@ -71,5 +88,25 @@ Page {
 
     BtConnector{
         id: btConn
+
+        onConnected: {
+            connectButton.enabled = false
+            disconnectButton.enabled = true
+            label.text = "Connected."
+        }
+
+        onDisconnected: {
+            connectButton.enabled = true
+            disconnectButton.enabled = false
+            label.text = "Press to connect."
+        }
+
+        onStickMoved: {
+            console.log("Stick moved. x: " + x + " y: " + y)
+        }
+
+        onButtonsChanged: {
+            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
+        }
     }
 }