TLIGHT_PRODUCTS / WS281X

PixelBuffer.h

Committer:
mutech
Date:
2016-09-10
Revision:
30:59b70f91b471
Parent:
29:a362df191524
Child:
32:64c391617f6c

File content as of revision 30:59b70f91b471:

/* PixelBuffer.h
 * mbed Microcontroller Library
 * Copyright (c) 2016 muetch, t.kuroki
 * Allrights reserved.
 *
 * Rev 0.97 2016-09-07
 * Rev 0.98 2016-09-08
 */

#pragma once

#ifndef PIXELBUFFER_H
#define PIXELBUFFER_H

#ifdef _WIN32
#include <stdint.h>
#include <string.h>
#else
#include "mbed.h"
#endif
#include "ColorLib.h"

//----------------------------------------------------------------------------
#ifndef MAX_PIXELS
#define MAX_PIXELS      170
#endif

#ifndef nullptr
#define nullptr         (0)
#endif

//----------------------------------------------------------------------------
/**
 * RGBPixels
 */
class RGBPixels
{
public:
    /**
    *   Initializes the addressable led bus
    *
    *   @param Buffer - The Pixel array buffer address.
    *   @param maxPixels - Number of the addressable leds
    */
    RGBPixels(RGBColor *buffer = nullptr, int maxPixels = MAX_PIXELS);
    RGBPixels(int maxPixels = MAX_PIXELS);
    ~RGBPixels();

    void setPixelBuffer(RGBColor *buffer, int maxPixels);
    int  maxPixels() { return _maxPixels; }
    int  numPixels(int value = -1);

    void setPixels(int index, RGBColor *color, int len);
    void setPixels(int index, HSVColor *color, int len);
    void setPixels(RGBColor *color, int len) { setPixels(0, color, len); }
    void setPixels(HSVColor *color, int len) { setPixels(0, color, len); }

    void fillPixels(int index, const RGBColor color, int len);
    void fillPixels(int index, const HSVColor color, int len);
    void fillPixels(int index, const int color, int len)        { fillPixels(index, (RGBColor)color, len); }
    void fillPixels(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fillPixels(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fillPixels(const int color, int len = MAX_PIXELS)      { fillPixels(0, (RGBColor)color, len); }

    void fill(int index, const RGBColor color, int len)   { fillPixels(index, color, len); }
    void fill(int index, const HSVColor color, int len)   { fillPixels(index, color, len); }
    void fill(int index, const int color, int len)        { fillPixels(index, (RGBColor)color, len); }
    void fill(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fill(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fill(const int color, int len = MAX_PIXELS)      { fillPixels(0, (RGBColor)color, len); }

    void clear(const RGBColor color) { fillPixels(color); }
    void clear(const HSVColor color) { fillPixels(color); }
    void clear(const int color = 0)  { fillPixels((RGBColor)color); }

    void repeatPixels(int block_size);
    void repeatPixels(RGBColor *source, int size);
    void repeatPixels(HSVColor *source, int size);

    void makeGradation(int index, RGBColor from, RGBColor to, int len);
    void makeGradation(RGBColor from, RGBColor to, int len = MAX_PIXELS) { makeGradation(0, from, to, len); }

    void makeRainbow(int index, HSVColor color, int len, int direction);
    void makeRainbow(HSVColor color, int len = MAX_PIXELS, int direction = 1) { makeRainbow(0, color, len, direction); }

    RGBColor operator[](int index) const
    {
        if ((uint16_t)index < _numPixels)
            return _pixels[index];
        return _dummyPixel;
    }

    RGBColor& operator[](int index)
    {
        if ((uint16_t)index < _numPixels)
            return _pixels[index];
        return _dummyPixel;
    }

    RGBPixels& operator=(const RGBPixels& rhs);

    operator RGBColor*() const { return _pixels; }

protected:
    uint16_t _maxPixels;
    uint16_t _numPixels;
    RGBColor *_pixels;

private:
    bool     _buf_owner;
    RGBColor _dummyPixel;

};

//----------------------------------------------------------------------------
/**
 * HSVPixels
 */
class HSVPixels
{
public:
    /**
    *   Initializes the addressable led bus
    *
    *   @param Buffer - The Pixel array buffer address.
    *   @param maxPixels - Number of the addressable leds
    */
    HSVPixels(HSVColor *buffer = nullptr, int maxPixels = MAX_PIXELS);
    HSVPixels(int maxPixels = MAX_PIXELS);
    ~HSVPixels();

    void setPixelBuffer(HSVColor *buffer, int maxPixels);
    int  maxPixels() { return _maxPixels; }
    int  numPixels(int value = -1);

    void setPixels(int index, HSVColor *color, int len);
    void setPixels(int index, RGBColor *color, int len);
    void setPixels(HSVColor *color, int len) { setPixels(0, color, len); }
    void setPixels(RGBColor *color, int len) { setPixels(0, color, len); }

    void fillPixels(int index, const HSVColor color, int len);
    void fillPixels(int index, const RGBColor color, int len);
    void fillPixels(int index, const int color, int len)        { fillPixels(index, (RGBColor)color, len); }
    void fillPixels(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fillPixels(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fillPixels(const int color, int len = MAX_PIXELS)      { fillPixels(0, (RGBColor)color, len); }

    void fill(int index, const HSVColor color, int len)   { fillPixels(index, color, len); }
    void fill(int index, const RGBColor color, int len)   { fillPixels(index, color, len); }
    void fill(int index, const int color, int len)        { fillPixels(index, (RGBColor)color, len); }
    void fill(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fill(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
    void fill(const int color, int len = MAX_PIXELS)      { fillPixels(0, (RGBColor)color, len); }

    void clear(const HSVColor color) { fillPixels(color); }
    void clear(const RGBColor color) { fillPixels(color); }
    void clear(const int color = 0)  { fillPixels((RGBColor)color); }

    void repeatPixels(int block_size);
    void repeatPixels(HSVColor *source, int size);
    void repeatPixels(RGBColor *source, int size);

    void makeGradation(int index, HSVColor from, HSVColor to, int len);
    void makeGradation(HSVColor from, HSVColor to, int len = MAX_PIXELS) { makeGradation(0, from, to, len); }

    void makeRainbow(int index, HSVColor color, int len, int direction);
    void makeRainbow(HSVColor color, int len = MAX_PIXELS, int direction = 1) { makeRainbow(0, color, len, direction); }

    HSVColor operator[](int index) const
    {
        if ((uint16_t)index < _numPixels)
            return _pixels[index];
        return _dummyPixel;
    }

    HSVColor& operator[](int index)
    {
        if ((uint16_t)index < _numPixels)
            return _pixels[index];
        return _dummyPixel;
    }

    HSVPixels& operator=(const HSVPixels& rhs);

    operator HSVColor*() const { return _pixels; }

protected:
    uint16_t _maxPixels;
    uint16_t _numPixels;
    HSVColor *_pixels;

private:
    bool     _buf_owner;
    HSVColor _dummyPixel;

};

//----------------------------------------------------------------------------
#endif      // end of PIXELBUFFER_H