Skovbrynet
/
NeoPixels
NeoStrip
Fork of NeoPixels by
main.cpp@1:a0c08e317195, 2017-07-02 (annotated)
- Committer:
- gert_lauritsen
- Date:
- Sun Jul 02 08:47:49 2017 +0000
- Revision:
- 1:a0c08e317195
- Parent:
- 0:f38492690f0e
NeoStrip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:f38492690f0e | 1 | /* |
aswild | 0:f38492690f0e | 2 | * Adafruit NeoPixel 8x8 matrix 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 | * 3 buttons are used for DigitalIns, 2 control the brightness up and down, |
aswild | 0:f38492690f0e | 7 | * and the third switches patterns |
aswild | 0:f38492690f0e | 8 | */ |
aswild | 0:f38492690f0e | 9 | |
aswild | 0:f38492690f0e | 10 | #include "mbed.h" |
aswild | 0:f38492690f0e | 11 | #include "NeoStrip.h" |
aswild | 0:f38492690f0e | 12 | #include "gt.h" |
aswild | 0:f38492690f0e | 13 | |
aswild | 0:f38492690f0e | 14 | #define N 64 |
aswild | 0:f38492690f0e | 15 | #define PATTERNS 3 |
aswild | 0:f38492690f0e | 16 | |
gert_lauritsen | 1:a0c08e317195 | 17 | Serial pc(USBTX, USBRX); |
aswild | 0:f38492690f0e | 18 | int hueToRGB(float h); |
aswild | 0:f38492690f0e | 19 | void pattern0(); |
aswild | 0:f38492690f0e | 20 | void pattern1(); |
aswild | 0:f38492690f0e | 21 | void pattern2(); |
aswild | 0:f38492690f0e | 22 | |
aswild | 0:f38492690f0e | 23 | // array of function pointers to the various patterns |
aswild | 0:f38492690f0e | 24 | void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; |
aswild | 0:f38492690f0e | 25 | |
gert_lauritsen | 1:a0c08e317195 | 26 | NeoStrip strip(p21, N); |
gert_lauritsen | 1:a0c08e317195 | 27 | int selekt=0; |
gert_lauritsen | 1:a0c08e317195 | 28 | /*DigitalIn b1(p20); // brightness up |
aswild | 0:f38492690f0e | 29 | DigitalIn b2(p19); // brightness down |
gert_lauritsen | 1:a0c08e317195 | 30 | DigitalIn b3(p21); // next pattern*/ |
aswild | 0:f38492690f0e | 31 | |
aswild | 0:f38492690f0e | 32 | // timer used for debugging |
aswild | 0:f38492690f0e | 33 | Timer timer; |
aswild | 0:f38492690f0e | 34 | |
aswild | 0:f38492690f0e | 35 | int main() |
aswild | 0:f38492690f0e | 36 | { |
gert_lauritsen | 1:a0c08e317195 | 37 | pc.baud(115200); |
gert_lauritsen | 1:a0c08e317195 | 38 | printf("Test af LED\r\n"); |
aswild | 0:f38492690f0e | 39 | |
aswild | 0:f38492690f0e | 40 | int pattern = 0; |
aswild | 0:f38492690f0e | 41 | float bright = 0.2; // 20% is plenty for indoor use |
gert_lauritsen | 1:a0c08e317195 | 42 | int b3o = selekt; // old copy of button 3 to poll for changes |
aswild | 0:f38492690f0e | 43 | |
aswild | 0:f38492690f0e | 44 | strip.setBrightness(bright); // set default brightness |
aswild | 0:f38492690f0e | 45 | |
aswild | 0:f38492690f0e | 46 | while (true) |
aswild | 0:f38492690f0e | 47 | { |
aswild | 0:f38492690f0e | 48 | timer.reset(); // use a timer to measure loop execution time for debugging purposes |
aswild | 0:f38492690f0e | 49 | timer.start(); // for this application, the main loop takes approximately 3ms to run |
aswild | 0:f38492690f0e | 50 | |
aswild | 0:f38492690f0e | 51 | // button 1 increases brightness |
gert_lauritsen | 1:a0c08e317195 | 52 | if ((selekt==1) && bright < 1) |
aswild | 0:f38492690f0e | 53 | { |
aswild | 0:f38492690f0e | 54 | bright += 0.01; |
aswild | 0:f38492690f0e | 55 | if (bright > 1) |
aswild | 0:f38492690f0e | 56 | bright = 1; |
aswild | 0:f38492690f0e | 57 | strip.setBrightness(bright); |
aswild | 0:f38492690f0e | 58 | } |
aswild | 0:f38492690f0e | 59 | |
aswild | 0:f38492690f0e | 60 | // button 2 decreases brightness |
gert_lauritsen | 1:a0c08e317195 | 61 | if ((selekt==2) && bright > 0) |
aswild | 0:f38492690f0e | 62 | { |
aswild | 0:f38492690f0e | 63 | bright -= 0.01; |
aswild | 0:f38492690f0e | 64 | if (bright < 0) |
aswild | 0:f38492690f0e | 65 | bright = 0; |
aswild | 0:f38492690f0e | 66 | strip.setBrightness(bright); |
aswild | 0:f38492690f0e | 67 | } |
aswild | 0:f38492690f0e | 68 | |
aswild | 0:f38492690f0e | 69 | // button 3 changes the pattern, only do stuff when its state has changed |
gert_lauritsen | 1:a0c08e317195 | 70 | if ((selekt==3) != b3o) |
aswild | 0:f38492690f0e | 71 | { |
gert_lauritsen | 1:a0c08e317195 | 72 | if (++pattern == PATTERNS) pattern = 0; |
gert_lauritsen | 1:a0c08e317195 | 73 | |
aswild | 0:f38492690f0e | 74 | } |
gert_lauritsen | 1:a0c08e317195 | 75 | selekt=0; |
aswild | 0:f38492690f0e | 76 | // run the pattern update function which sets the strip's pixels |
aswild | 0:f38492690f0e | 77 | patterns[pattern](); |
aswild | 0:f38492690f0e | 78 | strip.write(); |
aswild | 0:f38492690f0e | 79 | |
aswild | 0:f38492690f0e | 80 | timer.stop(); |
aswild | 0:f38492690f0e | 81 | // print loop time if b3 is pressed |
gert_lauritsen | 1:a0c08e317195 | 82 | if (pc.readable()) { |
gert_lauritsen | 1:a0c08e317195 | 83 | switch(pc.getc()) { |
gert_lauritsen | 1:a0c08e317195 | 84 | case '1': selekt=1; break; |
gert_lauritsen | 1:a0c08e317195 | 85 | case '2': selekt=2; break; |
gert_lauritsen | 1:a0c08e317195 | 86 | case '3': selekt=3; break; |
gert_lauritsen | 1:a0c08e317195 | 87 | } |
gert_lauritsen | 1:a0c08e317195 | 88 | } |
aswild | 0:f38492690f0e | 89 | wait_ms(10); |
aswild | 0:f38492690f0e | 90 | } |
aswild | 0:f38492690f0e | 91 | } |
aswild | 0:f38492690f0e | 92 | |
aswild | 0:f38492690f0e | 93 | // pattern0 displays a static image |
aswild | 0:f38492690f0e | 94 | void pattern0() |
aswild | 0:f38492690f0e | 95 | { |
aswild | 0:f38492690f0e | 96 | strip.setPixels(0, N, gt_img); |
aswild | 0:f38492690f0e | 97 | } |
aswild | 0:f38492690f0e | 98 | |
aswild | 0:f38492690f0e | 99 | // display a shifting rainbow, all colors have maximum |
aswild | 0:f38492690f0e | 100 | // saturation and value, with evenly spaced hue |
aswild | 0:f38492690f0e | 101 | void pattern1() |
aswild | 0:f38492690f0e | 102 | { |
aswild | 0:f38492690f0e | 103 | static float dh = 360.0 / N; |
aswild | 0:f38492690f0e | 104 | static float x = 0; |
aswild | 0:f38492690f0e | 105 | |
aswild | 0:f38492690f0e | 106 | for (int i = 0; i < N; i++) |
aswild | 0:f38492690f0e | 107 | strip.setPixel(i, hueToRGB((dh * i) - x)); |
aswild | 0:f38492690f0e | 108 | |
aswild | 0:f38492690f0e | 109 | x += 1; |
aswild | 0:f38492690f0e | 110 | if (x > 360) |
aswild | 0:f38492690f0e | 111 | x = 0; |
aswild | 0:f38492690f0e | 112 | } |
aswild | 0:f38492690f0e | 113 | |
aswild | 0:f38492690f0e | 114 | // display a shifting gradient between red and blue |
aswild | 0:f38492690f0e | 115 | void pattern2() |
aswild | 0:f38492690f0e | 116 | { |
aswild | 0:f38492690f0e | 117 | // offset for each pixel to allow the pattern to move |
aswild | 0:f38492690f0e | 118 | static float x = 0; |
aswild | 0:f38492690f0e | 119 | |
aswild | 0:f38492690f0e | 120 | float r, b, y; |
aswild | 0:f38492690f0e | 121 | |
aswild | 0:f38492690f0e | 122 | for (int i = 0; i < N; i++) |
aswild | 0:f38492690f0e | 123 | { |
aswild | 0:f38492690f0e | 124 | // y is a scaled position between 0 (red) and 1.0 (blue) |
aswild | 0:f38492690f0e | 125 | y = 1.0 * i / (N - 1) + x; |
aswild | 0:f38492690f0e | 126 | if (y > 1) |
aswild | 0:f38492690f0e | 127 | y -= 1; |
aswild | 0:f38492690f0e | 128 | |
aswild | 0:f38492690f0e | 129 | // if on the left half, red is decreasing and blue is increasng |
aswild | 0:f38492690f0e | 130 | if (y < 0.5) |
aswild | 0:f38492690f0e | 131 | { |
aswild | 0:f38492690f0e | 132 | b = 2 * y; |
aswild | 0:f38492690f0e | 133 | r = 1 - b; |
aswild | 0:f38492690f0e | 134 | } |
aswild | 0:f38492690f0e | 135 | |
aswild | 0:f38492690f0e | 136 | // else red is increasing and blue is decreasing |
aswild | 0:f38492690f0e | 137 | else |
aswild | 0:f38492690f0e | 138 | { |
aswild | 0:f38492690f0e | 139 | r = 2 * (y - 0.5); |
aswild | 0:f38492690f0e | 140 | b = 1 - r; |
aswild | 0:f38492690f0e | 141 | } |
aswild | 0:f38492690f0e | 142 | |
aswild | 0:f38492690f0e | 143 | // scale to integers and set the pixel |
aswild | 0:f38492690f0e | 144 | strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200)); |
aswild | 0:f38492690f0e | 145 | } |
aswild | 0:f38492690f0e | 146 | |
aswild | 0:f38492690f0e | 147 | x += 0.003; |
aswild | 0:f38492690f0e | 148 | if (x > 1) |
aswild | 0:f38492690f0e | 149 | x = 0; |
aswild | 0:f38492690f0e | 150 | } |
aswild | 0:f38492690f0e | 151 | |
aswild | 0:f38492690f0e | 152 | // Converts HSV to RGB with the given hue, assuming |
aswild | 0:f38492690f0e | 153 | // maximum saturation and value |
aswild | 0:f38492690f0e | 154 | int hueToRGB(float h) |
aswild | 0:f38492690f0e | 155 | { |
aswild | 0:f38492690f0e | 156 | // lots of floating point magic from the internet and scratching my head |
aswild | 0:f38492690f0e | 157 | float r, g, b; |
aswild | 0:f38492690f0e | 158 | if (h > 360) |
aswild | 0:f38492690f0e | 159 | h -= 360; |
aswild | 0:f38492690f0e | 160 | if (h < 0) |
aswild | 0:f38492690f0e | 161 | h += 360; |
aswild | 0:f38492690f0e | 162 | int i = (int)(h / 60.0); |
aswild | 0:f38492690f0e | 163 | float f = (h / 60.0) - i; |
aswild | 0:f38492690f0e | 164 | float q = 1 - f; |
aswild | 0:f38492690f0e | 165 | |
aswild | 0:f38492690f0e | 166 | switch (i % 6) |
aswild | 0:f38492690f0e | 167 | { |
aswild | 0:f38492690f0e | 168 | case 0: r = 1; g = f; b = 0; break; |
aswild | 0:f38492690f0e | 169 | case 1: r = q; g = 1; b = 0; break; |
aswild | 0:f38492690f0e | 170 | case 2: r = 0; g = 1; b = f; break; |
aswild | 0:f38492690f0e | 171 | case 3: r = 0; g = q; b = 1; break; |
aswild | 0:f38492690f0e | 172 | case 4: r = f; g = 0; b = 1; break; |
aswild | 0:f38492690f0e | 173 | case 5: r = 1; g = 0; b = q; break; |
aswild | 0:f38492690f0e | 174 | default: r = 0; g = 0; b = 0; break; |
aswild | 0:f38492690f0e | 175 | } |
aswild | 0:f38492690f0e | 176 | |
aswild | 0:f38492690f0e | 177 | // scale to integers and return the packed value |
aswild | 0:f38492690f0e | 178 | uint8_t R = (uint8_t)(r * 255); |
aswild | 0:f38492690f0e | 179 | uint8_t G = (uint8_t)(g * 255); |
aswild | 0:f38492690f0e | 180 | uint8_t B = (uint8_t)(b * 255); |
aswild | 0:f38492690f0e | 181 | |
aswild | 0:f38492690f0e | 182 | return (R << 16) | (G << 8) | B; |
aswild | 0:f38492690f0e | 183 | } |
aswild | 0:f38492690f0e | 184 |