2021-05-27 10:59:22 -04:00
|
|
|
/*
|
|
|
|
universe.cpp
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "universe.h"
|
2022-12-09 10:28:02 -05:00
|
|
|
#include <mutex>
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
namespace DMX {
|
|
|
|
|
|
|
|
/**
|
2021-08-02 10:09:14 -04:00
|
|
|
* @brief Universe::Universe
|
2021-08-25 17:20:33 -04:00
|
|
|
* @param timeout_period
|
2021-08-02 10:09:14 -04:00
|
|
|
*/
|
2021-08-06 12:36:31 -04:00
|
|
|
Universe::Universe(int timeout_period)
|
2021-09-01 12:34:10 -04:00
|
|
|
: null_start_data({0})
|
2023-04-16 11:05:04 -04:00
|
|
|
, device_class_(RECEIVER)
|
2021-09-01 12:34:10 -04:00
|
|
|
, rx_timeout_period_(timeout_period)
|
2022-12-02 13:04:06 -05:00
|
|
|
, status_(DMX_NULL)
|
2023-04-16 11:03:53 -04:00
|
|
|
, active_data_slots_(1) // start code
|
2021-08-02 10:09:14 -04:00
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
2021-08-02 10:09:14 -04:00
|
|
|
|
|
|
|
/**
|
2022-12-06 14:04:18 -05:00
|
|
|
* @brief milliseconds since the last update
|
|
|
|
* @return
|
2021-08-02 10:09:14 -04:00
|
|
|
*/
|
2022-12-10 11:12:44 -05:00
|
|
|
long Universe::age() const
|
2021-08-02 10:09:14 -04:00
|
|
|
{
|
2022-12-09 11:18:27 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
2022-12-06 14:04:18 -05:00
|
|
|
if (!last_updated_.time_since_epoch().count())
|
|
|
|
return -1; // universe has never been seen
|
|
|
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_updated_);
|
|
|
|
|
|
|
|
return elapsed.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::rxRate Rate (in Hz) at which the universe is being recv'd.
|
|
|
|
* @return Hz
|
|
|
|
*
|
|
|
|
* Calculated as the rolling mean of (the number of frames in
|
|
|
|
* the last rx_timeout_peroid ms) / rx_timeout_period seconds.
|
|
|
|
*/
|
|
|
|
double Universe::rxRate()
|
|
|
|
{
|
|
|
|
rx_timeout_();
|
2022-12-09 11:18:27 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
2022-12-06 14:04:18 -05:00
|
|
|
// updates per second
|
|
|
|
return rx_times_.size() / (rx_timeout_period_ / 1000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::status
|
|
|
|
* @return
|
|
|
|
*
|
|
|
|
* Like Schrödinger's cat, the status of the universe may exist in a superpostion
|
|
|
|
* of states. Only by observing it will it collaps into a single status.
|
|
|
|
*/
|
2022-12-10 11:12:44 -05:00
|
|
|
uint8_t Universe::status() const
|
2022-12-06 14:04:18 -05:00
|
|
|
{
|
2022-12-09 11:18:27 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
2022-12-06 14:04:18 -05:00
|
|
|
return status_;
|
2021-08-02 10:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::slot
|
|
|
|
* @param address
|
|
|
|
* @return
|
|
|
|
*/
|
2022-12-10 11:12:44 -05:00
|
|
|
uint8_t Universe::slot(const uint16_t address) const
|
2021-06-19 21:12:40 -04:00
|
|
|
{
|
2022-12-09 11:54:52 -05:00
|
|
|
std::shared_lock lck(mtx_data);
|
2022-12-06 13:36:27 -05:00
|
|
|
try {
|
|
|
|
return null_start_data.at(address);
|
2022-12-09 10:28:02 -05:00
|
|
|
} catch (...) {
|
2021-08-15 23:36:29 -04:00
|
|
|
return 0;
|
2022-12-06 13:36:27 -05:00
|
|
|
}
|
2021-06-19 21:12:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
/**
|
2022-12-06 14:04:18 -05:00
|
|
|
* @brief Universe::setStatus
|
|
|
|
* @param val
|
2021-07-31 10:33:26 -04:00
|
|
|
*/
|
2022-12-06 14:04:18 -05:00
|
|
|
void Universe::setStatus(uint8_t val)
|
2021-07-31 10:33:26 -04:00
|
|
|
{
|
2022-12-06 14:04:18 -05:00
|
|
|
if (val == RX_TIMEOUT)
|
|
|
|
val = DMX_LOST;
|
|
|
|
|
|
|
|
if (val == status_)
|
|
|
|
return;
|
|
|
|
|
2022-12-09 11:18:27 -05:00
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
status_ = val;
|
|
|
|
}
|
2022-12-06 14:04:18 -05:00
|
|
|
do_callbacks_(cb_statusChange);
|
2021-07-31 10:33:26 -04:00
|
|
|
}
|
2021-05-27 10:59:22 -04:00
|
|
|
|
2021-07-31 10:33:26 -04:00
|
|
|
|
2021-09-10 12:47:11 -04:00
|
|
|
/**
|
2022-12-06 14:04:18 -05:00
|
|
|
* @brief Universe::setValue
|
|
|
|
* @param start
|
|
|
|
* @param footprint
|
|
|
|
* @param data
|
2021-09-10 12:47:11 -04:00
|
|
|
*/
|
2022-12-06 14:04:18 -05:00
|
|
|
void Universe::setValue(const uint16_t start, const uint16_t footprint,
|
|
|
|
const uint8_t * const data)
|
2021-09-10 12:47:11 -04:00
|
|
|
{
|
2023-04-16 11:05:04 -04:00
|
|
|
if (!isEditable())
|
|
|
|
return;
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
// start and footprint valid?
|
|
|
|
if (start < 1 || start + footprint > null_start_data.size())
|
|
|
|
return;
|
2021-09-10 12:47:11 -04:00
|
|
|
|
2022-12-09 10:28:02 -05:00
|
|
|
{
|
2022-12-09 11:54:52 -05:00
|
|
|
std::unique_lock lk_data(mtx_data);
|
2022-12-09 10:28:02 -05:00
|
|
|
std::copy_n(data, footprint, null_start_data.begin() + start);
|
|
|
|
}
|
2022-12-09 11:18:27 -05:00
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
last_updated_ = std::chrono::system_clock::now();
|
|
|
|
}
|
2023-04-16 11:03:53 -04:00
|
|
|
|
|
|
|
// set active_data_slots to at least end of footprint
|
|
|
|
if (start + footprint > active_data_slots_)
|
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
active_data_slots_ = start + footprint;
|
|
|
|
}
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
setStatus(DMX_ACTIVE);
|
|
|
|
do_callbacks_(cb_dataChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::setValue
|
|
|
|
* @param address
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
void Universe::setValue(const uint16_t address, const uint8_t value)
|
|
|
|
{
|
|
|
|
setValue(address, 1, &value);
|
2021-09-10 12:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-31 10:33:26 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::setData Accept new data from receiver
|
2021-08-31 13:24:32 -04:00
|
|
|
* @param data varaibly sized data, beginning with a start code.
|
2021-07-31 10:33:26 -04:00
|
|
|
*
|
|
|
|
* The only setData method used to establish rx rate.
|
|
|
|
*/
|
2021-08-31 13:24:32 -04:00
|
|
|
void Universe::setData(const std::vector<uint8_t>& data)
|
|
|
|
{
|
|
|
|
if (data.empty())
|
2023-04-26 09:59:14 -04:00
|
|
|
return; // no data
|
|
|
|
switch (data.front()) {
|
|
|
|
case E111_NULL_START:
|
|
|
|
rx_dimmer_data_(data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (rx_handlers_.count(data.front()))
|
|
|
|
if (auto sp = rx_handlers_.at(data.front()).lock())
|
|
|
|
(*sp)(data); // the owner is still holding the token
|
2023-04-16 11:03:53 -04:00
|
|
|
}
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
|
2021-08-31 13:25:02 -04:00
|
|
|
/**
|
2023-04-26 09:59:14 -04:00
|
|
|
* @brief Universe::onRxData
|
|
|
|
* @param code
|
|
|
|
* @param cb
|
|
|
|
* @return
|
2021-08-31 13:25:02 -04:00
|
|
|
*/
|
2023-04-26 09:59:14 -04:00
|
|
|
std::shared_ptr<void> Universe::onRxData(uint8_t code,
|
|
|
|
const std::function<void(const std::vector<uint8_t>&)> cb)
|
2021-08-31 13:25:02 -04:00
|
|
|
{
|
2023-04-26 09:59:14 -04:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
// wrap the callback with a shared pointer
|
|
|
|
auto sp = std::make_shared<std::function<void(const std::vector<uint8_t>&)>>(std::move(cb));
|
|
|
|
// add callback to list (as a weak pointer)
|
|
|
|
if (rx_handlers_.count(code))
|
|
|
|
rx_handlers_[code] = sp;
|
|
|
|
else
|
|
|
|
rx_handlers_.insert({code, sp});
|
|
|
|
// return token that caller must keep throughout it's scope
|
|
|
|
return sp;
|
2021-08-31 13:25:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-21 13:24:21 -05:00
|
|
|
/**
|
2022-12-06 14:04:18 -05:00
|
|
|
* @brief Universe::onData
|
2022-12-02 13:06:22 -05:00
|
|
|
* @param cb
|
|
|
|
* @return
|
|
|
|
*/
|
2022-12-06 14:04:18 -05:00
|
|
|
std::shared_ptr<void> Universe::onDataChange(const std::function<void(Universe*)> cb)
|
2022-12-02 13:06:22 -05:00
|
|
|
{
|
2022-12-09 11:18:27 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
2022-12-02 13:06:22 -05:00
|
|
|
// wrap the callback with a shared pointer
|
2022-12-06 13:27:45 -05:00
|
|
|
auto sp = std::make_shared<std::function<void(Universe*)>>(std::move(cb));
|
2022-12-02 13:06:22 -05:00
|
|
|
// add callback to list (as a weak pointer)
|
2022-12-06 14:04:18 -05:00
|
|
|
cb_dataChange.push_back(sp);
|
2022-12-02 13:06:22 -05:00
|
|
|
// return token that caller must keep throughout it's scope
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-19 22:37:34 -04:00
|
|
|
/**
|
2022-12-06 14:04:18 -05:00
|
|
|
* @brief Universe::onStatusChange
|
2021-09-11 09:44:16 -04:00
|
|
|
* @param cb
|
|
|
|
* @return
|
2021-08-25 17:20:33 -04:00
|
|
|
*/
|
2022-12-06 14:04:18 -05:00
|
|
|
std::shared_ptr<void> Universe::onStatusChange(const std::function<void(Universe*)> cb)
|
2021-05-27 10:59:22 -04:00
|
|
|
{
|
2022-12-09 11:18:27 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
2021-09-11 09:44:16 -04:00
|
|
|
// wrap the callback with a shared pointer
|
2022-12-06 13:35:18 -05:00
|
|
|
auto sp = std::make_shared<std::function<void(Universe*)>>(std::move(cb));
|
2021-09-11 09:44:16 -04:00
|
|
|
// add callback to list (as a weak pointer)
|
2022-12-06 14:04:18 -05:00
|
|
|
cb_statusChange.push_back(sp);
|
2021-09-11 09:44:16 -04:00
|
|
|
// return token that caller must keep throughout it's scope
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-26 09:59:14 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::rx_dimmer_data_
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
void Universe::rx_dimmer_data_(const std::vector<uint8_t> &data)
|
|
|
|
{
|
|
|
|
// accept variable lenth input, but never more than the limit
|
|
|
|
const auto max_length = null_start_data.size();
|
|
|
|
const auto length = data.size() < max_length ? data.size() : max_length;
|
|
|
|
{
|
|
|
|
std::unique_lock lk_data(mtx_data);
|
|
|
|
if (length < max_length)
|
|
|
|
null_start_data.fill(0); // wipe old data
|
|
|
|
std::copy_n(data.cbegin(), length, null_start_data.begin()); // copy new data
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
active_data_slots_ = length;
|
|
|
|
}
|
|
|
|
rx_timeout_(true); // update rx times
|
|
|
|
do_callbacks_(cb_dataChange); // run callbacks
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-31 10:33:26 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::rx_timeout_
|
|
|
|
* @param add_now
|
|
|
|
*/
|
|
|
|
void Universe::rx_timeout_(bool add_now)
|
|
|
|
{
|
2021-08-15 23:36:29 -04:00
|
|
|
auto now = std::chrono::system_clock::now();
|
2022-11-28 11:23:13 -05:00
|
|
|
|
2022-11-22 12:05:25 -05:00
|
|
|
if (add_now)
|
2022-11-24 11:34:33 -05:00
|
|
|
{
|
2022-12-02 13:04:06 -05:00
|
|
|
setStatus(DMX_ACTIVE);
|
2022-12-09 11:18:27 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
2022-12-02 12:52:52 -05:00
|
|
|
last_updated_ = now;
|
2022-11-24 11:34:33 -05:00
|
|
|
rx_times_.push(now);
|
|
|
|
}
|
2022-12-02 15:32:26 -05:00
|
|
|
else if (status_ == DMX_ACTIVE && age() >= E111_DATA_LOSS_TIMEOUT)
|
2022-11-28 11:22:06 -05:00
|
|
|
// E1.11 DMX timeout period may be different the the rx_timeout_period
|
2022-12-02 13:04:06 -05:00
|
|
|
setStatus(DMX_LOST);
|
2022-12-02 15:33:22 -05:00
|
|
|
else if (age() >= rx_timeout_period_)
|
|
|
|
// setStatus will be reimplimented to set the correct status
|
|
|
|
setStatus(RX_TIMEOUT);
|
2022-11-22 12:05:25 -05:00
|
|
|
|
2021-08-15 23:36:29 -04:00
|
|
|
auto elapsed = [] (auto& a, auto& b) {
|
2022-11-28 11:17:35 -05:00
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(a - b).count();
|
2021-07-31 10:33:26 -04:00
|
|
|
};
|
|
|
|
|
2022-12-02 13:04:06 -05:00
|
|
|
long age;
|
2022-11-22 12:05:25 -05:00
|
|
|
while (!rx_times_.empty())
|
2022-11-28 11:23:13 -05:00
|
|
|
{ // drop timestamps that are older than the timeout period
|
2022-11-28 11:17:35 -05:00
|
|
|
age = elapsed(now, rx_times_.front());
|
2022-11-22 12:05:25 -05:00
|
|
|
if (age < rx_timeout_period_)
|
|
|
|
break;
|
|
|
|
else
|
2022-12-09 11:18:27 -05:00
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
rx_times_.pop();
|
|
|
|
}
|
2022-11-21 13:41:22 -05:00
|
|
|
}
|
2021-07-31 10:33:26 -04:00
|
|
|
}
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
|
2023-04-16 11:05:04 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::isEditable
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool Universe::isEditable() const
|
|
|
|
{
|
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
|
|
|
return device_class_ == CONTROLLER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-16 11:03:53 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::activeSlots
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
uint16_t Universe::activeSlots() const
|
|
|
|
{
|
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
|
|
|
return active_data_slots_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-06 14:04:18 -05:00
|
|
|
/**
|
|
|
|
* @brief Universe::doCallbacks
|
|
|
|
* @param callbacks
|
|
|
|
*/
|
|
|
|
void Universe::do_callbacks_(std::vector<std::weak_ptr<const std::function<void(Universe*)>>> & callbacks)
|
|
|
|
{
|
2022-12-10 11:12:44 -05:00
|
|
|
for (auto it = callbacks.cbegin(); it != callbacks.cend();)
|
2022-12-06 14:04:18 -05:00
|
|
|
{
|
|
|
|
if (auto sp = it->lock())
|
|
|
|
{ // the owner is still holding the token
|
|
|
|
(*sp)(this);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
else
|
2022-12-10 11:12:44 -05:00
|
|
|
{ // the owner has released the token
|
2022-12-10 13:34:45 -05:00
|
|
|
std::unique_lock lk(mtx_control);
|
2022-12-10 11:12:44 -05:00
|
|
|
it = callbacks.erase(it);
|
|
|
|
}
|
2022-12-06 14:04:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
} // namespace DMX
|