Skip to content

Commit

Permalink
introduce trueColor mode
Browse files Browse the repository at this point in the history
there's now an option to reduce the brightness of a specific color to get a nice white tone with rgb strips
currently hard-coded to blue but will come into the gui soon.
  • Loading branch information
sansha committed Jul 11, 2016
1 parent 39ce706 commit f7ab987
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 33 deletions.
4 changes: 2 additions & 2 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A client program should ALWAYS change mode to 0 if it wants to adjust the indivi

//************************* DEFINE PROGRAM OPTIONS (comment to disable unless otherwise noted)******************************+

#define DEBUG //define DEBUG information
//#define DEBUG //define DEBUG information

#define PWM_CONFIG 0 //0 to config via #define; 1 to config with cli

Expand All @@ -29,7 +29,7 @@ A client program should ALWAYS change mode to 0 if it wants to adjust the indivi

//#define DETAILED_PIN_INIT_INFORMATION //detailed info in pin init
//blue is normally the brightest color so we'll decrease its brightness in software by this amount
#define BLUE_ADJUSTMENT 20 //20 = 20%
#define BLUE_ADJUSTMENT 40 //20 = 20%
#define FADE_TIME_MS 1000 //default value of fadeDelayMs
#define FADE_DELAY_US 0 //default value of fadeDelayUs
#define SIMULTANEOUS_DELAY_FACTOR 3 //factor to multiply the delay in simultaneous fade mode (useful?)
Expand Down
2 changes: 1 addition & 1 deletion file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ bool readColorConfig(void)
}
if (variables[0].find("b") != std::string::npos)
{
trueColorAdjust = 20;
trueColorAdjust = BLUE_ADJUSTMENT;
}
//add this as a new element in leds vector element
// leds.push_back(LED(variables[0], stoi(variables[1],nullptr), ledIsColor, 0, 0));
Expand Down
Binary file modified led-blaster
Binary file not shown.
44 changes: 26 additions & 18 deletions led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ int LED::getTargetBrightness()
{
return this->targetBrightness;
}
int LED::getTrueColorMultipier()
{
return this->trueColorMultiplier;
}
int LED::getPwmSteps()
{
return LED::pwmSteps;
Expand Down Expand Up @@ -78,7 +82,7 @@ void LED::setColorCode(std::string new_colorcode)
void LED::setTrueColorMultipier(int new_Multiplier)
{
trueColorMultiplier = 0;
if (trueColorMultiplier < 0 && trueColorMultiplier < 100)
if (new_Multiplier > 0 && new_Multiplier < 100)
{
this->trueColorMultiplier = new_Multiplier;
}
Expand All @@ -93,9 +97,9 @@ void LED::setIsColor(bool newisColor)
}
void LED::setCurrentBrightness(int new_cBrightness)
{
if (new_cBrightness >= LED::pwmSteps)
if (new_cBrightness >= LED::getPwmSteps())
{
this->currentBrightness = LED::pwmSteps;
this->currentBrightness = LED::getPwmSteps();
}
else if (new_cBrightness < 0)
{
Expand All @@ -112,26 +116,17 @@ void LED::setTargetBrightness(int new_tBrightness)
{
//new exception for blue, other treatment for blue colored strips as they are
//brighter than other leds
if (new_tBrightness >= LED::pwmSteps)
if (new_tBrightness > LED::getPwmSteps())
{
this->targetBrightness = LED::pwmSteps;
this->targetBrightness = LED::getPwmSteps();
}
else if (new_tBrightness < 0)
{
this->targetBrightness = 0;
}
else
//apply color
{
if (trueColorMultiplier > 0 && trueColorMultiplier < 100)
{
//lower the brightness of the color by a factor (0 = 0%, at 100% [not allowed] there wouldnt be any light)
this->targetBrightness = new_tBrightness - ((new_tBrightness * trueColorMultiplier) / 100);
}
else
{
this->targetBrightness = new_tBrightness;
}
this->targetBrightness = new_tBrightness;
}
}

Expand Down Expand Up @@ -254,10 +249,23 @@ void * LED::fade(void)
{
//this->fading = true;
//#define DEBUG
//calculate the real target brightness it will have (after the trueColorAdjust)
int trueTargetBrightness;
if (trueColorMultiplier > 0 && trueColorMultiplier < 100 && this->targetBrightness > 0)
{
//lower the brightness of the color by a factor (0 = 0%, at 100% [not allowed] there wouldnt be any light)
trueTargetBrightness = this->targetBrightness - ((this->targetBrightness * trueColorMultiplier) / 100);
}
else
{
trueTargetBrightness = this->targetBrightness;
}
std::cout << "true brightness: " << trueTargetBrightness << std::endl;
//calculate the delayUs needed to archieve the specified fadeTime
//steps * delayUs * 1000 = fadeTime [in ms]
//fadeTime is the variable which sets the Time needed to fade
int totalSteps = ((this->targetBrightness) - (this->currentBrightness));

int totalSteps = ((trueTargetBrightness) - (this->currentBrightness));
//we want to have a positive steps number
if (totalSteps < 0)
{
Expand All @@ -272,13 +280,13 @@ void * LED::fade(void)
startTime = gpioTick();
#endif
//fading to target brightness
for (int current = this->currentBrightness; current < this->targetBrightness; current++)
for (int current = this->currentBrightness; current < trueTargetBrightness; current++)
{
//increase currentBRIGHTNESS of that color and write it to the pin
this->setCurrentBrightness(current + 1); //we use +1 so it will actually reach the targetBrightness
gpioDelay(delayUs); //make a delay
}
for (int current = this->currentBrightness; current > this->targetBrightness; current--)
for (int current = this->currentBrightness; current > trueTargetBrightness; current--)
{
//decrease currentBRIGHTNESS of that color and write it to the pin
this->setCurrentBrightness(current - 1); //we use -1 so it will actually reach the targetBrightness
Expand Down
1 change: 1 addition & 0 deletions led.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace led {
bool isRandomlyFading();
int getCurrentBrightness();
int getTargetBrightness();
int getTrueColorMultipier();
static int getPwmSteps();
static int getFadeTime();
pthread_t getFadeThread();
Expand Down
Binary file modified tests/file.o
Binary file not shown.
Binary file modified tests/file_test
Binary file not shown.
6 changes: 3 additions & 3 deletions tests/file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ TEST_F(fileTest, assignValuesSimple)
TEST_F(fileTest, assignValuesNormal)
{
//0 means value was assigned
LED::setFadeTime(0);
led::LED::setFadeTime(0);
assignConfigValues("time", "1000", config);
EXPECT_EQ(1000, LED::getFadeTime());
EXPECT_EQ(1000, led::LED::getFadeTime());
assignConfigValues("server_path", "/dev/bla", config);
EXPECT_EQ("/dev/bla", config->serverPath);
}
TEST_F(fileTest, assignValuesNegative)
{
//0 means value was assigned
LED::setFadeTime(0);
led::LED::setFadeTime(0);
assignConfigValues("time", "-1000", config);
EXPECT_EQ(1000, LED::getFadeTime());;
}
Expand Down
Binary file modified tests/file_test.o
Binary file not shown.
Binary file modified tests/led.o
Binary file not shown.
Binary file modified tests/led_test
Binary file not shown.
30 changes: 21 additions & 9 deletions tests/led_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct ledTest : testing::Test
std::cout << "usage of pigpio is not possible. the led_test cannot be executed" << std::endl;
exit(0);
}
led[0] = new LED("b", 26, true, 0, 0, 0);
led[0] = new LED("b", 26, true, 0, 0, 20);
led[1] = new LED("w", 17, false, 0, 0, 0);
led[2] = new LED("r", 18, true, 0, 0, 0);
led[3] = new LED("g", 22, true, 0, 0, 0);
Expand All @@ -176,7 +176,7 @@ struct ledTest : testing::Test

TEST_F(ledClassConstructorTest, ledConstructor)
{
LED ledObject("w", 25, 0, 0, 0, 0);
LED ledObject("w", 25, false, 0, 0, 0);
EXPECT_EQ(25,ledObject.getPin());
EXPECT_EQ("w",ledObject.getColorCode());
EXPECT_EQ(false, ledObject.IsColor());
Expand Down Expand Up @@ -261,11 +261,20 @@ TEST_F(ledMapTest, ledMapfadeSimultaneous)

TEST_F(ledTest, setTargetBrightnessTest)
{
for (size_t ledNumber = 0; ledNumber < 1; ledNumber++)
for (size_t ledNumber = 0; ledNumber < 4; ledNumber++)
{
//set targetBrightness to a valid value ( the maximum possible value)
led[ledNumber]->setTargetBrightness( led[ledNumber]->getPwmSteps() );
EXPECT_EQ(led[ledNumber]->getPwmSteps(), led[ledNumber]->getTargetBrightness()) << "normal assignment doesn't work";
led[ledNumber]->setTrueColorMultipier(0);
led[ledNumber]->setTargetBrightness(1000);
EXPECT_EQ(1000, led[ledNumber]->getTargetBrightness()) << "normal assignment doesn't work";
//test the trueColorMultiplier
led[ledNumber]->setTrueColorMultipier(20);
led[ledNumber]->setTargetBrightness(500);
EXPECT_EQ(500, led[ledNumber]->getTargetBrightness()) << "trueColorMultiplier doesn't work";
led[ledNumber]->setTargetBrightness(200);
EXPECT_EQ(200, led[ledNumber]->getTargetBrightness()) << "trueColorMultiplier doesn't work";
led[ledNumber]->setTargetBrightness(1000);
EXPECT_EQ(1000, led[ledNumber]->getTargetBrightness()) << "trueColorMultiplier doesn't work";
//set it to a negative value
led[ledNumber]->setTargetBrightness( -100 );
EXPECT_EQ(0, led[ledNumber]->getTargetBrightness()) << "negative values produce an error";
Expand Down Expand Up @@ -302,9 +311,10 @@ TEST_F(ledTest, fadeFunctionTest)
led[ledNumber]->setTargetBrightness(0);
led[ledNumber]->fade();
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), led[ledNumber]->getTargetBrightness());
led[ledNumber]->setTargetBrightness(led[ledNumber]->getPwmSteps());
led[ledNumber]->setTargetBrightness(1000);
led[ledNumber]->fade();
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), led[ledNumber]->getTargetBrightness());
int expectedBrightnessTrueColor = led[ledNumber]->getTargetBrightness() - (led[ledNumber]->getTargetBrightness() * led[ledNumber]->getTrueColorMultipier())/ 100;
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), expectedBrightnessTrueColor );
led[ledNumber]->setTargetBrightness(0);
led[ledNumber]->fade();
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), led[ledNumber]->getTargetBrightness());
Expand All @@ -325,7 +335,8 @@ TEST_F(ledTest, fadeThreadsTest)
//wait until thread is finished
EXPECT_EQ(true, led[ledNumber]->isFading());
led[ledNumber]->fadeWait();
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), led[ledNumber]->getTargetBrightness());
int expectedBrightnessTrueColor = led[ledNumber]->getTargetBrightness() - (led[ledNumber]->getTargetBrightness() * led[ledNumber]->getTrueColorMultipier())/ 100;
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), expectedBrightnessTrueColor);
EXPECT_EQ(false, led[ledNumber]->isFading());
//now check if fade down works
led[ledNumber]->setTargetBrightness(0);
Expand Down Expand Up @@ -364,7 +375,8 @@ TEST_F(ledTest, fadeThreadsSimultaneous)
{
//wait until thread is finished
led[ledNumber]->fadeWait();
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), led[ledNumber]->getTargetBrightness());
int expectedBrightnessTrueColor = led[ledNumber]->getTargetBrightness() - (led[ledNumber]->getTargetBrightness() * led[ledNumber]->getTrueColorMultipier())/ 100;
EXPECT_EQ(led[ledNumber]->getCurrentBrightness(), expectedBrightnessTrueColor);
EXPECT_EQ(false, led[ledNumber]->isFading());
}
//now check if fade down works
Expand Down
Binary file modified tests/led_test.o
Binary file not shown.

0 comments on commit f7ab987

Please sign in to comment.