From f1b9d7a06ec703c27824d33ee6f167289214a4c1 Mon Sep 17 00:00:00 2001 From: Micah Elizabeth Scott <micah@scanlime.org> Date: Sun, 9 Mar 2014 17:41:17 -0700 Subject: [PATCH] CPU utilization tweaks; default frame rate, no draw when disconnected --- examples/cpp/lib/effect.h | 29 +++++++++++++++++------------ examples/cpp/lib/opcclient.h | 15 ++++++++++----- examples/cpp/spokes.cpp | 1 - 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/examples/cpp/lib/effect.h b/examples/cpp/lib/effect.h index 0337c7c..a7db83e 100644 --- a/examples/cpp/lib/effect.h +++ b/examples/cpp/lib/effect.h @@ -182,7 +182,8 @@ inline EffectRunner::EffectRunner() lastTime.tv_sec = 0; lastTime.tv_usec = 0; - // Default server + // Defaults + setMaxFrameRate(300); setServer("localhost"); } @@ -279,22 +280,26 @@ inline void EffectRunner::doFrame(float timeDelta) frameInfo.advance(timeDelta); effect->beginFrame(frameInfo); - uint8_t *dest = OPCClient::Header::view(frameBuffer).data(); + // Only calculate the effect if we have a connection + if (opc.tryConnect()) { + + uint8_t *dest = OPCClient::Header::view(frameBuffer).data(); - for (PixelInfoIter i = frameInfo.pixels.begin(), e = frameInfo.pixels.end(); i != e; ++i) { - Vec3 rgb(0, 0, 0); - const PixelInfo &p = *i; + for (PixelInfoIter i = frameInfo.pixels.begin(), e = frameInfo.pixels.end(); i != e; ++i) { + Vec3 rgb(0, 0, 0); + const PixelInfo &p = *i; - if (p.isMapped()) { - effect->calculatePixel(rgb, p); - } + if (p.isMapped()) { + effect->calculatePixel(rgb, p); + } - for (unsigned i = 0; i < 3; i++) { - *(dest++) = std::min<int>(255, std::max<int>(0, rgb[i] * 255 + 0.5)); + for (unsigned i = 0; i < 3; i++) { + *(dest++) = std::min<int>(255, std::max<int>(0, rgb[i] * 255 + 0.5)); + } } - } - opc.write(frameBuffer); + opc.write(frameBuffer); + } effect->endFrame(frameInfo); diff --git a/examples/cpp/lib/opcclient.h b/examples/cpp/lib/opcclient.h index 0ccff62..413fa31 100644 --- a/examples/cpp/lib/opcclient.h +++ b/examples/cpp/lib/opcclient.h @@ -47,6 +47,8 @@ public: bool resolve(const char *hostport, int defaultPort = 7890); bool write(const uint8_t *data, ssize_t length); bool write(const std::vector<uint8_t> &data); + + bool tryConnect(); bool isConnected(); struct Header { @@ -139,12 +141,15 @@ inline bool OPCClient::isConnected() return fd > 0; } +inline bool OPCClient::tryConnect() +{ + return isConnected() || connectSocket(); +} + inline bool OPCClient::write(const uint8_t *data, ssize_t length) { - if (!isConnected()) { - if (!connectSocket()) { - return false; - } + if (!tryConnect()) { + return false; } while (length > 0) { @@ -170,7 +175,7 @@ inline bool OPCClient::connectSocket() fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (connect(fd, (struct sockaddr*) &address, sizeof address) < 0) { - close(fd); + closeSocket(); return false; } diff --git a/examples/cpp/spokes.cpp b/examples/cpp/spokes.cpp index ab8d5db..1d03fa9 100644 --- a/examples/cpp/spokes.cpp +++ b/examples/cpp/spokes.cpp @@ -76,7 +76,6 @@ int main(int argc, char **argv) EffectRunner r; r.setEffect(&br); - r.setMaxFrameRate(200); r.setLayout("../layouts/grid32x16z.json"); return r.main(argc, argv); } -- GitLab