add hsv2rgb; rainbow puke should work now
This commit is contained in:
parent
c1d0fef585
commit
3607db3c4c
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 B. Stultiens
|
||||
* modified by true for 12-bit values
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "hsv2rgb.h"
|
||||
|
||||
|
||||
/*
|
||||
void hsv2rgb_8b(uint16_t h, uint16_t s, uint16_t v, uint16_t *r, uint16_t *g , uint16_t *b)
|
||||
{
|
||||
uint8_t sextant;
|
||||
uint8_t bb;
|
||||
uint16_t ww;
|
||||
uint8_t h_fraction;
|
||||
|
||||
if (!(s)) {
|
||||
*(r) = *(g) = *(b) = (v);
|
||||
return;
|
||||
}
|
||||
|
||||
sextant = h >> 8;
|
||||
HSV_SEXTANT_TEST(sextant); // Optional: Limit hue sextants to defined space
|
||||
|
||||
HSV_POINTER_SWAP(sextant, r, g, b); // Swap pointers depending which sextant we are in
|
||||
|
||||
*g = v; // Top level
|
||||
|
||||
// Perform actual calculations
|
||||
|
||||
// Bottom level: v * (1.0 - s)
|
||||
// --> (v * (255 - s) + error_corr) / 256
|
||||
|
||||
bb = ~s;
|
||||
ww = v * bb;
|
||||
ww += 1; // Error correction
|
||||
ww += ww >> 8; // Error correction
|
||||
*b = ww >> 8;
|
||||
|
||||
h_fraction = h & 0xff; // 0...255
|
||||
|
||||
if(!(sextant & 1)) {
|
||||
// *r = ...slope_up...;
|
||||
|
||||
// Slope up: v * (1.0 - s * (1.0 - h))
|
||||
// --> (v * (255 - (s * (256 - h) + error_corr1) / 256) + error_corr2) / 256
|
||||
|
||||
ww = !h_fraction ? ((uint16_t)s << 8) : (s * (uint8_t)(-h_fraction));
|
||||
ww += ww >> 8; // Error correction 1
|
||||
bb = ww >> 8;
|
||||
bb = ~bb;
|
||||
ww = v * bb;
|
||||
ww += v >> 1; // Error correction 2
|
||||
*r = ww >> 8;
|
||||
} else {
|
||||
// *r = ...slope_down...;
|
||||
|
||||
// Slope down: v * (1.0 - s * h)
|
||||
// --> (v * (255 - (s * h + error_corr1) / 256) + error_corr2) / 256
|
||||
|
||||
ww = s * h_fraction;
|
||||
ww += ww >> 8; // Error correction 1
|
||||
bb = ww >> 8;
|
||||
bb = ~bb;
|
||||
ww = v * bb;
|
||||
ww += v >> 1; // Error correction 2
|
||||
*r = ww >> 8;
|
||||
|
||||
// A perfect match for h_fraction == 0 implies:
|
||||
// *r = (ww >> 8) + (h_fraction ? 0 : 1)
|
||||
// However, this is an extra calculation that may not be required.
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void hsv2rgb_32b_8(uint16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g , uint8_t *b)
|
||||
{
|
||||
HSV_MONOCHROMATIC_TEST(s, v, r, g, b); // Exit with grayscale if s == 0
|
||||
|
||||
uint8_t sextant = h >> 8;
|
||||
|
||||
HSV_SEXTANT_TEST(sextant); // Optional: Limit hue sextants to defined space
|
||||
|
||||
HSV_POINTER_SWAP(sextant, r, g, b); // Swap pointers depending which sextant we are in
|
||||
|
||||
*g = v; // Top level
|
||||
|
||||
// Perform actual calculations
|
||||
|
||||
// Bottom level: v * (1.0 - s)
|
||||
// --> (v * (255 - s) + error_corr + 1) / 256
|
||||
|
||||
uint16_t ww; // Intermediate result
|
||||
ww = v * (255 - s); // We don't use ~s to prevent size-promotion side effects
|
||||
ww += 1; // Error correction
|
||||
ww += ww >> 8; // Error correction
|
||||
*b = ww >> 8;
|
||||
|
||||
uint8_t h_fraction = h & 0xff; // 0...255
|
||||
uint32_t d; // Intermediate result
|
||||
|
||||
if(!(sextant & 1)) {
|
||||
// *r = ...slope_up...;
|
||||
d = v * (uint32_t)((255 << 8) - (uint16_t)(s * (256 - h_fraction)));
|
||||
d += d >> 8; // Error correction
|
||||
d += v; // Error correction
|
||||
*r = d >> 16;
|
||||
} else {
|
||||
// *r = ...slope_down...;
|
||||
d = v * (uint32_t)((255 << 8) - (uint16_t)(s * h_fraction));
|
||||
d += d >> 8; // Error correction
|
||||
d += v; // Error correction
|
||||
*r = d >> 16;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void hsv2rgb_32b_16(uint16_t h, uint16_t s, uint16_t v, uint16_t *r, uint16_t *g , uint16_t *b)
|
||||
{
|
||||
HSV_MONOCHROMATIC_TEST(s, v, r, g, b); // Exit with grayscale if s == 0
|
||||
|
||||
uint8_t sextant = h >> 8;
|
||||
|
||||
HSV_SEXTANT_TEST(sextant); // Optional: Limit hue sextants to defined space
|
||||
|
||||
HSV_POINTER_SWAP(sextant, r, g, b); // Swap pointers depending which sextant we are in
|
||||
|
||||
*g = v; // Top level
|
||||
|
||||
// Bottom level: v * (1.0 - s)
|
||||
// --> (v * (255 - s) + error_corr + 1) / 256
|
||||
|
||||
uint16_t ww; // Intermediate result
|
||||
ww = v * (255 - s); // We don't use ~s to prevent size-promotion side effects
|
||||
ww += 1; // Error correction
|
||||
ww += ww >> 8; // Error correction
|
||||
*b = ww >> 8;
|
||||
|
||||
uint8_t h_fraction = h & 0xff; // 0...255
|
||||
uint32_t d; // Intermediate result
|
||||
|
||||
if(!(sextant & 1)) {
|
||||
// *r = ...slope_up...;
|
||||
d = v * (uint32_t)((255 << 8) - (uint16_t)(s * (256 - h_fraction)));
|
||||
d += d >> 8; // Error correction
|
||||
d += v; // Error correction
|
||||
*r = d >> 16;
|
||||
} else {
|
||||
// *r = ...slope_down...;
|
||||
d = v * (uint32_t)((255 << 8) - (uint16_t)(s * h_fraction));
|
||||
d += d >> 8; // Error correction
|
||||
d += v; // Error correction
|
||||
*r = d >> 16;
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 B. Stultiens
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef __INC_HSV2RGB_H__
|
||||
#define __INC_HSV2RGB_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef struct color_rgb {
|
||||
uint16_t r;
|
||||
uint16_t g;
|
||||
uint16_t b;
|
||||
} color_rgb;
|
||||
|
||||
typedef struct color_hsv {
|
||||
uint16_t h;
|
||||
uint16_t s;
|
||||
uint16_t v;
|
||||
} color_hsv;
|
||||
|
||||
|
||||
#define HSV2RGB_BITS 8 // 10
|
||||
#define HSV2RGB_COUNT ((1 < HSV2RGB_BITS)-1)
|
||||
|
||||
#define HSV_HUE_SEXTANT (1 << HSV2RGB_BITS)
|
||||
#define HSV_HUE_STEPS (6 * HSV_HUE_SEXTANT)
|
||||
|
||||
#define HSV_HUE_MIN 0
|
||||
#define HSV_HUE_MAX (HSV_HUE_STEPS - 1)
|
||||
#define HSV_SAT_MIN 0
|
||||
#define HSV_SAT_MAX 255
|
||||
#define HSV_VAL_MIN 0
|
||||
#define HSV_VAL_MAX 255
|
||||
|
||||
/* Options: */
|
||||
#define HSV_USE_SEXTANT_TEST /* Limit the hue to 0...360 degrees */
|
||||
|
||||
#define hsv2rgb(h,s,v,r,g,b) hsv2rgb_32b_8(h,s,v,r,g,b)
|
||||
|
||||
|
||||
//void hsv2rgb_8b(uint16_t h, uint16_t s, uint16_t v, uint16_t *r, uint16_t *g , uint16_t *b);
|
||||
void hsv2rgb_32b_8(uint16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g , uint8_t *b);
|
||||
//void hsv2rgb_32b_16(uint16_t h, uint16_t s, uint16_t v, uint16_t *r, uint16_t *g , uint16_t *b);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Macros that are common to all implementations
|
||||
*/
|
||||
#ifdef HSV_USE_SEXTANT_TEST
|
||||
#define HSV_SEXTANT_TEST(sextant) \
|
||||
if((sextant) > 5) { \
|
||||
(sextant) = 5; \
|
||||
}
|
||||
|
||||
#else
|
||||
#define HSV_SEXTANT_TEST(sextant)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Pointer swapping:
|
||||
* sext. r g b r<>b g<>b r <> g result
|
||||
* 0 0 0 v u c !u v c u v c
|
||||
* 0 0 1 d v c d v c
|
||||
* 0 1 0 c v u u v c u v c
|
||||
* 0 1 1 c d v v d c d v c d v c
|
||||
* 1 0 0 u c v u v c u v c
|
||||
* 1 0 1 v c d v d c d v c d v c
|
||||
*
|
||||
* if(sextant & 2)
|
||||
* r <-> b
|
||||
*
|
||||
* if(sextant & 4)
|
||||
* g <-> b
|
||||
*
|
||||
* if(!(sextant & 6) {
|
||||
* if(!(sextant & 1))
|
||||
* r <-> g
|
||||
* } else {
|
||||
* if(sextant & 1)
|
||||
* r <-> g
|
||||
* }
|
||||
*/
|
||||
#define HSV_SWAPPTR(a,b) do { uint8_t *tmp = (a); (a) = (b); (b) = tmp; } while(0)
|
||||
//#define HSV_SWAPPTR(a,b) do { uint16_t *tmp = (a); (a) = (b); (b) = tmp; } while(0)
|
||||
#define HSV_POINTER_SWAP(sextant,r,g,b) \
|
||||
do { \
|
||||
if((sextant) & 2) { \
|
||||
HSV_SWAPPTR((r), (b)); \
|
||||
} \
|
||||
if((sextant) & 4) { \
|
||||
HSV_SWAPPTR((g), (b)); \
|
||||
} \
|
||||
if(!((sextant) & 6)) { \
|
||||
if(!((sextant) & 1)) { \
|
||||
HSV_SWAPPTR((r), (g)); \
|
||||
} \
|
||||
} else { \
|
||||
if((sextant) & 1) { \
|
||||
HSV_SWAPPTR((r), (g)); \
|
||||
} \
|
||||
} \
|
||||
} while(0)
|
||||
#define HSV_MONOCHROMATIC_TEST(s,v,r,g,b) \
|
||||
do { \
|
||||
if(!(s)) { \
|
||||
*(r) = *(g) = *(b) = (v); \
|
||||
return; \
|
||||
} \
|
||||
} while(0)
|
||||
#endif
|
|
@ -49,12 +49,6 @@
|
|||
|
||||
|
||||
|
||||
#include <tinyNeoPixel_Static.h>
|
||||
|
||||
tinyNeoPixel rgb = tinyNeoPixel(RGB_COUNT, PIN_PA1, NEO_GRB, rgbled);
|
||||
|
||||
|
||||
|
||||
enum {
|
||||
RGB_PROG_IDLE,
|
||||
RGB_PROG_INIT,
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
#include <Arduino.h>
|
||||
#include "rgbled.h"
|
||||
|
||||
#include "hsv2rgb.h"
|
||||
|
||||
|
||||
|
||||
tinyNeoPixel rgb = tinyNeoPixel(RGB_COUNT, PIN_PA1, NEO_GRB, rgbled);
|
||||
|
||||
|
||||
|
||||
uint8_t rgbp_rainbow(uint8_t init);
|
||||
|
@ -43,9 +49,11 @@ ISR(TCB0_INT_vect)
|
|||
|
||||
|
||||
// rgb program 0: rainbow puke
|
||||
#define RAINBOW_HUE_INC 50
|
||||
#define RAINBOW_OFFSET (1536/5)
|
||||
#define RAINBOW_TIMEOUT 240
|
||||
#define RAINBOW_HUE_INC 40 // how much to increment the hue every frame
|
||||
#define RAINBOW_OFFSET (1536/5) // offset between each LED
|
||||
#define RAINBOW_TIMEOUT 240 // how long to show this program
|
||||
#define RAINBOW_SAT 0xff // saturation
|
||||
#define RAINBOW_VAL 0x40 // value (brightness); keep low enough to keep average current down
|
||||
|
||||
uint16_t rainbow_hue = 0;
|
||||
uint16_t rainbow_timeout;
|
||||
|
@ -53,24 +61,33 @@ uint16_t rainbow_timeout;
|
|||
uint8_t rgbp_rainbow(uint8_t init)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t r, g, b;
|
||||
uint16_t hue;
|
||||
|
||||
// set our timer when initializing. otherwise every call is identical
|
||||
if (init) {
|
||||
rainbow_timeout = RAINBOW_TIMEOUT;
|
||||
}
|
||||
|
||||
if (--rainbow_timeout) {
|
||||
hue = rainbow_hue;
|
||||
// copy stored hue to working hue
|
||||
hue = rainbow_hue;
|
||||
|
||||
for (i = 0; i < RGB_COUNT; i++) {
|
||||
hue += RAINBOW_OFFSET;
|
||||
if (hue >= 1536) hue -= 1536;
|
||||
// each LED will increment its hue
|
||||
hue += RAINBOW_OFFSET;
|
||||
|
||||
// hue wheel is 256*6 large, so bound the value
|
||||
if (hue >= 1536) hue -= 1536;
|
||||
|
||||
// apply color
|
||||
// TODO
|
||||
// compute rgb from hue/sat/value
|
||||
hsv2rgb(hue, RAINBOW_SAT, RAINBOW_VAL, &r, &g, &b);
|
||||
|
||||
// apply it to this LED
|
||||
rgb.setPixelColor(i, r, g, b);
|
||||
}
|
||||
|
||||
// move the hue wheel for the next cycle through the program
|
||||
// increment stored hue wheel for the next cycle through the program
|
||||
rainbow_hue += RAINBOW_HUE_INC;
|
||||
if (rainbow_hue > 1536) rainbow_hue -= 1536;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <tinyNeoPixel_Static.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
@ -11,6 +12,8 @@
|
|||
|
||||
|
||||
|
||||
extern tinyNeoPixel rgb;
|
||||
|
||||
extern uint8_t rgbled[3 * RGB_COUNT];
|
||||
extern uint8_t (*rgb_program[1])(uint8_t);
|
||||
|
||||
|
|
Loading…
Reference in New Issue