From 740bd851c05885541c93e242f38c02426c650e29 Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Tue, 31 Aug 2021 13:24:32 -0400 Subject: [PATCH] don't modify data being set --- protocols/dmx/universe.cpp | 40 +++++++++++++++++++++++--------------- protocols/dmx/universe.h | 2 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/protocols/dmx/universe.cpp b/protocols/dmx/universe.cpp index 22ef74a..bceb79c 100644 --- a/protocols/dmx/universe.cpp +++ b/protocols/dmx/universe.cpp @@ -84,25 +84,33 @@ double Universe::rxRate() /** * @brief Universe::setData Accept new data from receiver - * @param vect varaibly sized data, beginning with a start code. + * @param data varaibly sized data, beginning with a start code. * * The only setData method used to establish rx rate. */ -void Universe::setData(std::vector vect) { - switch (vect.front()) // start code - { - case E111_NULL_START: - vect.resize(null_start_data.size(), 0); // pad shorter, truncate larger - null_start_mutex.lock(); - std::copy(vect.begin(), vect.end(), null_start_data.begin()); - null_start_mutex.unlock(); - rx_timeout_(true); - for (const auto &cb : callbacks_) - cb(this); - break; - default: - break; - } +void Universe::setData(const std::vector& data) +{ + // no data? + if (data.empty()) + return; + + // too many slots! + if (data.size() > null_start_data.size()) + return; + + // copy data + null_start_mutex.lock(); + if (data.size() < null_start_data.size()) + null_start_data.fill(0); + std::copy(data.begin(), data.end(), null_start_data.begin()); + null_start_mutex.unlock(); + + // update rx times + rx_timeout_(true); + + // notify callbacks + for (const auto &cb : callbacks_) + cb(this); } diff --git a/protocols/dmx/universe.h b/protocols/dmx/universe.h index c1e41e6..1577a58 100644 --- a/protocols/dmx/universe.h +++ b/protocols/dmx/universe.h @@ -56,7 +56,7 @@ class Universe { void setValue (const uint16_t, const uint8_t); void setValue (const uint16_t, const uint16_t, const uint8_t*); - void setData (std::vector); + void setData (const std::vector &); void onData (const DataHandler); protected: