Skip to content

Commit

Permalink
New Plugin: Boblight
Browse files Browse the repository at this point in the history
  • Loading branch information
mzanetti committed Jan 6, 2019
1 parent 391f7f3 commit 987f37b
Show file tree
Hide file tree
Showing 12 changed files with 1,108 additions and 1 deletion.
89 changes: 89 additions & 0 deletions boblight/bobchannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "bobchannel.h"

BobChannel::BobChannel(const int &id, QObject *parent) :
QObject(parent),
m_id(id)
{
m_animation = new QPropertyAnimation(this, "finalColor", this);
m_animation->setDuration(500);
m_animation->setEasingCurve(QEasingCurve::InOutQuad);
}

int BobChannel::id() const
{
return m_id;
}

QColor BobChannel::color() const
{
return m_color;
}

void BobChannel::setColor(const QColor &color)
{
m_color = color;
emit colorChanged();

if (m_animation->state() == QPropertyAnimation::Running) {
m_animation->stop();
}

m_animation->setStartValue(m_finalColor);
m_animation->setEndValue(color);
m_animation->start();
}

bool BobChannel::power() const
{
return m_power;
}

void BobChannel::setPower(bool power)
{
if (power != m_power) {
m_power = power;
emit powerChanged();

if (m_animation->state() == QPropertyAnimation::Running) {
m_animation->stop();
}

QColor target = m_color;
target.setAlpha(target.alpha() * (m_power ? 1 : 0));
m_animation->setStartValue(m_finalColor);
m_animation->setEndValue(target);
m_animation->start();
}
}

QColor BobChannel::finalColor() const
{
return m_finalColor;
}

void BobChannel::setFinalColor(const QColor &color)
{
m_finalColor = color;
emit finalColorChanged();
}

66 changes: 66 additions & 0 deletions boblight/bobchannel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef BOBCHANNEL_H
#define BOBCHANNEL_H

#include <QColor>
#include <QObject>
#include <QPropertyAnimation>

class BobChannel : public QObject
{
Q_OBJECT
Q_PROPERTY(bool power READ power WRITE setPower NOTIFY powerChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)

// don't directly write to this, the propertyAnimation requires a writable property tho...
Q_PROPERTY(QColor finalColor READ finalColor WRITE setFinalColor NOTIFY finalColorChanged)

public:
explicit BobChannel(const int &id, QObject *parent = 0);

int id() const;

QColor color() const;
void setColor(const QColor &color);

bool power() const;
void setPower(bool power);

QColor finalColor() const;
void setFinalColor(const QColor &color);

private:
QPropertyAnimation *m_animation;
int m_id;
bool m_power = false;
QColor m_color = Qt::white;
QColor m_finalColor = Qt::black;

signals:
void colorChanged();
void brightnessChanged();
void finalColorChanged();
void powerChanged();

};

#endif // BOBCHANNEL_H
181 changes: 181 additions & 0 deletions boblight/bobclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* Copyright (C) 2014 Michael Zanetti <michael_zanetti@gmx.net> *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "bobclient.h"
#include "extern-plugininfo.h"

#include "libboblight/boblight.h"

#include <QDebug>
#include <QtConcurrent>

BobClient::BobClient(const QString &host, const int &port, QObject *parent) :
QObject(parent),
m_host(host),
m_port(port),
m_connected(false)
{
m_syncTimer = new QTimer(this);
m_syncTimer->setSingleShot(false);
m_syncTimer->setInterval(50);

connect(m_syncTimer, SIGNAL(timeout()), this, SLOT(sync()));
}

BobClient::~BobClient()
{
if (m_boblight) {
boblight_destroy(m_boblight);
}
}

bool BobClient::connectToBoblight()
{
if (connected()) {
return true;
}
m_boblight = boblight_init();

//try to connect, if we can't then bitch to stderr and destroy boblight
if (!boblight_connect(m_boblight, m_host.toLatin1().data(), m_port, 1000000)) {
qCWarning(dcBoblight) << "Failed to connect:" << boblight_geterror(m_boblight);
boblight_destroy(m_boblight);
m_boblight = nullptr;
setConnected(false);
return false;
}

qCDebug(dcBoblight) << "Connected to boblightd successfully.";
boblight_setpriority(m_boblight, m_priority);
for (int i = 0; i < lightsCount(); ++i) {
BobChannel *channel = new BobChannel(i, this);
channel->setColor(QColor(255,255,255,0));
connect(channel, SIGNAL(colorChanged()), this, SLOT(sync()));
m_channels.insert(i, channel);
}
setConnected(true);
return true;
}

bool BobClient::connected()
{
return m_connected;
}

void BobClient::setPriority(int priority)
{
m_priority = priority;
if (connected()) {
qCDebug(dcBoblight) << "setting priority to" << priority << boblight_setpriority(m_boblight, priority);
}
emit priorityChanged(priority);
}

void BobClient::setPower(int channel, bool power)
{
qCDebug(dcBoblight()) << "BobClient: setPower" << channel << power;
m_channels.value(channel)->setPower(power);
emit powerChanged(channel, power);
}

BobChannel *BobClient::getChannel(const int &id)
{
foreach (BobChannel *channel, m_channels) {
if (channel->id() == id)
return channel;
}
return 0;
}

void BobClient::setColor(int channel, QColor color)
{
if (channel == -1) {
for (int i = 0; i < lightsCount(); ++i) {
setColor(i, color);
}
} else {
BobChannel *c = getChannel(channel);
if (c) {
c->setColor(color);
qCDebug(dcBoblight) << "set channel" << channel << "to color" << color;
emit colorChanged(channel, color);
}
}
}

void BobClient::setBrightness(int channel, int brightness)
{
QColor color = m_channels.value(channel)->color();
color.setAlpha(qRound(brightness * 255.0 / 100));
m_channels.value(channel)->setColor(color);
emit brightnessChanged(channel, brightness);

if (brightness > 0) {
m_channels.value(channel)->setPower(true);
emit powerChanged(channel, true);
}
}

void BobClient::sync()
{
if (!m_connected)
return;

foreach (BobChannel *channel, m_channels) {
int rgb[3];
rgb[0] = channel->finalColor().red() * channel->finalColor().alphaF();
rgb[1] = channel->finalColor().green() * channel->finalColor().alphaF();
rgb[2] = channel->finalColor().blue() * channel->finalColor().alphaF();
boblight_addpixel(m_boblight, channel->id(), rgb);
}

if (!boblight_sendrgb(m_boblight, 1, NULL)) {
qCWarning(dcBoblight) << "Boblight connection error:" << boblight_geterror(m_boblight);
boblight_destroy(m_boblight);
qDeleteAll(m_channels);
m_channels.clear();
setConnected(false);
}
}

void BobClient::setConnected(bool connected)
{
m_connected = connected;
emit connectionChanged();

// if disconnected, delete all channels
if (!connected) {
m_syncTimer->stop();
qDeleteAll(m_channels);
} else {
m_syncTimer->start();
}
}

int BobClient::lightsCount()
{
return boblight_getnrlights(m_boblight);
}

QColor BobClient::currentColor(const int &channel)
{
return getChannel(channel)->color();
}
Loading

0 comments on commit 987f37b

Please sign in to comment.