From 5d0ed0a5e2c31a629e9b6a55e6abee628cb98e75 Mon Sep 17 00:00:00 2001 From: Nick Singer Date: Thu, 7 Aug 2014 17:38:08 +0200 Subject: [PATCH] Added HSV support --- ledcontroller.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ ledcontroller.h | 1 + 2 files changed, 52 insertions(+) diff --git a/ledcontroller.c b/ledcontroller.c index 977f2fe..3dbe955 100644 --- a/ledcontroller.c +++ b/ledcontroller.c @@ -128,6 +128,57 @@ void led_set(uint8_t lednum, uint16_t red, uint16_t green, uint16_t blue) } +void led_set_hsv(uint8_t lednum, uint16_t hue, uint8_t saturation, uint8_t value) +{ + uint16_t red, green, blue; + + //Calculate hue + if ( hue < 61 ) { + red = 255; + green = ( 425UL * hue ) / 100; + blue = 0; + } else if ( hue < 121 ) { + red = 255 - ( ( 425UL * ( hue - 60 ) ) / 100 ); + green = 255; + blue = 0; + } else if ( hue < 181 ) { + red = 0; + green = 255; + blue = ( 425UL * ( hue - 120 ) ) / 100; + } else if ( hue < 241 ) { + red = 0; + green = 255 - ( ( 425UL * ( hue - 180 ) ) / 100 ); + blue = 255; + } else if ( hue < 301 ) { + red = ( 425UL * ( hue - 240 ) ) / 100; + green = 0; + blue = 255; + } else if ( hue < 360) { + red = 255; + green = 0; + blue = 255 - ( ( 425UL * ( hue - 300 ) ) / 100); + } + + //Calculate saturation + uint8_t diff; + saturation = 100 - saturation; + diff = ( ( 255 - red ) * saturation ) / 100; + red += diff; + + diff = ( ( 255 - green ) * saturation ) / 100; + green += diff; + + diff = ( ( 255 - blue ) * saturation ) / 100; + blue += diff; + + //Calculate value + red = ( red * value ) / 100; + green = ( green * value ) / 100; + blue = ( blue * value ) / 100; + + led_set(lednum, red<<8, green<<8, blue<<8); +} + /* * Write the data stored in the LED buffers via led_set(). */ diff --git a/ledcontroller.h b/ledcontroller.h index c0c798e..5b99cfa 100644 --- a/ledcontroller.h +++ b/ledcontroller.h @@ -18,6 +18,7 @@ void led_init(void); void led_set(uint8_t lednum, uint16_t red, uint16_t green, uint16_t blue); +void led_set_hsv(uint8_t lednum, uint16_t hue, uint8_t saturation, uint8_t value); void led_flush(void); void led_clear(void); void led_turn_all_on(void);