Feature/raspberry pi blinkt color objects (#503)
* fixed BACnetXYcolor and BACnetColorCommand encode and decoding and improved unit test coverage. Refactored BACnetXYcolor to/from ascii into lighting module. * added to the color, color temperature, and lighting output objects a fade/ramp/step engine. Added color and color command coercion into the channel object and enabled color temperature object coercion. Added CreateObject and DeleteObject service handling to the color, color temperature, lighting output, and channel objects. * added blinkt demo app for Raspberry Pi [WIP] * updated gitignore to simplify handling of apps folder contents * fixed piface demo build * added RaspiOS to pipeline for piface and blinkt! demo builds * added device object timer function for child object types into example Device object. Refactored device object to increment database revision for create or delete object services. Refactored example app/server to use mstimer library and device child object timers. --------- Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
@@ -41,14 +41,16 @@ static double clamp(double d, double min, double max)
|
||||
* @param x_coordinate - return x of CIE xy 0.0..1.0
|
||||
* @param y_coordinate - return y of CIE xy 0.0..1.0
|
||||
* @param brightness - return brightness of the CIE xy color 0..255
|
||||
* @param gamma_correction - true if gamma correction is applied
|
||||
* @note http://en.wikipedia.org/wiki/Srgb
|
||||
*/
|
||||
void color_rgb_to_xy(uint8_t r,
|
||||
static void color_rgb_to_xy_gamma_correction(uint8_t r,
|
||||
uint8_t g,
|
||||
uint8_t b,
|
||||
float *x_coordinate,
|
||||
float *y_coordinate,
|
||||
uint8_t *brightness)
|
||||
uint8_t *brightness,
|
||||
bool gamma_correction)
|
||||
{
|
||||
float X, Y, Z;
|
||||
float x, y;
|
||||
@@ -63,18 +65,20 @@ void color_rgb_to_xy(uint8_t r,
|
||||
green /= 255.0f;
|
||||
blue /= 255.0f;
|
||||
|
||||
/* Apply a gamma correction to the RGB values,
|
||||
which makes the color more vivid and more the
|
||||
like the color displayed on the screen of your device.
|
||||
This gamma correction is also applied to the screen
|
||||
of your computer or phone, thus we need this to create
|
||||
the same color on the light as on screen. */
|
||||
red = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f)
|
||||
: (red / 12.92f);
|
||||
green = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f)
|
||||
: (green / 12.92f);
|
||||
blue = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f)
|
||||
: (blue / 12.92f);
|
||||
if (gamma_correction) {
|
||||
/* Apply a gamma correction to the RGB values,
|
||||
which makes the color more vivid and more the
|
||||
like the color displayed on the screen of your device.
|
||||
This gamma correction is also applied to the screen
|
||||
of your computer or phone, thus we need this to create
|
||||
the same color on the light as on screen. */
|
||||
red = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f)
|
||||
: (red / 12.92f);
|
||||
green = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f)
|
||||
: (green / 12.92f);
|
||||
blue = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f)
|
||||
: (blue / 12.92f);
|
||||
}
|
||||
|
||||
/* Convert the RGB values to XYZ using the
|
||||
Wide RGB D65 conversion formula */
|
||||
@@ -107,6 +111,48 @@ void color_rgb_to_xy(uint8_t r,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert sRGB to CIE xy
|
||||
* @param r - R value of sRGB 0..255
|
||||
* @param g - G value of sRGB 0..255
|
||||
* @param b - B value of sRGB 0..255
|
||||
* @param x_coordinate - return x of CIE xy 0.0..1.0
|
||||
* @param y_coordinate - return y of CIE xy 0.0..1.0
|
||||
* @param brightness - return brightness of the CIE xy color 0..255
|
||||
* @note http://en.wikipedia.org/wiki/Srgb
|
||||
*/
|
||||
void color_rgb_to_xy(uint8_t r,
|
||||
uint8_t g,
|
||||
uint8_t b,
|
||||
float *x_coordinate,
|
||||
float *y_coordinate,
|
||||
uint8_t *brightness)
|
||||
{
|
||||
color_rgb_to_xy_gamma_correction(r, g, b,
|
||||
x_coordinate, y_coordinate, brightness, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert sRGB to CIE xy, with gamma correction
|
||||
* @param r - R value of sRGB 0..255
|
||||
* @param g - G value of sRGB 0..255
|
||||
* @param b - B value of sRGB 0..255
|
||||
* @param x_coordinate - return x of CIE xy 0.0..1.0
|
||||
* @param y_coordinate - return y of CIE xy 0.0..1.0
|
||||
* @param brightness - return brightness of the CIE xy color 0..255
|
||||
* @note http://en.wikipedia.org/wiki/Srgb
|
||||
*/
|
||||
void color_rgb_to_xy_gamma(uint8_t r,
|
||||
uint8_t g,
|
||||
uint8_t b,
|
||||
float *x_coordinate,
|
||||
float *y_coordinate,
|
||||
uint8_t *brightness)
|
||||
{
|
||||
color_rgb_to_xy_gamma_correction(r, g, b,
|
||||
x_coordinate, y_coordinate, brightness, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert sRGB from CIE xy and brightness
|
||||
* @param red - return R value of sRGB
|
||||
@@ -115,14 +161,16 @@ void color_rgb_to_xy(uint8_t r,
|
||||
* @param x_coordinate - x of CIE xy
|
||||
* @param y_coordinate - y of CIE xy
|
||||
* @param brightness - brightness of the CIE xy color
|
||||
* @param gamma_correction - true if gamma correction is needed
|
||||
* @note http://en.wikipedia.org/wiki/Srgb
|
||||
*/
|
||||
void color_rgb_from_xy(uint8_t *red,
|
||||
static void color_rgb_from_xy_gamma_correction(uint8_t *red,
|
||||
uint8_t *green,
|
||||
uint8_t *blue,
|
||||
float x_coordinate,
|
||||
float y_coordinate,
|
||||
uint8_t brightness)
|
||||
uint8_t brightness,
|
||||
bool gamma_correction)
|
||||
{
|
||||
float r, g, b;
|
||||
float x, y, z, X, Y, Z;
|
||||
@@ -142,13 +190,15 @@ void color_rgb_from_xy(uint8_t *red,
|
||||
g = -X * 0.5217933f + Y * 1.4472381f + Z * 0.0677227f;
|
||||
b = X * 0.0349342f - Y * 0.0968930f + Z * 1.2884099f;
|
||||
|
||||
/* Apply reverse gamma correction */
|
||||
r = r <= 0.0031308f ? 12.92f * r
|
||||
: (1.0f + 0.055f) * pow(r, (1.0f / 2.4f)) - 0.055f;
|
||||
g = g <= 0.0031308f ? 12.92f * g
|
||||
: (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f;
|
||||
b = b <= 0.0031308f ? 12.92f * b
|
||||
: (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f;
|
||||
if (gamma_correction) {
|
||||
/* Apply reverse gamma correction */
|
||||
r = r <= 0.0031308f ? 12.92f * r
|
||||
: (1.0f + 0.055f) * pow(r, (1.0f / 2.4f)) - 0.055f;
|
||||
g = g <= 0.0031308f ? 12.92f * g
|
||||
: (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f;
|
||||
b = b <= 0.0031308f ? 12.92f * b
|
||||
: (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f;
|
||||
}
|
||||
|
||||
/* Convert the RGB values to your color object
|
||||
The rgb values from the above formulas are
|
||||
@@ -171,6 +221,48 @@ void color_rgb_from_xy(uint8_t *red,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert sRGB from CIE xy and brightness
|
||||
* @param red - return R value of sRGB
|
||||
* @param green - return G value of sRGB
|
||||
* @param blue - return B value of sRGB
|
||||
* @param x_coordinate - x of CIE xy
|
||||
* @param y_coordinate - y of CIE xy
|
||||
* @param brightness - brightness of the CIE xy color
|
||||
* @note http://en.wikipedia.org/wiki/Srgb
|
||||
*/
|
||||
void color_rgb_from_xy(uint8_t *red,
|
||||
uint8_t *green,
|
||||
uint8_t *blue,
|
||||
float x_coordinate,
|
||||
float y_coordinate,
|
||||
uint8_t brightness)
|
||||
{
|
||||
color_rgb_from_xy_gamma_correction(red, green, blue,
|
||||
x_coordinate, y_coordinate, brightness, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert sRGB from CIE xy and brightness, with gamma correction
|
||||
* @param red - return R value of sRGB
|
||||
* @param green - return G value of sRGB
|
||||
* @param blue - return B value of sRGB
|
||||
* @param x_coordinate - x of CIE xy
|
||||
* @param y_coordinate - y of CIE xy
|
||||
* @param brightness - brightness of the CIE xy color
|
||||
* @note http://en.wikipedia.org/wiki/Srgb
|
||||
*/
|
||||
void color_rgb_from_xy_gamma(uint8_t *red,
|
||||
uint8_t *green,
|
||||
uint8_t *blue,
|
||||
float x_coordinate,
|
||||
float y_coordinate,
|
||||
uint8_t brightness)
|
||||
{
|
||||
color_rgb_from_xy_gamma_correction(red, green, blue,
|
||||
x_coordinate, y_coordinate, brightness, true);
|
||||
}
|
||||
|
||||
/* table for converting RGB to and from ASCII color names */
|
||||
struct css_color_rgb {
|
||||
const char *name;
|
||||
|
||||
@@ -22,6 +22,13 @@ BACNET_STACK_EXPORT
|
||||
void color_rgb_from_xy(uint8_t *red, uint8_t *green, uint8_t *blue,
|
||||
float x_coordinate, float y_coordinate, uint8_t brightness);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
void color_rgb_to_xy_gamma(uint8_t r, uint8_t g, uint8_t b,
|
||||
float *x_coordinate, float *y_coordinate, uint8_t *brightness);
|
||||
BACNET_STACK_EXPORT
|
||||
void color_rgb_from_xy_gamma(uint8_t *red, uint8_t *green, uint8_t *blue,
|
||||
float x_coordinate, float y_coordinate, uint8_t brightness);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
const char * color_rgb_to_ascii(uint8_t red, uint8_t green, uint8_t blue);
|
||||
BACNET_STACK_EXPORT
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @file
|
||||
* @author Steve Karg
|
||||
* @date 2011
|
||||
* @brief Performs linear interpolation using single precision floating
|
||||
* point math or integer math, or a mixture of both.
|
||||
*
|
||||
* @section LICENSE
|
||||
*
|
||||
* Copyright (C) 2011 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
* Performs linear interpolation using single precision floating
|
||||
* point math or integer math, or a mixture of both.
|
||||
* Linear interpolation is a method of constructing new data
|
||||
* points within the range of a discrete set of known data points.
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "linear.h"
|
||||
|
||||
/**
|
||||
* Linearly Interpolate the values between y1 and y3 based on x.
|
||||
*
|
||||
* @param x1 - first x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param x2 - intermediate x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param x3 - last x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param y1 - first y value, where y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* @param y3 - last y value, where y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* @return y2 - an intermediate y value y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* and the y value is linearly proportional to x1, x2, and x2.
|
||||
*/
|
||||
float linear_interpolate(float x1,
|
||||
float x2,
|
||||
float x3,
|
||||
float y1,
|
||||
float y3)
|
||||
{
|
||||
float y2;
|
||||
|
||||
if (y3 > y1) {
|
||||
y2 = y1 + (((x2 - x1) * (y3 - y1)) / (x3 - x1));
|
||||
} else {
|
||||
y2 = y1 - (((x2 - x1) * (y1 - y3)) / (x3 - x1));
|
||||
}
|
||||
|
||||
return y2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Linearly Interpolate the values between y1 and y3 based on x
|
||||
* and round up the result. Useful for integer interpolation.
|
||||
*
|
||||
* @param x1 - first x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param x2 - intermediate x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param x3 - last x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param y1 - first y value, where y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* @param y3 - last y value, where y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* @return y2 - an intermediate y value y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* and the y value is linearly proportional to x1, x2, and x2.
|
||||
*/
|
||||
float linear_interpolate_round(float x1,
|
||||
float x2,
|
||||
float x3,
|
||||
float y1,
|
||||
float y3)
|
||||
{
|
||||
float y2;
|
||||
|
||||
y2 = linear_interpolate(x1, x2, x3, y1, y3);
|
||||
/* round away from zero */
|
||||
if (y2 > 0.0) {
|
||||
y2 += 0.5;
|
||||
} else if (y2 < 0.0) {
|
||||
y2 -= 0.5;
|
||||
}
|
||||
|
||||
return y2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Linearly Interpolate the values between y1 and y3 based on x
|
||||
* using integer math.
|
||||
*
|
||||
* @param x1 - first x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param x2 - intermediate x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param x3 - last x value, where x1 <= x2 <= x3 or x1 >= x2 >= x3
|
||||
* @param y1 - first y value, where y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* @param y3 - last y value, where y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* @return y2 - an intermediate y value y1 <= y2 <= y3 or y1 >= y2 >= y3
|
||||
* and the y value is linearly proportional to x1, x2, and x2.
|
||||
*/
|
||||
long linear_interpolate_int(long x1,
|
||||
long x2,
|
||||
long x3,
|
||||
long y1,
|
||||
long y3)
|
||||
{
|
||||
long y2;
|
||||
|
||||
if (y3 > y1) {
|
||||
y2 = y1 + (((x2 - x1) * (y3 - y1)) / (x3 - x1));
|
||||
} else {
|
||||
y2 = y1 - (((x2 - x1) * (y1 - y3)) / (x3 - x1));
|
||||
}
|
||||
|
||||
return y2;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @file
|
||||
* @author Steve Karg
|
||||
* @date 2011
|
||||
*/
|
||||
#ifndef LINEAR_H
|
||||
#define LINEAR_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
float linear_interpolate(float x1,
|
||||
float x2,
|
||||
float x3,
|
||||
float y1,
|
||||
float y3);
|
||||
|
||||
float linear_interpolate_round(float x1,
|
||||
float x2,
|
||||
float x3,
|
||||
float y1,
|
||||
float y3);
|
||||
|
||||
long linear_interpolate_int(long x1,
|
||||
long x2,
|
||||
long x3,
|
||||
long y1,
|
||||
long y3);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif
|
||||
Reference in New Issue
Block a user