Merge remote-tracking branch 'remotes/origin/add_effect_engine' into add_effect_engine

Former-commit-id: c7f7fe4307f7dfaddaef23b71c6ead3491d0e669
This commit is contained in:
T. van der Zwan 2013-12-13 09:06:19 +01:00
commit a20dcbfe8c
11 changed files with 223 additions and 29 deletions

View File

@ -133,7 +133,8 @@ void BoblightClientConnection::handleMessage(const QString & message)
if (messageParts[3] == "rgb" && messageParts.size() == 7) if (messageParts[3] == "rgb" && messageParts.size() == 7)
{ {
bool rc1, rc2, rc3; bool rc1, rc2, rc3;
uint8_t red = 255 * messageParts[4].toFloat(&rc1); uint8_t red = qMax(0, qMin(255, int(255 * messageParts[4].toFloat(&rc1))));
if (!rc1) if (!rc1)
{ {
// maybe a locale issue. switch to a locale with a comma instead of a dot as decimal seperator (or vice versa) // maybe a locale issue. switch to a locale with a comma instead of a dot as decimal seperator (or vice versa)
@ -141,11 +142,11 @@ void BoblightClientConnection::handleMessage(const QString & message)
_locale.setNumberOptions(QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator); _locale.setNumberOptions(QLocale::OmitGroupSeparator | QLocale::RejectGroupSeparator);
// try again // try again
red = 255 * messageParts[4].toFloat(&rc1); red = qMax(0, qMin(255, int(255 * messageParts[4].toFloat(&rc1))));
} }
uint8_t green = 255 * messageParts[5].toFloat(&rc2); uint8_t green = qMax(0, qMin(255, 255 * int(messageParts[5].toFloat(&rc2))));
uint8_t blue = 255 * messageParts[6].toFloat(&rc3); uint8_t blue = qMax(0, qMin(255, 255 * int(messageParts[6].toFloat(&rc3))));
if (rc1 && rc2 && rc3) if (rc1 && rc2 && rc3)
{ {
@ -153,6 +154,13 @@ void BoblightClientConnection::handleMessage(const QString & message)
rgb.red = red; rgb.red = red;
rgb.green = green; rgb.green = green;
rgb.blue = blue; rgb.blue = blue;
// send current color values to hyperion if this is the last led assuming leds values are send in order of id
if ((ledIndex == _ledColors.size() -1) && _priority < 255)
{
_hyperion->setColors(_priority, _ledColors, -1);
}
return; return;
} }
} }

View File

@ -40,6 +40,7 @@ SET(Hyperion_HEADERS
${CURRENT_SOURCE_DIR}/device/LedDeviceLpd6803.h ${CURRENT_SOURCE_DIR}/device/LedDeviceLpd6803.h
${CURRENT_SOURCE_DIR}/device/LedDeviceLpd8806.h ${CURRENT_SOURCE_DIR}/device/LedDeviceLpd8806.h
${CURRENT_SOURCE_DIR}/device/LedDeviceLightpack.h ${CURRENT_SOURCE_DIR}/device/LedDeviceLightpack.h
${CURRENT_SOURCE_DIR}/device/LedDevicePaintpack.h
${CURRENT_SOURCE_DIR}/device/LedDeviceMultiLightpack.h ${CURRENT_SOURCE_DIR}/device/LedDeviceMultiLightpack.h
) )
@ -65,6 +66,7 @@ SET(Hyperion_SOURCES
${CURRENT_SOURCE_DIR}/device/LedDeviceLpd8806.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceLpd8806.cpp
${CURRENT_SOURCE_DIR}/device/LedDeviceAdalight.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceAdalight.cpp
${CURRENT_SOURCE_DIR}/device/LedDeviceLightpack.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceLightpack.cpp
${CURRENT_SOURCE_DIR}/device/LedDevicePaintpack.cpp
${CURRENT_SOURCE_DIR}/device/LedDeviceMultiLightpack.cpp ${CURRENT_SOURCE_DIR}/device/LedDeviceMultiLightpack.cpp
) )
@ -87,6 +89,7 @@ add_library(hyperion
target_link_libraries(hyperion target_link_libraries(hyperion
hyperion-utils hyperion-utils
effectengine effectengine
hidapi-libusb
serialport serialport
${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev ${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}

View File

@ -23,6 +23,7 @@
#include "device/LedDeviceTest.h" #include "device/LedDeviceTest.h"
#include "device/LedDeviceWs2801.h" #include "device/LedDeviceWs2801.h"
#include "device/LedDeviceAdalight.h" #include "device/LedDeviceAdalight.h"
#include "device/LedDevicePaintpack.h"
#include "device/LedDeviceLightpack.h" #include "device/LedDeviceLightpack.h"
#include "device/LedDeviceMultiLightpack.h" #include "device/LedDeviceMultiLightpack.h"
@ -40,7 +41,7 @@ LedDevice* Hyperion::createDevice(const Json::Value& deviceConfig)
std::transform(type.begin(), type.end(), type.begin(), ::tolower); std::transform(type.begin(), type.end(), type.begin(), ::tolower);
LedDevice* device = nullptr; LedDevice* device = nullptr;
if (type == "ws2801") if (type == "ws2801" || type == "lightberry")
{ {
const std::string output = deviceConfig["output"].asString(); const std::string output = deviceConfig["output"].asString();
const unsigned rate = deviceConfig["rate"].asInt(); const unsigned rate = deviceConfig["rate"].asInt();
@ -99,6 +100,13 @@ LedDevice* Hyperion::createDevice(const Json::Value& deviceConfig)
device = deviceLightpack; device = deviceLightpack;
} }
else if (type == "paintpack")
{
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack();
devicePainLightpack->open();
device = devicePainLightpack;
}
else if (type == "multi-lightpack") else if (type == "multi-lightpack")
{ {
LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack(); LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack();

View File

@ -0,0 +1,77 @@
// Hyperion includes
#include "LedDevicePaintpack.h"
LedDevicePaintpack::LedDevicePaintpack() :
LedDevice(),
_deviceHandle(nullptr)
{
// empty
}
int LedDevicePaintpack::open()
{
// initialize the usb context
int error = hid_init();
if (error != 0)
{
std::cerr << "Error while initializing the hidapi context" << std::endl;
return -1;
}
std::cout << "Hidapi initialized" << std::endl;
// Initialise the paintpack device
const unsigned short Paintpack_VendorId = 0x0ebf;
const unsigned short Paintpack_ProductId = 0x0025;
_deviceHandle = hid_open(Paintpack_VendorId, Paintpack_ProductId, nullptr);
if (_deviceHandle == nullptr)
{
// Failed to open the device
std::cerr << "Failed to open HID Paintpakc device " << std::endl;
return -1;
}
return 0;
}
LedDevicePaintpack::~LedDevicePaintpack()
{
if (_deviceHandle != nullptr)
{
hid_close(_deviceHandle);
_deviceHandle = nullptr;
}
hid_exit();
}
int LedDevicePaintpack::write(const std::vector<ColorRgb>& ledValues)
{
if (_ledBuffer.size() < 3 + ledValues.size()*3)
{
_ledBuffer.resize(3 + ledValues.size()*3, uint8_t(0));
_ledBuffer[0] = 0;
_ledBuffer[1] = 3;
_ledBuffer[2] = 0;
}
auto bufIt = _ledBuffer.begin()+3;
for (const ColorRgb & ledValue : ledValues)
{
*bufIt = ledValue.red;
++bufIt;
*bufIt = ledValue.green;
++bufIt;
*bufIt = ledValue.blue;
++bufIt;
}
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size());
}
int LedDevicePaintpack::switchOff()
{
std::fill(_ledBuffer.begin()+3, _ledBuffer.end(), uint8_t(0));
return hid_write(_deviceHandle, _ledBuffer.data(), _ledBuffer.size());
}

View File

@ -0,0 +1,59 @@
#pragma once
// STL includes
#include <vector>
// libusb include
#include <hidapi/hidapi.h>
// Hyperion includes
#include <hyperion/LedDevice.h>
///
/// LedDevice implementation for a paintpack device ()
///
class LedDevicePaintpack : public LedDevice
{
public:
/**
* Constructs the paintpack device
*/
LedDevicePaintpack();
/**
* Destructs the paintpack device, closes USB connection if open
*/
virtual ~LedDevicePaintpack();
/**
* Opens the Paintpack device
*
* @return Zero on succes else negative
*/
int open();
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb>& ledValues);
///
/// Switch the leds off
///
/// @return Zero on success else negative
///
virtual int switchOff();
private:
/// libusb device handle
hid_device * _deviceHandle;
/// buffer for led data
std::vector<uint8_t> _ledBuffer;
};

View File

@ -114,10 +114,33 @@ void JsonClientConnection::handleColorCommand(const Json::Value &message)
// extract parameters // extract parameters
int priority = message["priority"].asInt(); int priority = message["priority"].asInt();
int duration = message.get("duration", -1).asInt(); int duration = message.get("duration", -1).asInt();
ColorRgb color = {uint8_t(message["color"][0u].asInt()), uint8_t(message["color"][1u].asInt()), uint8_t(message["color"][2u].asInt())};
std::vector<ColorRgb> colorData(_hyperion->getLedCount());
const Json::Value & jsonColor = message["color"];
Json::UInt i = 0;
for (; i < jsonColor.size()/3 && i < _hyperion->getLedCount(); ++i)
{
colorData[i].red = uint8_t(message["color"][3u*i].asInt());
colorData[i].green = uint8_t(message["color"][3u*i+1u].asInt());
colorData[i].blue = uint8_t(message["color"][3u*i+2u].asInt());
}
// copy full blocks of led colors
unsigned size = i;
while (i + size < _hyperion->getLedCount())
{
memcpy(&(colorData[i]), colorData.data(), size * sizeof(ColorRgb));
i += size;
}
// copy remaining block of led colors
if (i < _hyperion->getLedCount())
{
memcpy(&(colorData[i]), colorData.data(), (_hyperion->getLedCount()-i) * sizeof(ColorRgb));
}
// set output // set output
_hyperion->setColor(priority, color, duration); _hyperion->setColors(priority, colorData, duration);
// send reply // send reply
sendSuccessReply(); sendSuccessReply();

View File

@ -21,8 +21,7 @@
"items" :{ "items" :{
"type" : "integer" "type" : "integer"
}, },
"minItems": 3, "minItems": 3
"maxItems": 3
} }
}, },
"additionalProperties": false "additionalProperties": false

View File

@ -11,7 +11,7 @@
#include "ColorTransformValues.h" #include "ColorTransformValues.h"
/// Data parameter for a color /// Data parameter for a color
typedef vlofgren::PODParameter<QColor> ColorParameter; typedef vlofgren::PODParameter<std::vector<QColor>> ColorParameter;
/// Data parameter for an image /// Data parameter for an image
typedef vlofgren::PODParameter<QImage> ImageParameter; typedef vlofgren::PODParameter<QImage> ImageParameter;
@ -21,40 +21,54 @@ typedef vlofgren::PODParameter<ColorTransformValues> TransformParameter;
namespace vlofgren { namespace vlofgren {
/// ///
/// Translates a string (as passed on the commandline) to a color /// Translates a string (as passed on the commandline) to a vector of colors
/// ///
/// @param[in] s The string (as passed on the commandline) /// @param[in] s The string (as passed on the commandline)
/// ///
/// @return The translated color /// @return The translated colors
/// ///
/// @throws Parameter::ParameterRejected If the string did not result in a color /// @throws Parameter::ParameterRejected If the string did not result in a color
/// ///
template<> template<>
QColor ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected) std::vector<QColor> ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{ {
// Check if we can create the color by name // Check if we can create the color by name
QColor color(s.c_str()); QColor color(s.c_str());
if (color.isValid()) if (color.isValid())
{ {
return color; return std::vector<QColor>{color};
} }
// check if we can create the color by hex RRGGBB value // check if we can create the color by hex RRGGBB value
if (s.length() == 6 && isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4]) && isxdigit(s[5])) if (s.length() >= 6u && (s.length()%6) == 0u && std::count_if(s.begin(), s.end(), isxdigit) == s.length())
{ {
bool ok = true; bool ok = true;
std::vector<QColor> colors;
for (size_t j = 0; j < s.length()/6; ++j)
{
int rgb[3]; int rgb[3];
for (int i = 0; i < 3 && ok; ++i) for (int i = 0; i < 3 && ok; ++i)
{ {
QString colorComponent(s.substr(2*i, 2).c_str()); QString colorComponent(s.substr(6*j+2*i, 2).c_str());
rgb[i] = colorComponent.toInt(&ok, 16); rgb[i] = colorComponent.toInt(&ok, 16);
} }
if (ok)
{
color.setRgb(rgb[0], rgb[1], rgb[2]);
colors.push_back(color);
}
else
{
break;
}
}
// check if all components parsed succesfully // check if all components parsed succesfully
if (ok) if (ok)
{ {
color.setRgb(rgb[0], rgb[1], rgb[2]); return colors;
return color;
} }
} }
@ -65,7 +79,7 @@ namespace vlofgren {
} }
throw Parameter::ParameterRejected(errorMessage.str()); throw Parameter::ParameterRejected(errorMessage.str());
return color; return std::vector<QColor>{color};
} }
template<> template<>

View File

@ -39,18 +39,21 @@ JsonConnection::~JsonConnection()
_socket.close(); _socket.close();
} }
void JsonConnection::setColor(QColor color, int priority, int duration) void JsonConnection::setColor(std::vector<QColor> colors, int priority, int duration)
{ {
std::cout << "Set color to " << color.red() << " " << color.green() << " " << color.blue() << std::endl; std::cout << "Set color to " << colors[0].red() << " " << colors[0].green() << " " << colors[0].blue() << (colors.size() > 1 ? " + ..." : "") << std::endl;
// create command // create command
Json::Value command; Json::Value command;
command["command"] = "color"; command["command"] = "color";
command["priority"] = priority; command["priority"] = priority;
Json::Value & rgbValue = command["color"]; Json::Value & rgbValue = command["color"];
rgbValue[0] = color.red(); for (const QColor & color : colors)
rgbValue[1] = color.green(); {
rgbValue[2] = color.blue(); rgbValue.append(color.red());
rgbValue.append(color.green());
rgbValue.append(color.blue());
}
if (duration > 0) if (duration > 0)
{ {
command["duration"] = duration; command["duration"] = duration;

View File

@ -41,7 +41,7 @@ public:
/// @param priority The priority /// @param priority The priority
/// @param duration The duration in milliseconds /// @param duration The duration in milliseconds
/// ///
void setColor(QColor color, int priority, int duration); void setColor(std::vector<QColor> color, int priority, int duration);
/// ///
/// Set the leds according to the given image (assume the image is stretched to the display size) /// Set the leds according to the given image (assume the image is stretched to the display size)

View File

@ -40,7 +40,7 @@ int main(int argc, char * argv[])
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData()); StringParameter & argAddress = parameters.add<StringParameter> ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData());
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority" , QString("Use to the provided priority channel (the lower the number, the higher the priority) [default: %1]").arg(defaultPriority).toAscii().constData()); IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority" , QString("Use to the provided priority channel (the lower the number, the higher the priority) [default: %1]").arg(defaultPriority).toAscii().constData());
IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]"); IntParameter & argDuration = parameters.add<IntParameter> ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]");
ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name)"); ColorParameter & argColor = parameters.add<ColorParameter> ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)");
ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file"); ImageParameter & argImage = parameters.add<ImageParameter> ('i', "image" , "Set the leds to the colors according to the given image file");
StringParameter & argEffect = parameters.add<StringParameter> ('e', "effect" , "Enable the effect with the given name"); StringParameter & argEffect = parameters.add<StringParameter> ('e', "effect" , "Enable the effect with the given name");
StringParameter & argEffectArgs = parameters.add<StringParameter> (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string."); StringParameter & argEffectArgs = parameters.add<StringParameter> (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string.");