hyperion.ng/include/hyperion/Hyperion.h

572 lines
18 KiB
C
Raw Normal View History

#pragma once
// stl includes
#include <list>
#include <QMap>
// QT includes
//#include <QObject>
#include <QString>
#include <QStringList>
#include <QSize>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QFileSystemWatcher>
// hyperion-utils includes
#include <utils/Image.h>
#include <utils/ColorRgb.h>
#include <utils/Logger.h>
#include <utils/Components.h>
#include <utils/VideoMode.h>
// Hyperion includes
#include <hyperion/LedString.h>
#include <hyperion/PriorityMuxer.h>
#include <hyperion/ColorAdjustment.h>
#include <hyperion/ComponentRegister.h>
// Effect engine includes
#include <effectengine/EffectDefinition.h>
#include <effectengine/ActiveEffectDefinition.h>
#include <effectengine/EffectSchema.h>
2018-12-27 23:11:32 +01:00
// settings utils
#include <utils/settings.h>
// Forward class declaration
2018-12-27 23:11:32 +01:00
class QTimer;
class HyperionDaemon;
class ImageProcessor;
class MessageForwarder;
class LinearColorSmoothing;
class EffectEngine;
class MultiColorAdjustment;
2018-12-27 23:11:32 +01:00
class ColorAdjustment;
class SettingsManager;
class BGEffectHandler;
class CaptureCont;
class BoblightServer;
class LedDeviceWrapper;
2013-09-06 21:26:58 +02:00
///
/// The main class of Hyperion. This gives other 'users' access to the attached LedDevice through
/// the priority muxer.
///
class Hyperion : public QObject
{
Q_OBJECT
public:
2013-09-06 21:26:58 +02:00
/// Type definition of the info structure used by the priority muxer
typedef PriorityMuxer::InputInfo InputInfo;
2013-09-06 21:26:58 +02:00
///
/// RGB-Color channel enumeration
///
enum RgbChannel
{
BLACK, WHITE, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, INVALID
};
2013-09-06 21:26:58 +02:00
///
/// Destructor; cleans up resources
2013-09-06 21:26:58 +02:00
///
~Hyperion();
///
/// free all alocated objects, should be called only from constructor or before restarting hyperion
///
void freeObjects(bool emitCloseSignal=false);
///
/// @brief creates a new Hyperion instance, usually called from the Hyperion Daemon
2018-12-27 23:11:32 +01:00
/// @param[in] daemon The Hyperion daemon parent
/// @param[in] instance The instance id
/// @param[in] rootPath Root path of all hyperion userdata
/// @return Hyperion instance pointer
///
2018-12-27 23:11:32 +01:00
static Hyperion* initInstance(HyperionDaemon* daemon, const quint8& instance, const QString configFile, const QString rootPath);
///
/// @brief Get a pointer of this Hyperion instance
2018-12-27 23:11:32 +01:00
/// @return Hyperion instance pointer
///
static Hyperion* getInstance();
2018-12-27 23:11:32 +01:00
///
/// @brief Get a pointer to the effect engine
/// @return EffectEngine instance pointer
///
EffectEngine* getEffectEngineInstance() { return _effectEngine; };
///
/// @brief Get a pointer to the priorityMuxer instance
/// @return PriorityMuxer instance pointer
///
PriorityMuxer* getMuxerInstance() { return &_muxer; };
ImageProcessor* getImageProcessor() { return _imageProcessor; };
2018-12-27 23:11:32 +01:00
///
/// @brief Get a setting by settings::type from SettingsManager
/// @param type The settingsType from enum
/// @return Data Document
///
QJsonDocument getSetting(const settings::type& type);
///
/// @brief Save a complete json config
/// @param config The entire config object
/// @param correct If true will correct json against schema before save
/// @return True on success else false
///
bool saveSettings(QJsonObject config, const bool& correct = false);
2013-09-06 21:26:58 +02:00
///
/// Returns the number of attached leds
///
2013-08-14 10:54:49 +02:00
unsigned getLedCount() const;
2018-12-27 23:11:32 +01:00
///
/// @brief Return the size of led grid
///
QSize getLedGridSize() const { return _ledGridSize; };
///
/// Returns the current priority
///
/// @return The current priority
///
int getCurrentPriority() const;
///
/// Returns true if current priority is given priority
///
/// @return bool
///
bool isCurrentPriority(const int priority) const;
///
/// Returns a list of all registered priorities
///
/// @return The list with priorities
///
QList<int> getActivePriorities() const;
///
/// Returns the information of a specific priorrity channel
///
/// @param[in] priority The priority channel
///
2018-12-27 23:11:32 +01:00
/// @return The information of the given, a not found priority will return lowest priority as fallback
///
2018-12-27 23:11:32 +01:00
const InputInfo getPriorityInfo(const int priority) const;
2018-12-31 15:48:29 +01:00
///
/// @brief Save an effect
/// @param obj The effect args
/// @param[out] resultMsg The feedback message
/// @return True on success else false
///
bool saveEffect(const QJsonObject& obj, QString& resultMsg);
2018-12-31 15:48:29 +01:00
///
/// @brief Delete an effect by name.
/// @param[in] effectName The effect name to delete
/// @param[out] resultMsg The message on error
/// @return True on success else false
///
bool deleteEffect(const QString& effectName, QString& resultMsg);
/// Get the list of available effects
/// @return The list of available effects
const std::list<EffectDefinition> &getEffects() const;
/// Get the list of active effects
/// @return The list of active effects
const std::list<ActiveEffectDefinition> &getActiveEffects();
/// Get the list of available effect schema files
/// @return The list of available effect schema files
const std::list<EffectSchema> &getEffectSchemas();
2018-12-27 23:11:32 +01:00
/// gets the current json config object from SettingsManager
/// @return json config
2018-12-27 23:11:32 +01:00
const QJsonObject& getQJsonConfig();
/// get path+filename of configfile
/// @return the current config path+filename
QString getConfigFilePath() { return _configFile; };
/// get filename of configfile
/// @return the current config filename
2018-12-27 23:11:32 +01:00
QString getConfigFileName() const;
2018-12-27 23:11:32 +01:00
///
/// @brief Register a new input by priority, the priority is not active (timeout -100 isn't muxer recognized) until you start to update the data with setInput()
/// A repeated call to update the base data of a known priority won't overwrite their current timeout
/// @param[in] priority The priority of the channel
/// @param[in] component The component of the channel
/// @param[in] origin Who set the channel (CustomString@IP)
2019-01-27 13:41:21 +01:00
/// @param[in] owner Specific owner string, might be empty
2018-12-27 23:11:32 +01:00
/// @param[in] smooth_cfg The smooth id to use
///
void registerInput(const int priority, const hyperion::Components& component, const QString& origin = "System", const QString& owner = "", unsigned smooth_cfg = 0);
/// enable/disable automatic/priorized source selection
/// @param enabled the state
void setSourceAutoSelectEnabled(bool enabled);
/// set current input source to visible
/// @param priority the priority channel which should be vidible
/// @return true if success, false on error
bool setCurrentSourcePriority(int priority );
/// gets current state of automatic/priorized source selection
/// @return the state
2018-12-27 23:11:32 +01:00
bool sourceAutoSelectEnabled();
///
/// @brief Called from components to update their current state. DO NOT CALL FROM USERS
/// @param[in] component The component from enum
/// @param[in] state The state of the component [true | false]
///
void setNewComponentState(const hyperion::Components& component, const bool& state);
///
2018-12-27 23:11:32 +01:00
/// @brief Enable/Disable components during runtime, called from external API (requests)
///
/// @param component The component from enum
/// @param state The state of the component [true | false]
///
void setComponentState(const hyperion::Components component, const bool state);
ComponentRegister& getComponentRegister() { return _componentRegister; };
bool configModified() { return _configMod; };
bool configWriteable() { return _configWrite; };
/// gets the methode how image is maped to leds
2018-12-27 23:11:32 +01:00
const int & getLedMappingType();
/// get the root path for all hyperion user data files
2018-12-27 23:11:32 +01:00
const QString &getRootPath() { return _rootPath; };
2018-12-27 23:11:32 +01:00
/// get unique id per instance
const QString &getId(){ return _id; };
/// set unique id
void setId(QString id){ _id = id; };
int getLatchTime() const;
/// forward smoothing config
unsigned addSmoothingConfig(int settlingTime_ms, double ledUpdateFrequency_hz=25.0, unsigned updateDelay=0);
2018-12-27 23:11:32 +01:00
const VideoMode & getCurrentVideoMode();
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// @brief Get the current active led device
/// @return The device nam
/// e
const QString & getActiveDevice();
public slots:
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// @brief Update the current color of a priority (prev registered with registerInput())
/// DO NOT use this together with setInputImage() at the same time!
/// @param priority The priority to update
/// @param ledColors The colors
/// @param timeout_ms The new timeout (defaults to -1 endless)
/// @param clearEffect Should be true when NOT called from an effect
/// @return True on success, false when priority is not found
2013-09-06 21:26:58 +02:00
///
bool setInput(const int priority, const std::vector<ColorRgb>& ledColors, const int timeout_ms = -1, const bool& clearEffect = true);
2013-08-18 13:33:56 +02:00
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// @brief Update the current image of a priority (prev registered with registerInput())
/// DO NOT use this together with setInput() at the same time!
/// @param priority The priority to update
/// @param image The new image
/// @param timeout_ms The new timeout (defaults to -1 endless)
/// @param clearEffect Should be true when NOT called from an effect
/// @return True on success, false when priority is not found
2013-09-06 21:26:58 +02:00
///
bool setInputImage(const int priority, const Image<ColorRgb>& image, int64_t timeout_ms = -1, const bool& clearEffect = true);
2013-08-18 13:33:56 +02:00
///
/// @brief Set the given priority to inactive
/// @param priority The priority
/// @return True on success false if not found
///
bool setInputInactive(const quint8& priority);
///
2018-12-27 23:11:32 +01:00
/// Writes a single color to all the leds for the given time and priority
/// Registers comp color or provided type against muxer
/// Should be never used to update leds continuous
///
2018-12-27 23:11:32 +01:00
/// @param[in] priority The priority of the written color
/// @param[in] ledColor The color to write to the leds
/// @param[in] origin The setter
/// @param[in] timeout_ms The time the leds are set to the given color [ms]
///
2018-12-27 23:11:32 +01:00
void setColor(int priority, const ColorRgb &ledColor, const int timeout_ms = -1, const QString& origin = "System" ,bool clearEffects = true);
///
/// Returns the list with unique adjustment identifiers
/// @return The list with adjustment identifiers
///
const QStringList & getAdjustmentIds() const;
///
/// Returns the ColorAdjustment with the given identifier
/// @return The adjustment with the given identifier (or nullptr if the identifier does not exist)
///
ColorAdjustment * getAdjustment(const QString& id);
/// Tell Hyperion that the corrections have changed and the leds need to be updated
void adjustmentsUpdated();
2013-09-06 21:26:58 +02:00
///
/// Clears the given priority channel. This will switch the led-colors to the colors of the next
/// lower priority channel (or off if no more channels are set)
///
/// @param[in] priority The priority channel
2018-12-27 23:11:32 +01:00
/// @return True on success else false (not found)
2013-09-06 21:26:58 +02:00
///
bool clear(int priority);
2013-08-18 13:33:56 +02:00
2013-09-06 21:26:58 +02:00
///
/// @brief Clears all priority channels. This will switch the leds off until a new priority is written.
2013-09-06 21:26:58 +02:00
///
void clearall(bool forceClearAll=false);
/// Run the specified effect on the given priority channel and optionally specify a timeout
/// @param effectName Name of the effec to run
/// @param priority The priority channel of the effect
/// @param timeout The timeout of the effect (after the timout, the effect will be cleared)
int setEffect(const QString & effectName, int priority, int timeout = -1, const QString & origin="System");
/// Run the specified effect on the given priority channel and optionally specify a timeout
/// @param effectName Name of the effec to run
/// @param args arguments of the effect script
/// @param priority The priority channel of the effect
/// @param timeout The timeout of the effect (after the timout, the effect will be cleared)
int setEffect(const QString &effectName
, const QJsonObject &args
, int priority
, int timeout = -1
, const QString &pythonScript = ""
, const QString &origin="System"
, const QString &imageData = ""
);
2018-12-27 23:11:32 +01:00
/// sets the methode how image is maped to leds at ImageProcessor
void setLedMappingType(const int& mappingType);
///
/// Set the video mode (2D/3D)
/// @param[in] mode The new video mode
///
2018-12-27 23:11:32 +01:00
void setVideoMode(const VideoMode& mode);
public:
static Hyperion *_hyperion;
signals:
/// Signal which is emitted when a priority channel is actively cleared
/// This signal will not be emitted when a priority channel time out
void channelCleared(int priority);
/// Signal which is emitted when all priority channels are actively cleared
/// This signal will not be emitted when a priority channel time out
void allChannelsCleared();
2018-12-27 23:11:32 +01:00
///
/// @brief Emits whenever a user request a component state change, it's up the component to listen
/// and update the component state at the componentRegister
/// @param component The component from enum
/// @param enabled The new state of the component
///
void componentStateChanged(const hyperion::Components component, bool enabled);
2018-12-27 23:11:32 +01:00
///
/// @brief Emits whenever the imageToLedsMapping has changed
/// @param mappingType The new mapping type
///
void imageToLedsMappingChanged(const int& mappingType);
///
/// @brief Emits whenever the visible priority delivers a image which is applied in update()
/// priorities with ledColors won't emit this signal
/// @param image The current image
///
void currentImage(const Image<ColorRgb> & image);
void closing();
/// Signal which is emitted, when a new json message should be forwarded
void forwardJsonMessage(QJsonObject);
/// Signal which is emitted, when a new proto image should be forwarded
void forwardProtoMessage(const QString, const Image<ColorRgb>);
2018-12-27 23:11:32 +01:00
///
/// @brief Is emitted from clients who request a videoMode change
///
void videoMode(const VideoMode& mode);
///
/// @brief A new videoMode was requested (called from Daemon!)
///
void newVideoMode(const VideoMode& mode);
///
/// @brief Emits whenever a config part changed. SIGNAL PIPE helper for SettingsManager -> HyperionDaemon
/// @param type The settings type from enum
/// @param data The data as QJsonDocument
///
void settingsChanged(const settings::type& type, const QJsonDocument& data);
2018-12-27 23:11:32 +01:00
///
/// @brief Emits whenever the adjustments have been updated
///
void adjustmentChanged();
2018-12-20 15:01:46 +01:00
///
2018-12-27 23:11:32 +01:00
/// @brief Signal pipe from EffectEngine to external, emits when effect list has been updated
2018-12-20 15:01:46 +01:00
///
2018-12-27 23:11:32 +01:00
void effectListUpdated();
///
/// @brief Emits whenever new data should be pushed to the LedDeviceWrapper which forwards it to the threaded LedDevice
2018-12-27 23:11:32 +01:00
///
void ledDeviceData(const std::vector<ColorRgb>& ledValues);
2018-12-20 15:01:46 +01:00
2018-12-31 15:48:29 +01:00
///
/// @brief Emits whenever new untransformed ledColos data is available, reflects the current visible device
///
void rawLedColors(const std::vector<ColorRgb>& ledValues);
private slots:
2013-09-06 21:26:58 +02:00
///
/// Updates the priority muxer with the current time and (re)writes the led color with applied
/// transforms.
///
void update();
/// check for configWriteable and modified changes, called by _fsWatcher or fallback _cTimer
void checkConfigState(QString cfile = NULL);
2018-12-27 23:11:32 +01:00
///
/// @brief Apply ComponentRegister emits for COMP_ALL. Enables/Disables core timers
/// @param comp The component
/// @param state The new state of the component
///
void updatedComponentState(const hyperion::Components comp, const bool state);
///
/// @brief Apply settings updates for LEDS and COLOR
/// @param type The type from enum
/// @param config The configuration
///
void handleSettingsUpdate(const settings::type& type, const QJsonDocument& config);
private:
///
/// Constructs the Hyperion instance based on the given Json configuration
///
JsonCpp to QTJson (Final Part) (#273) * Update CMakeLists.txt * Update Hyperion.h * Update LedDevice.h * Update LedDeviceFactory.h * Update Hyperion.cpp * Update LedString.cpp * Update JsonClientConnection.cpp * Update LedDeviceAdalight.cpp * Update LedDeviceAdalight.h * Update LedDeviceAPA102.cpp * Update LedDeviceAdalightApa102.h * Update LedDeviceAdalightApa102.cpp * Update LedDeviceAPA102.h * Update LedDeviceAtmo.cpp * Update LedDeviceAtmo.h * Update LedDeviceAtmoOrb.cpp * Update LedDeviceAtmoOrb.h * Update LedDeviceDMX.cpp * Update LedDeviceDMX.h * Update LedDeviceFactory.cpp * Update LedDeviceFadeCandy.cpp * Update LedDeviceFadeCandy.h * Update LedDeviceFile.cpp * Update LedDeviceFile.h * Update LedDeviceHyperionUsbasp.cpp * Update LedDeviceHyperionUsbasp.h * Update LedDeviceLightpack.cpp * Update LedDeviceLightpack.h * Update LedDeviceLpd6803.cpp * Update LedDeviceLpd6803.h * Update LedDeviceLpd8806.cpp * Update LedDeviceLpd8806.h * Update LedDeviceMultiLightpack.cpp * Update LedDeviceMultiLightpack.h * Update LedDeviceP9813.cpp * Update LedDeviceP9813.h * Update LedDevicePaintpack.cpp * Update LedDevicePaintpack.h * Update LedDevicePhilipsHue.cpp * Update LedDevicePhilipsHue.h * Update LedDevicePiBlaster.cpp * Update LedDevicePiBlaster.h * Update LedDeviceRawHID.cpp * Update LedDeviceRawHID.h * Update LedDeviceSedu.cpp * Update LedDeviceSedu.h * Update LedDeviceSk6812SPI.cpp * Update LedDeviceSk6812SPI.h * Update LedDeviceTinkerforge.cpp * Update LedDeviceTinkerforge.h * Update LedDeviceTpm2.cpp * Update LedDeviceTpm2.h * Update LedDeviceTpm2net.cpp * Update LedDeviceTpm2net.h * Update LedDeviceUdpE131.cpp * Update LedDeviceUdpE131.h * Update LedDeviceUdpH801.cpp * Update LedDeviceUdpH801.h * Update LedDeviceUdpRaw.cpp * Update LedDeviceUdpRaw.h * Update LedDeviceWs2801.cpp * Update LedDeviceWs2801.h * Update LedDeviceWS2812b.cpp * Update LedDeviceWS2812b.h * Update LedDeviceWs2812SPI.cpp * Update LedDeviceWs2812SPI.h * Update LedDeviceWS281x.cpp * Update LedDeviceWS281x.h * Update ProviderHID.cpp * Update ProviderHID.h * Update ProviderRs232.cpp * Update ProviderRs232.h * Update ProviderSpi.cpp * Update ProviderSpi.h * Update ProviderUdp.cpp * Update ProviderUdp.h * Update LedDevice.cpp * Update CMakeLists.txt * Update hyperiond.cpp * Update hyperiond.h * Update TestSpi.cpp * Delete AUTHORS * Delete CMakeLists.txt * Delete LICENSE * Delete json_batchallocator.h * Delete json_internalarray.inl * Delete json_internalmap.inl * Delete json_reader.cpp * Delete json_tool.h * Delete json_value.cpp * Delete json_valueiterator.inl * Delete json_writer.cpp * Delete sconscript * Delete autolink.h * Delete config.h * Delete features.h * Delete forwards.h * Delete json.h * Delete reader.h * Delete value.h * Delete writer.h
2016-10-13 21:59:58 +02:00
/// @param[in] qjsonConfig The Json configuration
///
2018-12-27 23:11:32 +01:00
Hyperion(HyperionDaemon* daemon, const quint8& instance, const QString configFile, const QString rootPath);
/// The parent Hyperion Daemon
HyperionDaemon* _daemon;
/// Settings manager of this instance
SettingsManager* _settingsManager;
/// Register that holds component states
ComponentRegister _componentRegister;
2013-09-06 21:26:58 +02:00
/// The specifiation of the led frame construction and picture integration
2013-08-18 13:33:56 +02:00
LedString _ledString;
/// specifiation of cloned leds
LedString _ledStringClone;
2018-12-27 23:11:32 +01:00
/// Image Processor
ImageProcessor* _imageProcessor;
std::vector<ColorOrder> _ledStringColorOrder;
2013-09-06 21:26:58 +02:00
/// The priority muxer
2013-08-18 13:33:56 +02:00
PriorityMuxer _muxer;
/// The adjustment from raw colors to led colors
MultiColorAdjustment * _raw2ledAdjustment;
/// The actual LedDeviceWrapper
LedDeviceWrapper* _ledDeviceWrapper;
/// The smoothing LedDevice
LinearColorSmoothing * _deviceSmooth;
/// Effect engine
EffectEngine * _effectEngine;
// Message forwarder
MessageForwarder * _messageForwarder;
/// the name of config file
QString _configFile;
/// root path for all hyperion user data files
QString _rootPath;
2018-12-27 23:11:32 +01:00
/// unique id per instance
QString _id;
/// Logger instance
Logger * _log;
/// count of hardware leds
unsigned _hwLedCount;
QByteArray _configHash;
QSize _ledGridSize;
2018-12-27 23:11:32 +01:00
/// Store the previous compID for smarter update()
hyperion::Components _prevCompId;
/// Observe filesystem changes (_configFile), if failed use Timer
QFileSystemWatcher _fsWatcher;
2018-12-27 23:11:32 +01:00
QTimer* _cTimer;
/// holds the prev states of configWriteable and modified
bool _prevConfigMod = false;
bool _prevConfigWrite = true;
/// holds the current states of configWriteable and modified
bool _configMod = false;
bool _configWrite = true;
2018-12-27 23:11:32 +01:00
/// Background effect instance, kept active to react on setting changes
BGEffectHandler* _BGEffectHandler;
/// Capture control for Daemon native capture
CaptureCont* _captureCont;
2018-12-27 23:11:32 +01:00
// lock Hyperion::update() for exec
bool _lockUpdate = false;
/// buffer for leds (with adjustment)
std::vector<ColorRgb> _ledBuffer;
/// Boblight instance
BoblightServer* _boblightServer;
};