neostrip code

Fork of NeoStrip by James Song

Committer:
jamesmsong
Date:
Fri Nov 25 00:52:48 2016 +0000
Revision:
2:d607600a697d
Parent:
0:9f237b11f0a8
Child:
3:00f3f32e6a1e
24Nov

Who changed what in which revision?

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