TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Wed Sep 07 21:07:17 2016 +0000
Revision:
28:b452e097da53
Parent:
27:bc79f444883b
Child:
29:a362df191524
WS2811/WS2812 support Library;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mutech 24:f93a61e727a3 1 /* WS281X.h (for LPC82X/STM32F0x/STM32F446/STM32F746xx)
mutech 0:dff187a80020 2 * mbed Microcontroller Library
mutech 0:dff187a80020 3 * Copyright (c) 2016 muetch, t.kuroki, MIT License
mutech 0:dff187a80020 4 *
mutech 0:dff187a80020 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mutech 0:dff187a80020 6 * and associated documentation files (the "Software"), to deal in the Software without restriction,
mutech 0:dff187a80020 7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
mutech 0:dff187a80020 8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
mutech 0:dff187a80020 9 * furnished to do so, subject to the following conditions:
mutech 0:dff187a80020 10 *
mutech 0:dff187a80020 11 * The above copyright notice and this permission notice shall be included in all copies or
mutech 0:dff187a80020 12 * substantial portions of the Software.
mutech 0:dff187a80020 13 *
mutech 0:dff187a80020 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mutech 0:dff187a80020 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mutech 0:dff187a80020 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mutech 0:dff187a80020 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mutech 0:dff187a80020 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mutech 27:bc79f444883b 19 *
mutech 27:bc79f444883b 20 * Rev 0.97 2016-09-07
mutech 28:b452e097da53 21 * Rev 0.98 2016-09-08
mutech 0:dff187a80020 22 */
mutech 0:dff187a80020 23
mutech 0:dff187a80020 24 #pragma once
mutech 0:dff187a80020 25
mutech 0:dff187a80020 26 #ifndef WS281X_H
mutech 0:dff187a80020 27 #define WS281X_H
mutech 0:dff187a80020 28
mutech 0:dff187a80020 29 #include "mbed.h"
mutech 0:dff187a80020 30 #include "ColorLib.h"
mutech 0:dff187a80020 31
mutech 0:dff187a80020 32 //----------------------------------------------------------------------------
mutech 16:01e073c662d7 33 #ifndef MAX_PIXELS
mutech 0:dff187a80020 34 #define MAX_PIXELS 170
mutech 16:01e073c662d7 35 #endif
mutech 0:dff187a80020 36
mutech 0:dff187a80020 37 //----------------------------------------------------------------------------
mutech 0:dff187a80020 38 /**
mutech 24:f93a61e727a3 39 * WS281X
mutech 24:f93a61e727a3 40 */
mutech 0:dff187a80020 41 class WS281X
mutech 0:dff187a80020 42 {
mutech 0:dff187a80020 43 public:
mutech 0:dff187a80020 44 /**
mutech 0:dff187a80020 45 Order of r, g and b bytes
mutech 0:dff187a80020 46 */
mutech 0:dff187a80020 47 enum RGBOrder
mutech 0:dff187a80020 48 {
mutech 28:b452e097da53 49 RGB = 0, RBG, GRB, GBR, BRG, BGR,
mutech 28:b452e097da53 50 GET_ORDER = -1
mutech 0:dff187a80020 51 };
mutech 0:dff187a80020 52
mutech 0:dff187a80020 53 /**
mutech 0:dff187a80020 54 * Initializes the addressable led bus
mutech 0:dff187a80020 55 *
mutech 0:dff187a80020 56 * @param wirePin - The output pin on wich the addressable leds are connected
mutech 0:dff187a80020 57 * @param pinMode - The output pin mode PullUp, PullDown, PullNone, OpenDrain
mutech 20:28fe0d0d081b 58 * @param maxPixels - Number of the addressable leds
mutech 0:dff187a80020 59 * @param RGBOrder - The order in wich the r, g and b bytes are expected
mutech 0:dff187a80020 60 */
mutech 20:28fe0d0d081b 61 WS281X(PinName wirePin, PinMode pinMode = PullNone, int maxPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB);
mutech 27:bc79f444883b 62 WS281X(PinName wirePin, PinMode pinMode = PullNone, RGBColor *buffer = 0, int maxPixels = 0, RGBOrder rgbOrder = WS281X::RGB);
mutech 0:dff187a80020 63 ~WS281X();
mutech 0:dff187a80020 64
mutech 28:b452e097da53 65 WS281X::RGBOrder rgbOrder(WS281X::RGBOrder order = GET_ORDER);
mutech 28:b452e097da53 66 MBED_DEPRECATED("don't setRGBOrder any more, rgbOrder() instead")
mutech 28:b452e097da53 67 void setRGBOrder(RGBOrder order = WS281X::RGB) { rgbOrder(order); }
mutech 28:b452e097da53 68 MBED_DEPRECATED("don't getRGBOrder any more, rgbOrder() instead")
mutech 0:dff187a80020 69 RGBOrder getRGBOrder() { return _rgbOrder; }
mutech 0:dff187a80020 70
mutech 27:bc79f444883b 71 void setPixelBuffer(RGBColor *buffer, int maxPixels);
mutech 27:bc79f444883b 72 int maxPixels() { return _maxPixels; }
mutech 27:bc79f444883b 73 int numPixels(int value = -1);
mutech 27:bc79f444883b 74 MBED_DEPRECATED("don't getMaxPixels any more, maxPixels instead")
mutech 20:28fe0d0d081b 75 int getMaxPixels() { return _maxPixels; }
mutech 27:bc79f444883b 76 MBED_DEPRECATED("don't setNumPixels any more, numPixels instead")
mutech 27:bc79f444883b 77 void setNumPixels(int value) { numPixels(value); }
mutech 27:bc79f444883b 78 MBED_DEPRECATED("don't getNumPixels any more, numPixels instead")
mutech 9:087006b19049 79 int getNumPixels() { return _numPixels; }
mutech 9:087006b19049 80
mutech 27:bc79f444883b 81 void setPixels(int index, RGBColor *color, int len);
mutech 27:bc79f444883b 82 void setPixels(int index, HSVColor *color, int len);
mutech 27:bc79f444883b 83 void setPixels(RGBColor *color, int len) { setPixels(0, color, len); }
mutech 27:bc79f444883b 84 void setPixels(HSVColor *color, int len) { setPixels(0, color, len); }
mutech 27:bc79f444883b 85 MBED_DEPRECATED("don't setColor any more, setPixels instead")
mutech 27:bc79f444883b 86 void setColor(int index, RGBColor *color, int len) { setPixels(index, color, len); }
mutech 27:bc79f444883b 87 MBED_DEPRECATED("don't setColor any more, setPixels instead")
mutech 27:bc79f444883b 88 void setColor(int index, HSVColor *color, int len) { setPixels(index, color, len); }
mutech 27:bc79f444883b 89
mutech 28:b452e097da53 90 void fillPixels(int index, const RGBColor color, int len);
mutech 28:b452e097da53 91 void fillPixels(int index, const HSVColor color, int len);
mutech 28:b452e097da53 92 void fillPixels(int index, const int color, int len) { fillPixels(index, (RGBColor)color, len); }
mutech 28:b452e097da53 93 void fillPixels(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 28:b452e097da53 94 void fillPixels(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 28:b452e097da53 95 void fillPixels(const int color, int len = MAX_PIXELS) { fillPixels(0, (RGBColor)color, len); }
mutech 27:bc79f444883b 96 MBED_DEPRECATED("don't fillColor any more, fillPixels instead")
mutech 28:b452e097da53 97 void fillColor(int index, const RGBColor color, int len) { fillPixels(index, color, len); }
mutech 27:bc79f444883b 98 MBED_DEPRECATED("don't fillColor any more, fillPixels instead")
mutech 28:b452e097da53 99 void fillColor(int index, const int32_t color, int len) { fillPixels(index, (RGBColor)color, len); }
mutech 27:bc79f444883b 100
mutech 27:bc79f444883b 101 void clear(const RGBColor color) { fillPixels(color); }
mutech 27:bc79f444883b 102 void clear(const HSVColor color) { fillPixels(color); }
mutech 28:b452e097da53 103 void clear(const int color = 0) { fillPixels((RGBColor)color); }
mutech 27:bc79f444883b 104
mutech 27:bc79f444883b 105 void repeatPixels(int block_size);
mutech 27:bc79f444883b 106 void repeatPixels(RGBColor *source, int size);
mutech 27:bc79f444883b 107 void repeatPixels(HSVColor *source, int size);
mutech 27:bc79f444883b 108 MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
mutech 27:bc79f444883b 109 void repeatBlock(int block_size) { repeatPixels(block_size); }
mutech 27:bc79f444883b 110 MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
mutech 27:bc79f444883b 111 void repeatBlock(RGBColor *source, int size) { repeatPixels(source, size); }
mutech 27:bc79f444883b 112 MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
mutech 27:bc79f444883b 113 void repeatBlock(HSVColor *source, int size) { repeatPixels(source, size); }
mutech 0:dff187a80020 114
mutech 28:b452e097da53 115 void makeGradation(int index, RGBColor from, RGBColor to, int len);
mutech 28:b452e097da53 116 void makeGradation(RGBColor from, RGBColor to, int len) { makeGradation(0, from, to, len); }
mutech 28:b452e097da53 117
mutech 28:b452e097da53 118 void makeRainbow(int index, HSVColor color, int len, int direction);
mutech 28:b452e097da53 119 void makeRainbow(HSVColor color, int len = MAX_PIXELS, int direction = 1) { makeRainbow(0, color, len, direction); }
mutech 28:b452e097da53 120
mutech 0:dff187a80020 121 void show();
mutech 8:0617f524d67d 122 void show(const RGBColor color);
mutech 0:dff187a80020 123
mutech 2:cc8e091fd975 124 RGBColor operator[](int index) const
mutech 0:dff187a80020 125 {
mutech 0:dff187a80020 126 if ((uint16_t)index < _numPixels)
mutech 0:dff187a80020 127 return _pixels[index];
mutech 0:dff187a80020 128 return _dummyPixel;
mutech 0:dff187a80020 129 }
mutech 0:dff187a80020 130
mutech 2:cc8e091fd975 131 RGBColor& operator[](int index)
mutech 0:dff187a80020 132 {
mutech 0:dff187a80020 133 if ((uint16_t)index < _numPixels)
mutech 0:dff187a80020 134 return _pixels[index];
mutech 0:dff187a80020 135 return _dummyPixel;
mutech 0:dff187a80020 136 }
mutech 0:dff187a80020 137
mutech 27:bc79f444883b 138 operator RGBColor*() const { return _pixels; }
mutech 27:bc79f444883b 139
mutech 0:dff187a80020 140 protected:
mutech 0:dff187a80020 141
mutech 0:dff187a80020 142 private:
mutech 24:f93a61e727a3 143 PinName _wirePin;
mutech 24:f93a61e727a3 144 gpio_t _gpio;
mutech 0:dff187a80020 145 RGBOrder _rgbOrder;
mutech 0:dff187a80020 146 int _1st, _2nd, _3rd;
mutech 0:dff187a80020 147
mutech 24:f93a61e727a3 148 bool _buf_owner;
mutech 20:28fe0d0d081b 149 uint16_t _maxPixels;
mutech 0:dff187a80020 150 uint16_t _numPixels;
mutech 2:cc8e091fd975 151 RGBColor *_pixels;
mutech 0:dff187a80020 152 RGBColor _dummyPixel;
mutech 0:dff187a80020 153
mutech 0:dff187a80020 154 #if defined(TARGET_NXP)
mutech 0:dff187a80020 155 typedef uint32_t regsize_t;
mutech 0:dff187a80020 156 #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1)
mutech 0:dff187a80020 157 typedef uint32_t regsize_t;
mutech 0:dff187a80020 158 #elif defined(TARGET_STM)
mutech 0:dff187a80020 159 typedef uint16_t regsize_t;
mutech 0:dff187a80020 160 #else
mutech 0:dff187a80020 161 #error "not supported CPU!!"
mutech 0:dff187a80020 162 #endif
mutech 0:dff187a80020 163 #if defined(TARGET_STM)
mutech 0:dff187a80020 164 void pin_mode_ex(PinName pin, PinMode mode);
mutech 0:dff187a80020 165 #endif
mutech 0:dff187a80020 166 void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value);
mutech 0:dff187a80020 167 };
mutech 0:dff187a80020 168
mutech 0:dff187a80020 169 //----------------------------------------------------------------------------
mutech 0:dff187a80020 170 #endif // end of WS281X_H