added long press events

master
nymea 2019-08-08 17:06:26 +02:00
parent 3bf9ca94c7
commit 9e0afd600a
5 changed files with 84 additions and 15 deletions

View File

@ -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<Nuimo *>(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<Nuimo *>(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 &paramTyp
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)

View File

@ -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);

View File

@ -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"
}
]
}

View File

@ -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;
}

View File

@ -24,6 +24,7 @@
#define NUIMO_H
#include <QObject>
#include <QTimer>
#include <QBluetoothUuid>
#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);