Skip to content

Commit

Permalink
process led update events in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
ktgeek committed May 17, 2021
1 parent 5a7cdec commit 19fc139
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ class LEDBinding

struct pollfd getPollFd() noexcept;

int getBrightness();
// updates the brightness value from the uled, stores it in this object, and returns the value
int updateBrightness();

bool setOn();
bool setOff();

bool processBrightnessChange();

// This assumes all LED Bindings are from the same blinkstick. Weird stuff will happen if they aren't.
static void bulkUpdate(LEDBindingVectorPtr bindings, IntVectorPtr changed);

std::string getName() noexcept;

private:
Expand All @@ -54,6 +58,7 @@ class LEDBinding
BlinkStickPtr mBlinkstick;
uint8_t mIndex;
RGBColorPtr mColor;
int mBrightness;
int mULedsFileDescriptor;
};
} // namespace BlinkstickUserspace
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ typedef std::shared_ptr<RGBColorVector> RGBColorVectorPtr;

typedef std::vector<std::string> StringVector;
typedef std::shared_ptr<StringVector> StringVectorPtr;

typedef std::vector<unsigned int> IntVector;
typedef std::shared_ptr<IntVector> IntVectorPtr;
} // namespace BlinkstickUserspace
9 changes: 8 additions & 1 deletion blinkstick-userspace-led-daemon/src/BluldRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// SOFTWARE.
#include <unistd.h>
#include <poll.h>
#include <string>

#include <hidapi/hidapi.h>

Expand Down Expand Up @@ -140,6 +141,9 @@ void BluldRunner::run()
fds[i] = mLEDBindings->at(i)->getPollFd();
}


IntVectorPtr touchedBindings = IntVectorPtr(new IntVector());

mKeepRunning = true;
while (mKeepRunning)
{
Expand All @@ -156,9 +160,12 @@ void BluldRunner::run()
{
if (fds[i].revents && POLLIN)
{
mLEDBindings->at(i)->processBrightnessChange();
touchedBindings->push_back(i);
}
}

LEDBinding::bulkUpdate(mLEDBindings, touchedBindings);
touchedBindings->clear();
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions blinkstick-userspace-led-daemon/src/LEDBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,17 @@ struct pollfd LEDBinding::getPollFd() noexcept
return fd;
}

int LEDBinding::getBrightness()
int LEDBinding::updateBrightness()
{
int returnVal;
const ssize_t dataRead = read(mULedsFileDescriptor, &returnVal, sizeof(returnVal));
const ssize_t dataRead = read(mULedsFileDescriptor, &mBrightness, sizeof(mBrightness));

// TODO: throw an exception in this case
if (dataRead < 1)
{
return 0;
mBrightness = 0;
}

return returnVal;
return mBrightness;
}

bool LEDBinding::setOn()
Expand All @@ -105,9 +104,9 @@ bool LEDBinding::setOff()

bool LEDBinding::processBrightnessChange()
{
const int brightness = getBrightness();
updateBrightness();

if (brightness)
if (mBrightness)
{
return setOn();
}
Expand All @@ -121,3 +120,18 @@ std::string LEDBinding::getName() noexcept
{
return mName;
}

void LEDBinding::bulkUpdate(LEDBindingVectorPtr bindings, IntVectorPtr changed_leds)
{
for (unsigned int changed_led : *changed_leds) {
bindings->at(changed_led)->updateBrightness();
}

RGBColorVectorPtr colors = RGBColorVectorPtr(new RGBColorVector());
for (LEDBindingPtr binding : *bindings)
{
colors->push_back(binding->mBrightness ? binding->mColor : nullptr);
}

bindings->front()->mBlinkstick->setColors(bindings->size(), colors);
}

0 comments on commit 19fc139

Please sign in to comment.