2013-08-13 11:10:45 +02:00
# pragma once
// STL includes
# include <vector>
# include <cstdint>
// QT includes
# include <QMap>
2017-06-17 23:29:04 +02:00
# include <QObject>
2017-09-01 08:50:37 +02:00
# include <QMap>
# include <QVector>
2013-08-13 11:10:45 +02:00
// Utils includes
2013-11-11 10:00:37 +01:00
# include <utils/ColorRgb.h>
2018-12-27 23:11:32 +01:00
# include <utils/Image.h>
2016-10-10 18:29:54 +02:00
# include <utils/Components.h>
2013-08-13 11:10:45 +02:00
2017-08-04 12:01:45 +02:00
// global defines
# define SMOOTHING_MODE_DEFAULT 0
# define SMOOTHING_MODE_PAUSE 1
2018-12-27 23:11:32 +01:00
class QTimer ;
class Logger ;
2017-08-04 12:01:45 +02:00
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// The PriorityMuxer handles the priority channels. Led values input/ images are written to the priority map
2013-09-06 21:26:58 +02:00
/// and the muxer keeps track of all active priorities. The current priority can be queried and per
2018-12-27 23:11:32 +01:00
/// priority the led colors. Handles also manual/auto selection mode, provides a lot of signals to hook into priority related events
2013-09-06 21:26:58 +02:00
///
2017-06-17 23:29:04 +02:00
class PriorityMuxer : public QObject
2013-08-13 11:10:45 +02:00
{
2017-06-17 23:29:04 +02:00
Q_OBJECT
2013-08-13 11:10:45 +02:00
public :
2013-09-06 21:26:58 +02:00
///
/// The information structure for a single priority channel
///
2013-08-13 11:10:45 +02:00
struct InputInfo
{
2013-09-06 21:26:58 +02:00
/// The priority of this channel
2013-08-13 11:10:45 +02:00
int priority ;
2013-09-06 21:26:58 +02:00
/// The absolute timeout of the channel
2013-08-13 11:10:45 +02:00
int64_t timeoutTime_ms ;
2013-09-06 21:26:58 +02:00
/// The colors for each led of the channel
2013-11-11 10:00:37 +01:00
std : : vector < ColorRgb > ledColors ;
2018-12-27 23:11:32 +01:00
/// The raw Image (size should be preprocessed!)
Image < ColorRgb > image ;
2017-02-28 17:53:41 +01:00
/// The component
2016-10-10 18:29:54 +02:00
hyperion : : Components componentId ;
2017-02-28 17:53:41 +01:00
/// Who set it
QString origin ;
2018-12-27 23:11:32 +01:00
/// id of smoothing config
2017-08-04 12:01:45 +02:00
unsigned smooth_cfg ;
2018-12-27 23:11:32 +01:00
/// specific owner description
QString owner ;
2013-08-13 11:10:45 +02:00
} ;
2016-08-06 08:28:42 +02:00
/// The lowest possible priority, which is used when no priority channels are active
2017-09-01 08:50:37 +02:00
const static int LOWEST_PRIORITY ;
2016-08-06 08:28:42 +02:00
2013-09-06 21:26:58 +02:00
///
/// Constructs the PriorityMuxer for the given number of leds (used to switch to black when
/// there are no priority channels
///
/// @param ledCount The number of leds
///
2013-08-18 13:33:56 +02:00
PriorityMuxer ( int ledCount ) ;
2013-08-13 11:10:45 +02:00
2013-09-06 21:26:58 +02:00
///
/// Destructor
///
2013-08-13 11:10:45 +02:00
~ PriorityMuxer ( ) ;
2018-12-27 23:11:32 +01:00
///
/// @brief Start/Stop the PriorityMuxer update timer; On disabled no priority and timeout updates will be performend
/// @param enable The new state
///
void setEnable ( const bool & enable ) ;
/// @brief Enable or disable auto source selection
/// @param enable True if it should be enabled else false
/// @param update True to update _currentPriority - INTERNAL usage.
/// @return True if changed has been applied, false if the state is unchanged
///
bool setSourceAutoSelectEnabled ( const bool & enabel , const bool & update = true ) ;
///
/// @brief Get the state of source auto selection
/// @return True if enabled, else false
///
bool isSourceAutoSelectEnabled ( ) const { return _sourceAutoSelectEnabled ; } ;
///
/// @brief Overwrite current lowest piority with manual selection; On success disables aito selection
/// @param priority The
/// @return True on success, false if priority not found
///
bool setPriority ( const uint8_t priority ) ;
///
/// @brief Update all ledColos with min length of >= 1 to fit the new led length
/// @param[in] ledCount The count of leds
///
void updateLedColorsLength ( const int & ledCount ) ;
2013-09-06 21:26:58 +02:00
///
/// Returns the current priority
///
/// @return The current priority
///
2019-02-03 14:36:57 +01:00
int getCurrentPriority ( ) const { return _currentPriority ; }
2013-08-13 11:10:45 +02:00
2013-09-06 21:26:58 +02:00
///
/// Returns the state (enabled/disabled) of a specific priority channel
/// @param priority The priority channel
/// @return True if the priority channel exists else false
///
2013-08-13 11:10:45 +02:00
bool hasPriority ( const int priority ) const ;
2013-09-06 21:26:58 +02:00
///
/// Returns the number of active priorities
///
/// @return The list with active priorities
///
2013-08-13 11:10:45 +02:00
QList < int > getPriorities ( ) const ;
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// Returns the information of a specified priority channel.
/// If a priority is no longer available the _lowestPriorityInfo (255) is returned
2013-09-06 21:26:58 +02:00
///
/// @param priority The priority channel
///
/// @return The information for the specified priority channel
///
2018-12-27 23:11:32 +01:00
const InputInfo getInputInfo ( const int priority ) const ;
///
/// @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)
/// @param[in] owner Speicifc owner string, might be empty
/// @param[in] smooth_cfg The smooth id to use
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
void registerInput ( const int priority , const hyperion : : Components & component , const QString & origin = " System " , const QString & owner = " " , unsigned smooth_cfg = SMOOTHING_MODE_DEFAULT ) ;
2013-08-13 11:10:45 +02:00
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())
/// @param priority The priority to update
/// @param ledColors The colors
/// @param timeout_ms The new timeout (defaults to -1 endless)
/// @return True on success, false when priority is not found
2013-09-06 21:26:58 +02:00
///
2019-06-05 18:19:08 +02:00
bool setInput ( const int priority , const std : : vector < ColorRgb > & ledColors , int64_t timeout_ms = - 1 ) ;
2018-12-27 23:11:32 +01: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())
/// @param priority The priority to update
/// @param image The new image
/// @param timeout_ms The new timeout (defaults to -1 endless)
/// @return True on success, false when priority is not found
///
2019-06-05 18:19:08 +02:00
bool setInputImage ( const int priority , const Image < ColorRgb > & image , int64_t timeout_ms = - 1 ) ;
2013-08-13 11:10:45 +02:00
2018-12-28 18:12:45 +01:00
///
/// @brief Set the given priority to inactive
/// @param priority The priority
/// @return True on success false if not found
///
2019-06-05 18:19:08 +02:00
bool setInputInactive ( const quint8 & priority ) ;
2018-12-28 18:12:45 +01:00
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// Clears the specified priority channel and update _currentPriority on success
2013-09-06 21:26:58 +02:00
///
/// @param[in] priority The priority of the channel to clear
2018-12-27 23:11:32 +01:00
/// @return True if priority has been cleared else false (not found)
2013-09-06 21:26:58 +02:00
///
2019-06-05 18:19:08 +02:00
bool clearInput ( const uint8_t priority ) ;
2013-08-13 11:10:45 +02:00
2013-09-06 21:26:58 +02:00
///
/// Clears all priority channels
///
2017-08-12 07:55:32 +02:00
void clearAll ( bool forceClearAll = false ) ;
2013-08-13 11:10:45 +02:00
2019-07-21 19:06:47 +02:00
///
/// @brief Queue a manual push where muxer doesn't recognize them (e.g. continous single color pushes)
///
void queuePush ( void ) { emit timeRunner ( ) ; } ;
2018-12-27 23:11:32 +01:00
signals :
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// @brief Signal which emits when a effect or color with timeout > -1 is running, once per second
///
void timeRunner ( ) ;
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
/// @brief A priority has been added (registerInput()) or deleted, method clear or timeout clear
/// @param priority The priority which has changed
/// @param state If true it was added else it was removed!
2013-09-06 21:26:58 +02:00
///
2018-12-27 23:11:32 +01:00
void priorityChanged ( const quint8 & priority , const bool & state ) ;
2013-08-13 11:10:45 +02:00
2017-06-17 23:29:04 +02:00
///
2018-12-27 23:11:32 +01:00
/// @brief Emits whenever the visible priority has changed
2019-02-03 14:36:57 +01:00
/// @param priority The new visible priority
2018-12-27 23:11:32 +01:00
///
void visiblePriorityChanged ( const quint8 & priority ) ;
///
/// @brief Emits whenever a priority changes active state
/// @param priority The priority who changed the active state
/// @param state The new state, state true = active else false
///
void activeStateChanged ( const quint8 & priority , const bool & state ) ;
///
/// @brief Emits whenever the auto selection state has been changed
/// @param state The new state of auto selection; True enabled else false
///
void autoSelectChanged ( const bool & state ) ;
///
/// @brief Emits whenever something changes which influences the priorities listing
/// Emits also in 1s interval when a COLOR or EFFECT is running with a timeout > -1
2017-06-17 23:29:04 +02:00
///
2018-12-27 23:11:32 +01:00
void prioritiesChanged ( void ) ;
///
/// internal used signal to resolve treading issues with timer
///
void signalTimeTrigger ( ) ;
2017-06-17 23:29:04 +02:00
private slots :
///
2018-12-27 23:11:32 +01:00
/// Slot which is called to adapt to 1s interval for signal timeRunner() / prioritiesChanged()
2017-06-17 23:29:04 +02:00
///
2018-12-27 23:11:32 +01:00
void timeTrigger ( ) ;
///
/// Updates the current time. Channels with a configured time out will be checked and cleared if
/// required.
///
void setCurrentTime ( void ) ;
2017-06-17 23:29:04 +02:00
2013-08-13 11:10:45 +02:00
private :
2018-12-27 23:11:32 +01:00
/// Logger instance
Logger * _log ;
2013-09-06 21:26:58 +02:00
/// The current priority (lowest value in _activeInputs)
2013-08-18 13:33:56 +02:00
int _currentPriority ;
2013-08-13 11:10:45 +02:00
2018-12-27 23:11:32 +01:00
/// The manual select priority set with setPriority
int _manualSelectedPriority ;
2013-09-06 21:26:58 +02:00
/// The mapping from priority channel to led-information
2013-08-18 13:33:56 +02:00
QMap < int , InputInfo > _activeInputs ;
2013-08-13 11:10:45 +02:00
2013-09-06 21:26:58 +02:00
/// The information of the lowest priority channel
2013-08-18 13:33:56 +02:00
InputInfo _lowestPriorityInfo ;
2018-12-27 23:11:32 +01:00
// Reflect the state of auto select
bool _sourceAutoSelectEnabled ;
// Timer to update Muxer times independent
QTimer * _updateTimer ;
QTimer * _timer ;
QTimer * _blockTimer ;
2013-08-13 11:10:45 +02:00
} ;