TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Thu Oct 13 23:51:20 2016 +0000
Revision:
32:64c391617f6c
Parent:
30:59b70f91b471
Child:
46:2374900f8845
WS281X

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 29:a362df191524 50 READ_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 29:a362df191524 65 WS281X::RGBOrder rgbOrder(WS281X::RGBOrder order = READ_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 30:59b70f91b471 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 30:59b70f91b471 95 void fillPixels(const int color, int len = MAX_PIXELS) { fillPixels(0, (RGBColor)color, len); }
mutech 30:59b70f91b471 96 MBED_DEPRECATED("don't fillColor any more, fillPixels instead")
mutech 30:59b70f91b471 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 30:59b70f91b471 99 void fillColor(int index, const int32_t color, int len) { fillPixels(index, (RGBColor)color, len); }
mutech 30:59b70f91b471 100
mutech 30:59b70f91b471 101 void fill(int index, const RGBColor color, int len) { fillPixels(index, color, len); }
mutech 30:59b70f91b471 102 void fill(int index, const HSVColor color, int len) { fillPixels(index, color, len); }
mutech 30:59b70f91b471 103 void fill(int index, const int color, int len) { fillPixels(index, (RGBColor)color, len); }
mutech 30:59b70f91b471 104 void fill(const RGBColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 30:59b70f91b471 105 void fill(const HSVColor color, int len = MAX_PIXELS) { fillPixels(0, color, len); }
mutech 30:59b70f91b471 106 void fill(const int color, int len = MAX_PIXELS) { fillPixels(0, (RGBColor)color, len); }
mutech 27:bc79f444883b 107
mutech 27:bc79f444883b 108 void clear(const RGBColor color) { fillPixels(color); }
mutech 27:bc79f444883b 109 void clear(const HSVColor color) { fillPixels(color); }
mutech 28:b452e097da53 110 void clear(const int color = 0) { fillPixels((RGBColor)color); }
mutech 27:bc79f444883b 111
mutech 27:bc79f444883b 112 void repeatPixels(int block_size);
mutech 27:bc79f444883b 113 void repeatPixels(RGBColor *source, int size);
mutech 27:bc79f444883b 114 void repeatPixels(HSVColor *source, int size);
mutech 27:bc79f444883b 115 MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
mutech 27:bc79f444883b 116 void repeatBlock(int block_size) { repeatPixels(block_size); }
mutech 27:bc79f444883b 117 MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
mutech 27:bc79f444883b 118 void repeatBlock(RGBColor *source, int size) { repeatPixels(source, size); }
mutech 27:bc79f444883b 119 MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead")
mutech 27:bc79f444883b 120 void repeatBlock(HSVColor *source, int size) { repeatPixels(source, size); }
mutech 0:dff187a80020 121
mutech 28:b452e097da53 122 void makeGradation(int index, RGBColor from, RGBColor to, int len);
mutech 28:b452e097da53 123 void makeGradation(RGBColor from, RGBColor to, int len) { makeGradation(0, from, to, len); }
mutech 28:b452e097da53 124
mutech 28:b452e097da53 125 void makeRainbow(int index, HSVColor color, int len, int direction);
mutech 28:b452e097da53 126 void makeRainbow(HSVColor color, int len = MAX_PIXELS, int direction = 1) { makeRainbow(0, color, len, direction); }
mutech 28:b452e097da53 127
mutech 0:dff187a80020 128 void show();
mutech 8:0617f524d67d 129 void show(const RGBColor color);
mutech 0:dff187a80020 130
mutech 2:cc8e091fd975 131 RGBColor operator[](int index) const
mutech 0:dff187a80020 132 {
mutech 32:64c391617f6c 133 if (_pixels && (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 2:cc8e091fd975 138 RGBColor& operator[](int index)
mutech 0:dff187a80020 139 {
mutech 32:64c391617f6c 140 if (_pixels && (uint16_t)index < _numPixels)
mutech 0:dff187a80020 141 return _pixels[index];
mutech 0:dff187a80020 142 return _dummyPixel;
mutech 0:dff187a80020 143 }
mutech 0:dff187a80020 144
mutech 27:bc79f444883b 145 operator RGBColor*() const { return _pixels; }
mutech 27:bc79f444883b 146
mutech 0:dff187a80020 147 protected:
mutech 0:dff187a80020 148
mutech 0:dff187a80020 149 private:
mutech 24:f93a61e727a3 150 PinName _wirePin;
mutech 24:f93a61e727a3 151 gpio_t _gpio;
mutech 0:dff187a80020 152 RGBOrder _rgbOrder;
mutech 0:dff187a80020 153 int _1st, _2nd, _3rd;
mutech 0:dff187a80020 154
mutech 32:64c391617f6c 155 bool _owned_buffer;
mutech 20:28fe0d0d081b 156 uint16_t _maxPixels;
mutech 0:dff187a80020 157 uint16_t _numPixels;
mutech 2:cc8e091fd975 158 RGBColor *_pixels;
mutech 0:dff187a80020 159 RGBColor _dummyPixel;
mutech 0:dff187a80020 160
mutech 0:dff187a80020 161 #if defined(TARGET_NXP)
mutech 0:dff187a80020 162 typedef uint32_t regsize_t;
mutech 0:dff187a80020 163 #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1)
mutech 0:dff187a80020 164 typedef uint32_t regsize_t;
mutech 0:dff187a80020 165 #elif defined(TARGET_STM)
mutech 0:dff187a80020 166 typedef uint16_t regsize_t;
mutech 0:dff187a80020 167 #else
mutech 0:dff187a80020 168 #error "not supported CPU!!"
mutech 0:dff187a80020 169 #endif
mutech 0:dff187a80020 170 #if defined(TARGET_STM)
mutech 0:dff187a80020 171 void pin_mode_ex(PinName pin, PinMode mode);
mutech 0:dff187a80020 172 #endif
mutech 0:dff187a80020 173 void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value);
mutech 0:dff187a80020 174 };
mutech 0:dff187a80020 175
mutech 0:dff187a80020 176 //----------------------------------------------------------------------------
mutech 0:dff187a80020 177 #endif // end of WS281X_H