First working version with some test executables

This commit is contained in:
T. van der Zwan
2013-07-26 20:38:34 +00:00
commit 10b5b80675
62 changed files with 15621 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
/*
* boblight
* Copyright (C) Bob 2009
*
* boblight 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, either version 3 of the License, or
* (at your option) any later version.
*
* boblight 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
//these definitions can be expanded to make normal prototypes, or functionpointers and dlsym lines
BOBLIGHT_FUNCTION(void*, boblight_init, ());
BOBLIGHT_FUNCTION(void, boblight_destroy, (void* vpboblight));
BOBLIGHT_FUNCTION(int, boblight_connect, (void* vpboblight, const char* address, int port, int usectimeout));
BOBLIGHT_FUNCTION(int, boblight_setpriority, (void* vpboblight, int priority));
BOBLIGHT_FUNCTION(const char*, boblight_geterror, (void* vpboblight));
BOBLIGHT_FUNCTION(int, boblight_getnrlights, (void* vpboblight));
BOBLIGHT_FUNCTION(const char*, boblight_getlightname, (void* vpboblight, int lightnr));
BOBLIGHT_FUNCTION(int, boblight_getnroptions, (void* vpboblight));
BOBLIGHT_FUNCTION(const char*, boblight_getoptiondescript,(void* vpboblight, int option));
BOBLIGHT_FUNCTION(int, boblight_setoption, (void* vpboblight, int lightnr, const char* option));
BOBLIGHT_FUNCTION(int, boblight_getoption, (void* vpboblight, int lightnr, const char* option, const char** output));
BOBLIGHT_FUNCTION(void, boblight_setscanrange, (void* vpboblight, int width, int height));
BOBLIGHT_FUNCTION(int, boblight_addpixel, (void* vpboblight, int lightnr, int* rgb));
BOBLIGHT_FUNCTION(void, boblight_addpixelxy, (void* vpboblight, int x, int y, int* rgb));
BOBLIGHT_FUNCTION(int, boblight_sendrgb, (void* vpboblight, int sync, int* outputused));
BOBLIGHT_FUNCTION(int, boblight_ping, (void* vpboblight, int* outputused));

103
include/boblight.h Normal file
View File

@@ -0,0 +1,103 @@
/*
* boblight
* Copyright (C) Bob 2009
*
* boblight 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, either version 3 of the License, or
* (at your option) any later version.
*
* boblight 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
//if you define BOBLIGHT_DLOPEN, all boblight functions are defined as pointers
//you can then call boblight_loadlibrary to load libboblight with dlopen and the function pointers with dlsym
//if you pass NULL to boblight_loadlibrary's first argument, the default filename for libboblight is used
//if boblight_loadlibrary returns NULL, dlopen and dlsym went ok, if not it returns a char* from dlerror
//if you want to use the boblight functions from multiple files, you can define BOBLIGHT_DLOPEN in one file,
//and define BOBLIGHT_DLOPEN_EXTERN in the other file, the functionpointers are then defined as extern
#ifndef LIBBOBLIGHT
#define LIBBOBLIGHT
#if !defined(BOBLIGHT_DLOPEN) && !defined(BOBLIGHT_DLOPEN_EXTERN)
#ifdef __cplusplus
extern "C" {
#endif
//generate normal prototypes
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) returnvalue name arguments
#include "boblight-functions.h"
#undef BOBLIGHT_FUNCTION
#ifdef __cplusplus
}
#endif
#elif defined(BOBLIGHT_DLOPEN)
#include <dlfcn.h>
#include <stddef.h>
//generate function pointers
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) returnvalue (* name ) arguments = NULL
#include "boblight-functions.h"
#undef BOBLIGHT_FUNCTION
#ifdef __cplusplus
#define BOBLIGHT_CAST(value) reinterpret_cast<value>
#else
#define BOBLIGHT_CAST(value) (value)
#endif
//gets a functionpointer from dlsym, and returns char* from dlerror if it didn't work
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) \
name = BOBLIGHT_CAST(returnvalue (*) arguments)(dlsym(p_boblight, #name)); \
{ char* error = dlerror(); if (error) return error; }
void* p_boblight = NULL; //where we put the lib
//load function pointers
char* boblight_loadlibrary(const char* filename)
{
if (filename == NULL)
filename = "libboblight.so";
if (p_boblight != NULL)
{
dlclose(p_boblight);
p_boblight = NULL;
}
p_boblight = dlopen(filename, RTLD_NOW);
if (p_boblight == NULL)
return dlerror();
//generate dlsym lines
#include "boblight-functions.h"
return NULL;
}
#undef BOBLIGHT_FUNCTION
#undef BOBLIGHT_CAST
//you can define BOBLIGHT_DLOPEN_EXTERN when you load the library in another file
#elif defined(BOBLIGHT_DLOPEN_EXTERN)
extern char* boblight_loadlibrary(const char* filename);
extern void* p_boblight;
#define BOBLIGHT_FUNCTION(returnvalue, name, arguments) extern returnvalue (* name ) arguments
#include "boblight-functions.h"
#undef BOBLIGHT_FUNCTION
#endif //BOBLIGHT_DLOPEN_EXTERN
#endif //LIBBOBLIGHT

View File

@@ -0,0 +1,41 @@
#pragma once
// hyperion-utils includes
#include <utils/RgbImage.h>
#include <hyperion/LedString.h>
#include <hyperion/ImageToLedsMap.h>
#include <hyperion/LedDevice.h>
class Hyperion
{
public:
Hyperion(const Json::Value& jsonConfig);
~Hyperion();
void setInputSize(const unsigned width, const unsigned height);
RgbImage& image()
{
return *mImage;
}
void commit();
void operator() (const RgbImage& inputImage);
void setColor(const RgbColor& color);
private:
void applyTransform(std::vector<RgbColor>& colors) const;
LedString mLedString;
RgbImage* mImage;
ImageToLedsMap mLedsMap;
LedDevice* mDevice;
};

View File

@@ -0,0 +1,27 @@
#pragma once
// hyperion-utils includes
#include <utils/RgbImage.h>
// hyperion includes
#include <hyperion/LedString.h>
class ImageToLedsMap
{
public:
ImageToLedsMap();
void createMapping(const RgbImage& image, const std::vector<Led>& leds);
std::vector<RgbColor> getMeanLedColor();
RgbColor findMeanColor(const std::vector<const RgbColor*>& colors);
std::vector<RgbColor> getMedianLedColor();
RgbColor findMedianColor(std::vector<const RgbColor*>& colors);
private:
std::vector<std::vector<const RgbColor*> > mColorsMap;
};

View File

@@ -0,0 +1,27 @@
#pragma once
// STL incldues
#include <vector>
// Utility includes
#include <utils/RgbColor.h>
class LedDevice
{
public:
/**
* Empty virtual destructor for pure virtual base class
*/
virtual ~LedDevice()
{
// empty
}
/**
* Writes the RGB-Color values to the leds.
*
* @param[in] ledValues The RGB-color per led
*/
virtual int write(const std::vector<RgbColor>& ledValues) = 0;
};

View File

@@ -0,0 +1,72 @@
#pragma once
// STL includes
#include <ctime>
#include <string>
#include <vector>
// Local includes
#include <utils/RgbColor.h>
// Forward class declarations
namespace Json { class Value; }
/**
* The Led structure contains the definition of the image portion used to determine a single led's
* color.
* <pre>
* |--------------------image--|
* | minX maxX |
* | |-----|maxY |
* | | | |
* | |-----|minY |
* | |
* | |
* | |
* |---------------------------|
* <endpre>
*/
struct Led
{
/** The index of the led */
unsigned index;
/** The minimum vertical scan line included for this leds color */
double minX_frac;
/** The maximum vertical scan line included for this leds color */
double maxX_frac;
/** The minimum horizontal scan line included for this leds color */
double minY_frac;
/** The maximum horizontal scan line included for this leds color */
double maxY_frac;
};
class LedString
{
public:
static LedString construct(const Json::Value& ledConfig, const Json::Value& colorConfig);
LedString();
~LedString();
const std::vector<Led>& leds() const;
private:
std::vector<Led> mLeds;
public:
/**
* Color adjustements per color
*/
struct
{
/** The color gradient */
double gamma;
/** The color offset */
double adjust;
/** The minimum required level for the led to turn on */
double blacklevel;
} red, green, blue;
};

31
include/utils/RgbColor.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
// STL includes
#include <stdint.h>
#include <iostream>
// Forward class declaration
struct RgbColor;
struct RgbColor
{
uint8_t red;
uint8_t green;
uint8_t blue;
static RgbColor BLACK;
static RgbColor RED;
static RgbColor GREEN;
static RgbColor BLUE;
static RgbColor YELLOW;
static RgbColor WHITE;
};
inline std::ostream& operator<<(std::ostream& os, const RgbColor& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
return os;
}

56
include/utils/RgbImage.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include <cassert>
#include <cstring>
// Local includes
#include "RgbColor.h"
class RgbImage
{
public:
RgbImage(const unsigned width, const unsigned height, const RgbColor background = RgbColor::BLACK);
~RgbImage();
inline unsigned width() const
{
return mWidth;
}
inline unsigned height() const
{
return mHeight;
}
void setPixel(const unsigned x, const unsigned y, const RgbColor color);
const RgbColor& operator()(const unsigned x, const unsigned y) const;
RgbColor& operator()(const unsigned x, const unsigned y);
inline void copy(const RgbImage& other)
{
std::cout << "This image size: [" << width() << "x" << height() << "]. Other image size: [" << other.width() << "x" << other.height() << "]" << std::endl;
assert(other.mWidth == mWidth);
assert(other.mHeight == mHeight);
memcpy(mColors, other.mColors, mWidth*mHeight*sizeof(RgbColor));
}
private:
inline unsigned toIndex(const unsigned x, const unsigned y) const
{
return y*mWidth + x;
}
private:
unsigned mWidth;
unsigned mHeight;
/** The colors of the image */
RgbColor* mColors;
};

View File

@@ -0,0 +1,66 @@
#pragma once
#include <string>
#include <istream>
#include <fstream>
#include <stdexcept>
#include <sstream>
// JSON-Schema includes
#include <utils/jsonschema/JsonSchemaChecker.h>
class JsonFactory
{
public:
static int load(const std::string& schema, const std::istream& config, Json::Value json);
static int load(const std::string& schema, const std::string& config, Json::Value& json)
{
// Load the schema and the config trees
Json::Value schemaTree = readJson(schema);
Json::Value configTree = readJson(config);
// create the validator
JsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaTree);
bool valid = schemaChecker.validate(configTree);
for (const std::string& message : schemaChecker.getMessages())
{
std::cout << message << std::endl;
}
if (!valid)
{
std::cerr << "Validation failed for configuration file: " << config.c_str() << std::endl;
return -3;
}
json = configTree;
return 0;
}
static Json::Value readJson(const std::string& filename)
{
// Open the file input stream
std::ifstream ifs(filename.c_str());
return readJson(ifs);
}
static Json::Value readJson(std::istream& stream)
{
// will contains the root value after parsing.
Json::Value jsonTree;
Json::Reader reader;
if (! reader.parse(stream, jsonTree, false))
{
// report to the user the failure and their locations in the document.
std::stringstream sstream;
sstream << "Failed to parse configuration: " << reader.getFormattedErrorMessages().c_str();
throw std::runtime_error(sstream.str());
}
return jsonTree;
}
};

View File

@@ -0,0 +1,75 @@
// Copyright (c) 2012 TNO, The Netherlands.
//
// This file contains information proprietary to TNO.
//
// Any disclosure or use of this information or any reproduction of this document or any part thereof for
// other than the specified purpose for which it is intended is expressly prohibited except as TNO may
// otherwise agree to in writing.
#pragma once
// stl includes
#include <string>
#include <list>
// jsoncpp includes
#include <json/json.h>
/**
* JsonSchemaChecker is a very basic implementation of json schema.
* The json schema definition draft can be found at
* http://tools.ietf.org/html/draft-zyp-json-schema-03
*
* The following keywords are supported:
* - type
* - required
* - properties
* - items
* - enum
* - minimum
* - maximum
*/
class JsonSchemaChecker
{
public:
JsonSchemaChecker();
virtual ~JsonSchemaChecker();
bool setSchema(const Json::Value & schema);
bool validate(const Json::Value & value);
const std::list<std::string> & getMessages() const;
private:
void collectReferences(const Json::Value & schema);
void validate(const Json::Value &value, const Json::Value & schema);
void setMessage(const std::string & message);
void collectDependencies(const Json::Value & value, const Json::Value &schema);
private:
// attribute check functions
void checkType(const Json::Value & value, const Json::Value & schema);
void checkProperties(const Json::Value & value, const Json::Value & schema);
void checkAdditionalProperties(const Json::Value & value, const Json::Value & schema, const Json::Value::Members & ignoredProperties);
void checkDependencies(const Json::Value & value, const Json::Value & schemaLink);
void checkMinimum(const Json::Value & value, const Json::Value & schema);
void checkMaximum(const Json::Value & value, const Json::Value & schema);
void checkItems(const Json::Value & value, const Json::Value & schema);
void checkMinItems(const Json::Value & value, const Json::Value & schema);
void checkMaxItems(const Json::Value & value, const Json::Value & schema);
void checkUniqueItems(const Json::Value & value, const Json::Value & schema);
void checkEnum(const Json::Value & value, const Json::Value & schema);
private:
Json::Value _schema;
std::list<std::string> _currentPath;
std::list<std::string> _messages;
bool _error;
std::map<std::string, const Json::Value *> _references; // ref 2 value
};