Skip to content

Instantly share code, notes, and snippets.

@fraguada
Created September 15, 2013 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fraguada/6574129 to your computer and use it in GitHub Desktop.
Save fraguada/6574129 to your computer and use it in GitHub Desktop.
Proof of concept for running multiple sequences on Neopixel LED strips. By no means optimized or clever...
//testing RTOS for multiple neopixel strips
//ChibiOS for Arduino sourced from: http://code.google.com/p/rtoslibs/
#include <ChibiOS_AVR.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(3, 8, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(3, 9, NEO_GRB + NEO_KHZ800);
uint8_t wait = 100;
uint8_t r = 255;
uint8_t g = 0;
uint8_t b = 255;
uint32_t steps = 20;
uint32_t steps2 = 40;
volatile uint32_t i1,i2,j1,j2 = 0;
//------------------------------------------------------------------------------
// thread 1 - high priority for neopixel pulse
// 100 byte stack beyond task switch and interrupt needs
static WORKING_AREA(waThread1, 100);
static msg_t Thread1(void *arg) {
while(true){
byte _r, _g, _b;
for(i1 = 1; i1 < steps; i1++)
{
//set color level color channel / steps
_r = (byte)r/i1;
_g = (byte)g/i1;
_b = (byte)b/i1;
for(j1 = 0; j1 < strip1.numPixels(); j1++)
{
//set led color
strip1.setPixelColor(j1, _r, _g, _b);
}
strip1.show();
// Serial.println("1 in" + i1);
chThdSleepMilliseconds(wait);
}
for(i1 = steps; i1 > 1; i1--)
{
//set color level color channel / steps
_r = (byte)r/i1;
_g = (byte)g/i1;
_b = (byte)b/i1;
for(j1 = 0; j1 < strip1.numPixels(); j1++)
{
//set led color
strip1.setPixelColor(j1, _r, _g, _b);
}
strip1.show();
//Serial.println("1 out" + i1);
chThdSleepMilliseconds(wait);
}
}
}
//------------------------------------------------------------------------------
// thread 2 - high priority for neopixel pulse
// 100 byte stack beyond task switch and interrupt needs
static WORKING_AREA(waThread2, 100);
static msg_t Thread2(void *arg) {
while(true){
byte _r, _g, _b;
for(i2 = 1; i2 < steps2; i2++)
{
//set color level color channel / steps
_r = (byte)r/i2;
_g = (byte)g/i2;
_b = (byte)b/i2;
for( j2 = 0; j2 < strip2.numPixels(); j2++)
{
//set led color
strip2.setPixelColor(j2, _r, _g, _b);
}
strip2.show();
chThdSleepMilliseconds(wait);
}
for( i2 = steps2; i2 > 1; i2--)
{
//set color level color channel / steps
_r = (byte)r/i2;
_g = (byte)g/i2;
_b = (byte)b/i2;
for( j2 = 0; j2 < strip2.numPixels(); j2++)
{
//set led color
strip2.setPixelColor(j2, _r, _g, _b);
}
strip2.show();
chThdSleepMilliseconds(wait);
}
}
}
void setup() {
strip1.begin();
strip2.begin();
strip1.show(); // Initialize all pixels to 'off'
strip2.show(); // Initialize all pixels to 'off'
Serial.begin(9600);
while (!Serial) {
}
chBegin(mainThread);
// chBegin never returns, main thread continues with mainThread()
while(1) {
}
}
//------------------------------------------------------------------------------
// main thread runs at NORMALPRIO
void mainThread() {
// start strip 1 pulse
chThdCreateStatic(waThread1, sizeof(waThread1),NORMALPRIO + 1, Thread1, NULL);
// start strip 2 pulse
chThdCreateStatic(waThread2, sizeof(waThread2),NORMALPRIO + 1, Thread2, NULL);
// let other things happen
while (1) {
chThdSleepMilliseconds(10);
}
}
//------------------------------------------------------------------------------
void loop() {
// not used
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment