Skip to content

Commit

Permalink
moved thread in dmxdevice, fixed gap instead of fixed frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Oct 24, 2024
1 parent 7b9f328 commit c644123
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 74 deletions.
124 changes: 57 additions & 67 deletions Source/Interface/interfaces/dmx/DMXInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

DMXInterface::DMXInterface() :
Interface(getTypeString()),
Thread("DMX Interface"),
dmxInterfaceNotifier(20)
{

Expand All @@ -27,7 +26,6 @@ DMXInterface::DMXInterface() :
defaultSubnet = addIntParameter("Subnet", "If applicable the subnet for this universe", 0, 0, 15, false);
defaultUniverse = addIntParameter("Universe", "The universe", 0, 0, 15, false);

sendRate = addIntParameter("Send Rate", "The rate at which to send data.", 40, 1, 200);
sendOnChangeOnly = addBoolParameter("Send On Change Only", "Only send a universe if one of its channels has changed", false);


Expand Down Expand Up @@ -55,10 +53,7 @@ void DMXInterface::onContainerParameterChanged(Parameter* p)
Interface::onContainerParameterChanged(p);
if (p == enabled)
{
if (dmxDevice != nullptr) dmxDevice->enabled = enabled->boolValue();
if (enabled->boolValue() && dmxDevice != nullptr) startThread();
else stopThread(1000);

if (dmxDevice != nullptr) dmxDevice->setEnabled(enabled->boolValue());
}
else if (p == dmxType)
{
Expand All @@ -70,8 +65,6 @@ void DMXInterface::setCurrentDMXDevice(DMXDevice* d)
{
if (dmxDevice.get() == d) return;

stopThread(1000);

GenericScopedLock lock(deviceLock);

if (dmxDevice != nullptr)
Expand Down Expand Up @@ -116,8 +109,6 @@ void DMXInterface::setCurrentDMXDevice(DMXDevice* d)
}

interfaceNotifier.addMessage(new InterfaceEvent(InterfaceEvent::DEVICE_CHANGED, this));

if (enabled->boolValue() && dmxDevice != nullptr) startThread();
}

void DMXInterface::dmxDeviceSetupChanged(DMXDevice*)
Expand Down Expand Up @@ -192,44 +183,43 @@ void DMXInterface::sendValuesForObjectInternal(Object* o)

for (int i = 0; i < channelsData.size(); i++)
{
if (logOutgoingData->boolValue()) NLOG(niceName, String(i + 1) << " : " << (int)(channelsData[i]));
//if (logOutgoingData->boolValue()) NLOG(niceName, String(i + 1) << " : " << (int)(channelsData[i]));
u->updateValue(i, (int)channelsData[i], sOnChangeOnly);
}
}

void DMXInterface::finishSendValues()
{
bool hasOneDirty = false;
Array<DMXUniverse*> universesToAdd;

String sentUniverses = "";

bool sendOnChange = sendOnChangeOnly->boolValue();
bool log = logOutgoingData->boolValue();

GenericScopedLock lock(deviceLock);
if (dmxDevice == nullptr) return;

for (auto& u : universes)
{
if (!u->isDirty && sendOnChange) continue;
universesToAdd.add(new DMXUniverse(u));
hasOneDirty |= u->isDirty;
u->isDirty = false;

if (log) sentUniverses += "\n" + u->toString();

dmxDevice->setDMXValues(u);

Array<uint8> values(u->values.getRawDataPointer(), u->values.size());
dmxInterfaceNotifier.addMessage(new DMXInterfaceEvent(DMXInterfaceEvent::UNIVERSE_SENT, u, values));
}

universesToSend.clear();
universesToSend.addArray(universesToAdd);

outActivityTrigger->trigger();

if (hasOneDirty && log)
{
NLOG(niceName, "Sending Universes : " + sentUniverses);
}


}


Expand All @@ -253,55 +243,55 @@ DMXUniverse* DMXInterface::getUniverse(int net, int subnet, int universe, bool c
return u;
}

void DMXInterface::run()
{
setPriority(Thread::Priority::high);
while (!threadShouldExit())
{
double loopStartTime = Time::getMillisecondCounterHiRes();

{
bool sendOnChange = sendOnChangeOnly->boolValue();

{
GenericScopedLock dlock(deviceLock);
if (dmxDevice == nullptr) continue;
}

GenericScopedLock ulock(universesToSend.getLock());

for (auto& u : universesToSend)
{
if (sendOnChange && !u->isDirty)
{
DBG("skipping here");
continue;
}

dmxDevice->sendDMXValues(u);

}


}


double t = Time::getMillisecondCounterHiRes();
double diffTime = t - loopStartTime;
double rateMS = 1000.0 / sendRate->intValue();



double msToWait = rateMS - diffTime;

if (msToWait > 0) wait(msToWait);
else
{
LOGWARNING("DMX Interface loop took too long : " << (int)(diffTime) << "ms");
}

}
}
//void DMXInterface::run()
//{
// setPriority(Thread::Priority::high);
// while (!threadShouldExit())
// {
// double loopStartTime = Time::getMillisecondCounterHiRes();
//
// {
// bool sendOnChange = sendOnChangeOnly->boolValue();
//
// {
// GenericScopedLock dlock(deviceLock);
// if (dmxDevice == nullptr) continue;
// }
//
// GenericScopedLock ulock(universesToSend.getLock());
//
// for (auto& u : universesToSend)
// {
// if (sendOnChange && !u->isDirty)
// {
// DBG("skipping here");
// continue;
// }
//
// dmxDevice->sendDMXValues(u);
//
// }
//
//
// }
//
//
// double t = Time::getMillisecondCounterHiRes();
// double diffTime = t - loopStartTime;
// double rateMS = 1000.0 / sendRate->intValue();
//
//
//
// double msToWait = rateMS - diffTime;
//
// if (msToWait > 0) wait(msToWait);
// else
// {
// LOGWARNING("DMX Interface loop took too long : " << (int)(diffTime) << "ms");
// }
//
// }
//}

InterfaceUI* DMXInterface::createUI()
{
Expand Down
8 changes: 1 addition & 7 deletions Source/Interface/interfaces/dmx/DMXInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

class DMXInterface :
public Interface,
public DMXDevice::DMXDeviceListener,
public Thread
public DMXDevice::DMXDeviceListener
{
public:
DMXInterface();
Expand All @@ -30,7 +29,6 @@ class DMXInterface :
BoolParameter* channelTestingMode;
FloatParameter* channelTestingFlashValue;

IntParameter* sendRate;
BoolParameter* sendOnChangeOnly;

IntParameter* defaultNet;
Expand All @@ -40,8 +38,6 @@ class DMXInterface :
OwnedArray<DMXUniverse> universes;
HashMap<int, DMXUniverse*> universeIdMap; //internally used

OwnedArray<DMXUniverse, CriticalSection> universesToSend;

void clearItem() override;

void onContainerParameterChanged(Parameter* p) override;
Expand All @@ -61,8 +57,6 @@ class DMXInterface :

DMXUniverse* getUniverse(int net, int subnet, int universe, bool createIfNotExist = true);

void run() override;

class DMXParams : public ControllableContainer
{
public:
Expand Down

0 comments on commit c644123

Please sign in to comment.