From ddd8f98d290a9adc88ef0602f73bfea7a0cd61e2 Mon Sep 17 00:00:00 2001 From: Ruediger Gad Date: Thu, 12 Apr 2012 09:04:58 +0200 Subject: [PATCH 1/1] Intermediate commit. --- btconnector.h | 36 ++++++++++++++++++++++++++++++-- qml/QZeeControl/MainPage.qml | 47 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/btconnector.h b/btconnector.h index aaeac76..7ea6e63 100644 --- a/btconnector.h +++ b/btconnector.h @@ -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: diff --git a/qml/QZeeControl/MainPage.qml b/qml/QZeeControl/MainPage.qml index d624df6..8198d51 100644 --- a/qml/QZeeControl/MainPage.qml +++ b/qml/QZeeControl/MainPage.qml @@ -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) + } } } -- 1.7.9.5