Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPIO Plug-In: add counter device #152

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 134 additions & 18 deletions gpio/deviceplugingpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

DevicePluginGpio::DevicePluginGpio()
{

}

Device::DeviceSetupStatus DevicePluginGpio::setupDevice(Device *device)
Expand Down Expand Up @@ -87,26 +86,55 @@ Device::DeviceSetupStatus DevicePluginGpio::setupDevice(Device *device)
if (device->deviceClassId() == gpioInputBbbDeviceClassId)
gpioId = device->paramValue(gpioInputBbbDeviceGpioParamTypeId).toInt();

GpioMonitor *monior = new GpioMonitor(gpioId, this);
GpioMonitor *monitor = new GpioMonitor(gpioId, this);

if (!monior->enable()) {
if (!monitor->enable()) {
qCWarning(dcGpioController()) << "Could not enable gpio monitor for device" << device->name();
return Device::DeviceSetupStatusFailure;
}

connect(monior, &GpioMonitor::valueChanged, this, &DevicePluginGpio::onGpioValueChanged);
connect(monitor, &GpioMonitor::valueChanged, this, &DevicePluginGpio::onGpioValueChanged);

m_monitorDevices.insert(monior, device);
m_monitorDevices.insert(monitor, device);

if (device->deviceClassId() == gpioOutputRpiDeviceClassId)
m_raspberryPiGpioMoniors.insert(monior->gpio()->gpioNumber(), monior);
if (device->deviceClassId() == gpioInputRpiDeviceClassId)
m_raspberryPiGpioMoniors.insert(monitor->gpio()->gpioNumber(), monitor);

if (device->deviceClassId() == gpioOutputBbbDeviceClassId)
m_beagleboneBlackGpioMoniors.insert(monior->gpio()->gpioNumber(), monior);
if (device->deviceClassId() == gpioInputBbbDeviceClassId)
m_beagleboneBlackGpioMoniors.insert(monitor->gpio()->gpioNumber(), monitor);

return Device::DeviceSetupStatusSuccess;
}

if (device->deviceClassId() == counterRpiDeviceClassId || device->deviceClassId() == counterBbbDeviceClassId) {

int gpioId = -1;
if (device->deviceClassId() == counterRpiDeviceClassId)
gpioId = device->paramValue(counterRpiDeviceGpioParamTypeId).toInt();

if (device->deviceClassId() == counterBbbDeviceClassId)
gpioId = device->paramValue(counterBbbDeviceGpioParamTypeId).toInt();

GpioMonitor *monitor = new GpioMonitor(gpioId, this);

if (!monitor->enable()) {
qCWarning(dcGpioController()) << "Could not enable gpio monitor for device" << device->name();
return Device::DeviceSetupStatusFailure;
}

connect(monitor, &GpioMonitor::valueChanged, this, &DevicePluginGpio::onGpioValueChanged);

m_monitorDevices.insert(monitor, device);

if (device->deviceClassId() == counterRpiDeviceClassId)
m_raspberryPiGpioMoniors.insert(monitor->gpio()->gpioNumber(), monitor);

if (device->deviceClassId() == counterBbbDeviceClassId)
m_beagleboneBlackGpioMoniors.insert(monitor->gpio()->gpioNumber(), monitor);

m_counterValues.insert(device->id(), 0);
return Device::DeviceSetupStatusSuccess;
}
return Device::DeviceSetupStatusSuccess;
}

Expand Down Expand Up @@ -164,7 +192,6 @@ Device::DeviceError DevicePluginGpio::discoverDevices(const DeviceClassId &devic
break;
}
}

deviceDescriptors.append(descriptor);
}

Expand Down Expand Up @@ -252,6 +279,20 @@ 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());
}

if (myDevices().filterByDeviceClassId(counterRpiDeviceClassId).isEmpty() && myDevices().filterByDeviceClassId(counterBbbDeviceClassId).isEmpty()) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_counterTimer);
m_counterTimer = nullptr;
}
}

Device::DeviceError DevicePluginGpio::executeAction(Device *device, const Action &action)
Expand All @@ -275,9 +316,9 @@ Device::DeviceError DevicePluginGpio::executeAction(Device *device, const Action

// GPIO Switch power action
if (deviceClass.vendorId() == raspberryPiVendorId) {
if (action.actionTypeId() == gpioOutputRpiPowerValueActionTypeId) {
if (action.actionTypeId() == gpioOutputRpiPowerActionTypeId) {
bool success = false;
if (action.param(gpioOutputRpiPowerValueActionPowerValueParamTypeId).value().toBool()) {
if (action.param(gpioOutputRpiPowerActionPowerParamTypeId).value().toBool()) {
success = gpio->setValue(Gpio::ValueHigh);
} else {
success = gpio->setValue(Gpio::ValueLow);
Expand All @@ -289,14 +330,14 @@ Device::DeviceError DevicePluginGpio::executeAction(Device *device, const Action
}

// Set the current state
device->setStateValue(gpioOutputRpiPowerValueStateTypeId, action.param(gpioOutputRpiPowerValueActionPowerValueParamTypeId).value());
device->setStateValue(gpioOutputRpiPowerStateTypeId, action.param(gpioOutputRpiPowerActionPowerParamTypeId).value());

return Device::DeviceErrorNoError;
}
} else if (deviceClass.vendorId() == beagleboneBlackVendorId) {
if (action.actionTypeId() == gpioOutputBbbPowerValueActionTypeId) {
if (action.actionTypeId() == gpioOutputBbbPowerActionTypeId) {
bool success = false;
if (action.param(gpioOutputBbbPowerValueActionPowerValueParamTypeId).value().toBool()) {
if (action.param(gpioOutputBbbPowerActionPowerParamTypeId).value().toBool()) {
success = gpio->setValue(Gpio::ValueHigh);
} else {
success = gpio->setValue(Gpio::ValueLow);
Expand All @@ -308,7 +349,7 @@ Device::DeviceError DevicePluginGpio::executeAction(Device *device, const Action
}

// Set the current state
device->setStateValue(gpioOutputBbbPowerValueStateTypeId, action.param(gpioOutputBbbPowerValueActionPowerValueParamTypeId).value());
device->setStateValue(gpioOutputBbbPowerStateTypeId, action.param(gpioOutputBbbPowerActionPowerParamTypeId).value());

return Device::DeviceErrorNoError;
}
Expand All @@ -326,14 +367,18 @@ void DevicePluginGpio::postSetupDevice(Device *device)

gpio->setValue(Gpio::ValueLow);
if (device->deviceClassId() == gpioOutputRpiDeviceClassId) {
device->setStateValue(gpioOutputRpiPowerValueStateTypeId, false);
device->setStateValue(gpioOutputRpiPowerStateTypeId, false);
}
if (device->deviceClassId() == gpioOutputBbbDeviceClassId) {
device->setStateValue(gpioOutputBbbPowerValueStateTypeId, false);
device->setStateValue(gpioOutputBbbPowerStateTypeId, false);
}
}

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;
Expand All @@ -344,6 +389,26 @@ void DevicePluginGpio::postSetupDevice(Device *device)
device->setStateValue(gpioInputBbbPressedStateTypeId, monitor->value());
}
}

if (device->deviceClassId() == counterRpiDeviceClassId || device->deviceClassId() == counterBbbDeviceClassId) {
if (!m_counterTimer) {
m_counterTimer = hardwareManager()->pluginTimerManager()->registerTimer(1);
connect(m_counterTimer, &PluginTimer::timeout, this, [this] (){

foreach (Device *device, myDevices()) {
if (device->deviceClassId() == counterRpiDeviceClassId) {
int counterValue = m_counterValues.value(device->id());
device->setStateValue(counterRpiCounterStateTypeId, counterValue);
m_counterValues[device->id()] = 0;
}
if (device->deviceClassId() == counterBbbDeviceClassId) {
int counterValue = m_counterValues.value(device->id());
device->setStateValue(counterBbbCounterStateTypeId, counterValue);
}
}
});
}
}
}

QList<GpioDescriptor> DevicePluginGpio::raspberryPiGpioDescriptors()
Expand Down Expand Up @@ -463,8 +528,59 @@ 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 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;
}
}
}


void DevicePluginGpio::onLongPressedTimeout()
{
QTimer *timer = static_cast<QTimer *>(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()));
}
}

10 changes: 8 additions & 2 deletions gpio/deviceplugingpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "gpiodescriptor.h"
#include "hardware/gpiomonitor.h"
#include "devices/deviceplugin.h"
#include "plugintimer.h"

#include <QTimer>

class DevicePluginGpio : public DevicePlugin
{
Expand All @@ -44,7 +47,7 @@ class DevicePluginGpio : public DevicePlugin
void deviceRemoved(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override;

void postSetupDevice(Device *device);
void postSetupDevice(Device *device) override;

private:
QHash<Gpio *, Device *> m_gpioDevices;
Expand All @@ -58,10 +61,13 @@ class DevicePluginGpio : public DevicePlugin

QList<GpioDescriptor> raspberryPiGpioDescriptors();
QList<GpioDescriptor> beagleboneBlackGpioDescriptors();
PluginTimer *m_counterTimer = nullptr;
QHash<DeviceId, int> m_counterValues;
QHash<Device *, QTimer *> m_longPressTimers;

private slots:
void onGpioValueChanged(const bool &value);

void onLongPressedTimeout();
};

#endif // DEVICEPLUGINGPIO_H
Loading