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>
|
2021-08-31 11:33:02 -04:00
|
|
|
#include <mutex>
|
2021-07-31 10:33:26 -04:00
|
|
|
#include <queue>
|
2021-05-27 10:59:22 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace DMX {
|
|
|
|
|
|
|
|
class Universe; // forward declare the Univserse class
|
2022-11-15 11:32:29 -05:00
|
|
|
using DimmerData = std::array<uint8_t, E111_LAST_SLOT + 1>; //!< Array of 513 bytes for basic data.
|
|
|
|
using DataHandler = std::function<void(Universe *)>; //!< Callback function for data handling.
|
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:
|
2021-08-06 12:36:31 -04:00
|
|
|
Universe (int timeout_period = E111_DATA_LOSS_TIMEOUT);
|
2021-08-02 10:09:14 -04:00
|
|
|
virtual ~Universe ();
|
2021-05-27 10:59:22 -04:00
|
|
|
|
2021-09-10 12:48:33 -04:00
|
|
|
virtual uint8_t slot (const uint16_t);
|
2021-08-02 10:09:14 -04:00
|
|
|
virtual double rxRate();
|
2022-12-02 15:32:26 -05:00
|
|
|
virtual long age();
|
2021-06-19 21:12:40 -04:00
|
|
|
|
2021-09-01 12:33:42 -04:00
|
|
|
virtual void setValue (const uint16_t address, const uint8_t value);
|
|
|
|
virtual void setValue (const uint16_t start, const uint16_t footprint,
|
|
|
|
const uint8_t* data);
|
2021-06-23 10:21:11 -04:00
|
|
|
|
2021-08-31 13:24:32 -04:00
|
|
|
void setData (const std::vector<uint8_t> &);
|
2022-12-02 16:35:05 -05:00
|
|
|
std::shared_ptr<void> onDataChange (const DataHandler);
|
2021-05-27 10:59:22 -04:00
|
|
|
|
2021-08-31 13:25:02 -04:00
|
|
|
virtual void altSCdata(const std::vector<uint8_t> &);
|
|
|
|
|
2022-11-21 12:41:59 -05:00
|
|
|
/**
|
|
|
|
* @brief The Status enum
|
|
|
|
*/
|
|
|
|
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
|
|
|
};
|
2022-11-21 13:24:21 -05:00
|
|
|
virtual void setStatus(uint8_t);
|
2022-11-21 17:42:08 -05:00
|
|
|
virtual uint8_t status();
|
2022-12-02 13:06:22 -05:00
|
|
|
virtual std::shared_ptr<void> onStatusChange(std::function<void()>);
|
2022-11-21 12:41:59 -05:00
|
|
|
|
2021-08-02 10:09:14 -04:00
|
|
|
protected:
|
2021-08-31 09:03:18 -04:00
|
|
|
DimmerData null_start_data; //!< NULL Start Code data
|
2021-08-31 11:33:02 -04:00
|
|
|
mutable std::mutex null_start_mutex; //!< memory protect Null Start data
|
|
|
|
|
2022-12-02 13:06:22 -05:00
|
|
|
void doDataCallbacks(); //!< execute data changed callbacks
|
|
|
|
void doStatusCallbacks(); //!< execute status changed callbacks
|
2022-11-21 13:24:21 -05:00
|
|
|
|
2022-12-02 15:35:08 -05:00
|
|
|
std::chrono::system_clock::time_point last_updated_; //!< time of the latest update
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
private:
|
2021-07-31 10:33:26 -04:00
|
|
|
std::queue<std::chrono::time_point<std::chrono::system_clock>> rx_times_;
|
|
|
|
|
|
|
|
void rx_timeout_(bool add_now = false);
|
2021-08-06 12:36:31 -04: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-02 13:06:22 -05:00
|
|
|
std::vector<std::weak_ptr<std::function<void()>>> cb_statusChange;
|
2022-12-02 12:56:17 -05:00
|
|
|
std::vector<std::weak_ptr<DataHandler>> cb_dataChange;
|
2021-05-27 10:59:22 -04:00
|
|
|
};
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
} // namespace DMX
|