neostrip code

Fork of NeoStrip by James Song

Committer:
otis22894
Date:
Sun Dec 11 21:12:28 2016 +0000
Revision:
4:623b0c643dd6
Parent:
3:00f3f32e6a1e
First commit

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 }
otis22894 3:00f3f32e6a1e 24 gpio_init_out(&gpio, pin);
otis22894 3:00f3f32e6a1e 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
otis22894 3:00f3f32e6a1e 47 // RDW: swapped g / r because who knows why it doesn't work
otis22894 3:00f3f32e6a1e 48 strip[p % N].red = (uint8_t)(green * bright);
otis22894 3:00f3f32e6a1e 49 strip[p % N].green = (uint8_t)(red * bright);
aswild 0:9f237b11f0a8 50 strip[p % N].blue = (uint8_t)(blue * bright);
aswild 0:9f237b11f0a8 51 }
aswild 0:9f237b11f0a8 52
aswild 0:9f237b11f0a8 53 void NeoStrip::setPixels(int p, int n, const int *colors)
aswild 0:9f237b11f0a8 54 {
aswild 0:9f237b11f0a8 55 int r, g, b;
aswild 0:9f237b11f0a8 56 for (int i = 0; i < n; i++)
aswild 0:9f237b11f0a8 57 {
aswild 0:9f237b11f0a8 58 r = (colors[i] & 0xFF0000) >> 16;
aswild 0:9f237b11f0a8 59 g = (colors[i] & 0x00FF00) >>8;
aswild 0:9f237b11f0a8 60 b = colors[i] & 0x0000FF;
aswild 0:9f237b11f0a8 61 setPixel(p+i, r, g, b);
aswild 0:9f237b11f0a8 62 }
aswild 0:9f237b11f0a8 63 }
aswild 0:9f237b11f0a8 64
otis22894 3:00f3f32e6a1e 65 void NeoStrip::training(){
otis22894 3:00f3f32e6a1e 66 for (int i = 0; i < 4; i++)
otis22894 3:00f3f32e6a1e 67 {
otis22894 3:00f3f32e6a1e 68 setPixel(i,(uint8_t)255,(uint8_t)165,(uint8_t)0);
otis22894 3:00f3f32e6a1e 69 }
otis22894 3:00f3f32e6a1e 70 write();
otis22894 3:00f3f32e6a1e 71 }
otis22894 3:00f3f32e6a1e 72
otis22894 3:00f3f32e6a1e 73 void NeoStrip::training_mode(int mode){
otis22894 3:00f3f32e6a1e 74 for (int i = 0; i < 4; i++)
otis22894 3:00f3f32e6a1e 75 {
otis22894 3:00f3f32e6a1e 76 if(i==mode){
otis22894 3:00f3f32e6a1e 77 setPixel(i,(uint8_t)0,(uint8_t)255,(uint8_t)0);
otis22894 3:00f3f32e6a1e 78 }else{
otis22894 3:00f3f32e6a1e 79 setPixel(i,(uint8_t)0,(uint8_t)0,(uint8_t)0);
otis22894 3:00f3f32e6a1e 80 }
otis22894 3:00f3f32e6a1e 81 }
otis22894 3:00f3f32e6a1e 82 write();
otis22894 3:00f3f32e6a1e 83 }
otis22894 3:00f3f32e6a1e 84
aswild 0:9f237b11f0a8 85 void NeoStrip::clear()
aswild 0:9f237b11f0a8 86 {
aswild 0:9f237b11f0a8 87 for (int i = 0; i < N; i++)
aswild 0:9f237b11f0a8 88 {
aswild 0:9f237b11f0a8 89 strip[i].red = 0;
aswild 0:9f237b11f0a8 90 strip[i].green = 0;
aswild 0:9f237b11f0a8 91 strip[i].blue = 0;
aswild 0:9f237b11f0a8 92 }
otis22894 3:00f3f32e6a1e 93 write();
aswild 0:9f237b11f0a8 94 }
aswild 0:9f237b11f0a8 95
aswild 0:9f237b11f0a8 96 void NeoStrip::write()
aswild 0:9f237b11f0a8 97 {
aswild 0:9f237b11f0a8 98 __disable_irq(); // disable interrupts
aswild 0:9f237b11f0a8 99 neo_out(strip, Nbytes); // output to the strip
aswild 0:9f237b11f0a8 100 __enable_irq(); // enable interrupts
aswild 0:9f237b11f0a8 101 wait_us(50); // wait 50us for the reset pulse
aswild 0:9f237b11f0a8 102 }
aswild 0:9f237b11f0a8 103
jamesmsong 2:d607600a697d 104 void NeoStrip::initialize()
jamesmsong 2:d607600a697d 105 {
otis22894 4:623b0c643dd6 106 const int red[] = {0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000};
otis22894 4:623b0c643dd6 107 setPixels(0, 4, red);
otis22894 4:623b0c643dd6 108 write();
jamesmsong 2:d607600a697d 109 }
aswild 0:9f237b11f0a8 110
jamesmsong 2:d607600a697d 111 void NeoStrip::progress(float time)
jamesmsong 2:d607600a697d 112 {
jamesmsong 2:d607600a697d 113 for (int i = 0; i < 4; i++)
jamesmsong 2:d607600a697d 114 {
otis22894 3:00f3f32e6a1e 115 if(time >= (i+1)*0.25){
jamesmsong 2:d607600a697d 116 setPixel(i, 0, 255, 0);
otis22894 3:00f3f32e6a1e 117 }else{
otis22894 3:00f3f32e6a1e 118 setPixel(i, 255, 0, 0);
jamesmsong 2:d607600a697d 119 }
jamesmsong 2:d607600a697d 120 }
otis22894 3:00f3f32e6a1e 121 write();
jamesmsong 2:d607600a697d 122 }
otis22894 4:623b0c643dd6 123
otis22894 4:623b0c643dd6 124 void NeoStrip::start_up(){
otis22894 4:623b0c643dd6 125 for (int i = 0; i < 4; i++)
otis22894 4:623b0c643dd6 126 {
otis22894 4:623b0c643dd6 127 strip[i].red = 0;
otis22894 4:623b0c643dd6 128 strip[i].green = 0;
otis22894 4:623b0c643dd6 129 strip[i].blue = 255;
otis22894 4:623b0c643dd6 130 write();
otis22894 4:623b0c643dd6 131 wait(0.1);
otis22894 4:623b0c643dd6 132 clear();
otis22894 4:623b0c643dd6 133 }
otis22894 4:623b0c643dd6 134 for (int i = 0; i < 4; i++)
otis22894 4:623b0c643dd6 135 {
otis22894 4:623b0c643dd6 136 strip[i].red = 0;
otis22894 4:623b0c643dd6 137 strip[i].green = 0;
otis22894 4:623b0c643dd6 138 strip[i].blue = 255;
otis22894 4:623b0c643dd6 139 write();
otis22894 4:623b0c643dd6 140 wait(0.15);
otis22894 4:623b0c643dd6 141 clear();
otis22894 4:623b0c643dd6 142 }
otis22894 4:623b0c643dd6 143 }
otis22894 4:623b0c643dd6 144
otis22894 4:623b0c643dd6 145 void NeoStrip::bad_breath() {
otis22894 4:623b0c643dd6 146 const int red[] = {0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000};
otis22894 4:623b0c643dd6 147 setPixels(0, 4, red);
otis22894 4:623b0c643dd6 148 write();
otis22894 4:623b0c643dd6 149 wait(0.2);
otis22894 4:623b0c643dd6 150 clear();
otis22894 4:623b0c643dd6 151 wait(0.2);
otis22894 4:623b0c643dd6 152 setPixels(0, 4, red);
otis22894 4:623b0c643dd6 153 write();
otis22894 4:623b0c643dd6 154 wait(0.2);
otis22894 4:623b0c643dd6 155 clear();
otis22894 4:623b0c643dd6 156 wait(0.2);
otis22894 4:623b0c643dd6 157 setPixels(0, 4, red);
otis22894 4:623b0c643dd6 158 write();
otis22894 4:623b0c643dd6 159 wait(0.2);
otis22894 4:623b0c643dd6 160 clear();
otis22894 4:623b0c643dd6 161 }