Threading and more

- webui remove restarts
- threading for LedDevice
This commit is contained in:
Paulchen-Panther
2019-01-01 19:47:07 +01:00
parent 7b6df922ea
commit 7352ff4d42
24 changed files with 482 additions and 282 deletions

View File

@@ -34,11 +34,8 @@ class LedDevice : public QObject
Q_OBJECT
public:
LedDevice();
///
/// Empty virtual destructor for pure virtual base class
///
virtual ~LedDevice() {}
LedDevice(const QJsonObject& config = QJsonObject(), QObject* parent = nullptr);
virtual ~LedDevice();
/// Switch the leds off (led hardware disable)
virtual int switchOff();
@@ -48,33 +45,38 @@ public:
virtual int setLedValues(const std::vector<ColorRgb>& ledValues);
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
virtual int open();
///
/// @brief Get color order of device
/// @return The color order
///
const QString & getColorOrder() { return _colorOrder; };
static int addToDeviceMap(QString name, LedDeviceCreateFuncType funcPtr);
static const LedDeviceRegistry& getDeviceMap();
void setActiveDevice(QString dev);
const QString & getActiveDevice() { return _activeDevice; };
static QJsonObject getLedDeviceSchemas();
void setLedCount(int ledCount);
int getLedCount() { return _ledCount; }
void setEnable(bool enable);
bool enabled() { return _enabled; };
int getLatchTime() { return _latchTime_ms; };
const int getLatchTime() { return _latchTime_ms; };
inline bool componentState() { return enabled(); };
public slots:
///
/// Is called on thread start, all construction tasks and init should run here
///
virtual void start() { _deviceReady = open(); };
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
signals:
///
/// Emits whenever the led device switches between on/off
@@ -83,16 +85,18 @@ signals:
void enableStateChanged(bool newState);
protected:
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
virtual bool init(const QJsonObject &deviceConfig);
///
/// Opens and configures the output device
///
/// @return Zero on succes else negative
///
virtual int open();
// Helper to pipe device config from constructor to start()
QJsonObject _devConfig;
/// The common Logger instance for all LedDevices
Logger * _log;
@@ -102,7 +106,6 @@ protected:
bool _deviceReady;
QString _activeDevice;
static LedDeviceRegistry _ledDeviceMap;
int _ledCount;
int _ledRGBCount;

View File

@@ -0,0 +1,111 @@
#pragma once
// util
#include <utils/Logger.h>
#include <utils/ColorRgb.h>
#include <utils/Components.h>
class LedDevice;
class Hyperion;
typedef LedDevice* ( *LedDeviceCreateFuncType ) ( const QJsonObject& );
typedef std::map<QString,LedDeviceCreateFuncType> LedDeviceRegistry;
///
/// @brief Creates and destroys LedDevice instances with LedDeviceFactory and moves the device to a thread. Pipes all signal/slots and methods to LedDevice instance
///
class LedDeviceWrapper : public QObject
{
Q_OBJECT
public:
LedDeviceWrapper(Hyperion* hyperion);
~LedDeviceWrapper();
///
/// @brief Contructs a new LedDevice, moves to thread and starts
/// @param config With the given config
///
void createLedDevice(const QJsonObject& config);
///
/// @brief Get all available device schemas
/// @return device schemas
///
static const QJsonObject getLedDeviceSchemas();
///
/// @brief add all device constrcutors to the map
///
static int addToDeviceMap(QString name, LedDeviceCreateFuncType funcPtr);
///
/// @brief Return all available device contructors
/// @return device constrcutors
///
static const LedDeviceRegistry& getDeviceMap();
///
/// @brief Get the current latchtime of the ledDevice
/// @ return latchtime in ms
///
int getLatchTime();
///
/// @brief Get the current active ledDevice
///
const QString & getActiveDevice();
///
/// @brief Return the last enable state
///
const bool & enabled() { return _enabled; };
///
/// @brief Get the current colorOrder from device
///
const QString & getColorOrder();
public slots:
///
/// @brief Handle new component state request
/// @apram component The comp from enum
/// @param state The new state
///
void handleComponentState(const hyperion::Components component, const bool state);
signals:
///
/// PIPER signal for Hyperion -> LedDevice
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
int write(const std::vector<ColorRgb>& ledValues);
private slots:
///
/// @brief Is called whenever the led device switches between on/off. The led device can disable it's component state
/// The signal comes from the LedDevice
/// @param newState The new state of the device
///
void handleInternalEnableState(bool newState);
protected:
/// contains all available led device constrcutors
static LedDeviceRegistry _ledDeviceMap;
private:
///
/// @brief switchOff() the device and Stops the device thread
///
void stopDeviceThread();
private:
// parent Hyperion
Hyperion* _hyperion;
// Pointer of current led device
LedDevice* _ledDevice;
// the enable state
bool _enabled;
};