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

Dependencies:   PixelArray

Dependents:   TI_NEOPIXEL_SPI_SAMPLE

TI_NEOPIXEL_SPI.cpp

Committer:
tichise
Date:
2019-07-28
Revision:
2:0148ac5c90fa
Parent:
0:c28aa7d4f97e
Child:
3:f0859c280204
Child:
5:69bb2a2fa11f

File content as of revision 2:0148ac5c90fa:

#include "TI_NEOPIXEL_SPI.h"
#include "mbed.h"

TI_NEOPIXEL_SPI::TI_NEOPIXEL_SPI(PinName input) : _ledStrip(input)
{
}

void TI_NEOPIXEL_SPI::switchLightOff(int count)
{

    neopixel::Pixel colors[count];

    for (int i = 0; i < count; i++)
    {
        colors[i].red = 0;
        colors[i].green = 0;
        colors[i].blue = 0;
    }

    _ledStrip.update(colors, count);
}

void TI_NEOPIXEL_SPI::switchLightOn(int count,  int startCount, int endCount, rgbColor rgbColor)
{
    neopixel::Pixel colors[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;
        }
    }

    _ledStrip.update(colors, count);
}

void TI_NEOPIXEL_SPI::changeColor(int count, int startCount, int endCount, rgbColor rgbColor)
{
    neopixel::Pixel colors[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;
        }
    }

    _ledStrip.update(colors, count);
}

void TI_NEOPIXEL_SPI::changePointColor(int count, int topIndex, int endIndex, rgbColor topColor, rgbColor bottomColor)
{
    neopixel::Pixel colors[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;
        }
        else if (i == endIndex)
        {
            colors[i].red = bottomColor.red;
            colors[i].green = bottomColor.green;
            colors[i].blue = bottomColor.blue;
        }
        else
        {
            colors[i].red = 0;
            colors[i].green = 0;
            colors[i].blue = 0;
        }
    }

    _ledStrip.update(colors, 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];

        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;
                }
            }
            else
            {
                colors[i].red = 0;
                colors[i].green = 0;
                colors[i].blue = 0;
            }
        }

        _ledStrip.update(colors, count);

        if (startCount <= j && j < endCount) {
            wait(0.07);
        }
    }
}

void TI_NEOPIXEL_SPI::chase(int count, int bufferCount, rgbColor c1, rgbColor c2)
{
    int virtualCount = count + bufferCount;
    neopixel::Pixel colors[virtualCount];

    for (int j = 0; j < virtualCount; j++)
    {
        colors[j].red = c2.red;
        colors[j].green = c2.green;
        colors[j].blue = c2.blue;
    }

    _ledStrip.update(colors, virtualCount);

    for (int j = 0; j < virtualCount + bufferCount; j++)
    {
        colors[j].red = c1.red;
        colors[j].green = c1.green;
        colors[j].blue = c1.blue;

        colors[j - bufferCount].red = c2.red;
        colors[j - bufferCount].green = c2.green;
        colors[j - bufferCount].blue = c2.blue;

        _ledStrip.update(colors, virtualCount);
        wait(0.05);
    }
}

void TI_NEOPIXEL_SPI::chaseRainbow(int count)
{
    for (int j = 0; j < count; j++)
    {
        neopixel::Pixel colors[count];

        for (int i = 0; i < count; i++)
        {
            if (i <= j)
            {
                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;

                // LEDを節約
                colors[i-4].red = 0;
                colors[i-4].green = 0;
                colors[i-4].blue = 0;
            }
            else
            {
                colors[i].red = 0;
                colors[i].green = 0;
                colors[i].blue = 0;
            }
        }

        _ledStrip.update(colors, count);
        wait(0.06);
    }
}

void TI_NEOPIXEL_SPI::circleRainbow(int count)
{
    for (int j = 0; j < count; j++)
    {
        neopixel::Pixel colors[count];

        for (int i = 0; i < count; i++)
        {
            if (i <= j)
            {
                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;
            }
            else
            {
                colors[i].red = 0;
                colors[i].green = 0;
                colors[i].blue = 0;
            }
        }

        _ledStrip.update(colors, count);
        wait(0.06);
    }
}

rgbColor TI_NEOPIXEL_SPI::convertHsvToRgb(float h, float s, float v)
{
    int i = floor(h * 6);
    float f = h * 6 - i;
    float p = v * (1 - s);
    float q = v * (1 - f * s);
    float t = v * (1 - (1 - f) * s);
    float r = 0, g = 0, b = 0;

    switch (i % 6)
    {
    case 0:
        r = v;
        g = t;
        b = p;
        break;
    case 1:
        r = q;
        g = v;
        b = p;
        break;
    case 2:
        r = p;
        g = v;
        b = t;
        break;
    case 3:
        r = p;
        g = q;
        b = v;
        break;
    case 4:
        r = t;
        g = p;
        b = v;
        break;
    case 5:
        r = v;
        g = p;
        b = q;
        break;
    }

    return (rgbColor){r * 255, g * 255, b * 255};
}