From 1e327a6d6d5eac09f5692d9091043e87ea688e5d Mon Sep 17 00:00:00 2001 From: Javier Date: Tue, 13 Jan 2015 11:29:11 +0100 Subject: initial import --- src/finesketch.cpp | 26 +++++++++ src/stylus.cpp | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/stylus.h | 59 ++++++++++++++++++++ 3 files changed, 245 insertions(+) create mode 100644 src/finesketch.cpp create mode 100644 src/stylus.cpp create mode 100644 src/stylus.h (limited to 'src') 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 +#include +#include +#include +#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 +#include +#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 +#include +#include + +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 -- cgit v1.2.3