Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// WS2812_RFduino_Test
// By Thomas Olson
// No complicated Pixel Library needed.
// Tested with WS2812B 4 pin versions.
// Modified by Sam Decrock to resemble NeoPixel API
// Modified by http://jos.ph to incorporate more of NeoPixel examples
// 20160510 ... verified works with Arduino 1.6.5 and simblee module
// >>> using 3.3v > 5v level shifter between pin 6 and NeoPixels
const int ws2812pin = 6;
const int nPIXELS = 144;
const int nLEDs = nPIXELS * 3;
uint8_t ledBar[nLEDs];
uint8_t brightness = 240;
void setup() {
pinMode(ws2812pin, OUTPUT);
digitalWrite(ws2812pin, LOW);
clearStrip();
delay(1);
}
// Taken from neopixel strandtest:
void loop() {
theaterChase(Color(127, 127, 127), 100, 50); // color, nCycles, wait
theaterChaseRainbow(1, 50); //nCycles, wait
rainbowCycle(10, 10); //nCycles, wait
colorWipe(Color(255, 0, 0), 5); // Red
colorWipe(Color(0, 255, 0), 5); // Green
colorWipe(Color(0, 0, 255), 5); // Blue
colorWipe(Color(255, 255, 255), 5); // White
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t color, uint8_t wait) {
for(uint16_t i=0; i<nPIXELS; i++) {
setPixelColor(i, color);
showStrip();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t nCycles, uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*nCycles; j++) { // 5 cycles of all colors on wheel
for(i=0; i< nPIXELS; i++)
setPixelColor(i, Wheel(((i * 256 / nPIXELS) + j) & 255));
showStrip();
delay(wait);
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t nCycles, uint8_t wait) {
for (int j=0; j < (256*nCycles)/2; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < nPIXELS; i=i+3)
setPixelColor(i+q, Wheel( (i+j*2) % 255)); //turn every third pixel on
showStrip();
delay(wait);
for (uint16_t i=0; i < nPIXELS; i=i+3)
setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t nCycles, uint8_t wait) {
for (int j=0; j<nCycles; j++) {
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < nPIXELS; i=i+3)
setPixelColor(i+q, c); //turn every third pixel on
showStrip();
delay(wait);
for (uint16_t i=0; i < nPIXELS; i=i+3)
setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
// ==============================================
// ---------------- NeoPixel API ----------------
// ==============================================
uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
void setPixelColor(int pixel, uint32_t color) {
uint8_t red = (uint8_t)(color >> 16);
uint8_t green = (uint8_t)(color >> 8);
uint8_t blue = (uint8_t)color;
red = (red * brightness) >> 8;
green = (green * brightness) >> 8;
blue = (blue * brightness) >> 8;
setPixelColor(pixel, red, green, blue);
}
void setPixelColor(int pixel, byte red, byte green, byte blue) {
ledBar[3*pixel] = green;
ledBar[3*pixel+1] = red;
ledBar[3*pixel+2] = blue;
}
void clearStrip() {
for(uint32_t i; i<nPIXELS; i++)
setPixelColor(i, 0);
showStrip();
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85)
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
if(WheelPos < 170) {
WheelPos -= 85;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void showStrip(){
noInterrupts();
for(int wsOut = 0; wsOut < nLEDs; wsOut++){
for(int x=7; x>=0; x--){
NRF_GPIO->OUTSET = (1UL << ws2812pin);
if(ledBar[wsOut] & (0x01 << x)) {
__ASM ( \
" NOP\n\t" \
" NOP\n\t" \
" NOP\n\t" \
" NOP\n\t" \
" NOP\n\t" \
);
NRF_GPIO->OUTCLR = (1UL << ws2812pin);
}else{
NRF_GPIO->OUTCLR = (1UL << ws2812pin);
__ASM ( \
" NOP\n\t" \
" NOP\n\t" \
" NOP\n\t" \
);
}
}
}
delayMicroseconds(50); // latch and reset WS2812.
interrupts();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment