Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
Time handling was being insane on the second core. Switched to using …
Browse files Browse the repository at this point in the history
…PICO SDK time handlers that are nicer anyway. Added CHASE animation.
  • Loading branch information
jbarket committed Aug 30, 2021
1 parent 7691ea6 commit fe9b427
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ If your board has WS2812 (or similar) LEDs, these can be configured in your boar
| BOARD_LEDS_COUNT | Total LEDs in your strand | No |
| LEDS_BASE_ANIMATION_FIRST_PIXEL | The index for the first pixel in your base animation. If you have additional LEDs that aren't part of the base animation, this is your chance to leave them out of it. By default, this is 0. | Yes |
| LEDS_BASE_ANIMATION_LAST_PIXEL | Same as above, but the index for the final pixel. By default, this is 11... 12 LEDs for 12 buttons on a standard stickless layout. | Yes |
| LEDS_BASE_ANIMATION | This can be either "RAINBOW" or "STATIC" to set your base animation | Yes |
| LEDS_BASE_ANIMATION | This can be either "RAINBOW", "CHASE" or "STATIC" to set your base animation | Yes |
| LEDS_RAINBOW_CYCLE_TIME | For "RAINBOW," this sets how long (in ms) it takes to cycle from one color step to the next | Yes |
| LEDS_CHASE_CYCLE_TIME | For "CHASE," this sets how long (in ms) it takes to move from one pixel to the next | Yes |
| LEDS_STATIC_COLOR_COLOR | For "STATIC", this sets the static color. This is a uint32_t value. This should be friendlier, I know. | Yes |

### Building the Project
Expand Down
6 changes: 5 additions & 1 deletion include/definitions/Defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@
#endif

#ifndef LEDS_RAINBOW_CYCLE_TIME
#define LEDS_RAINBOW_CYCLE_TIME 500
#define LEDS_RAINBOW_CYCLE_TIME 40
#endif

#ifndef LEDS_CHASE_CYCLE_TIME
#define LEDS_CHASE_CYCLE_TIME 85
#endif

#ifndef LEDS_STATIC_COLOR_COLOR
Expand Down
2 changes: 0 additions & 2 deletions include/definitions/OpenStickBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
#define LEDS_BUTTON_07 11
#define LEDS_BUTTON_08 10

#define LEDS_BASE_ANIMATION "STATIC"

#define DEFAULT_SOCD_MODE SOCD_MODE_NEUTRAL

#endif
8 changes: 8 additions & 0 deletions lib/AnimationStation/src/AnimationStation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "AnimationStation.hpp"
#include "Effects/Rainbow.hpp"
#include "Effects/StaticColor.hpp"
#include "Effects/Chase.hpp"

AnimationStation::AnimationStation(int numPixels) {
this->numPixels = numPixels;
Expand All @@ -31,6 +32,13 @@ void AnimationStation::SetRainbow(bool defaultAnimation, int firstPixel, int las
this->animations.push_back(new Rainbow(firstPixel, lastPixel, cycleTime, defaultAnimation));
}

void AnimationStation::SetChase(bool defaultAnimation, int firstPixel, int lastPixel = -1, int cycleTime = 50) {
if (lastPixel < 0)
lastPixel = this->numPixels - 1;

this->animations.push_back(new Chase(firstPixel, lastPixel, cycleTime, defaultAnimation));
}

void AnimationStation::Animate() {
for (auto & element : this->animations) {
element->Animate(this->frame);
Expand Down
1 change: 1 addition & 0 deletions lib/AnimationStation/src/AnimationStation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AnimationStation
void Animate();
void SetStaticColor(bool defaultAnimation, uint32_t color, int firstPixel, int lastPixel);
void SetRainbow(bool defaultAnimation, int firstPixel, int lastPixel, int cycleTime);
void SetChase(bool defaultAnimation, int firstPixel, int lastPixel, int cycleTime);
void Clear();

uint32_t frame[100];
Expand Down
77 changes: 77 additions & 0 deletions lib/AnimationStation/src/Effects/Chase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "Chase.hpp"
#include "Helpers.hpp"

void Chase::Animate(uint32_t (&frame)[100]) {
if (!time_reached(this->nextRunTime)) {
return;
}

for (int i = this->firstPixel; i < this->lastPixel + 1; ++i) {
if (this->IsChasePixel(i)) {
frame[i] = Helpers::Wheel(this->WheelFrame(i));
} else {
frame[i] = 0;
}
}

currentPixel++;

if (currentPixel > this->lastPixel) {
currentPixel = this->firstPixel;
}

if (reverse) {
currentFrame--;

if (currentFrame < 0) {
currentFrame = 1;
reverse = false;
}
}
else {
currentFrame++;

if (currentFrame > 255) {
currentFrame = 254;
reverse = true;
}
}

this->nextRunTime = make_timeout_time_ms(this->cycleTime);
}

bool Chase::IsChasePixel(int i) {
if (i == this->currentPixel || i == (this->currentPixel - 1) % this->totalPixels || i == (this->currentPixel - 2) % this->totalPixels) {
return true;
}

return false;
}

int Chase::WheelFrame(int i) {
int frame = this->currentFrame;

if (i == (this->currentPixel - 1) % totalPixels) {
if (this->reverse) {
frame = frame + 16;
}
else {
frame = frame - 16;
}
}

if (i == (this->currentPixel - 2) % totalPixels) {
if (this->reverse) {
frame = frame + 32;
}
else {
frame = frame - 32;
}
}

if (frame < 0) {
return 0;
}

return frame;
}
30 changes: 30 additions & 0 deletions lib/AnimationStation/src/Effects/Chase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _CHASE_H_
#define _CHASE_H_

#include <stdio.h>
#include <stdlib.h>
#include "../Animation.hpp"

class Chase : public Animation {
public:
Chase() : Animation() {
}
Chase(int firstPixel, int lastPixel, int cycleTime = 50, bool defaultAnimation = false) : Animation(firstPixel, lastPixel, defaultAnimation) {
this->cycleTime = cycleTime;
this->currentPixel = firstPixel;
this->totalPixels = this->lastPixel - this->firstPixel + 1;
}
void Animate(uint32_t (&frame)[100]);
protected:
bool IsChasePixel(int i);
int WheelFrame(int i);

int cycleTime;
int currentFrame = 0;
int currentPixel = 0;
int totalPixels;
bool reverse = false;
absolute_time_t nextRunTime = 0;
};

#endif
7 changes: 4 additions & 3 deletions lib/AnimationStation/src/Effects/Rainbow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include "Helpers.hpp"

void Rainbow::Animate(uint32_t (&frame)[100]) {
if (to_ms_since_boot(get_absolute_time()) - this->nextRunTime < 0)
return;
if (!time_reached(this->nextRunTime)) {
return;
}

for (int i = this->firstPixel; i < this->lastPixel + 1; ++i) {
frame[i] = Helpers::Wheel(this->currentFrame);
Expand All @@ -29,5 +30,5 @@ void Rainbow::Animate(uint32_t (&frame)[100]) {
}
}

this->nextRunTime = to_ms_since_boot(get_absolute_time()) + this->cycleTime;
this->nextRunTime = make_timeout_time_ms(this->cycleTime);
}
2 changes: 1 addition & 1 deletion lib/AnimationStation/src/Effects/Rainbow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Rainbow : public Animation {
int cycleTime;
int currentFrame = 0;
bool reverse = false;
uint32_t nextRunTime = 0;
absolute_time_t nextRunTime = 0;
};

#endif
2 changes: 1 addition & 1 deletion lib/Helpers/src/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _HELPERS_H_

#include <stdint.h>
#include "pico/stdlib.h"

class Helpers
{
Expand All @@ -10,5 +11,4 @@ class Helpers
static uint32_t Wheel(uint8_t pos);
};


#endif
7 changes: 6 additions & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ void core1()
if (LEDS_BASE_ANIMATION == "STATIC") {
as.SetStaticColor(true, LEDS_STATIC_COLOR_COLOR, LEDS_BASE_ANIMATION_FIRST_PIXEL, LEDS_BASE_ANIMATION_LAST_PIXEL);
}
else {

if (LEDS_BASE_ANIMATION == "CHASE") {
as.SetChase(true, LEDS_BASE_ANIMATION_FIRST_PIXEL, LEDS_BASE_ANIMATION_LAST_PIXEL, LEDS_CHASE_CYCLE_TIME);
}

if (LEDS_BASE_ANIMATION == "RAINBOW") {
as.SetRainbow(true, LEDS_BASE_ANIMATION_FIRST_PIXEL, LEDS_BASE_ANIMATION_LAST_PIXEL, LEDS_RAINBOW_CYCLE_TIME);
}

Expand Down

0 comments on commit fe9b427

Please sign in to comment.