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:
Label {
id: label
anchors.centerIn: parent
- text: "Click to Scan"
+ text: "Press to connect."
}
Button{
- id: scanButton
+ id: connectButton
anchors {
horizontalCenter: parent.horizontalCenter
}
}
+ Button{
+ id: disconnectButton
+
+ anchors {
+ horizontalCenter: parent.horizontalCenter
+ top: connectButton.bottom
+ topMargin: 10
+ }
+
+ text: "Disconnect"
+
+ onClicked: {
+ btConn.disconnect()
+ }
+ }
+
BluetoothDiscoveryModel{
id: btDiscovery
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
}
}
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)
+ }
}
}