Skip to content

Commit

Permalink
Update examples, renamed handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ArminJo committed Nov 12, 2019
1 parent a9a0b3b commit 603a9f0
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 58 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The original **SCANNER** pattern is extended and includes the **CYLON** as well
## Patterns only for nxn Matrix
**MOVING_PICTURE**, **MOVE**, **TICKER**, **FIRE**, **SNAKE**
## Your own patterns
**Just put your pattern code to the functions UserPattern\[1,2]() and UserPattern\[1,2]Update() in AllPatternOnOneStrip.cpp to realize your own patterns. Enable TEST_USER_PATTERNS on line 39 to test them.**
**Put your pattern code to the functions UserPattern\[1,2]() and UserPattern\[1,2]Update() in AllPatternOnOneStrip.cpp to realize your own patterns. Enable TEST_USER_PATTERNS on line 41 to test them.**

# NeoPixel library
the included NeoPixel library is an extensions of the Adafruit NeoPixel library and supports multiple virtual NeoPixel (and NeoPattern) objects on one physical strip. It also contains a lot of useful functions like:
Expand Down Expand Up @@ -65,7 +65,7 @@ In case you need `NEO_MATRIX_COLUMNS` layout, try to rotate your Matrix and use

# SNAKE GAME
## SnakeGame Example
The game can be controlled by 2 or 4 buttons or by serial input (WASD). To enable serial input control you must define the symbol `USE_SERIAL_CONTROL` or comment out line 33 in `MatrixSnake.h`.
The game can be controlled by 2 or 4 buttons or by serial input (WASD). To enable serial input control you must comment out line 33 `#define USE_SERIAL_CONTROL` in the library file /MatrixSnake.h/ or define global symbol `USE_SERIAL_CONTROL` which is not yet possible in Arduino IDE:-(.<br/>
The experimental Python script in the extras folder converts key presses and game controller input to appropriate serial output for the game.<br/>
After 7 seconds of inactivity the Snake demo with a simple AI is started.
## SnakeAutorun Example
Expand All @@ -81,6 +81,8 @@ NeoPatterns on breadboard
- Swapped parameter aNumberOfSteps and aIntervalMillis of `Stripes()`.
- Pattern `HEARTBEAT` and `BouncinBall` added.
- Swapped first parameter and added parameter aDirection to `Fire()`.
- Changed internal functions.
- Reworked `UserPattern`.

### Version 1.1.0
- Function `getPatternName()` added.
Expand Down
55 changes: 32 additions & 23 deletions examples/AllPatternOnOneStrip/AllPatternOnOneStrip.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Shows all patterns included in the NeoPixel library for NeoPixel strips.
*
* Just add your pattern code to the functions Pattern[1,2]() and Pattern[1,2]Update() in Neopatterns.cpp (line 588ff.)
* Add your pattern code to the functions Pattern[1,2]() and Pattern[1,2]Update() in Neopatterns.cpp (line 588ff.)
* to realize and see your own patterns.
* Enable TEST_OWN_PATTERNS on line 38 to test your patterns.
*
Expand Down Expand Up @@ -66,7 +66,7 @@ void setup() {
bar16.begin(); // This initializes the NeoPixel library.
bar16.ColorWipe(COLOR32(0, 0, 02), 50, 0, REVERSE); // light Blue

#ifdef INFO
#ifdef INFO
Serial.println("started");
#endif
delay(500);
Expand All @@ -89,17 +89,18 @@ void loop() {
* set all pixel to aColor1 and let a pixel of color2 move through
* Starts with all pixel aColor1 and also ends with it.
*/
void UserPattern1(NeoPatterns * aNeoPatterns, color32_t aColor1, color32_t aColor2, uint16_t aIntervalMillis, uint8_t aDirection) {
void UserPattern1(NeoPatterns * aNeoPatterns, color32_t aPixelColor, color32_t aBackgroundColor, uint16_t aIntervalMillis,
uint8_t aDirection) {
/*
* Sample implementation not supporting DIRECTION_DOWN
*/
aNeoPatterns->ActivePattern = PATTERN_USER_PATTERN1;
aNeoPatterns->Interval = aIntervalMillis;
aNeoPatterns->Color1 = aColor1;
aNeoPatterns->Color2 = aColor2;
aNeoPatterns->Color1 = aPixelColor;
aNeoPatterns->LongValue1.BackgroundColor = aBackgroundColor;
aNeoPatterns->Direction = aDirection;
aNeoPatterns->TotalStepCounter = aNeoPatterns->numPixels() + 1;
aNeoPatterns->ColorSet(aColor1);
aNeoPatterns->ColorSet(aBackgroundColor);
aNeoPatterns->show();
aNeoPatterns->lastUpdate = millis();
}
Expand All @@ -119,9 +120,9 @@ bool UserPattern1Update(NeoPatterns * aNeoPatterns, bool aDoUpdate) {

for (uint16_t i = 0; i < aNeoPatterns->numPixels(); i++) {
if (i == aNeoPatterns->Index) {
aNeoPatterns->setPixelColor(i, aNeoPatterns->Color2);
} else {
aNeoPatterns->setPixelColor(i, aNeoPatterns->Color1);
} else {
aNeoPatterns->setPixelColor(i, aNeoPatterns->LongValue1.BackgroundColor);
}
}

Expand All @@ -132,7 +133,8 @@ bool UserPattern1Update(NeoPatterns * aNeoPatterns, bool aDoUpdate) {
* let a pixel of aColor move up and down
* starts and ends with all pixel cleared
*/
void UserPattern2(NeoPatterns * aNeoPatterns, color32_t aColor, uint16_t aIntervalMillis, uint8_t aDirection) {
void UserPattern2(NeoPatterns * aNeoPatterns, color32_t aColor, uint16_t aIntervalMillis, uint16_t aRepetitions,
uint8_t aDirection) {
/*
* Sample implementation not supporting DIRECTION_DOWN
*/
Expand All @@ -141,7 +143,9 @@ void UserPattern2(NeoPatterns * aNeoPatterns, color32_t aColor, uint16_t aInterv
aNeoPatterns->Color1 = aColor;
aNeoPatterns->Direction = aDirection;
aNeoPatterns->Index = 0;
aNeoPatterns->TotalStepCounter = 2 * aNeoPatterns->numPixels(); // up and down but do nor use upper pixel twice
// *2 for up and down. (aNeoPatterns->numPixels() - 1) do not use end pixel twice.
// +1 for the initial pattern with end pixel. + 2 for the first and last clear pattern.
aNeoPatterns->TotalStepCounter = ((aRepetitions + 1) * 2 * (aNeoPatterns->numPixels() - 1)) + 1 + 2;
aNeoPatterns->clear();
aNeoPatterns->show();
aNeoPatterns->lastUpdate = millis();
Expand All @@ -152,32 +156,36 @@ void UserPattern2(NeoPatterns * aNeoPatterns, color32_t aColor, uint16_t aInterv
*/
bool UserPattern2Update(NeoPatterns * aNeoPatterns, bool aDoUpdate) {
/*
* Sample implementation not supporting initial direction DIRECTION_DOWN
* Sample implementation
*/
if (aDoUpdate) {
// clear old pixel
aNeoPatterns->setPixelColor(aNeoPatterns->Index, COLOR32_BLACK);

if (aNeoPatterns->decrementTotalStepCounterAndSetNextIndex()) {
return true;
}
/*
* Next index
*/
if (aNeoPatterns->Index == aNeoPatterns->numPixels()) {
// change direction
aNeoPatterns->Direction = DIRECTION_DOWN;
// do nor use upper pixel twice
aNeoPatterns->Index -= 2;
if (aNeoPatterns->Direction == DIRECTION_UP) {
// do not use top pixel twice
if (aNeoPatterns->Index == (aNeoPatterns->numPixels() - 1)) {
aNeoPatterns->Direction = DIRECTION_DOWN;
}
} else {
// do not use bottom pixel twice
if (aNeoPatterns->Index == 0) {
aNeoPatterns->Direction = DIRECTION_UP;
}
}
}
/*
* Refresh pattern
*/
for (uint16_t i = 0; i < aNeoPatterns->numPixels(); i++) {
if (i == aNeoPatterns->Index) {
aNeoPatterns->setPixelColor(i, aNeoPatterns->Color1);
} else {
aNeoPatterns->setPixelColor(i, COLOR32_BLACK);
}
if (aNeoPatterns->TotalStepCounter != 1) {
// last pattern is clear
aNeoPatterns->setPixelColor(aNeoPatterns->Index, aNeoPatterns->Color1);
}
return false;
}
Expand All @@ -190,14 +198,15 @@ void ownPatterns(NeoPatterns * aLedsPtr) {

uint8_t tDuration = random(20, 120);
uint8_t tColor = random(255);
uint8_t tRepetitions = random(2);

switch (sState) {
case 0:
UserPattern1(aLedsPtr, COLOR32_RED_HALF, NeoPatterns::Wheel(tColor), tDuration, FORWARD);
break;

case 1:
UserPattern2(aLedsPtr, NeoPatterns::Wheel(tColor), tDuration, FORWARD);
UserPattern2(aLedsPtr, NeoPatterns::Wheel(tColor), tDuration, tRepetitions, FORWARD);
sState = -1; // Start from beginning
break;

Expand Down
26 changes: 13 additions & 13 deletions examples/AllPatternsOnMultiDevices/AllPatternsOnMultiDevices.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@
void TestPatterns(NeoPatterns * aLedsPtr);
#ifdef ALL_PATTERN_ON_ONE_STRIP
#define PIN_NEOPIXEL_ALL 2
NeoPatterns allPixel = NeoPatterns(104, PIN_NEOPIXEL_ALL, NEO_GRB + NEO_KHZ800, &allPatternsRandomExample);
NeoPatterns bar16 = NeoPatterns(&allPixel, 0, 16, &allPatternsRandomExample);
NeoPatterns allPixel = NeoPatterns(104, PIN_NEOPIXEL_ALL, NEO_GRB + NEO_KHZ800, &allPatternsRandomHandler);
NeoPatterns bar16 = NeoPatterns(&allPixel, 0, 16, &allPatternsRandomHandler);
NeoPatterns bar24 = NeoPatterns(&allPixel, 19, 24, &TestPatterns);
NeoPatterns ring12 = NeoPatterns(&allPixel, 46, 12, &allPatternsRandomExample);
NeoPatterns ring16 = NeoPatterns(&allPixel, 61, 16, &allPatternsRandomExample);
NeoPatterns ring24 = NeoPatterns(&allPixel, 80, 24, &allPatternsRandomExample);
NeoPatterns ring12 = NeoPatterns(&allPixel, 46, 12, &allPatternsRandomHandler);
NeoPatterns ring16 = NeoPatterns(&allPixel, 61, 16, &allPatternsRandomHandler);
NeoPatterns ring24 = NeoPatterns(&allPixel, 80, 24, &allPatternsRandomHandler);
#else
// construct the NeoPatterns instances
NeoPatterns bar16 = NeoPatterns(16, PIN_NEOPIXEL_BAR_16, NEO_GRB + NEO_KHZ800, &allPatternsRandomExample);
NeoPatterns bar24 = NeoPatterns(24, PIN_NEOPIXEL_BAR_24, NEO_GRB + NEO_KHZ800, &allPatternsRandomExample);
NeoPatterns ring12 = NeoPatterns(12, PIN_NEOPIXEL_RING_12, NEO_GRB + NEO_KHZ800, &allPatternsRandomExample);
NeoPatterns ring16 = NeoPatterns(16, PIN_NEOPIXEL_RING_16, NEO_GRB + NEO_KHZ800, &allPatternsRandomExample);
NeoPatterns ring24 = NeoPatterns(24, PIN_NEOPIXEL_RING_24, NEO_GRB + NEO_KHZ800, &allPatternsRandomExample);
NeoPatterns bar16 = NeoPatterns(16, PIN_NEOPIXEL_BAR_16, NEO_GRB + NEO_KHZ800, &allPatternsRandomHandler);
NeoPatterns bar24 = NeoPatterns(24, PIN_NEOPIXEL_BAR_24, NEO_GRB + NEO_KHZ800, &allPatternsRandomHandler);
NeoPatterns ring12 = NeoPatterns(12, PIN_NEOPIXEL_RING_12, NEO_GRB + NEO_KHZ800, &allPatternsRandomHandler);
NeoPatterns ring16 = NeoPatterns(16, PIN_NEOPIXEL_RING_16, NEO_GRB + NEO_KHZ800, &allPatternsRandomHandler);
NeoPatterns ring24 = NeoPatterns(24, PIN_NEOPIXEL_RING_24, NEO_GRB + NEO_KHZ800, &allPatternsRandomHandler);
#endif

/*
Expand All @@ -81,7 +81,7 @@ NeoPatterns ring24 = NeoPatterns(24, PIN_NEOPIXEL_RING_24, NEO_GRB + NEO_KHZ800,
* See MatrixNeoPatterns.h for further explanation.
*/
MatrixSnake NeoPixelMatrix = MatrixSnake(8, 8, PIN_NEOPIXEL_MATRIX,
NEO_MATRIX_BOTTOM | NEO_MATRIX_RIGHT | NEO_MATRIX_ROWS | NEO_MATRIX_PROGRESSIVE, NEO_GRB + NEO_KHZ800, &MatrixAndSnakePatternsDemo);
NEO_MATRIX_BOTTOM | NEO_MATRIX_RIGHT | NEO_MATRIX_ROWS | NEO_MATRIX_PROGRESSIVE, NEO_GRB + NEO_KHZ800, &MatrixAndSnakePatternsDemoHandler);

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Expand Down Expand Up @@ -134,7 +134,7 @@ void setup() {
NeoPixelMatrix.clear(); // Clear matrix
NeoPixelMatrix.show();
NeoPixelMatrix.Delay(7000); // start later
setMatrixAndSnakePatternsDemoTickerText(F("I love NeoPixel"));
setMatrixAndSnakePatternsDemoHandlerTickerText(F("I love NeoPixel"));

/*
* Print voltage once on matrix
Expand Down Expand Up @@ -272,7 +272,7 @@ void TestPatterns(NeoPatterns * aLedsPtr) {
break;
case 8:
// switch to random
initMultipleFallingStars(aLedsPtr, COLOR32_WHITE_HALF, 7, 30, 3, &allPatternsRandomExample);
initMultipleFallingStars(aLedsPtr, COLOR32_WHITE_HALF, 7, 30, 3, &allPatternsRandomHandler);
sState = -1; // Start from beginning
break;
default:
Expand Down
6 changes: 3 additions & 3 deletions examples/MatrixDemo/MatrixDemo.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MatrixDemo.cpp
*
* Simply runs the MatrixAndSnakePatternsDemo for one 8x8 matrix at PIN_NEO_PIXEL_MATRIX.
* Simply runs the MatrixAndSnakePatternsDemoHandler for one 8x8 matrix at PIN_NEO_PIXEL_MATRIX.
*
* You need to install "Adafruit NeoPixel" library under "Tools -> Manage Libraries..." or "Ctrl+Shift+I" -> use "neoPixel" as filter string
*
Expand Down Expand Up @@ -42,7 +42,7 @@
* See MatrixNeoPatterns.h for further explanation.
*/
MatrixSnake NeoPixelMatrix = MatrixSnake(NEOPIXEL_MATRIX_NUM_COLUMNS, NEOPIXEL_MATRIX_NUM_ROWS, PIN_NEOPIXEL_MATRIX,
NEO_MATRIX_BOTTOM | NEO_MATRIX_RIGHT | NEO_MATRIX_ROWS | NEO_MATRIX_PROGRESSIVE, NEO_GRB + NEO_KHZ800, &MatrixAndSnakePatternsDemo);
NEO_MATRIX_BOTTOM | NEO_MATRIX_RIGHT | NEO_MATRIX_ROWS | NEO_MATRIX_PROGRESSIVE, NEO_GRB + NEO_KHZ800, &MatrixAndSnakePatternsDemoHandler);

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Expand All @@ -68,7 +68,7 @@ void setup() {
Serial.print(F("Free Ram/Stack[bytes]="));
Serial.println(SP - (uint16_t) __brkval);

MatrixAndSnakePatternsDemo(&NeoPixelMatrix);
MatrixAndSnakePatternsDemoHandler(&NeoPixelMatrix);
}

uint8_t sWheelPosition = 0; // hold the color index for the changing ticker colors
Expand Down
31 changes: 27 additions & 4 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@ MatrixSnake KEYWORD1

update KEYWORD2
RainbowCycle KEYWORD2
TheaterChase KEYWORD2
ColorWipe KEYWORD2
Scanner KEYWORD2
ScannerExtended KEYWORD2
Fade KEYWORD2
Fire KEYWORD2
Cylon KEYWORD2
Delay KEYWORD2
BouncingBall KEYWORD2
Heartbeat KEYWORD2
UserPattern1 KEYWORD2
UserPattern2 KEYWORD2

printPatternName KEYWORD2
printInfo KEYWORD2
setCallback KEYWORD2
showPatternInitially KEYWORD2
decrementTotalStepCounter KEYWORD2
setNextIndex KEYWORD2
decrementTotalStepCounterAndSetNextIndex KEYWORD2
updateAndWaitForPatternToStop KEYWORD2

initMultipleFallingStars KEYWORD2
allPatternsRandomHandler KEYWORD2
MatrixAndSnakePatternsDemoHandler KEYWORD2

ProcessSelectiveColor KEYWORD2
FadeSelectiveColor KEYWORD2
MovingPicturePGM KEYWORD2
Expand All @@ -33,7 +50,7 @@ loadPicturePGM KEYWORD2
Wheel KEYWORD2
HeatColor KEYWORD2
Snake KEYWORD2
begin KEYWORD2
begin KEYWORD2

#######################################
# Constants (LITERAL1)
Expand All @@ -43,4 +60,10 @@ DIRECTION_RIGHT LITERAL1
DIRECTION_LEFT LITERAL1
DIRECTION_UP LITERAL1
DIRECTION_DOWN LITERAL1
COLOR32 LITERAL1
COLOR32 LITERAL1

FLAG_SCANNER_EXT_ROCKET LITERAL1
FLAG_SCANNER_EXT_CYLON LITERAL1
FLAG_SCANNER_EXT_VANISH_COMPLETE LITERAL1
FLAG_SCANNER_EXT_START_AT_BOTH_ENDS LITERAL1
FLAG_DO_NOT_CLEAR LITERAL1
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version=2.0.0
author=Armin Joachimsmeyer
maintainer=Armin Joachimsmeyer <[email protected]>
sentence=Patterns for NeoPixel strips and matrixes including the patterns of the NeoPattern Example by Adafruit.<br/>
paragraph=Patterns from Adafruit are: <ul><li>SCANNER</li><li>STRIPES</li><li>DELAY</li><li>PROCESS_SELECTIVE</li><li>FADE_SELECTIVE</li></ul>The original SCANNER pattern is extended and includes the CYLON as well as the ROCKET or FALLING_STAR pattern. The more versatile STRIPES pattern replaces the old THEATER_CHASE one.<br/><br/>NeoPixel-Matrix pattern are:<ul><li>MOVING_PICTURE</li><li>MOVE</li><li>TICKER</li><li>FIRE</li><li>SNAKE</li></ul><br/>The SNAKE pattern is an implementation of the Snake game and can be played with 2 or 4 buttons attached to the Arduino.<br/>The SnakeAutorun example will start your own code provided in the function getNextSnakeDirection() to solve the Snake game.<br/><br/>The extras folder contains sample breadboard pictures as well as a Python script, which enables Snake to be played by PC keyboard or game controller.<br/>YouTube demos are available under <a href="https://github.com/ArminJo/NeoPatterns">https://github.com/ArminJo/NeoPatterns</a>.<br/><br/>To test your own pattern, just add your pattern code to the functions UserPattern\[1,2]() and UserPattern\[1,2]Update() in NeoPatternsSimpleDemo.cpp to see the patterns. Enable TEST_USER_PATTERNS on line 39 to activate them.<br/>
paragraph=Patterns from Adafruit are: <ul><li>SCANNER</li><li>STRIPES</li><li>DELAY</li><li>PROCESS_SELECTIVE</li><li>FADE_SELECTIVE</li></ul>The original SCANNER pattern is extended and includes the CYLON as well as the ROCKET or FALLING_STAR pattern. The more versatile STRIPES pattern replaces the old THEATER_CHASE one.<br/><br/>NeoPixel-Matrix pattern are:<ul><li>MOVING_PICTURE</li><li>MOVE</li><li>TICKER</li><li>FIRE</li><li>SNAKE</li></ul><br/>The SNAKE pattern is an implementation of the Snake game and can be played with 2 or 4 buttons attached to the Arduino.<br/>The SnakeAutorun example will start your own code provided in the function getNextSnakeDirection() to solve the Snake game.<br/><br/>The extras folder contains sample breadboard pictures as well as a Python script, which enables Snake to be played by PC keyboard or game controller.<br/>YouTube demos are available under <a href="https://github.com/ArminJo/NeoPatterns">https://github.com/ArminJo/NeoPatterns</a>.<br/><br/>To test your own pattern, just add your pattern code to the functions UserPattern\[1,2]() and UserPattern\[1,2]Update() in AllPatternOnOneStrip.cpp to see the patterns. Enable TEST_USER_PATTERNS on line 41 to activate them.<br/>
category=Display
url=https://github.com/ArminJo/NeoPatterns
architectures=avr
4 changes: 2 additions & 2 deletions src/MatrixSnake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ void SnakeAutorunCompleteHandler(NeoPatterns * aLedsPtr) {
const char sDefaultTickerText[] PROGMEM = "I love Neopixel";
const char * sTickerTextPtr = sDefaultTickerText;

void setMatrixAndSnakePatternsDemoTickerText(const __FlashStringHelper * aTextForTicker) {
void setMatrixAndSnakePatternsDemoHandlerTickerText(const __FlashStringHelper * aTextForTicker) {
sTickerTextPtr = reinterpret_cast<const char*>(aTextForTicker);
}

Expand All @@ -887,7 +887,7 @@ void setMatrixAndSnakePatternsDemoTickerText(const __FlashStringHelper * aTextFo
* 2. Moves heart in from top / bottom, show 2 heart beats, and move heart out
* 3. Show 2 snake runs / fire. Snake shows up on the odd loops, fire on the even ones
*/
void MatrixAndSnakePatternsDemo(NeoPatterns * aLedsPtr) {
void MatrixAndSnakePatternsDemoHandler(NeoPatterns * aLedsPtr) {
MatrixSnake* tLedsPtr = (MatrixSnake*) aLedsPtr;
static int8_t sState = 0;
static uint8_t sHeartDirection = DIRECTION_DOWN;
Expand Down
4 changes: 2 additions & 2 deletions src/MatrixSnake.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ uint8_t computeDirection(position aStartPosition, position aEndPosition);

extern const char sDefaultTickerText[] PROGMEM; // = "I love Neopixel"
extern const char * sTickerTextPtr; // = sDefaultTickerText;
void setMatrixAndSnakePatternsDemoTickerText(const __FlashStringHelper * aTextForTicker);
void MatrixAndSnakePatternsDemo(NeoPatterns * aLedsPtr);
void setMatrixAndSnakePatternsDemoHandlerTickerText(const __FlashStringHelper * aTextForTicker);
void MatrixAndSnakePatternsDemoHandler(NeoPatterns * aLedsPtr);

void initSnakeAutorun(MatrixSnake * aLedsPtr, uint16_t aIntervalMillis, color32_t aColor, uint16_t aRepetitions = 1);
uint8_t getNextSnakeDirection(MatrixSnake * aSnake);
Expand Down
Loading

0 comments on commit 603a9f0

Please sign in to comment.