2022-06-09 16:41:55 -04:00
|
|
|
/*
|
|
|
|
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()
|
2023-05-21 12:41:39 -04:00
|
|
|
: Device(StController)
|
|
|
|
, minimum_polling_interval(2500)
|
2023-05-21 10:05:06 -04:00
|
|
|
, _periodic_polling_enabled(true)
|
|
|
|
, _pollster(std::thread(&Controller::_periodic_polling, this))
|
2023-05-21 10:22:19 -04:00
|
|
|
, last_poll(std::chrono::system_clock::from_time_t(0))
|
2023-05-21 10:36:59 -04:00
|
|
|
, _talktome_request({.VLC_disable=true})
|
2023-05-21 10:05:06 -04:00
|
|
|
, _priority_request(DpCritial)
|
2022-06-09 16:41:55 -04:00
|
|
|
{
|
2023-05-21 11:21:41 -04:00
|
|
|
setShortName("OpenLCP Controller");
|
|
|
|
setLongName("Generic OpenLCP Art-Net Controller");
|
2022-06-09 16:41:55 -04:00
|
|
|
}
|
|
|
|
|
2023-05-21 10:05:06 -04:00
|
|
|
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)
|
|
|
|
{
|
2023-05-21 10:22:19 -04:00
|
|
|
std::unique_lock lk_data(_mtx_control_data);
|
2023-05-21 10:05:06 -04:00
|
|
|
_talktome_request = talktome;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Controller::setPriorityRequest
|
|
|
|
* @param priority
|
|
|
|
*/
|
|
|
|
void Controller::setPriorityRequest(Priority priority)
|
|
|
|
{
|
2023-05-21 10:22:19 -04:00
|
|
|
std::unique_lock lk_data(_mtx_control_data);
|
2023-05-21 10:05:06 -04:00
|
|
|
_priority_request = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-20 13:41:26 -04:00
|
|
|
/**
|
|
|
|
* @brief Controller::txArtPoll
|
|
|
|
*/
|
2023-05-21 10:22:19 -04:00
|
|
|
void Controller::txArtPoll()
|
2023-05-20 13:41:26 -04:00
|
|
|
{
|
2023-05-20 14:39:50 -04:00
|
|
|
auto data = std::make_shared<poll_data>();
|
2023-05-21 10:22:19 -04:00
|
|
|
{
|
|
|
|
std::shared_lock lk_data(_mtx_control_data);
|
|
|
|
data->talk_to_me = _talktome_request;
|
|
|
|
data->diagnostic_level = _priority_request;
|
|
|
|
}
|
2023-05-20 14:39:50 -04:00
|
|
|
auto packet = std::make_shared<ArtPoll>(data);
|
2023-05-20 13:41:26 -04:00
|
|
|
send(packet, broadcastIp());
|
2023-05-21 10:22:19 -04:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2023-05-20 13:41:26 -04:00
|
|
|
}
|
|
|
|
|
2023-05-21 10:05:06 -04:00
|
|
|
|
|
|
|
void Controller::_periodic_polling()
|
|
|
|
{
|
|
|
|
std::chrono::nanoseconds elapsed;
|
|
|
|
std::unique_lock lk_thread(_mtx_poll_thread);
|
|
|
|
|
|
|
|
while (_periodic_polling_enabled) {
|
2023-05-21 10:22:19 -04:00
|
|
|
// enforce strict minimum update interval
|
|
|
|
{
|
|
|
|
std::shared_lock lk_ctl(_mtx_poll_control);
|
|
|
|
elapsed = std::chrono::system_clock::now() - last_poll;
|
|
|
|
}
|
2023-05-21 10:05:06 -04:00
|
|
|
if (elapsed < minimum_polling_interval)
|
|
|
|
std::this_thread::sleep_for(minimum_polling_interval - elapsed);
|
2023-05-21 10:22:19 -04:00
|
|
|
if(!_periodic_polling_enabled) // may have been disabled while enforcing minimum interval
|
|
|
|
break;
|
2023-05-21 10:05:06 -04:00
|
|
|
|
2023-05-21 10:22:19 -04:00
|
|
|
txArtPoll();
|
2023-05-21 10:05:06 -04:00
|
|
|
|
|
|
|
// sleep before the next cycle
|
|
|
|
_poll_request.wait_for(lk_thread, minimum_polling_interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:41:55 -04:00
|
|
|
} // namespace ARTNET
|