From 9e0afd600ac5a03993a1034d2115c2282422dad5 Mon Sep 17 00:00:00 2001 From: nymea Date: Thu, 8 Aug 2019 17:06:26 +0200 Subject: [PATCH] added long press events --- senic/devicepluginsenic.cpp | 25 ++++++++++++++----- senic/devicepluginsenic.h | 2 +- senic/devicepluginsenic.json | 14 ++++++++++- senic/nuimo.cpp | 48 ++++++++++++++++++++++++++++++++---- senic/nuimo.h | 10 ++++++-- 5 files changed, 84 insertions(+), 15 deletions(-) diff --git a/senic/devicepluginsenic.cpp b/senic/devicepluginsenic.cpp index b4d9cf21..c8cfa066 100644 --- a/senic/devicepluginsenic.cpp +++ b/senic/devicepluginsenic.cpp @@ -73,8 +73,9 @@ Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device) BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress); Nuimo *nuimo = new Nuimo(bluetoothDevice, this); + nuimo->setLongPressTime(configValue(senicPluginLongPressTimeParamTypeId).toInt()); connect(nuimo, &Nuimo::buttonPressed, this, &DevicePluginSenic::onButtonPressed); - connect(nuimo, &Nuimo::buttonReleased, this, &DevicePluginSenic::onButtonReleased); + connect(nuimo, &Nuimo::buttonLongPressed, this, &DevicePluginSenic::onButtonLongPressed); connect(nuimo, &Nuimo::swipeDetected, this, &DevicePluginSenic::onSwipeDetected); connect(nuimo, &Nuimo::rotationValueChanged, this, &DevicePluginSenic::onRotationValueChanged); connect(nuimo, &Nuimo::connectedChanged, this, &DevicePluginSenic::onConnectedChanged); @@ -194,7 +195,6 @@ void DevicePluginSenic::onConnectedChanged(bool connected) { Nuimo *nuimo = static_cast(sender()); Device *device = m_nuimos.value(nuimo); - device->setStateValue(nuimoConnectedStateTypeId, connected); } @@ -205,15 +205,21 @@ void DevicePluginSenic::onButtonPressed() Device *device = m_nuimos.value(nuimo); emitEvent(Event(nuimoPressedEventTypeId, device->id(), ParamList() << Param(nuimoPressedEventButtonNameParamTypeId, "•"))); - if(m_autoSymbolMode) { - nuimo->showImage(Nuimo::MatrixType::MatrixTypeCircle); + if (m_autoSymbolMode) { + nuimo->showImage(Nuimo::MatrixTypeCircle); } } -void DevicePluginSenic::onButtonReleased() +void DevicePluginSenic::onButtonLongPressed() { - // ENHANCEMENT: user timer to detekt long press events + Nuimo *nuimo = static_cast(sender()); + Device *device = m_nuimos.value(nuimo); + emitEvent(Event(nuimoLongPressedEventTypeId, device->id())); + + if (m_autoSymbolMode) { + nuimo->showImage(Nuimo::MatrixTypeFilledCircle); + } } @@ -284,6 +290,13 @@ void DevicePluginSenic::onPluginConfigurationChanged(const ParamTypeId ¶mTyp qCDebug(dcSenic()) << "Auto symbol mode" << (value.toBool() ? "enabled." : "disabled."); m_autoSymbolMode = value.toBool(); } + + if (paramTypeId == senicPluginLongPressTimeParamTypeId) { + qCDebug(dcSenic()) << "Long press time" << value.toUInt(); + foreach(Nuimo *nuimo, m_nuimos.keys()) { + nuimo->setLongPressTime(value.toInt()); + } + } } void DevicePluginSenic::onBatteryValueChanged(const uint &percentage) diff --git a/senic/devicepluginsenic.h b/senic/devicepluginsenic.h index 2302a555..5890f636 100644 --- a/senic/devicepluginsenic.h +++ b/senic/devicepluginsenic.h @@ -59,7 +59,7 @@ private slots: void onConnectedChanged(bool connected); void onBatteryValueChanged(const uint &percentage); void onButtonPressed(); - void onButtonReleased(); + void onButtonLongPressed(); void onSwipeDetected(const Nuimo::SwipeDirection &direction); void onRotationValueChanged(const uint &value); void onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision); diff --git a/senic/devicepluginsenic.json b/senic/devicepluginsenic.json index f70fc8fa..cb078851 100644 --- a/senic/devicepluginsenic.json +++ b/senic/devicepluginsenic.json @@ -9,6 +9,13 @@ "displayName": "Automatically display symbols", "type": "bool", "defaultValue": true + }, + { + "id": "f9900dfe-310b-462b-b09d-a999df441075", + "name": "longPressTime", + "displayName": "Long press time", + "type": "int", + "defaultValue": 250 } ], "vendors": [ @@ -22,7 +29,7 @@ "name": "nuimo", "displayName": "Nuimo", "createMethods": ["discovery"], - "interfaces": ["simplemultibutton", "connectable", "batterylevel"], + "interfaces": ["simplemultibutton", "longpressbutton", "connectable", "batterylevel"], "paramTypes": [ { "id": "71553f6a-2ed4-4896-bb7b-52e7dca948b2", @@ -141,6 +148,11 @@ "allowedValues": ["•", "←", "↑", "→", "↓"] } ] + }, + { + "id": "a2f4add5-f76a-4dca-ae68-4107533bee0e", + "name": "longPressed", + "displayName": "Button long pressed" } ] } diff --git a/senic/nuimo.cpp b/senic/nuimo.cpp index 6c2dabda..9501471d 100644 --- a/senic/nuimo.cpp +++ b/senic/nuimo.cpp @@ -41,6 +41,12 @@ Nuimo::Nuimo(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent) : { connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::connectedChanged, this, &Nuimo::onConnectedChanged); connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::servicesDiscoveryFinished, this, &Nuimo::onServiceDiscoveryFinished); + + if (!m_longPressTimer) { + m_longPressTimer = new QTimer(this); + m_longPressTimer->setSingleShot(true); + } + connect(m_longPressTimer, &QTimer::timeout, this, &Nuimo::onLongPressTimer); } @@ -210,6 +216,19 @@ void Nuimo::showImage(const Nuimo::MatrixType &matrixType) " "); time = 5; break; + case MatrixTypeFilledCircle: + matrix = QByteArray( + " " + " " + " *** " + " ***** " + " ***** " + " ***** " + " *** " + " " + " "); + time = 5; + break; case MatrixTypeLight: matrix = QByteArray( " " @@ -228,6 +247,11 @@ void Nuimo::showImage(const Nuimo::MatrixType &matrixType) showMatrix(matrix, time); } +void Nuimo::setLongPressTime(int milliSeconds) +{ + m_longPressTime = milliSeconds; +} + void Nuimo::showMatrix(const QByteArray &matrix, const int &seconds) { if (!m_ledMatrixService) @@ -264,11 +288,22 @@ void Nuimo::printService(QLowEnergyService *service) } } +void Nuimo::onLongPressTimer() +{ + if (m_buttonPressed) { + emit buttonLongPressed(); + } else { + emit buttonPressed(); + } +} + void Nuimo::onConnectedChanged(const bool &connected) { qCDebug(dcSenic()) << m_bluetoothDevice->name() << m_bluetoothDevice->address().toString() << (connected ? "connected" : "disconnected"); + + m_longPressTimer->stop(); emit connectedChanged(connected); if (!connected) { @@ -468,12 +503,15 @@ void Nuimo::onInputServiceStateChanged(const QLowEnergyService::ServiceState &st void Nuimo::onInputCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) { if (characteristic.uuid() == m_inputButtonCharacteristic.uuid()) { - bool pressed = (bool)value.toHex().toUInt(nullptr, 16); - qCDebug(dcSenic()) << "Button:" << (pressed ? "pressed": "released"); - if (pressed) { - emit buttonPressed(); + m_buttonPressed = (bool)value.toHex().toUInt(nullptr, 16); + qCDebug(dcSenic()) << "Button:" << (m_buttonPressed ? "pressed": "released"); + if (m_buttonPressed) { + //emit buttonPressed(); + m_longPressTimer->start(m_longPressTime); } else { - emit buttonReleased(); + if (!m_longPressTimer->isActive()) + m_longPressTimer->stop(); + emit buttonLongPressed(); } return; } diff --git a/senic/nuimo.h b/senic/nuimo.h index 8bec379e..b51f04f9 100644 --- a/senic/nuimo.h +++ b/senic/nuimo.h @@ -24,6 +24,7 @@ #define NUIMO_H #include +#include #include #include "typeutils.h" @@ -53,14 +54,15 @@ public: MatrixTypeNext, MatrixTypePrevious, MatrixTypeCircle, + MatrixTypeFilledCircle, MatrixTypeLight }; explicit Nuimo(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent = nullptr); BluetoothLowEnergyDevice *bluetoothDevice(); - void showImage(const MatrixType &matrixType); + void setLongPressTime(int milliSeconds); private: BluetoothLowEnergyDevice *m_bluetoothDevice = nullptr; @@ -78,14 +80,18 @@ private: QLowEnergyCharacteristic m_inputRotationCharacteristic; uint m_rotationValue; + QTimer *m_longPressTimer = nullptr; + int m_longPressTime = 250; + bool m_buttonPressed = false; void showMatrix(const QByteArray &matrix, const int &seconds); void printService(QLowEnergyService *service); + void onLongPressTimer(); signals: void connectedChanged(bool connected); void buttonPressed(); - void buttonReleased(); + void buttonLongPressed(); void batteryValueChanged(const uint &percentage); void swipeDetected(const SwipeDirection &direction); void rotationValueChanged(const uint &value);