diff --git a/Led.cpp b/Led.cpp new file mode 100644 index 0000000..6ae0c13 --- /dev/null +++ b/Led.cpp @@ -0,0 +1,88 @@ +#include "Led.h" +#include + +#define MIN_VALUE 0 +#define MAX_VALUE 255 + + +Led::Led() : + RED_PIN{ 0 }, + GREEN_PIN{ 0 }, + BLUE_PIN{ 0 } +{} + + + +Led::Led( int RED_PIN, int GREEN_PIN, int BLUE_PIN ) : + RED_PIN{ RED_PIN }, + GREEN_PIN{ GREEN_PIN }, + BLUE_PIN{ BLUE_PIN } +{ + pinMode( RED_PIN, OUTPUT ); + pinMode( GREEN_PIN, OUTPUT ); + pinMode( BLUE_PIN, OUTPUT ); +} + + + +void Led::setRGB( int red, int green, int blue ) +{ + assert( red >= MIN_VALUE && red <= MAX_VALUE ); + assert( green >= MIN_VALUE && green <= MAX_VALUE ); + assert( blue >= MIN_VALUE && blue <= MAX_VALUE ); + + analogWrite( RED_PIN, red ); + analogWrite( GREEN_PIN, green ); + analogWrite( BLUE_PIN, blue ); +} + + + +void Led::setRGB( float red, float green, float blue ) +{ + setRGB( int( red * MAX_VALUE ), int( green * MAX_VALUE ), int( blue * MAX_VALUE ) ); +} + + + +void Led::setHSV( float hue, float sat, float val ) +{ + float chroma = sat * val; + int h = int( hue * 6 ); + float x = chroma * ( 1 - abs( h % 2 - 1 ) ); + + float r = 0.0f, g = 0.0f, b = 0.0f; + + switch( h ) + { + case 0: + r = chroma; + g = x; + break; + case 1: + r = x; + g = chroma; + break; + case 2: + g = chroma; + b = x; + break; + case 3: + g = x; + b = chroma; + break; + case 4: + r = x; + b = chroma; + break; + case 5: + r = chroma; + b = x; + break; + + default: assert( false ); break; + } + + float m = val - chroma; + setRGB( r + m, g + m, b + m ); +} diff --git a/Led.h b/Led.h new file mode 100644 index 0000000..7af9280 --- /dev/null +++ b/Led.h @@ -0,0 +1,27 @@ +#pragma once + +class Led +{ +public: + + Led(); + + Led( int RED_PIN, int GREEN_PIN, int BLUE_PIN ); + + // red, green, blue in range [0,255] + void setRGB( int red, int green, int blue ); + + // red, green, blue in range [0,1] + void setRGB( float red, float green, float blue ); + + // hue, sat, val in range [0,1] + void setHSV( float hue, float sat, float val ); + +private: + const int RED_PIN; + const int GREEN_PIN; + const int BLUE_PIN; +}; + + + diff --git a/line-follower.vcxproj b/line-follower.vcxproj index 49cb08d..2b348db 100644 --- a/line-follower.vcxproj +++ b/line-follower.vcxproj @@ -83,6 +83,7 @@ + @@ -100,6 +101,7 @@ + VisualMicroDebugger