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"
|
|
|
|
|
|
|
|
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})
|
|
|
|
, rx_timeout_period_(timeout_period)
|
2021-08-02 10:09:14 -04:00
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
2021-08-02 10:09:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::~Universe
|
|
|
|
*/
|
|
|
|
Universe::~Universe()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::slot
|
|
|
|
* @param address
|
|
|
|
* @return
|
|
|
|
*/
|
2021-08-26 16:46:06 -04:00
|
|
|
uint8_t Universe::slot(const uint16_t address) const
|
2021-06-19 21:12:40 -04:00
|
|
|
{
|
2021-08-15 23:36:29 -04:00
|
|
|
if (address == 0)
|
|
|
|
return 0;
|
2021-08-31 09:03:18 -04:00
|
|
|
if (address > null_start_data.size() - 1)
|
2021-08-15 23:36:29 -04:00
|
|
|
return 0;
|
2021-08-31 11:33:02 -04:00
|
|
|
|
|
|
|
null_start_mutex.lock();
|
|
|
|
uint8_t val = null_start_data[address];
|
|
|
|
null_start_mutex.unlock();
|
|
|
|
|
|
|
|
return val;
|
2021-06-19 21:12:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
/**
|
2021-07-31 10:33:26 -04:00
|
|
|
* @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
|
2021-08-06 12:36:31 -04:00
|
|
|
* the last rx_timeout_peroid ms) / rx_timeout_period seconds.
|
2021-07-31 10:33:26 -04:00
|
|
|
*/
|
2021-08-02 10:10:55 -04:00
|
|
|
double Universe::rxRate()
|
2021-07-31 10:33:26 -04:00
|
|
|
{
|
|
|
|
rx_timeout_();
|
2021-08-07 13:48:00 -04:00
|
|
|
// updates per second
|
2021-08-06 12:36:31 -04:00
|
|
|
return rx_times_.size() / (rx_timeout_period_ / 1000.0);
|
2021-07-31 10:33:26 -04:00
|
|
|
}
|
2021-05-27 10:59:22 -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)
|
|
|
|
{
|
|
|
|
// no data?
|
|
|
|
if (data.empty())
|
|
|
|
return;
|
|
|
|
|
2021-08-31 13:25:02 -04:00
|
|
|
// Alternate Start Code
|
|
|
|
if (data.front() != E111_NULL_START)
|
|
|
|
return altSCdata(data);
|
|
|
|
|
2021-08-31 13:24:32 -04:00
|
|
|
// too many slots!
|
|
|
|
if (data.size() > null_start_data.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// copy data
|
|
|
|
null_start_mutex.lock();
|
|
|
|
if (data.size() < null_start_data.size())
|
|
|
|
null_start_data.fill(0);
|
|
|
|
std::copy(data.begin(), data.end(), null_start_data.begin());
|
|
|
|
null_start_mutex.unlock();
|
|
|
|
|
|
|
|
// update rx times
|
|
|
|
rx_timeout_(true);
|
|
|
|
|
|
|
|
// notify callbacks
|
|
|
|
for (const auto &cb : callbacks_)
|
|
|
|
cb(this);
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
|
2021-08-31 13:25:02 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::altSCdata
|
|
|
|
* @param data
|
|
|
|
*
|
|
|
|
* \attention re-impliment this method to rx custom alternate start code data.
|
|
|
|
*/
|
|
|
|
void Universe::altSCdata(const std::vector<uint8_t> & data)
|
|
|
|
{
|
|
|
|
switch (data.front()) {
|
|
|
|
case E111_ASC_TEXT_ASCII:
|
|
|
|
break;
|
|
|
|
case E111_ASC_TEST:
|
|
|
|
break;
|
|
|
|
case E111_ASC_TEXT_UTF8:
|
|
|
|
break;
|
|
|
|
case E111_ASC_MANUFACTURER:
|
|
|
|
break;
|
|
|
|
case E111_ASC_SIP:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-19 22:37:34 -04:00
|
|
|
/**
|
2021-08-25 17:20:33 -04:00
|
|
|
* @brief Universe::setValue
|
|
|
|
* @param address
|
|
|
|
* @param value
|
|
|
|
*/
|
2021-06-23 10:21:11 -04:00
|
|
|
void Universe::setValue(const uint16_t address, const uint8_t value)
|
2021-06-19 22:37:34 -04:00
|
|
|
{
|
2021-08-15 23:36:29 -04:00
|
|
|
if (address == 0)
|
|
|
|
return;
|
2021-08-31 09:03:18 -04:00
|
|
|
if (address > null_start_data.size() - 1)
|
2021-08-15 23:36:29 -04:00
|
|
|
return;
|
2021-08-31 11:33:02 -04:00
|
|
|
|
|
|
|
null_start_mutex.lock();
|
2021-08-31 09:03:18 -04:00
|
|
|
null_start_data[address] = value;
|
2021-08-31 11:33:02 -04:00
|
|
|
null_start_mutex.unlock();
|
2021-06-19 22:37:34 -04:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
|
2021-06-19 22:37:34 -04:00
|
|
|
/**
|
2021-08-25 17:20:33 -04:00
|
|
|
* @brief Universe::setValue
|
|
|
|
* @param start
|
|
|
|
* @param footprint
|
2021-09-01 12:33:42 -04:00
|
|
|
* @param data
|
2021-08-25 17:20:33 -04:00
|
|
|
*/
|
2021-06-23 10:21:11 -04:00
|
|
|
void Universe::setValue(const uint16_t start, const uint16_t footprint,
|
2021-09-01 12:33:42 -04:00
|
|
|
const uint8_t* data)
|
2021-06-19 22:37:34 -04:00
|
|
|
{
|
2021-08-15 23:36:29 -04:00
|
|
|
if (start == 0)
|
|
|
|
return;
|
2021-08-31 09:03:18 -04:00
|
|
|
if (start + footprint > null_start_data.size() - 1)
|
2021-08-15 23:36:29 -04:00
|
|
|
return;
|
2021-08-31 11:33:02 -04:00
|
|
|
|
|
|
|
null_start_mutex.lock();
|
2021-09-01 12:35:40 -04:00
|
|
|
std::copy(data, data + footprint, null_start_data.begin() + start);
|
2021-08-31 11:33:02 -04:00
|
|
|
null_start_mutex.unlock();
|
2021-06-19 22:37:34 -04:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
/**
|
2021-08-25 17:20:33 -04:00
|
|
|
* @brief Universe::onData
|
|
|
|
* @param callback
|
|
|
|
*/
|
2021-05-27 10:59:22 -04:00
|
|
|
void Universe::onData(const DataHandler callback)
|
|
|
|
{
|
|
|
|
callbacks_.push_back(callback);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
auto elapsed = [] (auto& a, auto& b) {
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(a - b);
|
2021-07-31 10:33:26 -04:00
|
|
|
};
|
|
|
|
|
2021-08-15 23:36:29 -04:00
|
|
|
if (add_now)
|
|
|
|
rx_times_.push(now);
|
2021-07-31 10:33:26 -04:00
|
|
|
|
2021-08-15 23:36:29 -04:00
|
|
|
while (rx_times_.size() > 0 &&
|
|
|
|
elapsed(now, rx_times_.front()).count() > rx_timeout_period_)
|
|
|
|
rx_times_.pop();
|
2021-07-31 10:33:26 -04:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:48:00 -04:00
|
|
|
} // namespace DMX
|