From 19fc139ac383cf7ad82c541110cffefcabad3f73 Mon Sep 17 00:00:00 2001 From: "Keith T. Garner" Date: Mon, 17 May 2021 14:27:54 -0500 Subject: [PATCH] process led update events in bulk --- .../LEDBinding.hpp | 7 ++++- .../blinkstick_userspace_led_daemon_fwd.hpp | 3 ++ .../src/BluldRunner.cpp | 9 +++++- .../src/LEDBinding.cpp | 28 ++++++++++++++----- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/LEDBinding.hpp b/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/LEDBinding.hpp index 6758ba5..8504438 100644 --- a/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/LEDBinding.hpp +++ b/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/LEDBinding.hpp @@ -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: @@ -54,6 +58,7 @@ class LEDBinding BlinkStickPtr mBlinkstick; uint8_t mIndex; RGBColorPtr mColor; + int mBrightness; int mULedsFileDescriptor; }; } // namespace BlinkstickUserspace diff --git a/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/blinkstick_userspace_led_daemon_fwd.hpp b/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/blinkstick_userspace_led_daemon_fwd.hpp index ba353f5..d45a0c5 100644 --- a/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/blinkstick_userspace_led_daemon_fwd.hpp +++ b/blinkstick-userspace-led-daemon/include/blinkstick_userspace_led_daemon/blinkstick_userspace_led_daemon_fwd.hpp @@ -43,4 +43,7 @@ typedef std::shared_ptr RGBColorVectorPtr; typedef std::vector StringVector; typedef std::shared_ptr StringVectorPtr; + +typedef std::vector IntVector; +typedef std::shared_ptr IntVectorPtr; } // namespace BlinkstickUserspace diff --git a/blinkstick-userspace-led-daemon/src/BluldRunner.cpp b/blinkstick-userspace-led-daemon/src/BluldRunner.cpp index ab62f9b..0e00013 100644 --- a/blinkstick-userspace-led-daemon/src/BluldRunner.cpp +++ b/blinkstick-userspace-led-daemon/src/BluldRunner.cpp @@ -21,6 +21,7 @@ // SOFTWARE. #include #include +#include #include @@ -140,6 +141,9 @@ void BluldRunner::run() fds[i] = mLEDBindings->at(i)->getPollFd(); } + + IntVectorPtr touchedBindings = IntVectorPtr(new IntVector()); + mKeepRunning = true; while (mKeepRunning) { @@ -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(); } } } diff --git a/blinkstick-userspace-led-daemon/src/LEDBinding.cpp b/blinkstick-userspace-led-daemon/src/LEDBinding.cpp index 849641c..4eb3598 100644 --- a/blinkstick-userspace-led-daemon/src/LEDBinding.cpp +++ b/blinkstick-userspace-led-daemon/src/LEDBinding.cpp @@ -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() @@ -105,9 +104,9 @@ bool LEDBinding::setOff() bool LEDBinding::processBrightnessChange() { - const int brightness = getBrightness(); + updateBrightness(); - if (brightness) + if (mBrightness) { return setOn(); } @@ -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); +}