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@27:bc79f444883b, 2016-09-06 (annotated)
- Committer:
- mutech
- Date:
- Tue Sep 06 22:12:59 2016 +0000
- Revision:
- 27:bc79f444883b
- Parent:
- 24:f93a61e727a3
- Child:
- 28:b452e097da53
WS2811/WS2812 support Library;
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:dff187a80020 | 21 | */ |
mutech | 0:dff187a80020 | 22 | |
mutech | 0:dff187a80020 | 23 | #pragma once |
mutech | 0:dff187a80020 | 24 | |
mutech | 0:dff187a80020 | 25 | #ifndef WS281X_H |
mutech | 0:dff187a80020 | 26 | #define WS281X_H |
mutech | 0:dff187a80020 | 27 | |
mutech | 0:dff187a80020 | 28 | #include "mbed.h" |
mutech | 0:dff187a80020 | 29 | #include "ColorLib.h" |
mutech | 0:dff187a80020 | 30 | |
mutech | 0:dff187a80020 | 31 | //---------------------------------------------------------------------------- |
mutech | 16:01e073c662d7 | 32 | #ifndef MAX_PIXELS |
mutech | 0:dff187a80020 | 33 | #define MAX_PIXELS 170 |
mutech | 16:01e073c662d7 | 34 | #endif |
mutech | 0:dff187a80020 | 35 | |
mutech | 0:dff187a80020 | 36 | //---------------------------------------------------------------------------- |
mutech | 0:dff187a80020 | 37 | /** |
mutech | 24:f93a61e727a3 | 38 | * WS281X |
mutech | 24:f93a61e727a3 | 39 | */ |
mutech | 0:dff187a80020 | 40 | class WS281X |
mutech | 0:dff187a80020 | 41 | { |
mutech | 0:dff187a80020 | 42 | public: |
mutech | 0:dff187a80020 | 43 | /** |
mutech | 0:dff187a80020 | 44 | Order of r, g and b bytes |
mutech | 0:dff187a80020 | 45 | */ |
mutech | 0:dff187a80020 | 46 | enum RGBOrder |
mutech | 0:dff187a80020 | 47 | { |
mutech | 0:dff187a80020 | 48 | RGB, RBG, GRB, GBR, BRG, BGR |
mutech | 0:dff187a80020 | 49 | }; |
mutech | 0:dff187a80020 | 50 | |
mutech | 0:dff187a80020 | 51 | /** |
mutech | 0:dff187a80020 | 52 | * Initializes the addressable led bus |
mutech | 0:dff187a80020 | 53 | * |
mutech | 0:dff187a80020 | 54 | * @param wirePin - The output pin on wich the addressable leds are connected |
mutech | 0:dff187a80020 | 55 | * @param pinMode - The output pin mode PullUp, PullDown, PullNone, OpenDrain |
mutech | 20:28fe0d0d081b | 56 | * @param maxPixels - Number of the addressable leds |
mutech | 0:dff187a80020 | 57 | * @param RGBOrder - The order in wich the r, g and b bytes are expected |
mutech | 0:dff187a80020 | 58 | */ |
mutech | 20:28fe0d0d081b | 59 | WS281X(PinName wirePin, PinMode pinMode = PullNone, int maxPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB); |
mutech | 27:bc79f444883b | 60 | WS281X(PinName wirePin, PinMode pinMode = PullNone, RGBColor *buffer = 0, int maxPixels = 0, RGBOrder rgbOrder = WS281X::RGB); |
mutech | 0:dff187a80020 | 61 | ~WS281X(); |
mutech | 0:dff187a80020 | 62 | |
mutech | 0:dff187a80020 | 63 | RGBOrder getRGBOrder() { return _rgbOrder; } |
mutech | 0:dff187a80020 | 64 | void setRGBOrder(RGBOrder rgbOrder = WS281X::RGB); |
mutech | 0:dff187a80020 | 65 | |
mutech | 27:bc79f444883b | 66 | void setPixelBuffer(RGBColor *buffer, int maxPixels); |
mutech | 27:bc79f444883b | 67 | int maxPixels() { return _maxPixels; } |
mutech | 27:bc79f444883b | 68 | int numPixels(int value = -1); |
mutech | 27:bc79f444883b | 69 | MBED_DEPRECATED("don't getMaxPixels any more, maxPixels instead") |
mutech | 20:28fe0d0d081b | 70 | int getMaxPixels() { return _maxPixels; } |
mutech | 27:bc79f444883b | 71 | MBED_DEPRECATED("don't setNumPixels any more, numPixels instead") |
mutech | 27:bc79f444883b | 72 | void setNumPixels(int value) { numPixels(value); } |
mutech | 27:bc79f444883b | 73 | MBED_DEPRECATED("don't getNumPixels any more, numPixels instead") |
mutech | 9:087006b19049 | 74 | int getNumPixels() { return _numPixels; } |
mutech | 9:087006b19049 | 75 | |
mutech | 27:bc79f444883b | 76 | void setPixels(int index, RGBColor *color, int len); |
mutech | 27:bc79f444883b | 77 | void setPixels(int index, HSVColor *color, int len); |
mutech | 27:bc79f444883b | 78 | void setPixels(RGBColor *color, int len) { setPixels(0, color, len); } |
mutech | 27:bc79f444883b | 79 | void setPixels(HSVColor *color, int len) { setPixels(0, color, len); } |
mutech | 27:bc79f444883b | 80 | MBED_DEPRECATED("don't setColor any more, setPixels instead") |
mutech | 27:bc79f444883b | 81 | void setColor(int index, RGBColor *color, int len) { setPixels(index, color, len); } |
mutech | 27:bc79f444883b | 82 | MBED_DEPRECATED("don't setColor any more, setPixels instead") |
mutech | 27:bc79f444883b | 83 | void setColor(int index, HSVColor *color, int len) { setPixels(index, color, len); } |
mutech | 27:bc79f444883b | 84 | |
mutech | 27:bc79f444883b | 85 | void fillPixels(const RGBColor color, int index, int len); |
mutech | 27:bc79f444883b | 86 | void fillPixels(const HSVColor color, int index, int len); |
mutech | 27:bc79f444883b | 87 | void fillPixels(const int color, int index, int len) { fillPixels((RGBColor)color, index, len); } |
mutech | 27:bc79f444883b | 88 | void fillPixels(const RGBColor color, int len = MAX_PIXELS) { fillPixels(color, 0, len); } |
mutech | 27:bc79f444883b | 89 | void fillPixels(const HSVColor color, int len = MAX_PIXELS) { fillPixels(color, 0, len); } |
mutech | 27:bc79f444883b | 90 | void fillPixels(const int color, int len = MAX_PIXELS) { fillPixels((RGBColor)color, 0, len); } |
mutech | 27:bc79f444883b | 91 | MBED_DEPRECATED("don't fillColor any more, fillPixels instead") |
mutech | 27:bc79f444883b | 92 | void fillColor(int index, const RGBColor color, int len) { fillPixels(color, index, len); } |
mutech | 27:bc79f444883b | 93 | MBED_DEPRECATED("don't fillColor any more, fillPixels instead") |
mutech | 27:bc79f444883b | 94 | void fillColor(int index, const int32_t color, int len) { fillPixels((RGBColor)color, index, len); } |
mutech | 27:bc79f444883b | 95 | |
mutech | 27:bc79f444883b | 96 | void clear(const RGBColor color) { fillPixels(color); } |
mutech | 27:bc79f444883b | 97 | void clear(const HSVColor color) { fillPixels(color); } |
mutech | 27:bc79f444883b | 98 | void clear(const int color = 0) { fillPixels((RGBColor)color); } |
mutech | 27:bc79f444883b | 99 | |
mutech | 27:bc79f444883b | 100 | void repeatPixels(int block_size); |
mutech | 27:bc79f444883b | 101 | void repeatPixels(RGBColor *source, int size); |
mutech | 27:bc79f444883b | 102 | void repeatPixels(HSVColor *source, int size); |
mutech | 27:bc79f444883b | 103 | MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead") |
mutech | 27:bc79f444883b | 104 | void repeatBlock(int block_size) { repeatPixels(block_size); } |
mutech | 27:bc79f444883b | 105 | MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead") |
mutech | 27:bc79f444883b | 106 | void repeatBlock(RGBColor *source, int size) { repeatPixels(source, size); } |
mutech | 27:bc79f444883b | 107 | MBED_DEPRECATED("don't repeatBlock any more, repeatPixels instead") |
mutech | 27:bc79f444883b | 108 | void repeatBlock(HSVColor *source, int size) { repeatPixels(source, size); } |
mutech | 0:dff187a80020 | 109 | |
mutech | 0:dff187a80020 | 110 | void show(); |
mutech | 8:0617f524d67d | 111 | void show(const RGBColor color); |
mutech | 0:dff187a80020 | 112 | |
mutech | 2:cc8e091fd975 | 113 | RGBColor operator[](int index) const |
mutech | 0:dff187a80020 | 114 | { |
mutech | 0:dff187a80020 | 115 | if ((uint16_t)index < _numPixels) |
mutech | 0:dff187a80020 | 116 | return _pixels[index]; |
mutech | 0:dff187a80020 | 117 | return _dummyPixel; |
mutech | 0:dff187a80020 | 118 | } |
mutech | 0:dff187a80020 | 119 | |
mutech | 2:cc8e091fd975 | 120 | RGBColor& operator[](int index) |
mutech | 0:dff187a80020 | 121 | { |
mutech | 0:dff187a80020 | 122 | if ((uint16_t)index < _numPixels) |
mutech | 0:dff187a80020 | 123 | return _pixels[index]; |
mutech | 0:dff187a80020 | 124 | return _dummyPixel; |
mutech | 0:dff187a80020 | 125 | } |
mutech | 0:dff187a80020 | 126 | |
mutech | 27:bc79f444883b | 127 | operator RGBColor*() const { return _pixels; } |
mutech | 27:bc79f444883b | 128 | |
mutech | 0:dff187a80020 | 129 | protected: |
mutech | 0:dff187a80020 | 130 | |
mutech | 0:dff187a80020 | 131 | private: |
mutech | 24:f93a61e727a3 | 132 | PinName _wirePin; |
mutech | 24:f93a61e727a3 | 133 | gpio_t _gpio; |
mutech | 0:dff187a80020 | 134 | RGBOrder _rgbOrder; |
mutech | 0:dff187a80020 | 135 | int _1st, _2nd, _3rd; |
mutech | 0:dff187a80020 | 136 | |
mutech | 24:f93a61e727a3 | 137 | bool _buf_owner; |
mutech | 20:28fe0d0d081b | 138 | uint16_t _maxPixels; |
mutech | 0:dff187a80020 | 139 | uint16_t _numPixels; |
mutech | 2:cc8e091fd975 | 140 | RGBColor *_pixels; |
mutech | 0:dff187a80020 | 141 | RGBColor _dummyPixel; |
mutech | 0:dff187a80020 | 142 | |
mutech | 0:dff187a80020 | 143 | #if defined(TARGET_NXP) |
mutech | 0:dff187a80020 | 144 | typedef uint32_t regsize_t; |
mutech | 0:dff187a80020 | 145 | #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1) |
mutech | 0:dff187a80020 | 146 | typedef uint32_t regsize_t; |
mutech | 0:dff187a80020 | 147 | #elif defined(TARGET_STM) |
mutech | 0:dff187a80020 | 148 | typedef uint16_t regsize_t; |
mutech | 0:dff187a80020 | 149 | #else |
mutech | 0:dff187a80020 | 150 | #error "not supported CPU!!" |
mutech | 0:dff187a80020 | 151 | #endif |
mutech | 0:dff187a80020 | 152 | #if defined(TARGET_STM) |
mutech | 0:dff187a80020 | 153 | void pin_mode_ex(PinName pin, PinMode mode); |
mutech | 0:dff187a80020 | 154 | #endif |
mutech | 0:dff187a80020 | 155 | void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value); |
mutech | 0:dff187a80020 | 156 | }; |
mutech | 0:dff187a80020 | 157 | |
mutech | 0:dff187a80020 | 158 | //---------------------------------------------------------------------------- |
mutech | 0:dff187a80020 | 159 | #endif // end of WS281X_H |