A library for SPI control of adafruit's neopixel ring and addressable LEDs.

Dependencies:   PixelArray

Dependents:   TI_NEOPIXEL_SPI_SAMPLE

Committer:
tichise
Date:
Thu Jan 02 08:14:13 2020 +0000
Revision:
3:f0859c280204
Parent:
2:0148ac5c90fa
Child:
4:70bc3528e07e
BurstSPI in PixelArray could not be compiled with PlatformIO, so the library was changed to NeoPixel.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tichise 0:c28aa7d4f97e 1 #include "TI_NEOPIXEL_SPI.h"
tichise 0:c28aa7d4f97e 2 #include "mbed.h"
tichise 0:c28aa7d4f97e 3
tichise 3:f0859c280204 4 TI_NEOPIXEL_SPI::TI_NEOPIXEL_SPI(PinName input) : _neoPixel(input)
tichise 0:c28aa7d4f97e 5 {
tichise 0:c28aa7d4f97e 6 }
tichise 0:c28aa7d4f97e 7
tichise 0:c28aa7d4f97e 8 void TI_NEOPIXEL_SPI::switchLightOff(int count)
tichise 0:c28aa7d4f97e 9 {
tichise 0:c28aa7d4f97e 10
tichise 3:f0859c280204 11 Pixel pixels[count];
tichise 0:c28aa7d4f97e 12
tichise 0:c28aa7d4f97e 13 for (int i = 0; i < count; i++)
tichise 0:c28aa7d4f97e 14 {
tichise 3:f0859c280204 15 pixels[i].r = 0;
tichise 3:f0859c280204 16 pixels[i].g = 0;
tichise 3:f0859c280204 17 pixels[i].b = 0;
tichise 0:c28aa7d4f97e 18 }
tichise 0:c28aa7d4f97e 19
tichise 3:f0859c280204 20 _neoPixel.send(pixels, count);
tichise 0:c28aa7d4f97e 21 }
tichise 0:c28aa7d4f97e 22
tichise 3:f0859c280204 23 void TI_NEOPIXEL_SPI::switchLightOn(int count, int startCount, int endCount, rgbColor rgbColor)
tichise 0:c28aa7d4f97e 24 {
tichise 3:f0859c280204 25 Pixel pixels[count];
tichise 0:c28aa7d4f97e 26
tichise 0:c28aa7d4f97e 27 for (int i = 0; i < count; i++)
tichise 0:c28aa7d4f97e 28 {
tichise 3:f0859c280204 29 if (startCount <= i && i < endCount)
tichise 3:f0859c280204 30 {
tichise 3:f0859c280204 31 pixels[i].r = rgbColor.red;
tichise 3:f0859c280204 32 pixels[i].g = rgbColor.green;
tichise 3:f0859c280204 33 pixels[i].b = rgbColor.blue;
tichise 3:f0859c280204 34 }
tichise 3:f0859c280204 35 else
tichise 3:f0859c280204 36 {
tichise 3:f0859c280204 37 pixels[i].r = 0;
tichise 3:f0859c280204 38 pixels[i].g = 0;
tichise 3:f0859c280204 39 pixels[i].b = 0;
tichise 2:0148ac5c90fa 40 }
tichise 0:c28aa7d4f97e 41 }
tichise 0:c28aa7d4f97e 42
tichise 3:f0859c280204 43 _neoPixel.send(pixels, count);
tichise 0:c28aa7d4f97e 44 }
tichise 0:c28aa7d4f97e 45
tichise 2:0148ac5c90fa 46 void TI_NEOPIXEL_SPI::changeColor(int count, int startCount, int endCount, rgbColor rgbColor)
tichise 0:c28aa7d4f97e 47 {
tichise 3:f0859c280204 48 Pixel pixels[count];
tichise 0:c28aa7d4f97e 49
tichise 0:c28aa7d4f97e 50 for (int i = 0; i < count; i++)
tichise 0:c28aa7d4f97e 51 {
tichise 3:f0859c280204 52 if (startCount <= i && i < endCount)
tichise 3:f0859c280204 53 {
tichise 3:f0859c280204 54 pixels[i].r = rgbColor.red;
tichise 3:f0859c280204 55 pixels[i].g = rgbColor.green;
tichise 3:f0859c280204 56 pixels[i].b = rgbColor.blue;
tichise 3:f0859c280204 57 }
tichise 3:f0859c280204 58 else
tichise 3:f0859c280204 59 {
tichise 3:f0859c280204 60 pixels[i].r = 0;
tichise 3:f0859c280204 61 pixels[i].g = 0;
tichise 3:f0859c280204 62 pixels[i].b = 0;
tichise 2:0148ac5c90fa 63 }
tichise 0:c28aa7d4f97e 64 }
tichise 0:c28aa7d4f97e 65
tichise 3:f0859c280204 66 _neoPixel.send(pixels, count);
tichise 0:c28aa7d4f97e 67 }
tichise 0:c28aa7d4f97e 68
tichise 2:0148ac5c90fa 69 void TI_NEOPIXEL_SPI::changePointColor(int count, int topIndex, int endIndex, rgbColor topColor, rgbColor bottomColor)
tichise 0:c28aa7d4f97e 70 {
tichise 3:f0859c280204 71 Pixel pixels[count];
tichise 0:c28aa7d4f97e 72
tichise 0:c28aa7d4f97e 73 for (int i = 0; i < count; i++)
tichise 0:c28aa7d4f97e 74 {
tichise 2:0148ac5c90fa 75 if (i == topIndex)
tichise 0:c28aa7d4f97e 76 {
tichise 3:f0859c280204 77 pixels[i].r = topColor.red;
tichise 3:f0859c280204 78 pixels[i].g = topColor.green;
tichise 3:f0859c280204 79 pixels[i].b = topColor.blue;
tichise 0:c28aa7d4f97e 80 }
tichise 2:0148ac5c90fa 81 else if (i == endIndex)
tichise 0:c28aa7d4f97e 82 {
tichise 3:f0859c280204 83 pixels[i].r = bottomColor.red;
tichise 3:f0859c280204 84 pixels[i].g = bottomColor.green;
tichise 3:f0859c280204 85 pixels[i].b = bottomColor.blue;
tichise 0:c28aa7d4f97e 86 }
tichise 0:c28aa7d4f97e 87 else
tichise 0:c28aa7d4f97e 88 {
tichise 3:f0859c280204 89 pixels[i].r = 0;
tichise 3:f0859c280204 90 pixels[i].g = 0;
tichise 3:f0859c280204 91 pixels[i].b = 0;
tichise 0:c28aa7d4f97e 92 }
tichise 0:c28aa7d4f97e 93 }
tichise 0:c28aa7d4f97e 94
tichise 3:f0859c280204 95 _neoPixel.send(pixels, count);
tichise 0:c28aa7d4f97e 96 }
tichise 0:c28aa7d4f97e 97
tichise 2:0148ac5c90fa 98 void TI_NEOPIXEL_SPI::circle(int count, int startCount, int endCount, rgbColor rgbColor)
tichise 0:c28aa7d4f97e 99 {
tichise 0:c28aa7d4f97e 100 for (int j = 0; j < count; j++)
tichise 0:c28aa7d4f97e 101 {
tichise 3:f0859c280204 102 Pixel pixels[count];
tichise 0:c28aa7d4f97e 103
tichise 0:c28aa7d4f97e 104 for (int i = 0; i < count; i++)
tichise 0:c28aa7d4f97e 105 {
tichise 2:0148ac5c90fa 106 if (i <= j)
tichise 0:c28aa7d4f97e 107 {
tichise 3:f0859c280204 108 if (startCount <= i && i < endCount)
tichise 3:f0859c280204 109 {
tichise 3:f0859c280204 110 pixels[i].r = rgbColor.red;
tichise 3:f0859c280204 111 pixels[i].g = rgbColor.green;
tichise 3:f0859c280204 112 pixels[i].b = rgbColor.blue;
tichise 3:f0859c280204 113 }
tichise 3:f0859c280204 114 else
tichise 3:f0859c280204 115 {
tichise 3:f0859c280204 116 pixels[i].r = 0;
tichise 3:f0859c280204 117 pixels[i].g = 0;
tichise 3:f0859c280204 118 pixels[i].b = 0;
tichise 2:0148ac5c90fa 119 }
tichise 0:c28aa7d4f97e 120 }
tichise 0:c28aa7d4f97e 121 else
tichise 0:c28aa7d4f97e 122 {
tichise 3:f0859c280204 123 pixels[i].r = 0;
tichise 3:f0859c280204 124 pixels[i].g = 0;
tichise 3:f0859c280204 125 pixels[i].b = 0;
tichise 0:c28aa7d4f97e 126 }
tichise 0:c28aa7d4f97e 127 }
tichise 0:c28aa7d4f97e 128
tichise 3:f0859c280204 129 _neoPixel.send(pixels, count);
tichise 2:0148ac5c90fa 130
tichise 3:f0859c280204 131 if (startCount <= j && j < endCount)
tichise 3:f0859c280204 132 {
tichise 2:0148ac5c90fa 133 wait(0.07);
tichise 2:0148ac5c90fa 134 }
tichise 0:c28aa7d4f97e 135 }
tichise 0:c28aa7d4f97e 136 }
tichise 0:c28aa7d4f97e 137
tichise 0:c28aa7d4f97e 138 void TI_NEOPIXEL_SPI::chase(int count, int bufferCount, rgbColor c1, rgbColor c2)
tichise 0:c28aa7d4f97e 139 {
tichise 0:c28aa7d4f97e 140 int virtualCount = count + bufferCount;
tichise 3:f0859c280204 141 Pixel pixels[virtualCount];
tichise 0:c28aa7d4f97e 142
tichise 0:c28aa7d4f97e 143 for (int j = 0; j < virtualCount; j++)
tichise 0:c28aa7d4f97e 144 {
tichise 3:f0859c280204 145 pixels[j].r = c2.red;
tichise 3:f0859c280204 146 pixels[j].g = c2.green;
tichise 3:f0859c280204 147 pixels[j].b = c2.blue;
tichise 0:c28aa7d4f97e 148 }
tichise 0:c28aa7d4f97e 149
tichise 3:f0859c280204 150 _neoPixel.send(pixels, virtualCount);
tichise 0:c28aa7d4f97e 151
tichise 0:c28aa7d4f97e 152 for (int j = 0; j < virtualCount + bufferCount; j++)
tichise 0:c28aa7d4f97e 153 {
tichise 3:f0859c280204 154 pixels[j].r = c1.red;
tichise 3:f0859c280204 155 pixels[j].g = c1.green;
tichise 3:f0859c280204 156 pixels[j].b = c1.blue;
tichise 0:c28aa7d4f97e 157
tichise 3:f0859c280204 158 pixels[j - bufferCount].r = c2.red;
tichise 3:f0859c280204 159 pixels[j - bufferCount].g = c2.green;
tichise 3:f0859c280204 160 pixels[j - bufferCount].b = c2.blue;
tichise 0:c28aa7d4f97e 161
tichise 3:f0859c280204 162 _neoPixel.send(pixels, virtualCount);
tichise 0:c28aa7d4f97e 163 wait(0.05);
tichise 0:c28aa7d4f97e 164 }
tichise 0:c28aa7d4f97e 165 }
tichise 0:c28aa7d4f97e 166
tichise 2:0148ac5c90fa 167 void TI_NEOPIXEL_SPI::chaseRainbow(int count)
tichise 2:0148ac5c90fa 168 {
tichise 2:0148ac5c90fa 169 for (int j = 0; j < count; j++)
tichise 2:0148ac5c90fa 170 {
tichise 3:f0859c280204 171 Pixel pixels[count];
tichise 2:0148ac5c90fa 172
tichise 2:0148ac5c90fa 173 for (int i = 0; i < count; i++)
tichise 2:0148ac5c90fa 174 {
tichise 2:0148ac5c90fa 175 if (i <= j)
tichise 2:0148ac5c90fa 176 {
tichise 2:0148ac5c90fa 177 uint8_t phase = 256 / count * i;
tichise 2:0148ac5c90fa 178 rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0);
tichise 3:f0859c280204 179 pixels[i].r = rgbColor.red;
tichise 3:f0859c280204 180 pixels[i].g = rgbColor.green;
tichise 3:f0859c280204 181 pixels[i].b = rgbColor.blue;
tichise 2:0148ac5c90fa 182
tichise 2:0148ac5c90fa 183 // LEDを節約
tichise 3:f0859c280204 184 pixels[i - 4].r = 0;
tichise 3:f0859c280204 185 pixels[i - 4].g = 0;
tichise 3:f0859c280204 186 pixels[i - 4].b = 0;
tichise 2:0148ac5c90fa 187 }
tichise 2:0148ac5c90fa 188 else
tichise 2:0148ac5c90fa 189 {
tichise 3:f0859c280204 190 pixels[i].r = 0;
tichise 3:f0859c280204 191 pixels[i].g = 0;
tichise 3:f0859c280204 192 pixels[i].b = 0;
tichise 2:0148ac5c90fa 193 }
tichise 2:0148ac5c90fa 194 }
tichise 2:0148ac5c90fa 195
tichise 3:f0859c280204 196 _neoPixel.send(pixels, count);
tichise 2:0148ac5c90fa 197 wait(0.06);
tichise 2:0148ac5c90fa 198 }
tichise 2:0148ac5c90fa 199 }
tichise 2:0148ac5c90fa 200
tichise 0:c28aa7d4f97e 201 void TI_NEOPIXEL_SPI::circleRainbow(int count)
tichise 0:c28aa7d4f97e 202 {
tichise 0:c28aa7d4f97e 203 for (int j = 0; j < count; j++)
tichise 0:c28aa7d4f97e 204 {
tichise 3:f0859c280204 205 Pixel pixels[count];
tichise 0:c28aa7d4f97e 206
tichise 0:c28aa7d4f97e 207 for (int i = 0; i < count; i++)
tichise 0:c28aa7d4f97e 208 {
tichise 0:c28aa7d4f97e 209 if (i <= j)
tichise 0:c28aa7d4f97e 210 {
tichise 0:c28aa7d4f97e 211 uint8_t phase = 256 / count * i;
tichise 0:c28aa7d4f97e 212 rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0);
tichise 3:f0859c280204 213 pixels[i].r = rgbColor.red;
tichise 3:f0859c280204 214 pixels[i].g = rgbColor.green;
tichise 3:f0859c280204 215 pixels[i].b = rgbColor.blue;
tichise 0:c28aa7d4f97e 216 }
tichise 0:c28aa7d4f97e 217 else
tichise 0:c28aa7d4f97e 218 {
tichise 3:f0859c280204 219 pixels[i].r = 0;
tichise 3:f0859c280204 220 pixels[i].g = 0;
tichise 3:f0859c280204 221 pixels[i].b = 0;
tichise 0:c28aa7d4f97e 222 }
tichise 0:c28aa7d4f97e 223 }
tichise 0:c28aa7d4f97e 224
tichise 3:f0859c280204 225 _neoPixel.send(pixels, count);
tichise 0:c28aa7d4f97e 226 wait(0.06);
tichise 0:c28aa7d4f97e 227 }
tichise 0:c28aa7d4f97e 228 }
tichise 0:c28aa7d4f97e 229
tichise 0:c28aa7d4f97e 230 rgbColor TI_NEOPIXEL_SPI::convertHsvToRgb(float h, float s, float v)
tichise 0:c28aa7d4f97e 231 {
tichise 0:c28aa7d4f97e 232 int i = floor(h * 6);
tichise 0:c28aa7d4f97e 233 float f = h * 6 - i;
tichise 0:c28aa7d4f97e 234 float p = v * (1 - s);
tichise 0:c28aa7d4f97e 235 float q = v * (1 - f * s);
tichise 0:c28aa7d4f97e 236 float t = v * (1 - (1 - f) * s);
tichise 0:c28aa7d4f97e 237 float r = 0, g = 0, b = 0;
tichise 0:c28aa7d4f97e 238
tichise 0:c28aa7d4f97e 239 switch (i % 6)
tichise 0:c28aa7d4f97e 240 {
tichise 0:c28aa7d4f97e 241 case 0:
tichise 0:c28aa7d4f97e 242 r = v;
tichise 0:c28aa7d4f97e 243 g = t;
tichise 0:c28aa7d4f97e 244 b = p;
tichise 0:c28aa7d4f97e 245 break;
tichise 0:c28aa7d4f97e 246 case 1:
tichise 0:c28aa7d4f97e 247 r = q;
tichise 0:c28aa7d4f97e 248 g = v;
tichise 0:c28aa7d4f97e 249 b = p;
tichise 0:c28aa7d4f97e 250 break;
tichise 0:c28aa7d4f97e 251 case 2:
tichise 0:c28aa7d4f97e 252 r = p;
tichise 0:c28aa7d4f97e 253 g = v;
tichise 0:c28aa7d4f97e 254 b = t;
tichise 0:c28aa7d4f97e 255 break;
tichise 0:c28aa7d4f97e 256 case 3:
tichise 0:c28aa7d4f97e 257 r = p;
tichise 0:c28aa7d4f97e 258 g = q;
tichise 0:c28aa7d4f97e 259 b = v;
tichise 0:c28aa7d4f97e 260 break;
tichise 0:c28aa7d4f97e 261 case 4:
tichise 0:c28aa7d4f97e 262 r = t;
tichise 0:c28aa7d4f97e 263 g = p;
tichise 0:c28aa7d4f97e 264 b = v;
tichise 0:c28aa7d4f97e 265 break;
tichise 0:c28aa7d4f97e 266 case 5:
tichise 0:c28aa7d4f97e 267 r = v;
tichise 0:c28aa7d4f97e 268 g = p;
tichise 0:c28aa7d4f97e 269 b = q;
tichise 0:c28aa7d4f97e 270 break;
tichise 0:c28aa7d4f97e 271 }
tichise 0:c28aa7d4f97e 272
tichise 0:c28aa7d4f97e 273 return (rgbColor){r * 255, g * 255, b * 255};
tichise 0:c28aa7d4f97e 274 }