121 lines
2.7 KiB
C++
121 lines
2.7 KiB
C++
/*
|
|
dmx/patch.cpp
|
|
|
|
Copyright (c) 2021 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 "dmx.h"
|
|
#include "patch.h"
|
|
|
|
namespace DMX {
|
|
|
|
/**
|
|
* @brief Patch::Patch
|
|
*/
|
|
Patch::Patch()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Add a Personality to the patch.
|
|
* @param device
|
|
* @param address
|
|
* @return
|
|
*/
|
|
bool Patch::patch(std::shared_ptr<Personality> device, uint16_t address)
|
|
{
|
|
if (address == 0 || address > E111_LAST_SLOT)
|
|
return false;
|
|
|
|
if (address + device->footprint() >= E111_LAST_SLOT)
|
|
return false;
|
|
|
|
members_.emplace_back(device,address);
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Unpatch all occurances of a Peronality in the patch.
|
|
* @param device
|
|
*/
|
|
void Patch::unpatch(std::shared_ptr<Personality> device)
|
|
{
|
|
for (auto it = members_.begin(); it != members_.end();)
|
|
{
|
|
auto wp = it->first;
|
|
if (wp.expired()) {
|
|
it = members_.erase(it);
|
|
continue;
|
|
}
|
|
auto personality = wp.lock();
|
|
if (personality == device) {
|
|
it = members_.erase(it);
|
|
continue;
|
|
}
|
|
++it;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Unpatch a Personality given it's postion in the patch.
|
|
* @param index
|
|
*/
|
|
void Patch::unpatch(size_t index)
|
|
{
|
|
members_.erase(members_.begin() + index);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Return a list of the patch members.
|
|
* @return
|
|
*/
|
|
const std::vector<std::pair<std::weak_ptr<Personality>,uint16_t>> * Patch::listing()
|
|
{
|
|
return &members_;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Attach the patch to a Universe.
|
|
* @param universe
|
|
*/
|
|
void Patch::setUniverse(std::shared_ptr<Universe> universe)
|
|
{
|
|
universe_ = universe;
|
|
data_change_token = universe->onDataChange([this](DMX::Universe*){universeDataChanged();});
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Update Devices when the Universe data changes.
|
|
*/
|
|
void Patch::universeDataChanged()
|
|
{
|
|
|
|
}
|
|
|
|
} // namespace DMX
|