Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WS281X.h
- Committer:
- mutech
- Date:
- 2016-08-25
- Revision:
- 20:28fe0d0d081b
- Parent:
- 16:01e073c662d7
- Child:
- 21:77275089d837
File content as of revision 20:28fe0d0d081b:
/* 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"
//----------------------------------------------------------------------------
#ifndef MAX_PIXELS
#define MAX_PIXELS 170
#endif
//----------------------------------------------------------------------------
/**
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 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();
RGBOrder getRGBOrder() { return _rgbOrder; }
void setRGBOrder(RGBOrder rgbOrder = WS281X::RGB);
void setPixelBuffer(RGBColor *Buffer, int maxPixels);
int getMaxPixels() { return _maxPixels; }
void setNumPixels(int numPixels);
int getNumPixels() { return _numPixels; }
void setColor(int index, RGBColor *color, int len);
void fillColor(int index, const RGBColor color, int len);
void repeatBlock(int block_size);
void clear(const RGBColor color);
void clear(const int color = 0) { clear((RGBColor)color); }
void show();
void show(const RGBColor color);
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;
}
protected:
private:
PinName _wirePin;
gpio_t _gpio;
RGBOrder _rgbOrder;
int _1st, _2nd, _3rd;
bool _buf_owner;
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