TLIGHT_PRODUCTS / WS281X

WS281X.h

Committer:
mutech
Date:
2016-10-13
Revision:
32:64c391617f6c
Parent:
30:59b70f91b471
Child:
46:2374900f8845

File content as of revision 32:64c391617f6c:

/* WS281X.h (for LPC82X/STM32F0x/STM32F446/STM32F746xx)
 * mbed Microcontroller Library
 * Copyright (c) 2016 muetch, t.kuroki, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Rev 0.97 2016-09-07
 * Rev 0.98 2016-09-08
 */

#pragma once

#ifndef WS281X_H
#define WS281X_H

#include "mbed.h"
#include "ColorLib.h"

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

//----------------------------------------------------------------------------
/**
 * WS281X
 */
class WS281X
{
public:
    /**
        Order of r, g and b bytes
    */
    enum RGBOrder
    {
        RGB = 0, RBG, GRB, GBR, BRG, BGR,
        READ_ORDER = -1
    };

    /**
    *   Initializes the addressable led bus
    *
    *   @param wirePin - The output pin on wich the addressable leds are connected
    *   @param pinMode - The output pin mode PullUp, PullDown, PullNone, OpenDrain
    *   @param maxPixels - Number of the addressable leds
    *   @param RGBOrder - The order in wich the r, g and b bytes are expected
    */
    WS281X(PinName wirePin, PinMode pinMode = PullNone, int maxPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB);
    WS281X(PinName wirePin, PinMode pinMode = PullNone, RGBColor *buffer = 0, int maxPixels = 0, RGBOrder rgbOrder = WS281X::RGB);
    ~WS281X();

    WS281X::RGBOrder rgbOrder(WS281X::RGBOrder order = READ_ORDER);
    MBED_DEPRECATED("don't setRGBOrder any more, rgbOrder() instead")
    void setRGBOrder(RGBOrder order = WS281X::RGB) { rgbOrder(order); }
    MBED_DEPRECATED("don't getRGBOrder any more, rgbOrder() instead")
    RGBOrder getRGBOrder() { return _rgbOrder; }

    void setPixelBuffer(RGBColor *buffer, int maxPixels);
    int maxPixels() { return _maxPixels; }
    int numPixels(int value = -1);
    MBED_DEPRECATED("don't getMaxPixels any more, maxPixels instead")
    int getMaxPixels() { return _maxPixels; }
    MBED_DEPRECATED("don't setNumPixels any more, numPixels instead")
    void setNumPixels(int value) { numPixels(value); }
    MBED_DEPRECATED("don't getNumPixels any more, numPixels instead")
    int getNumPixels() { return _numPixels; }

    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); }
    MBED_DEPRECATED("don't setColor any more, setPixels instead")
    void setColor(int index, RGBColor *color, int len) { setPixels(index, color, len); }
    MBED_DEPRECATED("don't setColor any more, setPixels instead")
    void setColor(int index, HSVColor *color, int len) { setPixels(index, 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); }
    MBED_DEPRECATED("don't fillColor any more, fillPixels instead")
    void fillColor(int index, const RGBColor color, int len)    { fillPixels(index, color, len); }
    MBED_DEPRECATED("don't fillColor any more, fillPixels instead")
    void fillColor(int index, const int32_t color, int len)     { fillPixels(index, (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);
    MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
    void repeatBlock(int block_size) { repeatPixels(block_size); }
    MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
    void repeatBlock(RGBColor *source, int size) { repeatPixels(source, size); }
    MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
    void repeatBlock(HSVColor *source, int size) { repeatPixels(source, size); }

    void makeGradation(int index, RGBColor from, RGBColor to, int len);
    void makeGradation(RGBColor from, RGBColor to, int len) { 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); }

    void show();
    void show(const RGBColor color);

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

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

    operator RGBColor*() const { return _pixels; }

protected:

private:
    PinName  _wirePin;
    gpio_t   _gpio;
    RGBOrder _rgbOrder;
    int _1st, _2nd, _3rd;

    bool     _owned_buffer;
    uint16_t _maxPixels;
    uint16_t _numPixels;
    RGBColor *_pixels;
    RGBColor _dummyPixel;

#if defined(TARGET_NXP)
    typedef uint32_t regsize_t;
#elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1)
    typedef uint32_t regsize_t;
#elif defined(TARGET_STM)
    typedef uint16_t regsize_t;
#else
#error "not supported CPU!!"
#endif
#if defined(TARGET_STM)
    void pin_mode_ex(PinName pin, PinMode mode);
#endif
    void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value);
};

//----------------------------------------------------------------------------
#endif      // end of WS281X_H