Skip to content

Commit

Permalink
Refactor neopixel app
Browse files Browse the repository at this point in the history
CLoses #14
  • Loading branch information
DanNixon committed Aug 9, 2016
1 parent caa5309 commit d8115a9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 85 deletions.
30 changes: 15 additions & 15 deletions library/examples/AppManager/NPPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@

class NPPattern
{
protected:
Adafruit_NeoPixel *strip;
int brightness;

public:
NPPattern(Adafruit_NeoPixel *inStrip, int brightness)
: strip(inStrip)
, brightness(brightness)
NPPattern(Adafruit_NeoPixel &strip, int brightness)
: m_strip(strip)
, m_brightness(brightness)
{
}

Expand All @@ -29,27 +25,31 @@ class NPPattern
wheelPos = 255 - wheelPos;
if (wheelPos < 85)
{
return strip->Color(255 - wheelPos * 3, 0, wheelPos * 3);
return Adafruit_NeoPixel::Color(255 - wheelPos * 3, 0, wheelPos * 3);
}
if (wheelPos < 170)
{
wheelPos -= 85;
return strip->Color(0, wheelPos * 3, 255 - wheelPos * 3);
return Adafruit_NeoPixel::Color(0, wheelPos * 3, 255 - wheelPos * 3);
}
wheelPos -= 170;
return strip->Color(wheelPos * 3, 255 - wheelPos * 3, 0);
return Adafruit_NeoPixel::Color(wheelPos * 3, 255 - wheelPos * 3, 0);
}

virtual void setBrightness(int inBrightness)
inline void setBrightness(int inBrightness)
{
strip->setBrightness(inBrightness);
brightness = strip->getBrightness();
m_strip.setBrightness(inBrightness);
m_brightness = m_strip.getBrightness();
}

virtual int getBrightness()
inline int getBrightness()
{
return brightness;
return m_brightness;
}

protected:
Adafruit_NeoPixel &m_strip;
int m_brightness;
};

#endif
55 changes: 25 additions & 30 deletions library/examples/AppManager/NeoTestApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,25 @@

class NeoTestApp : public App
{
static const int PIN = 15, PATTERN_COUNT = 2;
Adafruit_NeoPixel *strip = 0;
int currPattern = 0;
NPPattern *patterns[PATTERN_COUNT];
public:
static const size_t PATTERN_COUNT = 2;

public:
NeoTestApp()
: App("NeoTestApp")
{
strip = new Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
patterns[0] = new RainbowCycle(strip, 20);
patterns[1] = new RainbowChase(strip, 50);
currPattern = 0;
m_patterns[0] = new RainbowCycle(m_badge->neoPixels(), 20);
m_patterns[1] = new RainbowChase(m_badge->neoPixels(), 50);
m_currentPattern = 0;
}

~NeoTestApp()
{
for (int i = 0; i < PATTERN_COUNT; i++)
for (size_t i = 0; i < PATTERN_COUNT; i++)
{
if (patterns[i])
free(patterns[i]);
if (m_patterns[i])
delete m_patterns[i];
}
if (strip)
free(strip);
}

/**
Expand Down Expand Up @@ -71,40 +66,34 @@ class NeoTestApp : public App
{ // short press
if (button->getID() == 1)
{ // up
patterns[currPattern]->setBrightness(patterns[currPattern]->getBrightness() + 10);
m_patterns[m_currentPattern]->setBrightness(m_patterns[m_currentPattern]->getBrightness() + 10);
}
if (button->getID() == 2)
{ // down
patterns[currPattern]->setBrightness(patterns[currPattern]->getBrightness() - 10);
m_patterns[m_currentPattern]->setBrightness(m_patterns[m_currentPattern]->getBrightness() - 10);
}
if (button->getID() == 0)
{ // left
patterns[currPattern]->setBrightness(patterns[currPattern]->getBrightness() - 1);
m_patterns[m_currentPattern]->setBrightness(m_patterns[m_currentPattern]->getBrightness() - 1);
}
if (button->getID() == 3)
{ // right
patterns[currPattern]->setBrightness(patterns[currPattern]->getBrightness() + 1);
m_patterns[m_currentPattern]->setBrightness(m_patterns[m_currentPattern]->getBrightness() + 1);
}
}
else
{ // long press
if (button->getID() == 1)
{ // up
currPattern++;
m_currentPattern++;
}
if (button->getID() == 2)
{ // down
currPattern--;
}
if (button->getID() == 0)
{ // left
}
if (button->getID() == 3)
{ // right
m_currentPattern--;
}
currPattern = currPattern >= PATTERN_COUNT
m_currentPattern = m_currentPattern >= PATTERN_COUNT
? PATTERN_COUNT - 1
: currPattern = currPattern < 0 ? 0 : currPattern;
: m_currentPattern = m_currentPattern < 0 ? 0 : m_currentPattern;
}
}

Expand All @@ -116,7 +105,7 @@ class NeoTestApp : public App
*/
void run()
{
patterns[currPattern]->run();
m_patterns[m_currentPattern]->run();
}

/**
Expand All @@ -125,8 +114,14 @@ class NeoTestApp : public App
void onExit()
{
App::onExit();
strip->setBrightness(0);
strip->show();

m_badge->neoPixels().setBrightness(0);
m_badge->neoPixels().show();
}

private:
int m_currentPattern = 0;
NPPattern *m_patterns[PATTERN_COUNT];
};

#endif
47 changes: 24 additions & 23 deletions library/examples/AppManager/RainbowChase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@

class RainbowChase : public NPPattern
{
unsigned long wait, next;
int j, q;

public:
RainbowChase(Adafruit_NeoPixel *inStrip, uint8_t inWait)
: NPPattern(inStrip, 31)
, wait(inWait)
, j(0)
, q(0)
RainbowChase(Adafruit_NeoPixel &strip, uint8_t wait)
: NPPattern(strip, 31)
, m_wait(wait)
, m_j(0)
, m_q(0)
{
next = millis();
m_next = millis();
}

virtual ~RainbowChase()
Expand All @@ -26,35 +23,39 @@ class RainbowChase : public NPPattern

void run()
{
if (next < millis())
if (m_next < millis())
{
next = millis() + wait;
if (j < 256)
m_next = millis() + m_wait;
if (m_j < 256)
{
if (q < 3)
if (m_q < 3)
{
for (uint16_t i = 0; i < strip->numPixels(); i += 3)
for (uint16_t i = 0; i < m_strip.numPixels(); i += 3)
{
strip->setPixelColor(i + q, 0);
strip->setPixelColor((i + q + 1) % strip->numPixels(), wheel((i + j) % 255));
m_strip.setPixelColor(i + m_q, 0);
m_strip.setPixelColor((i + m_q + 1) % m_strip.numPixels(), wheel((i + m_j) % 255));
}
strip->setBrightness(brightness);
strip->show();
q++;
m_strip.setBrightness(m_brightness);
m_strip.show();
m_q++;
}
else
{
q = 0;
m_q = 0;
}
if (q >= 3)
j++;
if (m_q >= 3)
m_j++;
}
else
{
j = 0;
m_j = 0;
}
}
}

private:
unsigned long m_wait, m_next;
int m_j, m_q;
};

#endif
35 changes: 18 additions & 17 deletions library/examples/AppManager/RainbowCycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@

class RainbowCycle : public NPPattern
{
unsigned long wait, next;
int j;

public:
RainbowCycle(Adafruit_NeoPixel *inStrip, uint8_t inWait)
: NPPattern(inStrip, 31)
, wait(inWait)
, j(0)
RainbowCycle(Adafruit_NeoPixel &strip, uint8_t wait)
: NPPattern(strip, 31)
, m_wait(wait)
, m_j(0)
{
next = millis();
m_next = millis();
}

virtual ~RainbowCycle()
Expand All @@ -25,25 +22,29 @@ class RainbowCycle : public NPPattern

void run()
{
if (next < millis())
if (m_next < millis())
{
next = millis() + wait;
if (j < 256)
m_next = millis() + m_wait;
if (m_j < 256)
{
for (int i = 0; i < strip->numPixels(); i++)
for (int i = 0; i < m_strip.numPixels(); i++)
{
strip->setPixelColor(i, wheel(((i * 256 / strip->numPixels()) + j) & 255));
m_strip.setPixelColor(i, wheel(((i * 256 / m_strip.numPixels()) + m_j) & 255));
}
strip->setBrightness(brightness);
strip->show();
j++;
m_strip.setBrightness(m_brightness);
m_strip.show();
m_j++;
}
else
{
j = 0;
m_j = 0;
}
}
}

private:
unsigned long m_wait, m_next;
int m_j;
};

#endif

0 comments on commit d8115a9

Please sign in to comment.