2 * Copyright 2012 Ruediger Gad
4 * This file is part of QZeeControl.
6 * QZeeControl is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * QZeeControl is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with QZeeControl. If not, see <http://www.gnu.org/licenses/>.
20 #include "btconnector.h"
22 BtConnector::BtConnector(QObject *parent)
41 void BtConnector::connect(QString address, int port){
42 qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
47 socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
48 QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
49 QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
50 QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
52 qDebug("Connecting...");
53 socket->connectToService(QBluetoothAddress(address), port);
56 QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
59 void BtConnector::disconnect(){
70 * Explicitly set D to false in case the remote was shut off
71 * using the power key, which equals the 'D' key.
72 * Well, good intention but doesn't seem to work.
77 void BtConnector::readData(){
78 // qDebug("readData...");
79 QByteArray data = socket->readAll();
80 // qDebug("read %d bytes.", data.size());
83 for(int i=0; i < data.size(); i++){
84 qDebug("%d: %d", i, ((signed char)data.at(i)));
89 * Actually it seems like that the first three bytes are used for
90 * identifying the "type" of data sent. However, for now using the
91 * first seems to suffice.
97 * X-Axis: positive values -> right, negative values -> left
98 * Y-Axis: positive values -> down, negative values -> up
101 setX((int)(signed char) data.at(4));
102 setY((int)(signed char) data.at(5));
104 emit(stickMoved(_x, _y));
107 * Emulate a digital joystick.
109 if(_up && (_y > -threshold())){
111 }else if(!_up && (_y < -threshold())){
115 if(_down && (_y < threshold())){
117 }else if(!_down && (_y > threshold())){
121 if(_left && (_x > -threshold())){
123 }else if(!_left && (_x < -threshold())){
127 if(_right && (_x < threshold())){
129 }else if(!_right && (_x > threshold())){
132 }else if(data.at(0) == 8){
136 * A -> 0, B -> 1, C -> 2, D ->3
137 * At index 3 to 6 (inclusive)
142 for(int i = 3; i <= 6; i++){
143 for(int b = 0; b <= 3; b++){
145 buttonMap ^= (1 << b);
150 // qDebug("Button map: %d", buttonMap);
151 emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
153 for(int i = 0; i <= 3; i++){
154 if(((buttonMap | oldButtonMap) & (1 << i)) > 0){
155 bool val = (buttonMap & (1 << i)) > 0;
173 oldButtonMap = buttonMap;