This allows an LPC1768 to take Art-Net data and send it to WS2812B LED's

Dependencies:   mbed mbed-rtos EthernetInterface

Committer:
tonydbeck
Date:
Wed Dec 26 21:05:02 2018 +0000
Revision:
15:c730bd607d9a
Full Working Version - December 18

Who changed what in which revision?

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