A library for SPI control of adafruit's neopixel ring and addressable LEDs.
Dependents: TI_NEOPIXEL_SPI_SAMPLE
TI_NEOPIXEL_SPI.cpp@0:c28aa7d4f97e, 2019-07-09 (annotated)
- Committer:
- tichise
- Date:
- Tue Jul 09 15:25:16 2019 +0000
- Revision:
- 0:c28aa7d4f97e
- Child:
- 2:0148ac5c90fa
new
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 | 0:c28aa7d4f97e | 23 | void TI_NEOPIXEL_SPI::switchLightOn(int count) |
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 | 0:c28aa7d4f97e | 29 | colors[i].red = 50; |
tichise | 0:c28aa7d4f97e | 30 | colors[i].green = 10; |
tichise | 0:c28aa7d4f97e | 31 | colors[i].blue = 170; |
tichise | 0:c28aa7d4f97e | 32 | } |
tichise | 0:c28aa7d4f97e | 33 | |
tichise | 0:c28aa7d4f97e | 34 | _ledStrip.update(colors, count); |
tichise | 0:c28aa7d4f97e | 35 | } |
tichise | 0:c28aa7d4f97e | 36 | |
tichise | 0:c28aa7d4f97e | 37 | void TI_NEOPIXEL_SPI::changeColor(int count, rgbColor rgbColor) |
tichise | 0:c28aa7d4f97e | 38 | { |
tichise | 0:c28aa7d4f97e | 39 | neopixel::Pixel colors[count]; |
tichise | 0:c28aa7d4f97e | 40 | |
tichise | 0:c28aa7d4f97e | 41 | for (int i = 0; i < count; i++) |
tichise | 0:c28aa7d4f97e | 42 | { |
tichise | 0:c28aa7d4f97e | 43 | colors[i].red = rgbColor.red; |
tichise | 0:c28aa7d4f97e | 44 | colors[i].green = rgbColor.green; |
tichise | 0:c28aa7d4f97e | 45 | colors[i].blue = rgbColor.blue; |
tichise | 0:c28aa7d4f97e | 46 | } |
tichise | 0:c28aa7d4f97e | 47 | |
tichise | 0:c28aa7d4f97e | 48 | _ledStrip.update(colors, count); |
tichise | 0:c28aa7d4f97e | 49 | } |
tichise | 0:c28aa7d4f97e | 50 | |
tichise | 0:c28aa7d4f97e | 51 | void TI_NEOPIXEL_SPI::changePointColor(int count, rgbColor topColor, rgbColor bottomColor) |
tichise | 0:c28aa7d4f97e | 52 | { |
tichise | 0:c28aa7d4f97e | 53 | neopixel::Pixel colors[count]; |
tichise | 0:c28aa7d4f97e | 54 | |
tichise | 0:c28aa7d4f97e | 55 | for (int i = 0; i < count; i++) |
tichise | 0:c28aa7d4f97e | 56 | { |
tichise | 0:c28aa7d4f97e | 57 | if (i == 0) |
tichise | 0:c28aa7d4f97e | 58 | { |
tichise | 0:c28aa7d4f97e | 59 | colors[i].red = topColor.red; |
tichise | 0:c28aa7d4f97e | 60 | colors[i].green = topColor.green; |
tichise | 0:c28aa7d4f97e | 61 | colors[i].blue = topColor.blue; |
tichise | 0:c28aa7d4f97e | 62 | } |
tichise | 0:c28aa7d4f97e | 63 | else if (i == count / 2) |
tichise | 0:c28aa7d4f97e | 64 | { |
tichise | 0:c28aa7d4f97e | 65 | colors[i].red = bottomColor.red; |
tichise | 0:c28aa7d4f97e | 66 | colors[i].green = bottomColor.green; |
tichise | 0:c28aa7d4f97e | 67 | colors[i].blue = bottomColor.blue; |
tichise | 0:c28aa7d4f97e | 68 | } |
tichise | 0:c28aa7d4f97e | 69 | else |
tichise | 0:c28aa7d4f97e | 70 | { |
tichise | 0:c28aa7d4f97e | 71 | colors[i].red = 0; |
tichise | 0:c28aa7d4f97e | 72 | colors[i].green = 0; |
tichise | 0:c28aa7d4f97e | 73 | colors[i].blue = 0; |
tichise | 0:c28aa7d4f97e | 74 | } |
tichise | 0:c28aa7d4f97e | 75 | } |
tichise | 0:c28aa7d4f97e | 76 | |
tichise | 0:c28aa7d4f97e | 77 | _ledStrip.update(colors, count); |
tichise | 0:c28aa7d4f97e | 78 | } |
tichise | 0:c28aa7d4f97e | 79 | |
tichise | 0:c28aa7d4f97e | 80 | void TI_NEOPIXEL_SPI::circle(int count, rgbColor rgbColor) |
tichise | 0:c28aa7d4f97e | 81 | { |
tichise | 0:c28aa7d4f97e | 82 | for (int j = 0; j < count; j++) |
tichise | 0:c28aa7d4f97e | 83 | { |
tichise | 0:c28aa7d4f97e | 84 | neopixel::Pixel colors[count]; |
tichise | 0:c28aa7d4f97e | 85 | |
tichise | 0:c28aa7d4f97e | 86 | for (int i = 0; i < count; i++) |
tichise | 0:c28aa7d4f97e | 87 | { |
tichise | 0:c28aa7d4f97e | 88 | if (j >= i) |
tichise | 0:c28aa7d4f97e | 89 | { |
tichise | 0:c28aa7d4f97e | 90 | colors[i].red = rgbColor.red; |
tichise | 0:c28aa7d4f97e | 91 | colors[i].green = rgbColor.green; |
tichise | 0:c28aa7d4f97e | 92 | colors[i].blue = rgbColor.blue; |
tichise | 0:c28aa7d4f97e | 93 | } |
tichise | 0:c28aa7d4f97e | 94 | else |
tichise | 0:c28aa7d4f97e | 95 | { |
tichise | 0:c28aa7d4f97e | 96 | colors[i].red = 0; |
tichise | 0:c28aa7d4f97e | 97 | colors[i].green = 0; |
tichise | 0:c28aa7d4f97e | 98 | colors[i].blue = 0; |
tichise | 0:c28aa7d4f97e | 99 | } |
tichise | 0:c28aa7d4f97e | 100 | } |
tichise | 0:c28aa7d4f97e | 101 | |
tichise | 0:c28aa7d4f97e | 102 | _ledStrip.update(colors, count); |
tichise | 0:c28aa7d4f97e | 103 | wait(0.07); |
tichise | 0:c28aa7d4f97e | 104 | } |
tichise | 0:c28aa7d4f97e | 105 | } |
tichise | 0:c28aa7d4f97e | 106 | |
tichise | 0:c28aa7d4f97e | 107 | void TI_NEOPIXEL_SPI::chase(int count, int bufferCount, rgbColor c1, rgbColor c2) |
tichise | 0:c28aa7d4f97e | 108 | { |
tichise | 0:c28aa7d4f97e | 109 | int virtualCount = count + bufferCount; |
tichise | 0:c28aa7d4f97e | 110 | neopixel::Pixel colors[virtualCount]; |
tichise | 0:c28aa7d4f97e | 111 | |
tichise | 0:c28aa7d4f97e | 112 | for (int j = 0; j < virtualCount; j++) |
tichise | 0:c28aa7d4f97e | 113 | { |
tichise | 0:c28aa7d4f97e | 114 | colors[j].red = c2.red; |
tichise | 0:c28aa7d4f97e | 115 | colors[j].green = c2.green; |
tichise | 0:c28aa7d4f97e | 116 | colors[j].blue = c2.blue; |
tichise | 0:c28aa7d4f97e | 117 | } |
tichise | 0:c28aa7d4f97e | 118 | |
tichise | 0:c28aa7d4f97e | 119 | _ledStrip.update(colors, virtualCount); |
tichise | 0:c28aa7d4f97e | 120 | |
tichise | 0:c28aa7d4f97e | 121 | for (int j = 0; j < virtualCount + bufferCount; j++) |
tichise | 0:c28aa7d4f97e | 122 | { |
tichise | 0:c28aa7d4f97e | 123 | colors[j].red = c1.red; |
tichise | 0:c28aa7d4f97e | 124 | colors[j].green = c1.green; |
tichise | 0:c28aa7d4f97e | 125 | colors[j].blue = c1.blue; |
tichise | 0:c28aa7d4f97e | 126 | |
tichise | 0:c28aa7d4f97e | 127 | colors[j - bufferCount].red = c2.red; |
tichise | 0:c28aa7d4f97e | 128 | colors[j - bufferCount].green = c2.green; |
tichise | 0:c28aa7d4f97e | 129 | colors[j - bufferCount].blue = c2.blue; |
tichise | 0:c28aa7d4f97e | 130 | |
tichise | 0:c28aa7d4f97e | 131 | _ledStrip.update(colors, virtualCount); |
tichise | 0:c28aa7d4f97e | 132 | wait(0.05); |
tichise | 0:c28aa7d4f97e | 133 | } |
tichise | 0:c28aa7d4f97e | 134 | } |
tichise | 0:c28aa7d4f97e | 135 | |
tichise | 0:c28aa7d4f97e | 136 | void TI_NEOPIXEL_SPI::circleRainbow(int count) |
tichise | 0:c28aa7d4f97e | 137 | { |
tichise | 0:c28aa7d4f97e | 138 | for (int j = 0; j < count; j++) |
tichise | 0:c28aa7d4f97e | 139 | { |
tichise | 0:c28aa7d4f97e | 140 | neopixel::Pixel colors[count]; |
tichise | 0:c28aa7d4f97e | 141 | |
tichise | 0:c28aa7d4f97e | 142 | for (int i = 0; i < count; i++) |
tichise | 0:c28aa7d4f97e | 143 | { |
tichise | 0:c28aa7d4f97e | 144 | if (i <= j) |
tichise | 0:c28aa7d4f97e | 145 | { |
tichise | 0:c28aa7d4f97e | 146 | uint8_t phase = 256 / count * i; |
tichise | 0:c28aa7d4f97e | 147 | rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0); |
tichise | 0:c28aa7d4f97e | 148 | colors[i].red = rgbColor.red; |
tichise | 0:c28aa7d4f97e | 149 | colors[i].green = rgbColor.green; |
tichise | 0:c28aa7d4f97e | 150 | colors[i].blue = rgbColor.blue; |
tichise | 0:c28aa7d4f97e | 151 | } |
tichise | 0:c28aa7d4f97e | 152 | else |
tichise | 0:c28aa7d4f97e | 153 | { |
tichise | 0:c28aa7d4f97e | 154 | colors[i].red = 0; |
tichise | 0:c28aa7d4f97e | 155 | colors[i].green = 0; |
tichise | 0:c28aa7d4f97e | 156 | colors[i].blue = 0; |
tichise | 0:c28aa7d4f97e | 157 | } |
tichise | 0:c28aa7d4f97e | 158 | } |
tichise | 0:c28aa7d4f97e | 159 | |
tichise | 0:c28aa7d4f97e | 160 | _ledStrip.update(colors, count); |
tichise | 0:c28aa7d4f97e | 161 | wait(0.06); |
tichise | 0:c28aa7d4f97e | 162 | } |
tichise | 0:c28aa7d4f97e | 163 | } |
tichise | 0:c28aa7d4f97e | 164 | |
tichise | 0:c28aa7d4f97e | 165 | rgbColor TI_NEOPIXEL_SPI::convertHsvToRgb(float h, float s, float v) |
tichise | 0:c28aa7d4f97e | 166 | { |
tichise | 0:c28aa7d4f97e | 167 | int i = floor(h * 6); |
tichise | 0:c28aa7d4f97e | 168 | float f = h * 6 - i; |
tichise | 0:c28aa7d4f97e | 169 | float p = v * (1 - s); |
tichise | 0:c28aa7d4f97e | 170 | float q = v * (1 - f * s); |
tichise | 0:c28aa7d4f97e | 171 | float t = v * (1 - (1 - f) * s); |
tichise | 0:c28aa7d4f97e | 172 | float r = 0, g = 0, b = 0; |
tichise | 0:c28aa7d4f97e | 173 | |
tichise | 0:c28aa7d4f97e | 174 | switch (i % 6) |
tichise | 0:c28aa7d4f97e | 175 | { |
tichise | 0:c28aa7d4f97e | 176 | case 0: |
tichise | 0:c28aa7d4f97e | 177 | r = v; |
tichise | 0:c28aa7d4f97e | 178 | g = t; |
tichise | 0:c28aa7d4f97e | 179 | b = p; |
tichise | 0:c28aa7d4f97e | 180 | break; |
tichise | 0:c28aa7d4f97e | 181 | case 1: |
tichise | 0:c28aa7d4f97e | 182 | r = q; |
tichise | 0:c28aa7d4f97e | 183 | g = v; |
tichise | 0:c28aa7d4f97e | 184 | b = p; |
tichise | 0:c28aa7d4f97e | 185 | break; |
tichise | 0:c28aa7d4f97e | 186 | case 2: |
tichise | 0:c28aa7d4f97e | 187 | r = p; |
tichise | 0:c28aa7d4f97e | 188 | g = v; |
tichise | 0:c28aa7d4f97e | 189 | b = t; |
tichise | 0:c28aa7d4f97e | 190 | break; |
tichise | 0:c28aa7d4f97e | 191 | case 3: |
tichise | 0:c28aa7d4f97e | 192 | r = p; |
tichise | 0:c28aa7d4f97e | 193 | g = q; |
tichise | 0:c28aa7d4f97e | 194 | b = v; |
tichise | 0:c28aa7d4f97e | 195 | break; |
tichise | 0:c28aa7d4f97e | 196 | case 4: |
tichise | 0:c28aa7d4f97e | 197 | r = t; |
tichise | 0:c28aa7d4f97e | 198 | g = p; |
tichise | 0:c28aa7d4f97e | 199 | b = v; |
tichise | 0:c28aa7d4f97e | 200 | break; |
tichise | 0:c28aa7d4f97e | 201 | case 5: |
tichise | 0:c28aa7d4f97e | 202 | r = v; |
tichise | 0:c28aa7d4f97e | 203 | g = p; |
tichise | 0:c28aa7d4f97e | 204 | b = q; |
tichise | 0:c28aa7d4f97e | 205 | break; |
tichise | 0:c28aa7d4f97e | 206 | } |
tichise | 0:c28aa7d4f97e | 207 | |
tichise | 0:c28aa7d4f97e | 208 | return (rgbColor){r * 255, g * 255, b * 255}; |
tichise | 0:c28aa7d4f97e | 209 | } |