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