diff --git a/deviceregistervaluehelper.cpp b/deviceregistervaluehelper.cpp index d767ad1..3516ec1 100644 --- a/deviceregistervaluehelper.cpp +++ b/deviceregistervaluehelper.cpp @@ -3,6 +3,7 @@ #include #include #include +#include void DeviceRegisterValueHelper::setController(DmxController *controller) { @@ -125,7 +126,10 @@ void DeviceRegisterValueHelper::setValue(quint8 value) if (sliderState.size() <= m_registerIndex) sliderState.resize(m_registerIndex + 1); - sliderState[m_registerIndex] = value; + { + QMutexLocker locker{&m_controller->mutex()}; + sliderState[m_registerIndex] = value; + } emit valueChanged(value); } diff --git a/devicesmodel.cpp b/devicesmodel.cpp index 63ba6b4..875b16b 100644 --- a/devicesmodel.cpp +++ b/devicesmodel.cpp @@ -5,6 +5,7 @@ #include #include #include +#include enum { IdRole = Qt::UserRole, @@ -211,21 +212,27 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int return true; case IdRole: - if (value.userType() != QMetaType::Int) - { - qWarning() << "hilfe" << __LINE__ << value.userType(); - return false; - } - device.id = value.toInt(); - emit dataChanged(index, index, { IdRole }); - return true; +// if (value.userType() != QMetaType::Int) +// { +// qWarning() << "hilfe" << __LINE__ << value.userType(); +// return false; +// } +// device.id = value.toInt(); +// emit dataChanged(index, index, { IdRole }); +// return true; + qWarning() << "hilfe" << __LINE__; + return false; case DeviceTypeIdRole: if (value.userType() != QMetaType::Int) { qWarning() << "hilfe" << __LINE__ << value.userType(); return false; } - device.deviceTypeId = value.toInt(); + + { + QMutexLocker locker{&m_controller->mutex()}; + device.deviceTypeId = value.toInt(); + } emit dataChanged(index, index, { DeviceTypeIdRole }); disconnect(m_controller, &DmxController::deviceDeviceTypeIdChanged, @@ -241,7 +248,11 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int qWarning() << "hilfe" << __LINE__ << value.userType(); return false; } - device.address = value.toInt(); + + { + QMutexLocker locker{&m_controller->mutex()}; + device.address = value.toInt(); + } emit dataChanged(index, index, { AddressRole }); disconnect(m_controller, &DmxController::deviceAddressChanged, @@ -257,7 +268,11 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int qWarning() << "hilfe" << __LINE__ << value.userType(); return false; } - device.position = value.value(); + + { + QMutexLocker locker{&m_controller->mutex()}; + device.position = value.value(); + } emit dataChanged(index, index, { PositionRole }); disconnect(m_controller, &DmxController::devicePositionChanged, @@ -305,9 +320,12 @@ bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent) auto id = max_iter != std::cend(devices) ? max_iter->id + 1 : 0; beginInsertRows({}, row, row+count-1); - auto iter = std::begin(devices) + row; - for (auto i = 0; i < count; i++) - iter = devices.insert(iter, DeviceConfig{ .id=id++, .name="", .deviceTypeId=0, .address=0, .position={} }) + 1; + { + QMutexLocker locker{&m_controller->mutex()}; + auto iter = std::begin(devices) + row; + for (auto i = 0; i < count; i++) + iter = devices.insert(iter, DeviceConfig{ .id=id++, .name="", .deviceTypeId=0, .address=0, .position={} }) + 1; + } endInsertRows(); disconnect(m_controller, &DmxController::deviceInserted, @@ -354,9 +372,12 @@ bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent) } beginRemoveRows({}, row, row+count-1); - auto begin = std::begin(devices) + row; - auto end = begin + count; - devices.erase(begin, end); + { + QMutexLocker locker{&m_controller->mutex()}; + auto begin = std::begin(devices) + row; + auto end = begin + count; + devices.erase(begin, end); + } endRemoveRows(); disconnect(m_controller, &DmxController::deviceRemoved, diff --git a/devicetyperegistersmodel.cpp b/devicetyperegistersmodel.cpp index b1b2ee7..47cc59c 100644 --- a/devicetyperegistersmodel.cpp +++ b/devicetyperegistersmodel.cpp @@ -5,6 +5,7 @@ #include #include #include +#include void DeviceTypeRegistersModel::setController(DmxController *controller) { @@ -240,7 +241,10 @@ bool DeviceTypeRegistersModel::setData(const QModelIndex &index, const QVariant switch (role) { case Qt::EditRole: - deviceTypeRegister.type = value.value(); + { + QMutexLocker locker{&m_controller->mutex()}; + deviceTypeRegister.type = value.value(); + } emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); disconnect(m_controller, &DmxController::deviceTypeRegisterTypeChanged, @@ -295,9 +299,12 @@ bool DeviceTypeRegistersModel::insertRows(int row, int count, const QModelIndex auto ®isters = deviceType.registers; beginInsertRows({}, row, row+count-1); - auto iter = std::begin(registers) + row; - for (auto i = 0; i < count; i++) - iter = registers.insert(iter, DeviceTypeRegisterConfig{ .type = DeviceTypeRegisterType::Dummy }) + 1; + { + QMutexLocker locker{&m_controller->mutex()}; + auto iter = std::begin(registers) + row; + for (auto i = 0; i < count; i++) + iter = registers.insert(iter, DeviceTypeRegisterConfig{ .type = DeviceTypeRegisterType::Dummy }) + 1; + } endInsertRows(); disconnect(m_controller, &DmxController::deviceTypeRegisterInserted, @@ -360,9 +367,12 @@ bool DeviceTypeRegistersModel::removeRows(int row, int count, const QModelIndex } beginRemoveRows({}, row, row+count-1); - auto begin = std::begin(registers) + row; - auto end = begin + count; - registers.erase(begin, end); + { + QMutexLocker locker{&m_controller->mutex()}; + auto begin = std::begin(registers) + row; + auto end = begin + count; + registers.erase(begin, end); + } endRemoveRows(); disconnect(m_controller, &DmxController::deviceTypeRegisterRemoved, diff --git a/devicetypesmodel.cpp b/devicetypesmodel.cpp index d2d36db..c529d90 100644 --- a/devicetypesmodel.cpp +++ b/devicetypesmodel.cpp @@ -5,6 +5,7 @@ #include #include #include +#include enum { IdRole = Qt::UserRole, @@ -198,14 +199,16 @@ bool DeviceTypesModel::setData(const QModelIndex &index, const QVariant &value, return true; case IdRole: - if (value.userType() != QMetaType::Int) - { - qWarning() << "hilfe" << __LINE__ << value.userType(); - return false; - } - deviceType.id = value.toInt(); - emit dataChanged(index, index, { IdRole }); - return true; +// if (value.userType() != QMetaType::Int) +// { +// qWarning() << "hilfe" << __LINE__ << value.userType(); +// return false; +// } +// deviceType.id = value.toInt(); +// emit dataChanged(index, index, { IdRole }); +// return true; + qWarning() << "hilfe" << __LINE__; + return false; case IconNameRole: if (value.userType() != QMetaType::QString) { @@ -260,9 +263,12 @@ bool DeviceTypesModel::insertRows(int row, int count, const QModelIndex &parent) auto id = max_iter != std::cend(deviceTypes) ? max_iter->id + 1 : 0; beginInsertRows({}, row, row+count-1); - auto iter = std::begin(deviceTypes) + row; - for (auto i = 0; i < count; i++) - iter = deviceTypes.insert(iter, DeviceTypeConfig{ .id=id++, .name="" }) + 1; + { + QMutexLocker locker{&m_controller->mutex()}; + auto iter = std::begin(deviceTypes) + row; + for (auto i = 0; i < count; i++) + iter = deviceTypes.insert(iter, DeviceTypeConfig{ .id=id++, .name="" }) + 1; + } endInsertRows(); disconnect(m_controller, &DmxController::deviceTypeInserted, @@ -309,9 +315,12 @@ bool DeviceTypesModel::removeRows(int row, int count, const QModelIndex &parent) } beginRemoveRows({}, row, row+count-1); - auto begin = std::begin(deviceTypes) + row; - auto end = begin + count; - deviceTypes.erase(begin, end); + { + QMutexLocker locker{&m_controller->mutex()}; + auto begin = std::begin(deviceTypes) + row; + auto end = begin + count; + deviceTypes.erase(begin, end); + } endRemoveRows(); disconnect(m_controller, &DmxController::deviceTypeRemoved, diff --git a/dmxcontroller.cpp b/dmxcontroller.cpp index 1ec765f..b6f5490 100644 --- a/dmxcontroller.cpp +++ b/dmxcontroller.cpp @@ -4,6 +4,7 @@ #include #include +#include DmxController::DmxController(QObject *parent) : QObject{parent}, @@ -124,7 +125,6 @@ DmxController::DmxController(QObject *parent) : } } { - std::fill(std::begin(buf), std::end(buf), 0); } bool DmxController::start() @@ -173,13 +173,19 @@ void DmxController::setRegisterGroup(int registerGroupId, quint8 value) void DmxController::setSliderStates(sliders_state_t &&sliderStates) { - m_sliderStates = std::move(sliderStates); + { + QMutexLocker locker{&m_mutex}; + m_sliderStates = std::move(sliderStates); + } emit sliderStatesChanged(m_sliderStates); } void DmxController::setSliderStates(const sliders_state_t &sliderStates) { - m_sliderStates = sliderStates; + { + QMutexLocker locker{&m_mutex}; + m_sliderStates = sliderStates; + } emit sliderStatesChanged(m_sliderStates); } @@ -187,6 +193,14 @@ void DmxController::sendDmxBuffer() { const auto now = QDateTime::currentDateTime(); + char buf[513] {0}; + + { + QMutexLocker locker{&m_mutex}; + + // TODO magic + } + m_serialPort.setBreakEnabled(true); QThread::usleep(88); m_serialPort.setBreakEnabled(false); diff --git a/dmxcontroller.h b/dmxcontroller.h index 164ec09..596c85e 100644 --- a/dmxcontroller.h +++ b/dmxcontroller.h @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include "dmxcontrollerthread.h" #include "lightproject.h" @@ -23,15 +23,15 @@ public: LightProject &lightProject() { return m_lightProject; } const LightProject &lightProject() const { return m_lightProject; } - QReadWriteLock &projectLock() { return m_projectLock; } - - int performance() const { return m_lastCounter; } + QMutex &mutex() { return m_mutex; } sliders_state_t &sliderStates() { return m_sliderStates; } const sliders_state_t &sliderStates() const { return m_sliderStates; } void setSliderStates(sliders_state_t &&sliderStates); void setSliderStates(const sliders_state_t &sliderStates); + int performance() const { return m_lastCounter; } + signals: void performanceChanged(int performance); @@ -67,14 +67,11 @@ private: DmxControllerThread m_thread; - char buf[513]; - LightProject m_lightProject; - QReadWriteLock m_projectLock; + QMutex m_mutex; + sliders_state_t m_sliderStates; QDateTime m_lastInfo; int m_counter; std::atomic m_lastCounter; - - sliders_state_t m_sliderStates; }; diff --git a/registergroupmodel.cpp b/registergroupmodel.cpp index 1404726..df5ffb3 100644 --- a/registergroupmodel.cpp +++ b/registergroupmodel.cpp @@ -3,6 +3,7 @@ #include #include #include +#include void RegisterGroupModel::setController(DmxController *controller) { @@ -59,7 +60,10 @@ void RegisterGroupModel::copyFromFaders() auto ®isterGroup = *registerGroupPtr; - registerGroup.sliders = m_controller->sliderStates(); + { + QMutexLocker locker{&m_controller->mutex()}; + registerGroup.sliders = m_controller->sliderStates(); + } } void RegisterGroupModel::copyToFaders() diff --git a/registergroupsmodel.cpp b/registergroupsmodel.cpp index fd02f30..1ee6d24 100644 --- a/registergroupsmodel.cpp +++ b/registergroupsmodel.cpp @@ -5,6 +5,7 @@ #include #include #include +#include enum { IdRole = Qt::UserRole, @@ -190,14 +191,16 @@ bool RegisterGroupsModel::setData(const QModelIndex &index, const QVariant &valu return true; case IdRole: - if (value.userType() != QMetaType::Int) - { - qWarning() << "hilfe" << __LINE__ << value.userType(); - return false; - } - registerGroup.id = value.toInt(); - emit dataChanged(index, index, { IdRole }); - return true; +// if (value.userType() != QMetaType::Int) +// { +// qWarning() << "hilfe" << __LINE__ << value.userType(); +// return false; +// } +// registerGroup.id = value.toInt(); +// emit dataChanged(index, index, { IdRole }); +// return true; + qWarning() << "hilfe" << __LINE__; + return false; default: qWarning() << "hilfe" << __LINE__; return false; @@ -236,9 +239,12 @@ bool RegisterGroupsModel::insertRows(int row, int count, const QModelIndex &pare auto id = max_iter != std::cend(registerGroups) ? max_iter->id + 1 : 0; beginInsertRows({}, row, row+count-1); - auto iter = std::begin(registerGroups) + row; - for (auto i = 0; i < count; i++) - iter = registerGroups.insert(iter, RegisterGroupConfig{ .id=id++, .name="" }) + 1; + { + QMutexLocker locker{&m_controller->mutex()}; + auto iter = std::begin(registerGroups) + row; + for (auto i = 0; i < count; i++) + iter = registerGroups.insert(iter, RegisterGroupConfig{ .id=id++, .name="" }) + 1; + } endInsertRows(); disconnect(m_controller, &DmxController::registerGroupInserted, @@ -285,9 +291,12 @@ bool RegisterGroupsModel::removeRows(int row, int count, const QModelIndex &pare } beginRemoveRows({}, row, row+count-1); - auto begin = std::begin(registerGroups) + row; - auto end = begin + count; - registerGroups.erase(begin, end); + { + QMutexLocker locker{&m_controller->mutex()}; + auto begin = std::begin(registerGroups) + row; + auto end = begin + count; + registerGroups.erase(begin, end); + } endRemoveRows(); disconnect(m_controller, &DmxController::registerGroupRemoved,