2021-05-27 10:59:22 -04:00
|
|
|
|
/*
|
|
|
|
|
universe.h
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
|
#include "dmx.h"
|
|
|
|
|
|
2021-07-31 10:33:26 -04:00
|
|
|
|
#include <chrono>
|
2021-05-27 10:59:22 -04:00
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <functional>
|
2021-09-11 09:44:16 -04:00
|
|
|
|
#include <memory>
|
2022-12-09 10:28:02 -05:00
|
|
|
|
#include <shared_mutex>
|
2021-07-31 10:33:26 -04:00
|
|
|
|
#include <queue>
|
2021-05-27 10:59:22 -04:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace DMX {
|
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
|
using DimmerData = std::array<uint8_t, E111_LAST_SLOT + 1>; //!< Array of 513 bytes for basic data.
|
2021-07-31 10:33:26 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The Universe class
|
|
|
|
|
*
|
2022-11-18 13:50:07 -05:00
|
|
|
|
* \cite DMX ANSI E1.11 describes may things about moving data over serial EIA-485-A
|
2021-07-31 10:33:26 -04:00
|
|
|
|
* links. This class encapselates that data, regardless of how it was
|
|
|
|
|
* transmitted.
|
|
|
|
|
*/
|
2021-05-27 10:59:22 -04:00
|
|
|
|
class Universe {
|
|
|
|
|
public:
|
2022-12-10 11:07:13 -05:00
|
|
|
|
explicit Universe (int timeout_period = E111_DATA_LOSS_TIMEOUT);
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
2022-12-10 11:12:44 -05:00
|
|
|
|
virtual long age() const;
|
2022-12-06 14:04:18 -05:00
|
|
|
|
virtual double rxRate();
|
2022-12-10 11:12:44 -05:00
|
|
|
|
virtual uint8_t status() const;
|
|
|
|
|
virtual uint8_t slot(const uint16_t) const;
|
2021-06-19 21:12:40 -04:00
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
|
virtual void setStatus(uint8_t);
|
|
|
|
|
virtual void setValue(const uint16_t start, const uint16_t footprint,
|
|
|
|
|
const uint8_t* const data);
|
|
|
|
|
virtual void setValue(const uint16_t address, const uint8_t value);
|
|
|
|
|
virtual void setData(const std::vector<uint8_t> &);
|
2022-12-08 17:41:07 -05:00
|
|
|
|
virtual void setAltData(const std::vector<uint8_t> &);
|
2021-08-31 13:25:02 -04:00
|
|
|
|
|
2022-12-06 13:35:18 -05:00
|
|
|
|
std::shared_ptr<void> onDataChange(const std::function<void(Universe*)>);
|
2022-12-06 13:27:45 -05:00
|
|
|
|
std::shared_ptr<void> onStatusChange(const std::function<void(Universe*)>);
|
2022-12-06 14:04:18 -05:00
|
|
|
|
|
2023-04-16 11:05:04 -04:00
|
|
|
|
virtual bool isEditable() const;
|
2023-04-16 11:03:53 -04:00
|
|
|
|
virtual uint16_t activeSlots() const;
|
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
|
/// Known states of the universe Status value
|
2022-11-21 12:41:59 -05:00
|
|
|
|
enum Status : uint8_t {
|
|
|
|
|
DMX_NULL = 0, //!< uninitialized
|
|
|
|
|
DMX_ACTIVE = 1, //!< actively sending/receiving data
|
2022-12-02 13:04:06 -05:00
|
|
|
|
DMX_LOST = 2, //!< no activity in E111_DATA_LOSS_TIMEOUT
|
|
|
|
|
RX_TIMEOUT = 250 //!< no activity in the constructed timeout period
|
2022-11-21 12:41:59 -05:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-02 10:09:14 -04:00
|
|
|
|
protected:
|
2022-12-06 14:51:03 -05:00
|
|
|
|
inline void doDataCallbacks() {do_callbacks_(cb_dataChange);} //!< Data Change Callbacks
|
|
|
|
|
inline void doStatusCallbacks() {do_callbacks_(cb_statusChange);} //!< Status Change Callbacks
|
2022-12-12 11:29:41 -05:00
|
|
|
|
void do_callbacks_(std::vector<std::weak_ptr<const std::function<void(Universe*)>>>&);
|
2022-12-06 13:35:18 -05:00
|
|
|
|
|
2022-12-09 11:54:52 -05:00
|
|
|
|
DimmerData null_start_data; //!< NULL Start Code data
|
2023-04-16 11:05:04 -04:00
|
|
|
|
DeviceClass device_class_; //!< operating class of the universe
|
2022-12-09 11:54:52 -05:00
|
|
|
|
mutable std::shared_mutex mtx_data; //!< memory protect Null Start data
|
|
|
|
|
mutable std::shared_mutex mtx_control; //!< thread protected access control
|
|
|
|
|
std::chrono::system_clock::time_point last_updated_; //!< time of the latest update
|
2022-12-02 15:35:08 -05:00
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
|
private:
|
2021-07-31 10:33:26 -04:00
|
|
|
|
void rx_timeout_(bool add_now = false);
|
2022-11-22 22:56:47 -05:00
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
|
const int rx_timeout_period_;
|
2022-11-22 22:56:47 -05:00
|
|
|
|
uint8_t status_; //!< the operating state of the universe
|
2022-12-06 14:04:18 -05:00
|
|
|
|
std::queue<std::chrono::time_point<std::chrono::system_clock>> rx_times_;
|
2022-12-06 13:35:18 -05:00
|
|
|
|
std::vector<std::weak_ptr<const std::function<void(Universe*)>>> cb_dataChange;
|
2022-12-06 13:27:45 -05:00
|
|
|
|
std::vector<std::weak_ptr<const std::function<void(Universe*)>>> cb_statusChange;
|
2023-04-16 11:03:53 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief \cite DMX 8.7 Minimum number of data slots
|
|
|
|
|
*
|
|
|
|
|
* > There shall be no minimum number of data slots on the data link. DMX512 data packets
|
|
|
|
|
* > with fewer than 512 slots may be transmitted, subject to the minimum timing requirements
|
|
|
|
|
* > of this Standard – see clause 8.10 and figure 5.
|
|
|
|
|
*/
|
|
|
|
|
uint16_t active_data_slots_;
|
2021-05-27 10:59:22 -04:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
|
} // namespace DMX
|