Hmā¦ seems that if speed is the most important thing to me, I should just go with DotStar LEDs.
What would you rate as the difficulty level of porting my code from neopixel library to a DotStar library for the Particle Photon?
My neopixel code:
#include <Particle.h>
#include <neopixel.h>
SYSTEM_MODE(AUTOMATIC)
const int PIXEL_COUNT = 1728;
const int PIXEL_PIN = D1;
const int PIXEL_TYPE = SK6812RGBW;
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
struct ANIMATION_DATA
{
uint8_t g;
uint8_t r;
uint8_t b;
int pos;
int len;
};
// circular buffer with 100 slots will overwrite unfinished animations when full
const int maxAnim = 100;
int animHead = 0;
int animTail = 0;
ANIMATION_DATA anim[maxAnim];
const uint8_t whiteAnim[] =
{ 255, 200, 175, 160, 155, 150, 145, 140, 135, 130
, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80
, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30
, 25, 20, 15, 10, 5, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
, 0, 0, 0, 0, 0, 0, 0
};
const uint8_t brightAnim[] =
{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
, 255, 255, 255, 255, 255, 255, 255, 255, 245, 240
, 240, 240, 240, 235, 230, 230, 230, 230, 225, 220
, 220, 220, 220, 215, 210, 210, 210, 210, 205, 200
, 200, 200, 200, 200, 195, 195, 190, 190, 190, 190
, 190, 195, 180, 180, 180, 175, 170, 165, 160, 155
, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105
, 100, 95, 90, 85, 80, 75, 70
};
const int cometLen = sizeof(whiteAnim) / sizeof(whiteAnim[0]);
const uint32_t msRefresh = 0;
const int stepSpeed = 10;
static int animationStep = 0;
void setup()
{
memset(anim, 0, sizeof(anim));
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Particle.function("led", addComet);
//strip.setBrightness(30);
}
void loop()
{
static uint32_t msDelay = 0;
if (millis() - msDelay < msRefresh) return;
msDelay = millis();
static bool sparkleState = false;
static int sparklePixel = 0;
if (animHead != animTail)
{
ANIMATION_DATA *a;
for (int animationStep = animTail; animationStep != animHead; )
{
a = &anim[animationStep];
if (a->len)
{
int px;
//Draw the Comet
for (int p = a->len - 1; p >= 0; p--)
{
px = constrain(a->pos - p, 0, PIXEL_COUNT - 1);
strip.setColorDimmed(px, a->g, a->r, a->b, whiteAnim[p], brightAnim[p]);
}
//Erase the pixels behind comet as it moves forward
if (a->pos - a->len >= 0)
for (int k = 0; k < stepSpeed; k++)
strip.setPixelColor(a->pos - k - a->len, 0);
//Make comet jump forward stepSpeed pixels at a time to make comet run seemingly faster
if (a->pos < PIXEL_COUNT + stepSpeed)
a->pos += stepSpeed;
else
a->len -= stepSpeed;
if (a->len <= 0)
{
*a = { 0, 0, 0, 0, 0 };
animTail++;
animTail %= maxAnim;
}
}
animationStep++;
animationStep %= maxAnim;
}
}
sparkleState = !sparkleState; // flip state
if (sparkleState)
{
sparklePixel = random(PIXEL_COUNT);
strip.setPixelColor(sparklePixel, random(255), random(255), random(255), 100);
}
else
strip.setPixelColor(sparklePixel, 0, 0, 0, 0);
strip.show();
}
struct topic {
char title[16];
uint8_t rgb[3];
};
const topic topicList[] =
{ { "login" , { 200, 200, 200 } } // white
, { "idea" , { 255, 255, 0 } } // yellow
, { "comment", { 0, 0, 255 } } // blue
, { "outcome", { 255, 0, 0 } } // green
, { "project", { 255, 0, 0 } } // green
, { "status" , { 100, 255, 0 } } // orange
, { "step" , { 0, 75, 255 } } // purple
, { "vote" , { 0, 255, 0 } } // red
, { "view" , { 105, 255, 200 } } // pink
};
const int topics = sizeof(topicList)/sizeof(topicList[0]);
int addComet(String command)
{
int retVal = -1;
for (int i = 0; i < topics; i++)
{
if (strcmp((const char*)command, topicList[i].title) == 0)
{
anim[animHead] = { topicList[i].rgb[0], topicList[i].rgb[1], topicList[i].rgb[2], 0, cometLen }; // purple
Particle.publish(command);
animHead++;
animHead %= maxAnim;
retVal = i;
break;
}
}
return retVal;
}