1
0
Fork 0
OpenLCP/protocol/artistic/artnet/controller.cpp

124 lines
3.6 KiB
C++

/*
controller.cpp
Copyright (c) 2022 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 "controller.h"
namespace ARTNET {
Controller::Controller()
: Device(StController)
, minimum_polling_interval(2500)
, _periodic_polling_enabled(true)
, _pollster(std::thread(&Controller::_periodic_polling, this))
, last_poll(std::chrono::system_clock::from_time_t(0))
, _talktome_request({.VLC_disable=true})
, _priority_request(DpCritial)
{
setShortName("OpenLCP Controller");
setLongName("Generic OpenLCP Art-Net Controller");
}
Controller::~Controller()
{
if (_pollster.joinable())
{
_periodic_polling_enabled = false; // disable
{
std::shared_lock lk_ctl(_mtx_poll_control);
_poll_request.notify_all(); // wake-up
}
_pollster.join();
}
}
/**
* @brief Controller::setTalkToMeRequest
* @param talktome
*/
void Controller::setTalkToMeRequest(TalkToMe talktome)
{
std::unique_lock lk_data(_mtx_control_data);
_talktome_request = talktome;
}
/**
* @brief Controller::setPriorityRequest
* @param priority
*/
void Controller::setPriorityRequest(Priority priority)
{
std::unique_lock lk_data(_mtx_control_data);
_priority_request = priority;
}
/**
* @brief Controller::txArtPoll
*/
void Controller::txArtPoll()
{
auto data = std::make_shared<poll_data>();
{
std::shared_lock lk_data(_mtx_control_data);
data->talk_to_me = _talktome_request;
data->diagnostic_level = _priority_request;
}
auto packet = std::make_shared<ArtPoll>(data);
send(packet, broadcastIp());
// every poll tx gets considered in the minimum_poll_interval
{
std::unique_lock lk_ctl(_mtx_poll_control);
last_poll = std::chrono::system_clock::now();
}
}
void Controller::_periodic_polling()
{
std::chrono::nanoseconds elapsed;
std::unique_lock lk_thread(_mtx_poll_thread);
while (_periodic_polling_enabled) {
// enforce strict minimum update interval
{
std::shared_lock lk_ctl(_mtx_poll_control);
elapsed = std::chrono::system_clock::now() - last_poll;
}
if (elapsed < minimum_polling_interval)
std::this_thread::sleep_for(minimum_polling_interval - elapsed);
if(!_periodic_polling_enabled) // may have been disabled while enforcing minimum interval
break;
txArtPoll();
// sleep before the next cycle
_poll_request.wait_for(lk_thread, minimum_polling_interval);
}
}
} // namespace ARTNET