added counter device

This commit is contained in:
nymea 2019-09-11 16:51:37 +02:00
parent 7123a92a01
commit b995c9f9bb
3 changed files with 187 additions and 20 deletions

View File

@ -27,7 +27,6 @@
DevicePluginGpio::DevicePluginGpio()
{
}
Device::DeviceSetupStatus DevicePluginGpio::setupDevice(Device *device)
@ -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;
}
@ -252,6 +280,14 @@ void DevicePluginGpio::deviceRemoved(Device *device)
delete monitor;
}
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)
@ -275,9 +311,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);
@ -289,14 +325,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);
@ -308,7 +344,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;
}
@ -326,10 +362,10 @@ 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);
}
}
@ -344,6 +380,28 @@ 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()
@ -463,8 +521,14 @@ void DevicePluginGpio::onGpioValueChanged(const bool &value)
if (device->deviceClassId() == gpioInputRpiDeviceClassId) {
device->setStateValue(gpioInputRpiPressedStateTypeId, value);
//start longpresss timer
} else if (device->deviceClassId() == gpioInputBbbDeviceClassId) {
device->setStateValue(gpioInputBbbPressedStateTypeId, value);
//start longpress timer
} else if (device->deviceClassId() == counterRpiDeviceClassId || device->deviceClassId() == counterBbbDeviceClassId) {
if (value) {
m_counterValues[device->id()] += 1;
}
}
}

View File

@ -28,6 +28,7 @@
#include "gpiodescriptor.h"
#include "hardware/gpiomonitor.h"
#include "devices/deviceplugin.h"
#include "plugintimer.h"
class DevicePluginGpio : public DevicePlugin
{
@ -44,7 +45,7 @@ public:
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;
@ -58,6 +59,8 @@ private:
QList<GpioDescriptor> raspberryPiGpioDescriptors();
QList<GpioDescriptor> beagleboneBlackGpioDescriptors();
PluginTimer *m_counterTimer = nullptr;
QHash<DeviceId, int> m_counterValues;
private slots:
void onGpioValueChanged(const bool &value);

View File

@ -13,6 +13,7 @@
"displayName": "GPIO Output",
"name": "gpioOutputRpi",
"createMethods": ["discovery"],
"interfaces": ["power"],
"paramTypes": [
{
"id": "9eda783f-6d9f-4d39-986d-d2cbfff5a7dd",
@ -39,7 +40,7 @@
"stateTypes": [
{
"id": "06843766-358e-44b0-8d52-2b46ef98459a",
"name": "powerValue",
"name": "power",
"displayName": "Power",
"type": "bool",
"defaultValue": false,
@ -54,6 +55,7 @@
"displayName": "GPIO Input",
"name": "gpioInputRpi",
"createMethods": ["discovery"],
"interfaces": ["longpressbutton"],
"paramTypes": [
{
"id": "b45ca4a8-c67a-411c-957c-0e78e1f12c0b",
@ -86,6 +88,54 @@
"defaultValue": false,
"displayNameEvent": "Pressed changed"
}
],
"eventTypes": [
{
"id": "0df945d3-38df-4560-b42a-12b05545904d",
"name": "longPressed",
"displayName": "Long pressed"
}
]
},
{
"id": "75b13371-a064-47a7-bb82-e9d93a5b5027",
"displayName": "Counter",
"name": "counterRpi",
"createMethods": ["discovery"],
"interfaces": [],
"paramTypes": [
{
"id": "a6feb722-1dc9-4262-96b0-96489507508f",
"name": "gpio",
"displayName": "GPIO",
"type": "int",
"defaultValue": -1
},
{
"id": "b2c194bd-1aef-4851-a290-dd45269cc592",
"name": "pin",
"displayName": "Pin number",
"type": "int",
"defaultValue": -1
},
{
"id": "f7b82516-ed2c-4d73-86fa-957b8b6737e4",
"name": "description",
"displayName": "Description",
"type": "QString",
"defaultValue": "-"
}
],
"stateTypes": [
{
"id": "891bc1ce-2f9b-4518-aed9-90e78bc2409e",
"name": "counter",
"displayName": "Counter",
"type": "int",
"defaultValue": 0,
"unit": "Herz",
"displayNameEvent": "Counter changed"
}
]
}
]
@ -100,6 +150,7 @@
"displayName": "GPIO Output",
"name": "gpioOutputBbb",
"createMethods": ["discovery"],
"interfaces": ["power"],
"paramTypes": [
{
"id": "62a9596d-fc7d-4554-9f45-9803635da619",
@ -126,7 +177,7 @@
"stateTypes": [
{
"id": "82b567c6-a33c-484e-b5e7-e04795498d00",
"name": "powerValue",
"name": "power",
"displayName": "Power",
"type": "bool",
"defaultValue": false,
@ -141,6 +192,7 @@
"displayName": "GPIO Input",
"name": "gpioInputBbb",
"createMethods": ["discovery"],
"interfaces": ["longpressbutton"],
"paramTypes": [
{
"id": "20773255-4576-4c8e-8c8b-051902919761",
@ -173,6 +225,54 @@
"defaultValue": false,
"displayNameEvent": "Pressed changed"
}
],
"eventTypes": [
{
"id": "6b439e89-2cac-482a-b012-452c7c665acb",
"name": "longPressed",
"displayName": "Long pressed"
}
]
},
{
"id": "3e311ef1-60c4-4b0e-a2fb-186bff9bd792",
"displayName": "Counter",
"name": "counterBbb",
"createMethods": ["discovery"],
"interfaces": [],
"paramTypes": [
{
"id": "68bc0f3b-18c3-4a60-a2df-85bc0605caec",
"name": "gpio",
"displayName": "GPIO",
"type": "int",
"defaultValue": -1
},
{
"id": "f9da4a22-b010-4823-9b1c-d1f422c3ad2b",
"name": "pin",
"displayName": "Pin number",
"type": "int",
"defaultValue": -1
},
{
"id": "cba6a527-9f5c-4c05-8602-60e0c920fd26",
"name": "description",
"displayName": "Description",
"type": "QString",
"defaultValue": "-"
}
],
"stateTypes": [
{
"id": "fb5181d0-644b-4ab7-afa0-b7ddc8951526",
"name": "counter",
"displayName": "Counter",
"type": "int",
"defaultValue": 0,
"unit": "Herz",
"displayNameEvent": "Counter changed"
}
]
}
]