Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Led.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "Led.h"
#include <assert.h>

#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 );
}
27 changes: 27 additions & 0 deletions Led.h
Original file line number Diff line number Diff line change
@@ -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;
};



2 changes: 2 additions & 0 deletions line-follower.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
</None>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Led.h" />
<ClInclude Include="AveragingQueue.h" />
<ClInclude Include="Constants.h" />
<ClInclude Include="LineFollower.h" />
Expand All @@ -100,6 +101,7 @@
<ClCompile Include="PIDController.cpp" />
<ClCompile Include="SensorArray.cpp" />
<ClCompile Include="Sensor.cpp" />
<ClCompile Include="Led.cpp" />
</ItemGroup>
<PropertyGroup>
<DebuggerFlavor>VisualMicroDebugger</DebuggerFlavor>
Expand Down