TLIGHT_PRODUCTS / WS281X

WS281X.h

Committer:
mutech
Date:
2016-07-26
Revision:
0:dff187a80020
Child:
2:cc8e091fd975

File content as of revision 0:dff187a80020:

/* WS281X.h (for LPC82X/STM32F0x/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.
 */

#pragma once

#ifndef WS281X_H
#define WS281X_H

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

//----------------------------------------------------------------------------
#define MAX_PIXELS      170

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

    /**
    *   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 numPixels - 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 numPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB);
    ~WS281X();

    RGBOrder getRGBOrder() { return _rgbOrder; }
    void setRGBOrder(RGBOrder rgbOrder = WS281X::RGB);

    void repeatBlock(int block_size);
    void clear(uint32_t color = 0);
    void clear(RGBColor color) { clear((uint32_t)color); }

    void show();
    void show(uint32_t color);
    void show(RGBColor color) { show((uint32_t)color); }

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

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

protected:

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

    uint16_t _numPixels;
    RGBColor _dummyPixel;
    RGBColor *_pixels;

#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
#if defined(TARGET_STM32F7)
    void _delay(int value);
#endif
    void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value);
};

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