Skip to content

Commit

Permalink
first pass at hsb library
Browse files Browse the repository at this point in the history
  • Loading branch information
mpinner committed Feb 13, 2013
1 parent ad640d8 commit ab7fdcc
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 0 deletions.
77 changes: 77 additions & 0 deletions HSB_to_RGB/HSB_to_RGB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
File: HSB_to_RGB.cpp
Created for Arduino by @mpinner
orginial code by David A. Mellis, November 2, 2007.
Released into the public domain.
*/

#include "HSB_to_RGB.h"

void HSB_to_RGB::getRGB(int hue, int sat, int val, int colors[3]) {
/* convert hue, saturation and brightness ( HSB/HSV ) to RGB
The dim_curve is used only on brightness/value and on saturation (inverted).
This looks the most natural.
*/


val = dim_curve[val];
sat = 255-dim_curve[255-sat];

int r;
int g;
int b;
int base;

if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
colors[0]=val;
colors[1]=val;
colors[2]=val;
} else {

base = ((255 - sat) * val)>>8;

switch(hue/60) {
case 0:
r = val;
g = (((val-base)*hue)/60)+base;
b = base;
break;

case 1:
r = (((val-base)*(60-(hue%60)))/60)+base;
g = val;
b = base;
break;

case 2:
r = base;
g = val;
b = (((val-base)*(hue%60))/60)+base;
break;

case 3:
r = base;
g = (((val-base)*(60-(hue%60)))/60)+base;
b = val;
break;

case 4:
r = (((val-base)*(hue%60))/60)+base;
g = base;
b = val;
break;

case 5:
r = val;
g = base;
b = (((val-base)*(60-(hue%60)))/60)+base;
break;
}

colors[0]=r;
colors[1]=g;
colors[2]=b;
}

}
63 changes: 63 additions & 0 deletions HSB_to_RGB/HSB_to_RGB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
File: HSB_to_RGB.h
Created for Arduino by @mpinner
orginial code by David A. Mellis, November 2, 2007.
Released into the public domain.
*/

#ifndef HSP_TO_RGB_h
#define HSP_TO_RGB_h

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#include <pins_arduino.h>
#endif

/*
Control a RGB led with Hue, Saturation and Brightness (HSB / HSV )
Hue is change by an analog input.
Brightness is changed by a fading function.
Saturation stays constant at 255
getRGB() function based on <http://www.codeproject.com/miscctrl/CPicker.asp>
dim_curve idea by Jims
created 05-01-2010 by kasperkamperman.com
*/

/*
dim_curve 'lookup table' to compensate for the nonlinearity of human vision.
Used in the getRGB function on saturation and brightness to make 'dimming' look more natural.
Exponential function used to create values below :
x from 0 - 255 : y = round(pow( 2.0, x+64/40.0) - 1)
*/

const byte dim_curve[] = {
0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 11, 11, 11,
11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15,
15, 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20,
20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
27, 27, 28, 28, 29, 29, 30, 30, 31, 32, 32, 33, 33, 34, 35, 35,
36, 36, 37, 38, 38, 39, 40, 40, 41, 42, 43, 43, 44, 45, 46, 47,
48, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 81, 82,
83, 85, 86, 88, 90, 91, 93, 94, 96, 98, 99, 101, 103, 105, 107, 109,
110, 112, 114, 116, 118, 121, 123, 125, 127, 129, 132, 134, 136, 139, 141, 144,
146, 149, 151, 154, 157, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 190,
193, 196, 200, 203, 207, 211, 214, 218, 222, 226, 230, 234, 238, 242, 248, 255,
};


class HSB_to_RGB {
public: void getRGB(int hue, int sat, int val, int colors[3]);
};

#endif
222 changes: 222 additions & 0 deletions HSB_to_RGB/example/led_wrist_brightness/led_wrist_brightness.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#include <HSB_to_RGB.h>

#include <Adafruit_NeoPixel.h>

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)

#define PIXELS 24
#define BRIGHTNESS 74



int rowOffset[] = {0, 0, 5, 24, 39, 60};
int rowsColumns[] = {8, 13, 16, 16, 13, 8};
int rowCount = 6;

int strobeSpeed = 4;





Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, 9, NEO_RGB + NEO_KHZ800);

HSB_to_RGB hsb = HSB_to_RGB();

int pixel;

void setup() {
//Serial.begin(9600);
strip.begin();
// strip.show(); // Initialize all pixels to 'off'
}

void loop() {

int each = 5;

///*
for (int i = 3; i < each; i++) {
strobeSpeed = i;
longitude();
}
//*/

for (int i = 1; i < each; i++) {
strobeSpeed = i;
longitudeSingleColor();
}

for (int i = 1; i < each; i++) {
strobeSpeed = i;
latitudeScan();
}

}


void latitudeScan() {

uint16_t pixel, colorIndex, hue;

float colorPortionRespondsibilities = 255.0 / PIXELS;

for(colorIndex = 0; colorIndex < 255; colorIndex++) {

for(pixel = 0; pixel < strip.numPixels(); pixel++) {

// hue = ((int)(colorIndex + ((float)pixel * colorPortionRespondsibilities)));

int hue = colorIndex + pixel * colorPortionRespondsibilities;

///*
if (hue > 255) {
hue = hue - 255;
}
//*/
// Serial.print(hue);
// Serial.print(",");


setPixelColor(pixel, hue , brightness());

}

strip.show();
// Serial.println();
// Serial.print(",");


}


}

void setPixelColor(uint16_t n, uint32_t c, uint16_t b) {

//uint8_t r;
//uint8_t g;
//uint8_t blue;
int colors[3] = {0,0,0};

hsb.getRGB((int)c, 255, b, colors);

strip.setPixelColor(n, colors[0], colors[1], colors[2]);

return;

}

void longitude(){

uint16_t i, j;
uint16_t pixel;


for(j=0; j<256; j+=2) { // 5 cycles of all colors on wheel

pixel = 0;
// int row = 0;
for (int row = 0; row < rowCount; row++) {

int columnCount = rowsColumns[row];
int colorRatio = 255/columnCount;
int columnOffest = rowOffset[row];

for(int column=0; column < columnCount; column++) {
// strip.setPixelColor(pixel, Wheel(j-column*colorRatio));
// setPixelColor(pixel, j-column*colorRatio+columnOffest,30);
int hue = column*colorRatio-columnOffest+j;
if (hue > 255) {
hue = hue - 255;
}
if (hue < 0) {
hue = hue + 255;
}
setPixelColor(pixel, hue,brightness());


pixel++;
}

}

// j+=analogRead(A0)/12;

strip.show();
}
}


void longitudeSingleColor(){

uint16_t i, j;
uint16_t pixel;


for(j=0; j<256; j++) { // 5 cycles of all colors on wheel

pixel = 0;
// int row = 0;
for (int row = 0; row < rowCount; row++) {

int columnCount = rowsColumns[row];
int colorRatio = 255/columnCount;
int columnOffest = rowOffset[row];

for(int column=0; column < columnCount; column++) {
// strip.setPixelColor(pixel, Wheel(j-column*colorRatio));
// setPixelColor(pixel, j-column*colorRatio+columnOffest,30);


int hue = column*colorRatio-columnOffest;


if (hue > 255) {
hue = hue - 255;
}
if (hue < 0) {
hue = hue + 255;
}



setPixelColor(pixel, hue, brightness());


pixel++;
}

}

// j+=analogRead(A0)/12;

strip.show();
}
}


int brightness() {

return analogRead(A1) / 4;
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

0 comments on commit ab7fdcc

Please sign in to comment.