From 703cdbc35591c344d92d39fff5c6d54abc97b2fb Mon Sep 17 00:00:00 2001 From: blhough Date: Fri, 23 Dec 2016 18:35:14 -0800 Subject: [PATCH 1/4] Add Led class --- Led.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Led.h | 25 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Led.cpp create mode 100644 Led.h diff --git a/Led.cpp b/Led.cpp new file mode 100644 index 0000000..6a5d1ca --- /dev/null +++ b/Led.cpp @@ -0,0 +1,80 @@ +#include "Led.h" +#include + +#define MIN_VALUE 0 +#define MAX_VALUE 255 + + +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, g = 0, b = 0; + + 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..c6dd2e7 --- /dev/null +++ b/Led.h @@ -0,0 +1,25 @@ +#pragma once + +class Led +{ +public: + + 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; +}; + + + From 83ed2bca4be25a7388ebb61a71e08b1188c8ad18 Mon Sep 17 00:00:00 2001 From: blhough Date: Fri, 23 Dec 2016 19:06:49 -0800 Subject: [PATCH 2/4] Add Led.h and Led.cpp to the project file --- line-follower.vcxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/line-follower.vcxproj b/line-follower.vcxproj index 7648ada..a06a42a 100644 --- a/line-follower.vcxproj +++ b/line-follower.vcxproj @@ -83,8 +83,12 @@ + + + + VisualMicroDebugger From 59d697367a89e4bf427b4b7bce475667347fb52b Mon Sep 17 00:00:00 2001 From: blhough Date: Sun, 25 Dec 2016 13:15:32 -0800 Subject: [PATCH 3/4] Add default constructor for Led class --- Led.cpp | 8 ++++++++ Led.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/Led.cpp b/Led.cpp index 6a5d1ca..060bfc8 100644 --- a/Led.cpp +++ b/Led.cpp @@ -5,6 +5,14 @@ #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 }, diff --git a/Led.h b/Led.h index c6dd2e7..7af9280 100644 --- a/Led.h +++ b/Led.h @@ -4,6 +4,8 @@ class Led { public: + Led(); + Led( int RED_PIN, int GREEN_PIN, int BLUE_PIN ); // red, green, blue in range [0,255] From b48f639dfbebe1b76769b3f2eabe489a54052d9e Mon Sep 17 00:00:00 2001 From: blhough Date: Sun, 25 Dec 2016 13:17:15 -0800 Subject: [PATCH 4/4] Initialize r, g, b with floats --- Led.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Led.cpp b/Led.cpp index 060bfc8..6ae0c13 100644 --- a/Led.cpp +++ b/Led.cpp @@ -51,7 +51,7 @@ void Led::setHSV( float hue, float sat, float val ) int h = int( hue * 6 ); float x = chroma * ( 1 - abs( h % 2 - 1 ) ); - float r = 0, g = 0, b = 0; + float r = 0.0f, g = 0.0f, b = 0.0f; switch( h ) {