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:   NeoPixels ECE4180_Werable_LCD_DEMO neopixel_spi NeoPixels ... more

Committer:
aswild
Date:
Wed May 21 02:28:43 2014 +0000
Revision:
1:f531a2be180d
Parent:
0:9f237b11f0a8
uses new gpio_init_out() HAL function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aswild 0:9f237b11f0a8 1 /**********************************************
aswild 0:9f237b11f0a8 2 * NeoStrip.cpp
aswild 0:9f237b11f0a8 3 *
aswild 0:9f237b11f0a8 4 * Allen Wild
aswild 0:9f237b11f0a8 5 * March 2014
aswild 0:9f237b11f0a8 6 *
aswild 0:9f237b11f0a8 7 * Controls a strip of Adafruit NeoPixels, addressable RGB LEDs
aswild 0:9f237b11f0a8 8 * Currently, because of the global nature of the IO register and bitmask variables,
aswild 0:9f237b11f0a8 9 * it is only possible to use one NeoStrip instance at a time.
aswild 0:9f237b11f0a8 10 *
aswild 0:9f237b11f0a8 11 * This library supports only the NXP LPC1768!
aswild 0:9f237b11f0a8 12 */
aswild 0:9f237b11f0a8 13
aswild 0:9f237b11f0a8 14 #include "mbed.h"
aswild 0:9f237b11f0a8 15 #include "NeoStrip.h"
aswild 0:9f237b11f0a8 16
aswild 0:9f237b11f0a8 17 // function to write to the strip, implemented in ARM assembly
aswild 0:9f237b11f0a8 18 extern "C" void neo_out(NeoColor*, int);
aswild 0:9f237b11f0a8 19
aswild 0:9f237b11f0a8 20 // FastIO register address and bitmask for the GPIO pin
aswild 0:9f237b11f0a8 21 // because these are imported in the assembly
aswild 0:9f237b11f0a8 22 uint32_t neo_fio_reg;
aswild 0:9f237b11f0a8 23 uint32_t neo_bitmask;
aswild 0:9f237b11f0a8 24
aswild 0:9f237b11f0a8 25 NeoStrip::NeoStrip(PinName pin, int N) : N(N)
aswild 0:9f237b11f0a8 26 {
aswild 0:9f237b11f0a8 27 bright = 0.5;
aswild 0:9f237b11f0a8 28 Nbytes = N * 3;
aswild 0:9f237b11f0a8 29 strip = (NeoColor*)malloc(N * sizeof(NeoColor));
aswild 0:9f237b11f0a8 30 if (strip == NULL)
aswild 0:9f237b11f0a8 31 {
aswild 0:9f237b11f0a8 32 printf("NeoStrip: ERROR unable to malloc strip data");
aswild 0:9f237b11f0a8 33 N = 0;
aswild 0:9f237b11f0a8 34 }
aswild 0:9f237b11f0a8 35
aswild 1:f531a2be180d 36 gpio_init_out(&gpio, pin); // initialize GPIO registers
aswild 0:9f237b11f0a8 37 neo_fio_reg = (uint32_t)gpio.reg_dir; // set registers and bitmask for
aswild 0:9f237b11f0a8 38 neo_bitmask = 1 << ((int)pin & 0x1F); // the assembly to use
aswild 0:9f237b11f0a8 39 }
aswild 0:9f237b11f0a8 40
aswild 0:9f237b11f0a8 41 void NeoStrip::setBrightness(float bright)
aswild 0:9f237b11f0a8 42 {
aswild 0:9f237b11f0a8 43 this->bright = bright;
aswild 0:9f237b11f0a8 44 }
aswild 0:9f237b11f0a8 45
aswild 0:9f237b11f0a8 46 void NeoStrip::setPixel(int p, int color)
aswild 0:9f237b11f0a8 47 {
aswild 0:9f237b11f0a8 48 int red = (color & 0xFF0000) >> 16;
aswild 0:9f237b11f0a8 49 int green = (color & 0x00FF00) >> 8;
aswild 0:9f237b11f0a8 50 int blue = (color & 0x0000FF);
aswild 0:9f237b11f0a8 51 setPixel(p, red, green, blue);
aswild 0:9f237b11f0a8 52 }
aswild 0:9f237b11f0a8 53
aswild 0:9f237b11f0a8 54 void NeoStrip::setPixel(int p, uint8_t red, uint8_t green, uint8_t blue)
aswild 0:9f237b11f0a8 55 {
aswild 0:9f237b11f0a8 56 // set the given pixel's RGB values
aswild 0:9f237b11f0a8 57 // the array is indexed modulo N to avoid overflow
aswild 0:9f237b11f0a8 58 strip[p % N].red = (uint8_t)(red * bright);
aswild 0:9f237b11f0a8 59 strip[p % N].green = (uint8_t)(green * bright);
aswild 0:9f237b11f0a8 60 strip[p % N].blue = (uint8_t)(blue * bright);
aswild 0:9f237b11f0a8 61 }
aswild 0:9f237b11f0a8 62
aswild 0:9f237b11f0a8 63 void NeoStrip::setPixels(int p, int n, const int *colors)
aswild 0:9f237b11f0a8 64 {
aswild 0:9f237b11f0a8 65 int r, g, b;
aswild 0:9f237b11f0a8 66 for (int i = 0; i < n; i++)
aswild 0:9f237b11f0a8 67 {
aswild 0:9f237b11f0a8 68 r = (colors[i] & 0xFF0000) >> 16;
aswild 0:9f237b11f0a8 69 g = (colors[i] & 0x00FF00) >>8;
aswild 0:9f237b11f0a8 70 b = colors[i] & 0x0000FF;
aswild 0:9f237b11f0a8 71 setPixel(p+i, r, g, b);
aswild 0:9f237b11f0a8 72 }
aswild 0:9f237b11f0a8 73 }
aswild 0:9f237b11f0a8 74
aswild 0:9f237b11f0a8 75 void NeoStrip::clear()
aswild 0:9f237b11f0a8 76 {
aswild 0:9f237b11f0a8 77 for (int i = 0; i < N; i++)
aswild 0:9f237b11f0a8 78 {
aswild 0:9f237b11f0a8 79 strip[i].red = 0;
aswild 0:9f237b11f0a8 80 strip[i].green = 0;
aswild 0:9f237b11f0a8 81 strip[i].blue = 0;
aswild 0:9f237b11f0a8 82 }
aswild 0:9f237b11f0a8 83 }
aswild 0:9f237b11f0a8 84
aswild 0:9f237b11f0a8 85 void NeoStrip::write()
aswild 0:9f237b11f0a8 86 {
aswild 0:9f237b11f0a8 87 __disable_irq(); // disable interrupts
aswild 0:9f237b11f0a8 88 neo_out(strip, Nbytes); // output to the strip
aswild 0:9f237b11f0a8 89 __enable_irq(); // enable interrupts
aswild 0:9f237b11f0a8 90 wait_us(50); // wait 50us for the reset pulse
aswild 0:9f237b11f0a8 91 }
aswild 0:9f237b11f0a8 92
aswild 0:9f237b11f0a8 93