diff options
author | Javier <dev.git@javispedro.com> | 2015-01-13 11:29:11 +0100 |
---|---|---|
committer | Javier <dev.git@javispedro.com> | 2015-01-13 11:29:11 +0100 |
commit | 1e327a6d6d5eac09f5692d9091043e87ea688e5d (patch) | |
tree | 90173c457e7c07426d7f7267b68b69d4f9c3e2c8 /src | |
download | finesketch-1e327a6d6d5eac09f5692d9091043e87ea688e5d.tar.gz finesketch-1e327a6d6d5eac09f5692d9091043e87ea688e5d.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/finesketch.cpp | 26 | ||||
-rw-r--r-- | src/stylus.cpp | 160 | ||||
-rw-r--r-- | src/stylus.h | 59 |
3 files changed, 245 insertions, 0 deletions
diff --git a/src/finesketch.cpp b/src/finesketch.cpp new file mode 100644 index 0000000..1a5c45f --- /dev/null +++ b/src/finesketch.cpp @@ -0,0 +1,26 @@ +#include <QtGui/QGuiApplication> +#include <QtQuick/QQuickView> +#include <QtQml/QQmlContext> +#include <sailfishapp.h> +#include "stylus.h" + +int main(int argc, char *argv[]) +{ + QGuiApplication *app = SailfishApp::application(argc, argv); + QQuickView *view = SailfishApp::createView(); + + Stylus *stylus = new Stylus(); + stylus->connectDevice("F4:6A:BC:10:4F:E9"); + + view->rootContext()->setContextProperty("stylus", stylus); + view->setSource(SailfishApp::pathTo("qml/finesketch.qml")); + + view->show(); + + int res = app->exec(); + + delete stylus; + + return res; +} + diff --git a/src/stylus.cpp b/src/stylus.cpp new file mode 100644 index 0000000..f4fd0a8 --- /dev/null +++ b/src/stylus.cpp @@ -0,0 +1,160 @@ +#include <QDataStream> +#include <gato/gato.h> +#include "stylus.h" + +static const QString stylus_name(QString::fromLatin1("JN104FE9")); +static const GatoUUID stylus_service(QString::fromLatin1("dcd68980-aadc-11e1-a22a-0002a5d5c51b")); +static const GatoUUID agg_char_uuid(QString::fromLatin1("00002a5a-0000-1000-8000-00805f9b34fb")); + +Stylus::Stylus(QObject *parent) : + QObject(parent), + _manager(new GatoCentralManager(this)), + _peripheral(0), + _p(0), _x(0), _y(0), _z(0) +{ + connect(_manager, SIGNAL(discoveredPeripheral(GatoPeripheral*,int)), + SLOT(handleDiscoveredPeripheral(GatoPeripheral*,int))); +} + +Stylus::~Stylus() +{ + if (_peripheral) { + disconnect(_peripheral, 0, this, 0); + _peripheral->disconnectPeripheral(); + } +} + +qreal Stylus::pressure() const +{ + return _p; +} + +qreal Stylus::x() const +{ + return _x; +} + +qreal Stylus::y() const +{ + return _y; +} + +qreal Stylus::z() const +{ + return _z; +} + +void Stylus::connectToAnyDevice() +{ + _manager->scanForPeripherals(); +} + +void Stylus::connectDevice(const QString &addr) +{ + connectToPeripheral(new GatoPeripheral(GatoAddress(addr), this)); +} + +void Stylus::disconnectDevice() +{ + if (_peripheral) { + _peripheral->disconnectPeripheral(); + _peripheral->deleteLater(); + _peripheral = 0; + } +} + +void Stylus::connectToPeripheral(GatoPeripheral *peripheral) +{ + if (_peripheral) { + disconnectDevice(); + } + + _peripheral = peripheral; + + qDebug() << "Connecting to peripheral" << peripheral->name() << peripheral->address().toString(); + + connect(_peripheral, SIGNAL(connected()), SLOT(handleConnected())); + connect(_peripheral, SIGNAL(disconnected()), SLOT(handleDisconnected())); + connect(_peripheral, SIGNAL(servicesDiscovered()), SLOT(handleServices())); + connect(_peripheral, SIGNAL(characteristicsDiscovered(GatoService)), SLOT(handleCharacteristics(GatoService))); + connect(_peripheral, SIGNAL(valueUpdated(GatoCharacteristic,QByteArray)), SLOT(handleValueUpdated(GatoCharacteristic,QByteArray))); + + _peripheral->connectPeripheral(); +} + +void Stylus::handleDiscoveredPeripheral(GatoPeripheral *peripheral, int rssi) +{ + qDebug() << "Found peripheral" << peripheral->address().toString() << peripheral->name() << rssi; + if (peripheral->name() == stylus_name) { + _manager->stopScan(); + connectToPeripheral(peripheral); + } +} +void Stylus::handleConnected() +{ + qDebug() << "Connected to" << _peripheral->name(); + _peripheral->discoverServices(); +} + +void Stylus::handleDisconnected() +{ + qDebug() << "Peripheral disconnected"; +} + +void Stylus::handleServices() +{ + qDebug() << "Services found"; + foreach (const GatoService &service, _peripheral->services()) { + qDebug() << service.uuid(); + if (service.uuid() == stylus_service) { + // Found the service we want + qDebug() << "Found stylus service"; + _peripheral->discoverCharacteristics(service); + } + } +} + +void Stylus::handleCharacteristics(const GatoService &service) +{ + foreach (const GatoCharacteristic &c, service.characteristics()) { + if (c.uuid() == agg_char_uuid) { + qDebug() << c.uuid(); + _peripheral->setNotification(c, true); + } + } +} + +void Stylus::handleValueUpdated(const GatoCharacteristic &characteristic, const QByteArray &value) +{ + if (characteristic.uuid() == agg_char_uuid) { + QDataStream s(value); + s.setByteOrder(QDataStream::BigEndian); + qint16 p, x, y, z; + s >> p >> x >> y >> z; + handleReport(p, x, y, z); + } +} + +void Stylus::handleReport(int p, int x, int y, int z) +{ + qreal newp = p / 7000.0; + if (newp != _p) { + _p = newp; + emit pressureChanged(); + } + qreal newx = x / 268.0; + if (newx != _x) { + _x = newx; + emit xChanged(); + } + qreal newy = y / 268.0; + if (newy != _y) { + _y = newy; + emit yChanged(); + } + qreal newz = z / 268.0; + if (newz != _z) { + _z = newz; + emit zChanged(); + } +} diff --git a/src/stylus.h b/src/stylus.h new file mode 100644 index 0000000..9b845b9 --- /dev/null +++ b/src/stylus.h @@ -0,0 +1,59 @@ +#ifndef STYLUS_H +#define STYLUS_H + +#include <QObject> +#include <gato/gatocentralmanager.h> +#include <gato/gatoperipheral.h> + +class Stylus : public QObject +{ + Q_OBJECT + Q_PROPERTY(qreal pressure READ pressure NOTIFY pressureChanged) + Q_PROPERTY(qreal x READ x NOTIFY xChanged) + Q_PROPERTY(qreal y READ y NOTIFY yChanged) + Q_PROPERTY(qreal z READ z NOTIFY zChanged) + +public: + explicit Stylus(QObject *parent = 0); + ~Stylus(); + + qreal pressure() const; + qreal x() const; + qreal y() const; + qreal z() const; + +signals: + void autoUpdateChanged(); + void pressureChanged(); + void xChanged(); + void yChanged(); + void zChanged(); + +public slots: + void connectToAnyDevice(); + void connectDevice(const QString &addr); + void disconnectDevice(); + +private: + void connectToPeripheral(GatoPeripheral *peripheral); + +private slots: + void handleDiscoveredPeripheral(GatoPeripheral *peripheral, int rssi); + void handleConnected(); + void handleDisconnected(); + void handleServices(); + void handleCharacteristics(const GatoService &service); + void handleValueUpdated(const GatoCharacteristic &characteristic, const QByteArray &value); + void handleReport(int p, int x, int y, int z); + +private: + GatoCentralManager *_manager; + GatoPeripheral *_peripheral; + + qreal _p; + qreal _x; + qreal _y; + qreal _z; +}; + +#endif // STYLUS_H |