A library for SPI control of adafruit's neopixel ring and addressable LEDs.
Dependents: TI_NEOPIXEL_SPI_SAMPLE
Diff: TI_NEOPIXEL_SPI.cpp
- Revision:
- 3:f0859c280204
- Parent:
- 2:0148ac5c90fa
- Child:
- 4:70bc3528e07e
--- a/TI_NEOPIXEL_SPI.cpp Sun Jul 28 00:13:03 2019 +0000 +++ b/TI_NEOPIXEL_SPI.cpp Thu Jan 02 08:14:13 2020 +0000 @@ -1,125 +1,135 @@ #include "TI_NEOPIXEL_SPI.h" #include "mbed.h" -TI_NEOPIXEL_SPI::TI_NEOPIXEL_SPI(PinName input) : _ledStrip(input) +TI_NEOPIXEL_SPI::TI_NEOPIXEL_SPI(PinName input) : _neoPixel(input) { } void TI_NEOPIXEL_SPI::switchLightOff(int count) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); } -void TI_NEOPIXEL_SPI::switchLightOn(int count, int startCount, int endCount, rgbColor rgbColor) +void TI_NEOPIXEL_SPI::switchLightOn(int count, int startCount, int endCount, rgbColor rgbColor) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { - if (startCount <= i && i < endCount) { - colors[i].red = rgbColor.red; - colors[i].green = rgbColor.green; - colors[i].blue = rgbColor.blue; - } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + if (startCount <= i && i < endCount) + { + pixels[i].r = rgbColor.red; + pixels[i].g = rgbColor.green; + pixels[i].b = rgbColor.blue; + } + else + { + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); } void TI_NEOPIXEL_SPI::changeColor(int count, int startCount, int endCount, rgbColor rgbColor) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { - if (startCount <= i && i < endCount) { - colors[i].red = rgbColor.red; - colors[i].green = rgbColor.green; - colors[i].blue = rgbColor.blue; - } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + if (startCount <= i && i < endCount) + { + pixels[i].r = rgbColor.red; + pixels[i].g = rgbColor.green; + pixels[i].b = rgbColor.blue; + } + else + { + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); } void TI_NEOPIXEL_SPI::changePointColor(int count, int topIndex, int endIndex, rgbColor topColor, rgbColor bottomColor) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { if (i == topIndex) { - colors[i].red = topColor.red; - colors[i].green = topColor.green; - colors[i].blue = topColor.blue; + pixels[i].r = topColor.red; + pixels[i].g = topColor.green; + pixels[i].b = topColor.blue; } else if (i == endIndex) { - colors[i].red = bottomColor.red; - colors[i].green = bottomColor.green; - colors[i].blue = bottomColor.blue; + pixels[i].r = bottomColor.red; + pixels[i].g = bottomColor.green; + pixels[i].b = bottomColor.blue; } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); } void TI_NEOPIXEL_SPI::circle(int count, int startCount, int endCount, rgbColor rgbColor) { for (int j = 0; j < count; j++) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { if (i <= j) { - if (startCount <= i && i < endCount) { - colors[i].red = rgbColor.red; - colors[i].green = rgbColor.green; - colors[i].blue = rgbColor.blue; - } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + if (startCount <= i && i < endCount) + { + pixels[i].r = rgbColor.red; + pixels[i].g = rgbColor.green; + pixels[i].b = rgbColor.blue; + } + else + { + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); - if (startCount <= j && j < endCount) { + if (startCount <= j && j < endCount) + { wait(0.07); } } @@ -128,28 +138,28 @@ void TI_NEOPIXEL_SPI::chase(int count, int bufferCount, rgbColor c1, rgbColor c2) { int virtualCount = count + bufferCount; - neopixel::Pixel colors[virtualCount]; + Pixel pixels[virtualCount]; for (int j = 0; j < virtualCount; j++) { - colors[j].red = c2.red; - colors[j].green = c2.green; - colors[j].blue = c2.blue; + pixels[j].r = c2.red; + pixels[j].g = c2.green; + pixels[j].b = c2.blue; } - _ledStrip.update(colors, virtualCount); + _neoPixel.send(pixels, virtualCount); for (int j = 0; j < virtualCount + bufferCount; j++) { - colors[j].red = c1.red; - colors[j].green = c1.green; - colors[j].blue = c1.blue; + pixels[j].r = c1.red; + pixels[j].g = c1.green; + pixels[j].b = c1.blue; - colors[j - bufferCount].red = c2.red; - colors[j - bufferCount].green = c2.green; - colors[j - bufferCount].blue = c2.blue; + pixels[j - bufferCount].r = c2.red; + pixels[j - bufferCount].g = c2.green; + pixels[j - bufferCount].b = c2.blue; - _ledStrip.update(colors, virtualCount); + _neoPixel.send(pixels, virtualCount); wait(0.05); } } @@ -158,7 +168,7 @@ { for (int j = 0; j < count; j++) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { @@ -166,24 +176,24 @@ { uint8_t phase = 256 / count * i; rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0); - colors[i].red = rgbColor.red; - colors[i].green = rgbColor.green; - colors[i].blue = rgbColor.blue; + pixels[i].r = rgbColor.red; + pixels[i].g = rgbColor.green; + pixels[i].b = rgbColor.blue; // LEDを節約 - colors[i-4].red = 0; - colors[i-4].green = 0; - colors[i-4].blue = 0; + pixels[i - 4].r = 0; + pixels[i - 4].g = 0; + pixels[i - 4].b = 0; } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); wait(0.06); } } @@ -192,7 +202,7 @@ { for (int j = 0; j < count; j++) { - neopixel::Pixel colors[count]; + Pixel pixels[count]; for (int i = 0; i < count; i++) { @@ -200,19 +210,19 @@ { uint8_t phase = 256 / count * i; rgbColor rgbColor = convertHsvToRgb(phase / 256.0, 1.0, 1.0); - colors[i].red = rgbColor.red; - colors[i].green = rgbColor.green; - colors[i].blue = rgbColor.blue; + pixels[i].r = rgbColor.red; + pixels[i].g = rgbColor.green; + pixels[i].b = rgbColor.blue; } else { - colors[i].red = 0; - colors[i].green = 0; - colors[i].blue = 0; + pixels[i].r = 0; + pixels[i].g = 0; + pixels[i].b = 0; } } - _ledStrip.update(colors, count); + _neoPixel.send(pixels, count); wait(0.06); } }