diff --git a/gpio/deviceplugingpio.cpp b/gpio/deviceplugingpio.cpp index 4e848b53..16246e7a 100644 --- a/gpio/deviceplugingpio.cpp +++ b/gpio/deviceplugingpio.cpp @@ -192,7 +192,6 @@ Device::DeviceError DevicePluginGpio::discoverDevices(const DeviceClassId &devic break; } } - deviceDescriptors.append(descriptor); } @@ -280,6 +279,12 @@ void DevicePluginGpio::deviceRemoved(Device *device) delete monitor; } + if (m_longPressTimers.contains(device)) { + QTimer *timer = m_longPressTimers.take(device); + timer->stop(); + timer->deleteLater(); + } + if (m_counterValues.contains(device->id())) { m_counterValues.remove(device->id()); } @@ -370,6 +375,10 @@ void DevicePluginGpio::postSetupDevice(Device *device) } if (device->deviceClassId() == gpioInputRpiDeviceClassId || device->deviceClassId() == gpioInputBbbDeviceClassId) { + QTimer *timer = new QTimer(this); + timer->setSingleShot(true); + m_longPressTimers.insert(device, timer); + GpioMonitor *monitor = m_monitorDevices.key(device); if (!monitor) return; @@ -399,8 +408,6 @@ void DevicePluginGpio::postSetupDevice(Device *device) } }); } - - } } @@ -522,9 +529,37 @@ void DevicePluginGpio::onGpioValueChanged(const bool &value) if (device->deviceClassId() == gpioInputRpiDeviceClassId) { device->setStateValue(gpioInputRpiPressedStateTypeId, value); //start longpresss timer + QTimer *timer = m_longPressTimers.value(device); + if (!timer){ + qWarning(dcGpioController()) << "Long press timer not available"; + return; + } + if (value) { + int seconds = configValue( gpioControllerPluginLongPressTimeParamTypeId).toInt(); + timer->start(seconds * 1000); + } else { + if (timer->isActive()) { + timer->stop(); + //emit timer pressed + } + } } else if (device->deviceClassId() == gpioInputBbbDeviceClassId) { device->setStateValue(gpioInputBbbPressedStateTypeId, value); - //start longpress timer + //start longpresss timer + QTimer *timer = m_longPressTimers.value(device); + if (!timer){ + qWarning(dcGpioController()) << "Long press timer not available"; + return; + } + if (value) { + int seconds = configValue( gpioControllerPluginLongPressTimeParamTypeId).toInt(); + timer->start(seconds * 1000); + } else { + if (timer->isActive()) { + timer->stop(); + //emit timer pressed + } + } } else if (device->deviceClassId() == counterRpiDeviceClassId || device->deviceClassId() == counterBbbDeviceClassId) { if (value) { m_counterValues[device->id()] += 1; @@ -532,3 +567,20 @@ void DevicePluginGpio::onGpioValueChanged(const bool &value) } } + +void DevicePluginGpio::onLongPressedTimeout() +{ + QTimer *timer = static_cast(sender()); + qCDebug(dcGpioController()) << "Button long pressed"; + timer->stop(); + Device *device = m_longPressTimers.key(timer); + if (!device) + return; + + if (device->deviceClassId() == gpioInputRpiDeviceClassId){ + emitEvent(Event(gpioInputRpiLongPressedEventTypeId, device->id())); + } else if (device->deviceClassId() == gpioInputBbbDeviceClassId){ + emitEvent(Event(gpioInputBbbLongPressedEventTypeId, device->id())); + } +} + diff --git a/gpio/deviceplugingpio.h b/gpio/deviceplugingpio.h index ea56ce08..73911bdb 100644 --- a/gpio/deviceplugingpio.h +++ b/gpio/deviceplugingpio.h @@ -30,6 +30,8 @@ #include "devices/deviceplugin.h" #include "plugintimer.h" +#include + class DevicePluginGpio : public DevicePlugin { Q_OBJECT @@ -61,10 +63,11 @@ private: QList beagleboneBlackGpioDescriptors(); PluginTimer *m_counterTimer = nullptr; QHash m_counterValues; + QHash m_longPressTimers; private slots: void onGpioValueChanged(const bool &value); - + void onLongPressedTimeout(); }; #endif // DEVICEPLUGINGPIO_H diff --git a/gpio/deviceplugingpio.json b/gpio/deviceplugingpio.json index aaaacac3..8969b43a 100644 --- a/gpio/deviceplugingpio.json +++ b/gpio/deviceplugingpio.json @@ -2,6 +2,16 @@ "displayName": "Gpio Controller", "name": "GpioController", "id": "127ead55-996a-44ac-ba82-fc3c634e018a", + "paramTypes": [ + { + "id": "bfb31f88-b481-49e1-9a0a-41b156b64efe", + "name": "longPressTime", + "displayName": "Long press time", + "type": "int", + "unit": "Seconds", + "defaultValue": 2 + } + ], "vendors": [ { "displayName": "Raspberry Pi 2/3",