1
0
Fork 0

use a callback to send RDM data

This commit is contained in:
Kevin Matz 2023-04-26 10:02:20 -04:00
parent db3da280da
commit f1612dc60e
2 changed files with 21 additions and 1 deletions

View File

@ -118,7 +118,8 @@ Responder::~Responder()
*/
void Responder::send(const std::vector<uint8_t> &data)
{
(void)data;
if (auto sp = sender_.lock())
(*sp)(data); // sender still exists
}
@ -146,6 +147,22 @@ void Responder::send(MsgPtr response)
}
/**
* @brief Set the message sending callback function.
* @param cb Callback function.
* @return Token that the owner of the callback must keep for it's lifetime.
*/
std::shared_ptr<void> Responder::setSender(std::function<void(const std::vector<uint8_t>&)> cb)
{
// wrap the callback with a shared pointer
auto sp = std::make_shared<std::function<void(const std::vector<uint8_t>&)>>(std::move(cb));
// store callback (as a weak pointer)
sender_ = sp;
// return token that caller must keep throughout it's scope
return sp;
}
/**
* @brief Responder::receive
* @param data

View File

@ -40,6 +40,8 @@ public:
explicit Responder(UID id, Device* parent = nullptr);
virtual ~Responder();
std::shared_ptr<void> setSender(std::function<void(const std::vector<uint8_t>&)> cb);
union {
uint16_t control_field;
struct {
@ -78,6 +80,7 @@ private:
uint16_t checksum_fail_counter_ = 0;
MsgPtr last_status_message_;
std::weak_ptr<std::function<void(const std::vector<uint8_t>&)>> sender_;
};