summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2014-12-19 21:15:11 +0100
committerFlorian Baumann <derflob@derflob.de>2014-12-19 21:15:11 +0100
commit8f45e55eaf3eb29887c995554272eb0bb5f6d567 (patch)
treed5eec3517b50b35fc60cb53bd94130e69489b425
parent5db4465944129256042804dd23342c645199d1d9 (diff)
downloadStripClock-hsl.tar.gz
StripClock-hsl.tar.bz2
inital hsl stuffhsl
-rw-r--r--StripClock.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/StripClock.c b/StripClock.c
index 7b48fa0..df974dc 100644
--- a/StripClock.c
+++ b/StripClock.c
@@ -1,11 +1,22 @@
#include <avr/interrupt.h>
#include <avr/io.h>
#include <string.h>
+#include <math.h>
#include "i2cmaster.h"
#include "ds1307.h"
#include "light_ws2812.h"
+#define GAMMA_R 2.2
+#define GAMMA_G 2.2
+#define GAMMA_B 2.2
+
+struct HSL {
+ float hue;
+ float saturation;
+ float lightness;
+};
+
#define ROTARY_RESOLUTION 4
const int8_t rotary_states[][4] = {
{ 0, -1, +1, 0 },
@@ -48,6 +59,9 @@ uint8_t updateStrip(void);
uint8_t updateStripSimple(void);
uint8_t updateStripEncoder(void);
+void HSL2RGB(struct HSL *from, struct cRGB *to);
+void gamma_correction(struct cRGB *rgb);
+
int main(void)
{
cli();
@@ -210,6 +224,11 @@ uint8_t updateStripEncoder(void)
break;
}
+ struct HSL hsl;
+
+ HSL2RGB(&hsl, &leds[0]);
+ gamma_correction(&leds[0]);
+
ws2812_setleds(leds, LEDS);
return 0;
@@ -299,3 +318,52 @@ uint8_t updateStrip(void)
return 0;
}
+
+void HSL2RGB(struct HSL *from, struct cRGB *to)
+{
+ float c = (1.0 - fabs(2 * from->lightness - 1.0)) * from->saturation;
+ float h_prime = from->hue / 60;
+ float x = c * (1.0 - fabs(fmod(h_prime, 2.0) - 1.0));
+ float r, g, b;
+ if (h_prime < 1.0) {
+ r = c;
+ g = x;
+ b = 0;
+ } else if (h_prime < 2.0) {
+ r = x;
+ g = c;
+ b = 0;
+ } else if (h_prime < 3.0) {
+ r = 0;
+ g = c;
+ b = x;
+ } else if (h_prime < 4.0) {
+ r = 0;
+ g = x;
+ b = c;
+ } else if (h_prime < 5.0) {
+ r = x;
+ g = 0;
+ b = c;
+ } else if (h_prime < 6.0) {
+ r = c;
+ g = 0;
+ b = x;
+ } else {
+ r = 0;
+ g = 0;
+ b = 0;
+ }
+
+ float m = from->lightness - 0.5 * c;
+ to->r = (r + m) * 255;
+ to->g = (g + m) * 255;
+ to->b = (b + m) * 255;
+}
+
+void gamma_correction(struct cRGB *rgb)
+{
+ rgb->r = pow((rgb->r / 255.0), GAMMA_R) * 255;
+ rgb->g = pow((rgb->g / 255.0), GAMMA_G) * 255;
+ rgb->b = pow((rgb->b / 255.0), GAMMA_B) * 255;
+}