A library for SPI control of adafruit's neopixel ring and addressable LEDs.
Dependents: TI_NEOPIXEL_SPI_SAMPLE
TI_NEOPIXEL_SPI.cpp@5:69bb2a2fa11f, 2020-01-04 (annotated)
- Committer:
- tichise
- Date:
- Sat Jan 04 02:53:13 2020 +0000
- Revision:
- 5:69bb2a2fa11f
- Parent:
- 2:0148ac5c90fa
switched back to PixelArray
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 | 5:69bb2a2fa11f | 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 | 5:69bb2a2fa11f | 29 | if (startCount <= i && i < endCount) |
tichise | 5:69bb2a2fa11f | 30 | { |
tichise | 2:0148ac5c90fa | 31 | colors[i].red = rgbColor.red; |
tichise | 2:0148ac5c90fa | 32 | colors[i].green = rgbColor.green; |
tichise | 2:0148ac5c90fa | 33 | colors[i].blue = rgbColor.blue; |
tichise | 5:69bb2a2fa11f | 34 | } |
tichise | 5:69bb2a2fa11f | 35 | else |
tichise | 5:69bb2a2fa11f | 36 | { |
tichise | 2:0148ac5c90fa | 37 | colors[i].red = 0; |
tichise | 2:0148ac5c90fa | 38 | colors[i].green = 0; |
tichise | 2:0148ac5c90fa | 39 | colors[i].blue = 0; |
tichise | 2:0148ac5c90fa | 40 | } |
tichise | 0:c28aa7d4f97e | 41 | } |
tichise | 0:c28aa7d4f97e | 42 | |
tichise | 0:c28aa7d4f97e | 43 | _ledStrip.update(colors, 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 | 0:c28aa7d4f97e | 48 | neopixel::Pixel colors[count]; |
tichise | 0:c28aa7d4f97e | 49 | |
tichise | 0:c28aa7d4f97e | 50 | for (int i = 0; i < count; i++) |
tichise | 0:c28aa7d4f97e | 51 | { |
tichise | 5:69bb2a2fa11f | 52 | if (startCount <= i && i < endCount) |
tichise | 5:69bb2a2fa11f | 53 | { |
tichise | 2:0148ac5c90fa | 54 | colors[i].red = rgbColor.red; |
tichise | 2:0148ac5c90fa | 55 | colors[i].green = rgbColor.green; |
tichise | 2:0148ac5c90fa | 56 | colors[i].blue = rgbColor.blue; |
tichise | 5:69bb2a2fa11f | 57 | } |
tichise | 5:69bb2a2fa11f | 58 | else |
tichise | 5:69bb2a2fa11f | 59 | { |
tichise | 2:0148ac5c90fa | 60 | colors[i].red = 0; |
tichise | 2:0148ac5c90fa | 61 | colors[i].green = 0; |
tichise | 2:0148ac5c90fa | 62 | colors[i].blue = 0; |
tichise | 2:0148ac5c90fa | 63 | } |
tichise | 0:c28aa7d4f97e | 64 | } |
tichise | 0:c28aa7d4f97e | 65 | |
tichise | 0:c28aa7d4f97e | 66 | _ledStrip.update(colors, 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 | 0:c28aa7d4f97e | 71 | neopixel::Pixel colors[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 | 0:c28aa7d4f97e | 77 | colors[i].red = topColor.red; |
tichise | 0:c28aa7d4f97e | 78 | colors[i].green = topColor.green; |
tichise | 0:c28aa7d4f97e | 79 | colors[i].blue = topColor.blue; |
tichise | 0:c28aa7d4f97e | 80 | } |
tichise | 2:0148ac5c90fa | 81 | else if (i == endIndex) |
tichise | 0:c28aa7d4f97e | 82 | { |
tichise | 0:c28aa7d4f97e | 83 | colors[i].red = bottomColor.red; |
tichise | 0:c28aa7d4f97e | 84 | colors[i].green = bottomColor.green; |
tichise | 0:c28aa7d4f97e | 85 | colors[i].blue = bottomColor.blue; |
tichise | 0:c28aa7d4f97e | 86 | } |
tichise | 0:c28aa7d4f97e | 87 | else |
tichise | 0:c28aa7d4f97e | 88 | { |
tichise | 0:c28aa7d4f97e | 89 | colors[i].red = 0; |
tichise | 0:c28aa7d4f97e | 90 | colors[i].green = 0; |
tichise | 0:c28aa7d4f97e | 91 | colors[i].blue = 0; |
tichise | 0:c28aa7d4f97e | 92 | } |
tichise | 0:c28aa7d4f97e | 93 | } |
tichise | 0:c28aa7d4f97e | 94 | |
tichise | 0:c28aa7d4f97e | 95 | _ledStrip.update(colors, 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 | 0:c28aa7d4f97e | 102 | neopixel::Pixel colors[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 | 5:69bb2a2fa11f | 108 | if (startCount <= i && i < endCount) |
tichise | 5:69bb2a2fa11f | 109 | { |
tichise | 2:0148ac5c90fa | 110 | colors[i].red = rgbColor.red; |
tichise | 2:0148ac5c90fa | 111 | colors[i].green = rgbColor.green; |
tichise | 2:0148ac5c90fa | 112 | colors[i].blue = rgbColor.blue; |
tichise | 5:69bb2a2fa11f | 113 | } |
tichise | 5:69bb2a2fa11f | 114 | else |
tichise | 5:69bb2a2fa11f | 115 | { |
tichise | 2:0148ac5c90fa | 116 | colors[i].red = 0; |
tichise | 2:0148ac5c90fa | 117 | colors[i].green = 0; |
tichise | 2:0148ac5c90fa | 118 | colors[i].blue = 0; |
tichise | 2:0148ac5c90fa | 119 | } |
tichise | 0:c28aa7d4f97e | 120 | } |
tichise | 0:c28aa7d4f97e | 121 | else |
tichise | 0:c28aa7d4f97e | 122 | { |
tichise | 0:c28aa7d4f97e | 123 | colors[i].red = 0; |
tichise | 0:c28aa7d4f97e | 124 | colors[i].green = 0; |
tichise | 0:c28aa7d4f97e | 125 | colors[i].blue = 0; |
tichise | 0:c28aa7d4f97e | 126 | } |
tichise | 0:c28aa7d4f97e | 127 | } |
tichise | 0:c28aa7d4f97e | 128 | |
tichise | 0:c28aa7d4f97e | 129 | _ledStrip.update(colors, count); |
tichise | 2:0148ac5c90fa | 130 | |
tichise | 5:69bb2a2fa11f | 131 | if (startCount <= j && j < endCount) |
tichise | 5:69bb2a2fa11f | 132 | { |
tichise | 5:69bb2a2fa11f | 133 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 134 | { |
tichise | 5:69bb2a2fa11f | 135 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 136 | } |
tichise | 5:69bb2a2fa11f | 137 | else |
tichise | 5:69bb2a2fa11f | 138 | { |
tichise | 5:69bb2a2fa11f | 139 | wait(0.015); |
tichise | 5:69bb2a2fa11f | 140 | } |
tichise | 2:0148ac5c90fa | 141 | } |
tichise | 0:c28aa7d4f97e | 142 | } |
tichise | 0:c28aa7d4f97e | 143 | } |
tichise | 0:c28aa7d4f97e | 144 | |
tichise | 0:c28aa7d4f97e | 145 | void TI_NEOPIXEL_SPI::chase(int count, int bufferCount, rgbColor c1, rgbColor c2) |
tichise | 0:c28aa7d4f97e | 146 | { |
tichise | 0:c28aa7d4f97e | 147 | int virtualCount = count + bufferCount; |
tichise | 0:c28aa7d4f97e | 148 | neopixel::Pixel colors[virtualCount]; |
tichise | 0:c28aa7d4f97e | 149 | |
tichise | 0:c28aa7d4f97e | 150 | for (int j = 0; j < virtualCount; j++) |
tichise | 0:c28aa7d4f97e | 151 | { |
tichise | 0:c28aa7d4f97e | 152 | colors[j].red = c2.red; |
tichise | 0:c28aa7d4f97e | 153 | colors[j].green = c2.green; |
tichise | 0:c28aa7d4f97e | 154 | colors[j].blue = c2.blue; |
tichise | 0:c28aa7d4f97e | 155 | } |
tichise | 0:c28aa7d4f97e | 156 | |
tichise | 5:69bb2a2fa11f | 157 | for (int j = 0; j < count; j++) |
tichise | 0:c28aa7d4f97e | 158 | { |
tichise | 0:c28aa7d4f97e | 159 | colors[j].red = c1.red; |
tichise | 0:c28aa7d4f97e | 160 | colors[j].green = c1.green; |
tichise | 0:c28aa7d4f97e | 161 | colors[j].blue = c1.blue; |
tichise | 0:c28aa7d4f97e | 162 | |
tichise | 0:c28aa7d4f97e | 163 | colors[j - bufferCount].red = c2.red; |
tichise | 0:c28aa7d4f97e | 164 | colors[j - bufferCount].green = c2.green; |
tichise | 0:c28aa7d4f97e | 165 | colors[j - bufferCount].blue = c2.blue; |
tichise | 0:c28aa7d4f97e | 166 | |
tichise | 0:c28aa7d4f97e | 167 | _ledStrip.update(colors, virtualCount); |
tichise | 5:69bb2a2fa11f | 168 | |
tichise | 5:69bb2a2fa11f | 169 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 170 | { |
tichise | 5:69bb2a2fa11f | 171 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 172 | } |
tichise | 5:69bb2a2fa11f | 173 | else |
tichise | 5:69bb2a2fa11f | 174 | { |
tichise | 5:69bb2a2fa11f | 175 | wait(0.015); |
tichise | 5:69bb2a2fa11f | 176 | } |
tichise | 0:c28aa7d4f97e | 177 | } |
tichise | 0:c28aa7d4f97e | 178 | } |
tichise | 0:c28aa7d4f97e | 179 | |
tichise | 5:69bb2a2fa11f | 180 | void TI_NEOPIXEL_SPI::chase2(int count, int bufferCount, rgbColor c1, rgbColor c2) |
tichise | 5:69bb2a2fa11f | 181 | { |
tichise | 5:69bb2a2fa11f | 182 | for (int j = 0; j < count; j++) |
tichise | 5:69bb2a2fa11f | 183 | { |
tichise | 5:69bb2a2fa11f | 184 | neopixel::Pixel colors[count]; |
tichise | 5:69bb2a2fa11f | 185 | |
tichise | 5:69bb2a2fa11f | 186 | for (int i = 0; i < count; i++) |
tichise | 5:69bb2a2fa11f | 187 | { |
tichise | 5:69bb2a2fa11f | 188 | if (i <= j) |
tichise | 5:69bb2a2fa11f | 189 | { |
tichise | 5:69bb2a2fa11f | 190 | colors[i].red = c1.red; |
tichise | 5:69bb2a2fa11f | 191 | colors[i].green = c1.green; |
tichise | 5:69bb2a2fa11f | 192 | colors[i].blue = c1.blue; |
tichise | 5:69bb2a2fa11f | 193 | |
tichise | 5:69bb2a2fa11f | 194 | // LEDを節約 |
tichise | 5:69bb2a2fa11f | 195 | colors[i - bufferCount].red = c2.red; |
tichise | 5:69bb2a2fa11f | 196 | colors[i - bufferCount].green = c2.green; |
tichise | 5:69bb2a2fa11f | 197 | colors[i - bufferCount].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 198 | } |
tichise | 5:69bb2a2fa11f | 199 | else |
tichise | 5:69bb2a2fa11f | 200 | { |
tichise | 5:69bb2a2fa11f | 201 | colors[i].red = c2.red; |
tichise | 5:69bb2a2fa11f | 202 | colors[i].green = c2.green; |
tichise | 5:69bb2a2fa11f | 203 | colors[i].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 204 | } |
tichise | 5:69bb2a2fa11f | 205 | } |
tichise | 5:69bb2a2fa11f | 206 | |
tichise | 5:69bb2a2fa11f | 207 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 208 | |
tichise | 5:69bb2a2fa11f | 209 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 210 | { |
tichise | 5:69bb2a2fa11f | 211 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 212 | } |
tichise | 5:69bb2a2fa11f | 213 | else |
tichise | 5:69bb2a2fa11f | 214 | { |
tichise | 5:69bb2a2fa11f | 215 | wait(0.015); |
tichise | 5:69bb2a2fa11f | 216 | } |
tichise | 5:69bb2a2fa11f | 217 | } |
tichise | 5:69bb2a2fa11f | 218 | } |
tichise | 5:69bb2a2fa11f | 219 | |
tichise | 5:69bb2a2fa11f | 220 | void TI_NEOPIXEL_SPI::chaseReverse(int count, int bufferCount, rgbColor c1, rgbColor c2) |
tichise | 5:69bb2a2fa11f | 221 | { |
tichise | 5:69bb2a2fa11f | 222 | printf("chaseReverse \n"); |
tichise | 5:69bb2a2fa11f | 223 | |
tichise | 5:69bb2a2fa11f | 224 | neopixel::Pixel colors[count]; |
tichise | 5:69bb2a2fa11f | 225 | |
tichise | 5:69bb2a2fa11f | 226 | for (int j = 0; j <= count; j++) |
tichise | 5:69bb2a2fa11f | 227 | { |
tichise | 5:69bb2a2fa11f | 228 | colors[j].red = c2.red; |
tichise | 5:69bb2a2fa11f | 229 | colors[j].green = c2.green; |
tichise | 5:69bb2a2fa11f | 230 | colors[j].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 231 | } |
tichise | 5:69bb2a2fa11f | 232 | |
tichise | 5:69bb2a2fa11f | 233 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 234 | |
tichise | 5:69bb2a2fa11f | 235 | for (int j = 0; j <= count; j++) |
tichise | 5:69bb2a2fa11f | 236 | { |
tichise | 5:69bb2a2fa11f | 237 | if (0 <= (count - j)) |
tichise | 5:69bb2a2fa11f | 238 | { |
tichise | 5:69bb2a2fa11f | 239 | colors[count - j].red = c1.red; |
tichise | 5:69bb2a2fa11f | 240 | colors[count - j].green = c1.green; |
tichise | 5:69bb2a2fa11f | 241 | colors[count - j].blue = c1.blue; |
tichise | 5:69bb2a2fa11f | 242 | } |
tichise | 5:69bb2a2fa11f | 243 | |
tichise | 5:69bb2a2fa11f | 244 | if (bufferCount <= j) |
tichise | 5:69bb2a2fa11f | 245 | { |
tichise | 5:69bb2a2fa11f | 246 | colors[count + bufferCount - j].red = c2.red; |
tichise | 5:69bb2a2fa11f | 247 | colors[count + bufferCount - j].green = c2.green; |
tichise | 5:69bb2a2fa11f | 248 | colors[count + bufferCount - j].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 249 | } |
tichise | 5:69bb2a2fa11f | 250 | |
tichise | 5:69bb2a2fa11f | 251 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 252 | |
tichise | 5:69bb2a2fa11f | 253 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 254 | { |
tichise | 5:69bb2a2fa11f | 255 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 256 | } |
tichise | 5:69bb2a2fa11f | 257 | else |
tichise | 5:69bb2a2fa11f | 258 | { |
tichise | 5:69bb2a2fa11f | 259 | wait(0.015); |
tichise | 5:69bb2a2fa11f | 260 | } |
tichise | 5:69bb2a2fa11f | 261 | } |
tichise | 5:69bb2a2fa11f | 262 | } |
tichise | 5:69bb2a2fa11f | 263 | |
tichise | 5:69bb2a2fa11f | 264 | void TI_NEOPIXEL_SPI::chaseRainbow(int count, int bufferCount) |
tichise | 2:0148ac5c90fa | 265 | { |
tichise | 2:0148ac5c90fa | 266 | for (int j = 0; j < count; j++) |
tichise | 2:0148ac5c90fa | 267 | { |
tichise | 2:0148ac5c90fa | 268 | neopixel::Pixel colors[count]; |
tichise | 2:0148ac5c90fa | 269 | |
tichise | 2:0148ac5c90fa | 270 | for (int i = 0; i < count; i++) |
tichise | 2:0148ac5c90fa | 271 | { |
tichise | 2:0148ac5c90fa | 272 | if (i <= j) |
tichise | 2:0148ac5c90fa | 273 | { |
tichise | 2:0148ac5c90fa | 274 | uint8_t phase = 256 / count * i; |
tichise | 2:0148ac5c90fa | 275 | rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0); |
tichise | 2:0148ac5c90fa | 276 | colors[i].red = rgbColor.red; |
tichise | 2:0148ac5c90fa | 277 | colors[i].green = rgbColor.green; |
tichise | 2:0148ac5c90fa | 278 | colors[i].blue = rgbColor.blue; |
tichise | 2:0148ac5c90fa | 279 | |
tichise | 2:0148ac5c90fa | 280 | // LEDを節約 |
tichise | 5:69bb2a2fa11f | 281 | colors[i - bufferCount].red = 0; |
tichise | 5:69bb2a2fa11f | 282 | colors[i - bufferCount].green = 0; |
tichise | 5:69bb2a2fa11f | 283 | colors[i - bufferCount].blue = 0; |
tichise | 2:0148ac5c90fa | 284 | } |
tichise | 2:0148ac5c90fa | 285 | else |
tichise | 2:0148ac5c90fa | 286 | { |
tichise | 2:0148ac5c90fa | 287 | colors[i].red = 0; |
tichise | 2:0148ac5c90fa | 288 | colors[i].green = 0; |
tichise | 2:0148ac5c90fa | 289 | colors[i].blue = 0; |
tichise | 2:0148ac5c90fa | 290 | } |
tichise | 2:0148ac5c90fa | 291 | } |
tichise | 2:0148ac5c90fa | 292 | |
tichise | 2:0148ac5c90fa | 293 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 294 | |
tichise | 5:69bb2a2fa11f | 295 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 296 | { |
tichise | 5:69bb2a2fa11f | 297 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 298 | } |
tichise | 5:69bb2a2fa11f | 299 | else |
tichise | 5:69bb2a2fa11f | 300 | { |
tichise | 5:69bb2a2fa11f | 301 | wait(0.015); |
tichise | 5:69bb2a2fa11f | 302 | } |
tichise | 2:0148ac5c90fa | 303 | } |
tichise | 2:0148ac5c90fa | 304 | } |
tichise | 2:0148ac5c90fa | 305 | |
tichise | 0:c28aa7d4f97e | 306 | void TI_NEOPIXEL_SPI::circleRainbow(int count) |
tichise | 0:c28aa7d4f97e | 307 | { |
tichise | 0:c28aa7d4f97e | 308 | for (int j = 0; j < count; j++) |
tichise | 0:c28aa7d4f97e | 309 | { |
tichise | 0:c28aa7d4f97e | 310 | neopixel::Pixel colors[count]; |
tichise | 0:c28aa7d4f97e | 311 | |
tichise | 0:c28aa7d4f97e | 312 | for (int i = 0; i < count; i++) |
tichise | 0:c28aa7d4f97e | 313 | { |
tichise | 0:c28aa7d4f97e | 314 | if (i <= j) |
tichise | 0:c28aa7d4f97e | 315 | { |
tichise | 0:c28aa7d4f97e | 316 | uint8_t phase = 256 / count * i; |
tichise | 0:c28aa7d4f97e | 317 | rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0); |
tichise | 0:c28aa7d4f97e | 318 | colors[i].red = rgbColor.red; |
tichise | 0:c28aa7d4f97e | 319 | colors[i].green = rgbColor.green; |
tichise | 0:c28aa7d4f97e | 320 | colors[i].blue = rgbColor.blue; |
tichise | 0:c28aa7d4f97e | 321 | } |
tichise | 0:c28aa7d4f97e | 322 | else |
tichise | 0:c28aa7d4f97e | 323 | { |
tichise | 0:c28aa7d4f97e | 324 | colors[i].red = 0; |
tichise | 0:c28aa7d4f97e | 325 | colors[i].green = 0; |
tichise | 0:c28aa7d4f97e | 326 | colors[i].blue = 0; |
tichise | 0:c28aa7d4f97e | 327 | } |
tichise | 0:c28aa7d4f97e | 328 | } |
tichise | 0:c28aa7d4f97e | 329 | |
tichise | 0:c28aa7d4f97e | 330 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 331 | |
tichise | 5:69bb2a2fa11f | 332 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 333 | { |
tichise | 5:69bb2a2fa11f | 334 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 335 | } |
tichise | 5:69bb2a2fa11f | 336 | else |
tichise | 5:69bb2a2fa11f | 337 | { |
tichise | 5:69bb2a2fa11f | 338 | wait(0.015); |
tichise | 5:69bb2a2fa11f | 339 | } |
tichise | 5:69bb2a2fa11f | 340 | } |
tichise | 5:69bb2a2fa11f | 341 | } |
tichise | 5:69bb2a2fa11f | 342 | |
tichise | 5:69bb2a2fa11f | 343 | void TI_NEOPIXEL_SPI::moon(int count, int startIndex, int stopIndex, rgbColor c1, rgbColor c2) |
tichise | 5:69bb2a2fa11f | 344 | { |
tichise | 5:69bb2a2fa11f | 345 | |
tichise | 5:69bb2a2fa11f | 346 | neopixel::Pixel colors[count]; |
tichise | 5:69bb2a2fa11f | 347 | |
tichise | 5:69bb2a2fa11f | 348 | for (int j = 0; j < count; j++) |
tichise | 5:69bb2a2fa11f | 349 | { |
tichise | 5:69bb2a2fa11f | 350 | if (0 <= j && j <= count) |
tichise | 5:69bb2a2fa11f | 351 | { |
tichise | 5:69bb2a2fa11f | 352 | colors[j].red = c2.red; |
tichise | 5:69bb2a2fa11f | 353 | colors[j].green = c2.green; |
tichise | 5:69bb2a2fa11f | 354 | colors[j].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 355 | } |
tichise | 5:69bb2a2fa11f | 356 | } |
tichise | 5:69bb2a2fa11f | 357 | |
tichise | 5:69bb2a2fa11f | 358 | int loopCount = (stopIndex - startIndex) / 2; |
tichise | 5:69bb2a2fa11f | 359 | int middlePoint = startIndex + loopCount; |
tichise | 5:69bb2a2fa11f | 360 | |
tichise | 5:69bb2a2fa11f | 361 | for (int i = 0; i <= loopCount - 1; i++) |
tichise | 5:69bb2a2fa11f | 362 | { |
tichise | 5:69bb2a2fa11f | 363 | colors[middlePoint + i].red = c1.red; |
tichise | 5:69bb2a2fa11f | 364 | colors[middlePoint + i].green = c1.green; |
tichise | 5:69bb2a2fa11f | 365 | colors[middlePoint + i].blue = c1.blue; |
tichise | 5:69bb2a2fa11f | 366 | |
tichise | 5:69bb2a2fa11f | 367 | colors[middlePoint - i].red = c1.red; |
tichise | 5:69bb2a2fa11f | 368 | colors[middlePoint - i].green = c1.green; |
tichise | 5:69bb2a2fa11f | 369 | colors[middlePoint - i].blue = c1.blue; |
tichise | 5:69bb2a2fa11f | 370 | |
tichise | 5:69bb2a2fa11f | 371 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 372 | |
tichise | 5:69bb2a2fa11f | 373 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 374 | { |
tichise | 5:69bb2a2fa11f | 375 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 376 | } |
tichise | 5:69bb2a2fa11f | 377 | else |
tichise | 5:69bb2a2fa11f | 378 | { |
tichise | 5:69bb2a2fa11f | 379 | wait(0.04); |
tichise | 5:69bb2a2fa11f | 380 | } |
tichise | 5:69bb2a2fa11f | 381 | } |
tichise | 5:69bb2a2fa11f | 382 | |
tichise | 5:69bb2a2fa11f | 383 | for (int i = 0; i <= loopCount - 1; i++) |
tichise | 5:69bb2a2fa11f | 384 | { |
tichise | 5:69bb2a2fa11f | 385 | colors[startIndex + i].red = c2.red; |
tichise | 5:69bb2a2fa11f | 386 | colors[startIndex + i].green = c2.green; |
tichise | 5:69bb2a2fa11f | 387 | colors[startIndex + i].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 388 | |
tichise | 5:69bb2a2fa11f | 389 | colors[stopIndex - i].red = c2.red; |
tichise | 5:69bb2a2fa11f | 390 | colors[stopIndex - i].green = c2.green; |
tichise | 5:69bb2a2fa11f | 391 | colors[stopIndex - i].blue = c2.blue; |
tichise | 5:69bb2a2fa11f | 392 | |
tichise | 5:69bb2a2fa11f | 393 | _ledStrip.update(colors, count); |
tichise | 5:69bb2a2fa11f | 394 | |
tichise | 5:69bb2a2fa11f | 395 | if (count == 16) |
tichise | 5:69bb2a2fa11f | 396 | { |
tichise | 5:69bb2a2fa11f | 397 | wait(0.05); |
tichise | 5:69bb2a2fa11f | 398 | } |
tichise | 5:69bb2a2fa11f | 399 | else |
tichise | 5:69bb2a2fa11f | 400 | { |
tichise | 5:69bb2a2fa11f | 401 | wait(0.04); |
tichise | 5:69bb2a2fa11f | 402 | } |
tichise | 0:c28aa7d4f97e | 403 | } |
tichise | 0:c28aa7d4f97e | 404 | } |
tichise | 0:c28aa7d4f97e | 405 | |
tichise | 0:c28aa7d4f97e | 406 | rgbColor TI_NEOPIXEL_SPI::convertHsvToRgb(float h, float s, float v) |
tichise | 0:c28aa7d4f97e | 407 | { |
tichise | 0:c28aa7d4f97e | 408 | int i = floor(h * 6); |
tichise | 0:c28aa7d4f97e | 409 | float f = h * 6 - i; |
tichise | 0:c28aa7d4f97e | 410 | float p = v * (1 - s); |
tichise | 0:c28aa7d4f97e | 411 | float q = v * (1 - f * s); |
tichise | 0:c28aa7d4f97e | 412 | float t = v * (1 - (1 - f) * s); |
tichise | 0:c28aa7d4f97e | 413 | float r = 0, g = 0, b = 0; |
tichise | 0:c28aa7d4f97e | 414 | |
tichise | 0:c28aa7d4f97e | 415 | switch (i % 6) |
tichise | 0:c28aa7d4f97e | 416 | { |
tichise | 0:c28aa7d4f97e | 417 | case 0: |
tichise | 0:c28aa7d4f97e | 418 | r = v; |
tichise | 0:c28aa7d4f97e | 419 | g = t; |
tichise | 0:c28aa7d4f97e | 420 | b = p; |
tichise | 0:c28aa7d4f97e | 421 | break; |
tichise | 0:c28aa7d4f97e | 422 | case 1: |
tichise | 0:c28aa7d4f97e | 423 | r = q; |
tichise | 0:c28aa7d4f97e | 424 | g = v; |
tichise | 0:c28aa7d4f97e | 425 | b = p; |
tichise | 0:c28aa7d4f97e | 426 | break; |
tichise | 0:c28aa7d4f97e | 427 | case 2: |
tichise | 0:c28aa7d4f97e | 428 | r = p; |
tichise | 0:c28aa7d4f97e | 429 | g = v; |
tichise | 0:c28aa7d4f97e | 430 | b = t; |
tichise | 0:c28aa7d4f97e | 431 | break; |
tichise | 0:c28aa7d4f97e | 432 | case 3: |
tichise | 0:c28aa7d4f97e | 433 | r = p; |
tichise | 0:c28aa7d4f97e | 434 | g = q; |
tichise | 0:c28aa7d4f97e | 435 | b = v; |
tichise | 0:c28aa7d4f97e | 436 | break; |
tichise | 0:c28aa7d4f97e | 437 | case 4: |
tichise | 0:c28aa7d4f97e | 438 | r = t; |
tichise | 0:c28aa7d4f97e | 439 | g = p; |
tichise | 0:c28aa7d4f97e | 440 | b = v; |
tichise | 0:c28aa7d4f97e | 441 | break; |
tichise | 0:c28aa7d4f97e | 442 | case 5: |
tichise | 0:c28aa7d4f97e | 443 | r = v; |
tichise | 0:c28aa7d4f97e | 444 | g = p; |
tichise | 0:c28aa7d4f97e | 445 | b = q; |
tichise | 0:c28aa7d4f97e | 446 | break; |
tichise | 0:c28aa7d4f97e | 447 | } |
tichise | 0:c28aa7d4f97e | 448 | |
tichise | 0:c28aa7d4f97e | 449 | return (rgbColor){r * 255, g * 255, b * 255}; |
tichise | 0:c28aa7d4f97e | 450 | } |