neostrip code
Fork of NeoStrip by
NeoStrip.cpp@3:00f3f32e6a1e, 2016-12-04 (annotated)
- Committer:
- otis22894
- Date:
- Sun Dec 04 17:34:41 2016 +0000
- Revision:
- 3:00f3f32e6a1e
- Parent:
- 2:d607600a697d
- Child:
- 4:623b0c643dd6
Initial
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
jamesmsong | 2:d607600a697d | 106 | for (int i = 0; i < 4; i++) |
jamesmsong | 2:d607600a697d | 107 | { |
otis22894 | 3:00f3f32e6a1e | 108 | setPixel(i, (uint8_t)255, (uint8_t)0, (uint8_t)0); |
jamesmsong | 2:d607600a697d | 109 | } |
otis22894 | 3:00f3f32e6a1e | 110 | write(); |
jamesmsong | 2:d607600a697d | 111 | } |
aswild | 0:9f237b11f0a8 | 112 | |
jamesmsong | 2:d607600a697d | 113 | void NeoStrip::progress(float time) |
jamesmsong | 2:d607600a697d | 114 | { |
jamesmsong | 2:d607600a697d | 115 | for (int i = 0; i < 4; i++) |
jamesmsong | 2:d607600a697d | 116 | { |
otis22894 | 3:00f3f32e6a1e | 117 | if(time >= (i+1)*0.25){ |
jamesmsong | 2:d607600a697d | 118 | setPixel(i, 0, 255, 0); |
otis22894 | 3:00f3f32e6a1e | 119 | }else{ |
otis22894 | 3:00f3f32e6a1e | 120 | setPixel(i, 255, 0, 0); |
jamesmsong | 2:d607600a697d | 121 | } |
jamesmsong | 2:d607600a697d | 122 | } |
otis22894 | 3:00f3f32e6a1e | 123 | write(); |
jamesmsong | 2:d607600a697d | 124 | } |