-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cycle through the 6 predefined colors that corresponds to a different…
… seed
- Loading branch information
1 parent
7bc4522
commit 8373c3b
Showing
10 changed files
with
483 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include "RgbLedState.h" | ||
|
||
// Create the neopixel with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL | ||
Adafruit_NeoPixel neoPixel = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); | ||
Led RgbLedState::led; | ||
|
||
///////////////// | ||
// CONSTRUCTOR // | ||
///////////////// | ||
|
||
RgbLedState::RgbLedState() {} | ||
|
||
////////// | ||
// INIT // | ||
////////// | ||
|
||
void RgbLedState::init() { | ||
led.red = 0; | ||
led.green = 255; | ||
led.blue = 0; | ||
led.brightness = 255; // max | ||
led.fadeAmount = 3; // step | ||
led.blinkSpeed = 60; // in ms | ||
led.pulseSpeed = 6; // in ms | ||
led.lastUpdate = 1410065407; // max long | ||
led.state = LED_ON; | ||
|
||
neoPixel.begin(); | ||
neoPixel.setPixelColor(0, neoPixel.Color(led.red, led.green, led.blue)); | ||
neoPixel.setBrightness(led.brightness); | ||
neoPixel.show(); | ||
} | ||
|
||
////////////////// | ||
// UPDATE STATE // | ||
////////////////// | ||
|
||
void RgbLedState::setOn() { | ||
if (led.state != LED_ON) { | ||
led.state = LED_ON; | ||
led.brightness = 255; | ||
} | ||
} | ||
|
||
void RgbLedState::setOff() { | ||
if (led.state != LED_OFF) { | ||
led.state = LED_OFF; | ||
led.brightness = 0; | ||
} | ||
} | ||
|
||
void RgbLedState::setPulsing() { | ||
if (led.state != LED_PULSING) { | ||
led.brightness = 0; | ||
led.fadeAmount = 3; | ||
led.state = LED_PULSING; | ||
} | ||
} | ||
|
||
void RgbLedState::setBlinking() { | ||
if (led.state != LED_BLINKING) { | ||
led.state = LED_BLINKING; | ||
} | ||
} | ||
|
||
void RgbLedState::setColor(int colorIndex) { | ||
if (colorIndex < 0 || colorIndex >= NUM_COLORS) { | ||
return; | ||
} | ||
led.red = colors[colorIndex][0]; | ||
led.green = colors[colorIndex][1]; | ||
led.blue = colors[colorIndex][2]; | ||
} | ||
|
||
|
||
////////// | ||
// LOOP // | ||
////////// | ||
|
||
void RgbLedState::loop() { | ||
switch (led.state) { | ||
case LED_OFF: { | ||
led.brightness = 0; | ||
break; | ||
} | ||
case LED_BLINKING: { | ||
if (millis() - led.lastUpdate > led.blinkSpeed) { | ||
led.brightness = (led.brightness == 0) ? 255 : 0; | ||
led.lastUpdate = millis(); | ||
} | ||
break; | ||
} | ||
case LED_PULSING: { | ||
if (millis() - led.lastUpdate > led.pulseSpeed) { | ||
led.brightness += led.fadeAmount; | ||
// reverse the direction of the fading at the ends of the fade | ||
if (led.brightness <= 0 || led.brightness >= 255) { | ||
led.fadeAmount = -led.fadeAmount; | ||
} | ||
led.lastUpdate = millis(); | ||
} | ||
break; | ||
} | ||
default: { //case LED_ON: | ||
led.brightness = 255; | ||
break; | ||
} | ||
} | ||
neoPixel.setPixelColor(0, neoPixel.Color(led.red * led.brightness / 255, led.green * led.brightness / 255, led.blue * led.brightness / 255)); | ||
neoPixel.show(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef RGB_LED_STATE_H | ||
#define RGB_LED_STATE_H | ||
|
||
#include <Arduino.h> | ||
#include <Adafruit_NeoPixel.h> | ||
|
||
enum State { | ||
LED_OFF = 0, | ||
LED_ON = 1, | ||
LED_PULSING = 2, | ||
LED_BLINKING = 3 | ||
}; | ||
|
||
#define NUM_COLORS 6 | ||
const uint8_t colors[NUM_COLORS][3] = { | ||
{0, 255, 0}, // green | ||
{255, 255, 0}, // yellow | ||
{255, 0, 0}, // red | ||
{0, 0, 255}, // blue | ||
{255, 255, 255},// white | ||
{255, 0, 255}, // magenta | ||
}; | ||
|
||
struct Led { | ||
uint8_t red; | ||
uint8_t green; | ||
uint8_t blue; | ||
int brightness; | ||
int fadeAmount; | ||
int blinkState; | ||
int blinkSpeed; // in ms | ||
int pulseSpeed; // in ms | ||
unsigned long lastUpdate; | ||
State state; | ||
}; | ||
|
||
class RgbLedState { | ||
|
||
public: | ||
RgbLedState(); | ||
void init(); | ||
void setOn(); | ||
void setOff(); | ||
void setPulsing(); | ||
void setBlinking(); | ||
void setColor(int colorIndex); | ||
void loop(); | ||
|
||
private: | ||
static Led led; | ||
}; | ||
|
||
#endif // RGB_LED_STATE_H |
Oops, something went wrong.