1
0
Fork 0
OpenLCP/dmx/universe.cpp

143 lines
3.8 KiB
C++
Raw Normal View History

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 {
/**
*/
Universe::Universe() {
null_start_data_.fill(0);
}
2021-06-19 21:12:40 -04:00
/**
Get value of a slot.
@param uint16_t address of the requested slot.
@return const uint8_t value of the slot.
*/
2021-06-19 22:37:34 -04:00
const uint8_t Universe::slot (const uint16_t address) const
2021-06-19 21:12:40 -04:00
{
2021-06-19 22:37:34 -04:00
if (address == 0) return 0;
if (address > null_start_data_.size() - 1) return 0;
2021-06-19 21:12:40 -04:00
return null_start_data_[address];
}
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
* the last 2.5 seconds) / 2.5 seconds.
*/
const double Universe::rxRate()
{
rx_timeout_();
return rx_times_.size() / 2.5;
}
2021-05-27 10:59:22 -04:00
2021-07-31 10:33:26 -04:00
/**
* @brief Universe::setData Accept new data from receiver
* @param vect varaibly sized data, beginning with a start code.
*
* The only setData method used to establish rx rate.
*/
void Universe::setData(std::vector<uint8_t> vect) {
2021-05-27 10:59:22 -04:00
switch (vect.front()) { // start code
case E111_NULL_START:
vect.resize(null_start_data_.size(), 0); // pad shorter, truncate larger
2021-06-19 21:12:40 -04:00
std::copy(vect.begin(), vect.end(), null_start_data_.begin());
2021-07-31 10:33:26 -04:00
rx_timeout_(true);
2021-05-27 10:59:22 -04:00
for (const auto &cb : callbacks_)
cb(this);
break;
default:
break;
}
}
2021-06-19 22:37:34 -04:00
/**
set value of a specific slot
@param uint16_t address of the slot being changed
@param uint8_t value being set
*/
void Universe::setValue(const uint16_t address, const uint8_t value)
2021-06-19 22:37:34 -04:00
{
if (address == 0) return;
if (address > null_start_data_.size() - 1) return;
null_start_data_[address] = value;
}
/**
set value of a block of slots
@param uint16_t address of the slot being changed
@param uint16_t footprint of profile
@param uint8_t* values being set
*/
void Universe::setValue(const uint16_t start, const uint16_t footprint,
2021-06-19 22:37:34 -04:00
const uint8_t* profile)
{
if (start == 0) return;
if (start + footprint > null_start_data_.size() - 1) return;
for (int i = 0; i < footprint; i++)
null_start_data_[start + i] = profile[i];
}
2021-05-27 10:59:22 -04:00
/**
register a data consumer callback function
@param const DataHandlerFunction something that knows what to do with DMX
*/
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)
{
auto now = std::chrono::system_clock::now();
auto elapsed = [](auto& a, auto& b) {
return std::chrono::duration_cast<std::chrono::milliseconds>(a - b);
};
if (add_now)
rx_times_.push(now);
while (rx_times_.size() > 0 &&
elapsed(now, rx_times_.front()).count() > 2500)
rx_times_.pop();
}
2021-05-27 10:59:22 -04:00
} // DMX