jason berry
/
NeoPixels
neopixels
main.cpp@1:a9f32c0c835d, 2021-02-04 (annotated)
- Committer:
- jasonberry
- Date:
- Thu Feb 04 14:03:17 2021 +0000
- Revision:
- 1:a9f32c0c835d
- Parent:
- 0:f38492690f0e
neopixels
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:f38492690f0e | 1 | /* |
jasonberry | 1:a9f32c0c835d | 2 | * Adafruit NeoPixel example |
aswild | 0:f38492690f0e | 3 | * |
aswild | 0:f38492690f0e | 4 | * This program displays a couple simple patterns on an 8x8 NeoPixel matrix. |
aswild | 0:f38492690f0e | 5 | * |
aswild | 0:f38492690f0e | 6 | */ |
aswild | 0:f38492690f0e | 7 | |
aswild | 0:f38492690f0e | 8 | #include "mbed.h" |
aswild | 0:f38492690f0e | 9 | #include "NeoStrip.h" |
aswild | 0:f38492690f0e | 10 | #include "gt.h" |
aswild | 0:f38492690f0e | 11 | |
jasonberry | 1:a9f32c0c835d | 12 | #define N 5 //number of leds |
jasonberry | 1:a9f32c0c835d | 13 | |
jasonberry | 1:a9f32c0c835d | 14 | |
aswild | 0:f38492690f0e | 15 | #define PATTERNS 3 |
aswild | 0:f38492690f0e | 16 | |
jasonberry | 1:a9f32c0c835d | 17 | //int hueToRGB(float h); |
jasonberry | 1:a9f32c0c835d | 18 | //void pattern0(); |
jasonberry | 1:a9f32c0c835d | 19 | //oid pattern1(); |
jasonberry | 1:a9f32c0c835d | 20 | //void pattern2(); |
aswild | 0:f38492690f0e | 21 | |
aswild | 0:f38492690f0e | 22 | // array of function pointers to the various patterns |
jasonberry | 1:a9f32c0c835d | 23 | //void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; |
aswild | 0:f38492690f0e | 24 | |
jasonberry | 1:a9f32c0c835d | 25 | NeoStrip strip(p25, N); |
jasonberry | 1:a9f32c0c835d | 26 | //DigitalIn b1(p20); // brightness up |
jasonberry | 1:a9f32c0c835d | 27 | //DigitalIn b2(p19); // brightness down |
jasonberry | 1:a9f32c0c835d | 28 | //DigitalIn b3(p22); // next pattern |
aswild | 0:f38492690f0e | 29 | |
aswild | 0:f38492690f0e | 30 | // timer used for debugging |
aswild | 0:f38492690f0e | 31 | Timer timer; |
aswild | 0:f38492690f0e | 32 | |
aswild | 0:f38492690f0e | 33 | int main() |
aswild | 0:f38492690f0e | 34 | { |
jasonberry | 1:a9f32c0c835d | 35 | // b1.mode(PullDown); |
jasonberry | 1:a9f32c0c835d | 36 | // b2.mode(PullDown); |
jasonberry | 1:a9f32c0c835d | 37 | // b3.mode(PullDown); |
aswild | 0:f38492690f0e | 38 | |
jasonberry | 1:a9f32c0c835d | 39 | //int pattern = 0; |
aswild | 0:f38492690f0e | 40 | |
jasonberry | 1:a9f32c0c835d | 41 | //bool b3o = b3; // old copy of button 3 to poll for changes |
jasonberry | 1:a9f32c0c835d | 42 | //int p = 0; |
jasonberry | 1:a9f32c0c835d | 43 | |
jasonberry | 1:a9f32c0c835d | 44 | |
jasonberry | 1:a9f32c0c835d | 45 | float bright = 0.2; // 20% is plenty for indoor use |
jasonberry | 1:a9f32c0c835d | 46 | int color = 0x122446; //0xFF0000; //red |
jasonberry | 1:a9f32c0c835d | 47 | |
aswild | 0:f38492690f0e | 48 | strip.setBrightness(bright); // set default brightness |
aswild | 0:f38492690f0e | 49 | |
jasonberry | 1:a9f32c0c835d | 50 | while(true) |
jasonberry | 1:a9f32c0c835d | 51 | { |
jasonberry | 1:a9f32c0c835d | 52 | //strip.setPixels(0, N, test_img); |
jasonberry | 1:a9f32c0c835d | 53 | strip.clear(); |
jasonberry | 1:a9f32c0c835d | 54 | strip.write(); |
jasonberry | 1:a9f32c0c835d | 55 | wait_ms(1000); |
jasonberry | 1:a9f32c0c835d | 56 | strip.setPixel(0,color); |
jasonberry | 1:a9f32c0c835d | 57 | strip.write(); |
jasonberry | 1:a9f32c0c835d | 58 | wait_ms(1000); |
jasonberry | 1:a9f32c0c835d | 59 | |
jasonberry | 1:a9f32c0c835d | 60 | strip.setPixel(1,color); |
jasonberry | 1:a9f32c0c835d | 61 | strip.write(); |
jasonberry | 1:a9f32c0c835d | 62 | wait_ms(1000); |
jasonberry | 1:a9f32c0c835d | 63 | |
jasonberry | 1:a9f32c0c835d | 64 | strip.setPixel(2,color); |
jasonberry | 1:a9f32c0c835d | 65 | strip.write(); |
jasonberry | 1:a9f32c0c835d | 66 | wait_ms(1000); |
jasonberry | 1:a9f32c0c835d | 67 | |
jasonberry | 1:a9f32c0c835d | 68 | strip.setPixel(3,color); |
jasonberry | 1:a9f32c0c835d | 69 | strip.write(); |
jasonberry | 1:a9f32c0c835d | 70 | wait_ms(1000); |
jasonberry | 1:a9f32c0c835d | 71 | |
jasonberry | 1:a9f32c0c835d | 72 | strip.setPixel(4,color); |
jasonberry | 1:a9f32c0c835d | 73 | strip.write(); |
jasonberry | 1:a9f32c0c835d | 74 | wait_ms(1000); |
jasonberry | 1:a9f32c0c835d | 75 | |
jasonberry | 1:a9f32c0c835d | 76 | } |
jasonberry | 1:a9f32c0c835d | 77 | } |
jasonberry | 1:a9f32c0c835d | 78 | |
jasonberry | 1:a9f32c0c835d | 79 | |
jasonberry | 1:a9f32c0c835d | 80 | |
jasonberry | 1:a9f32c0c835d | 81 | |
jasonberry | 1:a9f32c0c835d | 82 | |
jasonberry | 1:a9f32c0c835d | 83 | |
jasonberry | 1:a9f32c0c835d | 84 | |
jasonberry | 1:a9f32c0c835d | 85 | |
jasonberry | 1:a9f32c0c835d | 86 | |
jasonberry | 1:a9f32c0c835d | 87 | |
jasonberry | 1:a9f32c0c835d | 88 | |
jasonberry | 1:a9f32c0c835d | 89 | |
jasonberry | 1:a9f32c0c835d | 90 | |
jasonberry | 1:a9f32c0c835d | 91 | |
jasonberry | 1:a9f32c0c835d | 92 | |
jasonberry | 1:a9f32c0c835d | 93 | |
jasonberry | 1:a9f32c0c835d | 94 | |
jasonberry | 1:a9f32c0c835d | 95 | |
jasonberry | 1:a9f32c0c835d | 96 | |
jasonberry | 1:a9f32c0c835d | 97 | |
jasonberry | 1:a9f32c0c835d | 98 | |
jasonberry | 1:a9f32c0c835d | 99 | |
jasonberry | 1:a9f32c0c835d | 100 | |
jasonberry | 1:a9f32c0c835d | 101 | |
jasonberry | 1:a9f32c0c835d | 102 | |
jasonberry | 1:a9f32c0c835d | 103 | |
jasonberry | 1:a9f32c0c835d | 104 | /*while (true) |
aswild | 0:f38492690f0e | 105 | { |
aswild | 0:f38492690f0e | 106 | timer.reset(); // use a timer to measure loop execution time for debugging purposes |
aswild | 0:f38492690f0e | 107 | timer.start(); // for this application, the main loop takes approximately 3ms to run |
aswild | 0:f38492690f0e | 108 | |
aswild | 0:f38492690f0e | 109 | // button 1 increases brightness |
jasonberry | 1:a9f32c0c835d | 110 | if(0) //(b1 && bright < 1) |
aswild | 0:f38492690f0e | 111 | { |
aswild | 0:f38492690f0e | 112 | bright += 0.01; |
aswild | 0:f38492690f0e | 113 | if (bright > 1) |
aswild | 0:f38492690f0e | 114 | bright = 1; |
aswild | 0:f38492690f0e | 115 | strip.setBrightness(bright); |
aswild | 0:f38492690f0e | 116 | } |
aswild | 0:f38492690f0e | 117 | |
aswild | 0:f38492690f0e | 118 | // button 2 decreases brightness |
jasonberry | 1:a9f32c0c835d | 119 | if (0)//(b2 && bright > 0) |
aswild | 0:f38492690f0e | 120 | { |
aswild | 0:f38492690f0e | 121 | bright -= 0.01; |
aswild | 0:f38492690f0e | 122 | if (bright < 0) |
aswild | 0:f38492690f0e | 123 | bright = 0; |
aswild | 0:f38492690f0e | 124 | strip.setBrightness(bright); |
aswild | 0:f38492690f0e | 125 | } |
aswild | 0:f38492690f0e | 126 | |
aswild | 0:f38492690f0e | 127 | // button 3 changes the pattern, only do stuff when its state has changed |
jasonberry | 1:a9f32c0c835d | 128 | if (0)//(b3 != b3o) |
aswild | 0:f38492690f0e | 129 | { |
aswild | 0:f38492690f0e | 130 | if (b3 && ++pattern == PATTERNS) |
aswild | 0:f38492690f0e | 131 | pattern = 0; |
aswild | 0:f38492690f0e | 132 | b3o = b3; |
aswild | 0:f38492690f0e | 133 | } |
aswild | 0:f38492690f0e | 134 | |
aswild | 0:f38492690f0e | 135 | // run the pattern update function which sets the strip's pixels |
aswild | 0:f38492690f0e | 136 | patterns[pattern](); |
aswild | 0:f38492690f0e | 137 | strip.write(); |
aswild | 0:f38492690f0e | 138 | |
aswild | 0:f38492690f0e | 139 | timer.stop(); |
aswild | 0:f38492690f0e | 140 | // print loop time if b3 is pressed |
aswild | 0:f38492690f0e | 141 | if (b3) |
aswild | 0:f38492690f0e | 142 | printf("Loop Time: %dus\n", timer.read_us()); |
aswild | 0:f38492690f0e | 143 | |
aswild | 0:f38492690f0e | 144 | wait_ms(10); |
aswild | 0:f38492690f0e | 145 | } |
jasonberry | 1:a9f32c0c835d | 146 | }*/ |
aswild | 0:f38492690f0e | 147 | |
aswild | 0:f38492690f0e | 148 | // pattern0 displays a static image |
jasonberry | 1:a9f32c0c835d | 149 | /*void pattern0() |
aswild | 0:f38492690f0e | 150 | { |
aswild | 0:f38492690f0e | 151 | strip.setPixels(0, N, gt_img); |
aswild | 0:f38492690f0e | 152 | } |
aswild | 0:f38492690f0e | 153 | |
aswild | 0:f38492690f0e | 154 | // display a shifting rainbow, all colors have maximum |
aswild | 0:f38492690f0e | 155 | // saturation and value, with evenly spaced hue |
aswild | 0:f38492690f0e | 156 | void pattern1() |
aswild | 0:f38492690f0e | 157 | { |
aswild | 0:f38492690f0e | 158 | static float dh = 360.0 / N; |
aswild | 0:f38492690f0e | 159 | static float x = 0; |
aswild | 0:f38492690f0e | 160 | |
aswild | 0:f38492690f0e | 161 | for (int i = 0; i < N; i++) |
aswild | 0:f38492690f0e | 162 | strip.setPixel(i, hueToRGB((dh * i) - x)); |
aswild | 0:f38492690f0e | 163 | |
aswild | 0:f38492690f0e | 164 | x += 1; |
aswild | 0:f38492690f0e | 165 | if (x > 360) |
aswild | 0:f38492690f0e | 166 | x = 0; |
aswild | 0:f38492690f0e | 167 | } |
aswild | 0:f38492690f0e | 168 | |
aswild | 0:f38492690f0e | 169 | // display a shifting gradient between red and blue |
aswild | 0:f38492690f0e | 170 | void pattern2() |
aswild | 0:f38492690f0e | 171 | { |
aswild | 0:f38492690f0e | 172 | // offset for each pixel to allow the pattern to move |
aswild | 0:f38492690f0e | 173 | static float x = 0; |
aswild | 0:f38492690f0e | 174 | |
aswild | 0:f38492690f0e | 175 | float r, b, y; |
aswild | 0:f38492690f0e | 176 | |
aswild | 0:f38492690f0e | 177 | for (int i = 0; i < N; i++) |
aswild | 0:f38492690f0e | 178 | { |
aswild | 0:f38492690f0e | 179 | // y is a scaled position between 0 (red) and 1.0 (blue) |
aswild | 0:f38492690f0e | 180 | y = 1.0 * i / (N - 1) + x; |
aswild | 0:f38492690f0e | 181 | if (y > 1) |
aswild | 0:f38492690f0e | 182 | y -= 1; |
aswild | 0:f38492690f0e | 183 | |
aswild | 0:f38492690f0e | 184 | // if on the left half, red is decreasing and blue is increasng |
aswild | 0:f38492690f0e | 185 | if (y < 0.5) |
aswild | 0:f38492690f0e | 186 | { |
aswild | 0:f38492690f0e | 187 | b = 2 * y; |
aswild | 0:f38492690f0e | 188 | r = 1 - b; |
aswild | 0:f38492690f0e | 189 | } |
aswild | 0:f38492690f0e | 190 | |
aswild | 0:f38492690f0e | 191 | // else red is increasing and blue is decreasing |
aswild | 0:f38492690f0e | 192 | else |
aswild | 0:f38492690f0e | 193 | { |
aswild | 0:f38492690f0e | 194 | r = 2 * (y - 0.5); |
aswild | 0:f38492690f0e | 195 | b = 1 - r; |
aswild | 0:f38492690f0e | 196 | } |
aswild | 0:f38492690f0e | 197 | |
aswild | 0:f38492690f0e | 198 | // scale to integers and set the pixel |
aswild | 0:f38492690f0e | 199 | strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200)); |
aswild | 0:f38492690f0e | 200 | } |
aswild | 0:f38492690f0e | 201 | |
aswild | 0:f38492690f0e | 202 | x += 0.003; |
aswild | 0:f38492690f0e | 203 | if (x > 1) |
aswild | 0:f38492690f0e | 204 | x = 0; |
aswild | 0:f38492690f0e | 205 | } |
aswild | 0:f38492690f0e | 206 | |
aswild | 0:f38492690f0e | 207 | // Converts HSV to RGB with the given hue, assuming |
aswild | 0:f38492690f0e | 208 | // maximum saturation and value |
aswild | 0:f38492690f0e | 209 | int hueToRGB(float h) |
aswild | 0:f38492690f0e | 210 | { |
aswild | 0:f38492690f0e | 211 | // lots of floating point magic from the internet and scratching my head |
aswild | 0:f38492690f0e | 212 | float r, g, b; |
aswild | 0:f38492690f0e | 213 | if (h > 360) |
aswild | 0:f38492690f0e | 214 | h -= 360; |
aswild | 0:f38492690f0e | 215 | if (h < 0) |
aswild | 0:f38492690f0e | 216 | h += 360; |
aswild | 0:f38492690f0e | 217 | int i = (int)(h / 60.0); |
aswild | 0:f38492690f0e | 218 | float f = (h / 60.0) - i; |
aswild | 0:f38492690f0e | 219 | float q = 1 - f; |
aswild | 0:f38492690f0e | 220 | |
aswild | 0:f38492690f0e | 221 | switch (i % 6) |
aswild | 0:f38492690f0e | 222 | { |
aswild | 0:f38492690f0e | 223 | case 0: r = 1; g = f; b = 0; break; |
aswild | 0:f38492690f0e | 224 | case 1: r = q; g = 1; b = 0; break; |
aswild | 0:f38492690f0e | 225 | case 2: r = 0; g = 1; b = f; break; |
aswild | 0:f38492690f0e | 226 | case 3: r = 0; g = q; b = 1; break; |
aswild | 0:f38492690f0e | 227 | case 4: r = f; g = 0; b = 1; break; |
aswild | 0:f38492690f0e | 228 | case 5: r = 1; g = 0; b = q; break; |
aswild | 0:f38492690f0e | 229 | default: r = 0; g = 0; b = 0; break; |
aswild | 0:f38492690f0e | 230 | } |
aswild | 0:f38492690f0e | 231 | |
aswild | 0:f38492690f0e | 232 | // scale to integers and return the packed value |
aswild | 0:f38492690f0e | 233 | uint8_t R = (uint8_t)(r * 255); |
aswild | 0:f38492690f0e | 234 | uint8_t G = (uint8_t)(g * 255); |
aswild | 0:f38492690f0e | 235 | uint8_t B = (uint8_t)(b * 255); |
aswild | 0:f38492690f0e | 236 | |
aswild | 0:f38492690f0e | 237 | return (R << 16) | (G << 8) | B; |
aswild | 0:f38492690f0e | 238 | } |
jasonberry | 1:a9f32c0c835d | 239 | */ |