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-09
Revision:
0:c28aa7d4f97e
Child:
2:0148ac5c90fa

File content as of revision 0:c28aa7d4f97e:

#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)
{
    neopixel::Pixel colors[count];

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

    _ledStrip.update(colors, count);
}

void TI_NEOPIXEL_SPI::changeColor(int count, rgbColor rgbColor)
{
    neopixel::Pixel colors[count];

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

    _ledStrip.update(colors, count);
}

void TI_NEOPIXEL_SPI::changePointColor(int count, rgbColor topColor, rgbColor bottomColor)
{
    neopixel::Pixel colors[count];

    for (int i = 0; i < count; i++)
    {
        if (i == 0)
        {
            colors[i].red = topColor.red;
            colors[i].green = topColor.green;
            colors[i].blue = topColor.blue;
        }
        else if (i == count / 2)
        {
            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, rgbColor rgbColor)
{
    for (int j = 0; j < count; j++)
    {
        neopixel::Pixel colors[count];

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