Library for controlling a strip of Adafruit NeoPixels with WS2812 drivers. Because of the strict timing requirements of the self-clocking data signal, the critical parts of code are written in ARM assembly. Currently, only the NXP LPC1768 platform is supported. More information about NeoPixels can be found at http://learn.adafruit.com/adafruit-neopixel-uberguide/overview

Dependents:   Merged_Demo

Fork of NeoStrip by Allen Wild

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NeoStrip.cpp Source File

NeoStrip.cpp

00001 /**********************************************
00002  * NeoStrip.cpp
00003  *
00004  * Allen Wild
00005  * March 2014
00006  *
00007  * Controls a strip of Adafruit NeoPixels, addressable RGB LEDs
00008  * Currently, because of the global nature of the IO register and bitmask variables,
00009  * it is only possible to use one NeoStrip instance at a time.
00010  *
00011  * This library supports only the NXP LPC1768!
00012  */
00013 
00014 #include "mbed.h"
00015 #include "NeoStrip.h"
00016 
00017 // function to write to the strip, implemented in ARM assembly
00018 extern "C" void neo_out(NeoColor*, int);
00019 
00020 // FastIO register address and bitmask for the GPIO pin
00021 // because these are imported in the assembly
00022 uint32_t neo_fio_reg;
00023 uint32_t neo_bitmask;
00024 
00025 NeoStrip::NeoStrip(PinName pin, int N) : N(N)
00026 {
00027     bright = 0.5;
00028     Nbytes = N * 3;
00029     strip = (NeoColor*)malloc(N * sizeof(NeoColor));
00030     if (strip == NULL)
00031     {
00032         printf("NeoStrip: ERROR unable to malloc strip data");
00033         N = 0;
00034     }
00035     
00036     gpio_init(&gpio, pin);      // initialize GPIO registers
00037     neo_fio_reg = (uint32_t)gpio.reg_dir;   // set registers and bitmask for
00038     neo_bitmask = 1 << ((int)pin & 0x1F);   // the assembly to use
00039 }
00040 
00041 void NeoStrip::setBrightness(float bright)
00042 {
00043     this->bright = bright;
00044 }
00045 
00046 void NeoStrip::setPixel(int p, int color)
00047 {
00048     int red = (color & 0xFF0000) >> 16;
00049     int green = (color & 0x00FF00) >> 8;
00050     int blue = (color & 0x0000FF);
00051     setPixel(p, red, green, blue);
00052 }
00053 
00054 void NeoStrip::setPixel(int p, uint8_t red, uint8_t green, uint8_t blue)
00055 {
00056     // set the given pixel's RGB values
00057     // the array is indexed modulo N to avoid overflow
00058     strip[p % N].red = (uint8_t)(red * bright);
00059     strip[p % N].green = (uint8_t)(green * bright);
00060     strip[p % N].blue = (uint8_t)(blue * bright);
00061 }
00062 
00063 void NeoStrip::setPixels(int p, int n, const int *colors)
00064 {
00065     int r, g, b;
00066     for (int i = 0; i < n; i++)
00067     {
00068         r = (colors[i] & 0xFF0000) >> 16;
00069         g = (colors[i] & 0x00FF00) >>8;
00070         b = colors[i] & 0x0000FF;
00071         setPixel(p+i, r, g, b);
00072     }
00073 }
00074 
00075 void NeoStrip::clear()
00076 {
00077     for (int i = 0; i < N; i++)
00078     {
00079         strip[i].red = 0;
00080         strip[i].green = 0;
00081         strip[i].blue = 0;
00082     }
00083 }
00084 
00085 void NeoStrip::write()
00086 {
00087     __disable_irq();        // disable interrupts
00088     neo_out(strip, Nbytes); // output to the strip
00089     __enable_irq();         // enable interrupts
00090     wait_us(50);            // wait 50us for the reset pulse
00091 }
00092 
00093