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@24:f93a61e727a3, 2016-08-27 (annotated)
- Committer:
- mutech
- Date:
- Sat Aug 27 19:53:37 2016 +0000
- Revision:
- 24:f93a61e727a3
- Parent:
- 21:77275089d837
- Child:
- 27:bc79f444883b
WS2811/WS2812 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 | 0:dff187a80020 | 19 | */ |
| mutech | 0:dff187a80020 | 20 | |
| mutech | 0:dff187a80020 | 21 | #pragma once |
| mutech | 0:dff187a80020 | 22 | |
| mutech | 0:dff187a80020 | 23 | #ifndef WS281X_H |
| mutech | 0:dff187a80020 | 24 | #define WS281X_H |
| mutech | 0:dff187a80020 | 25 | |
| mutech | 0:dff187a80020 | 26 | #include "mbed.h" |
| mutech | 0:dff187a80020 | 27 | #include "ColorLib.h" |
| mutech | 0:dff187a80020 | 28 | |
| mutech | 0:dff187a80020 | 29 | //---------------------------------------------------------------------------- |
| mutech | 16:01e073c662d7 | 30 | #ifndef MAX_PIXELS |
| mutech | 0:dff187a80020 | 31 | #define MAX_PIXELS 170 |
| mutech | 16:01e073c662d7 | 32 | #endif |
| mutech | 0:dff187a80020 | 33 | |
| mutech | 0:dff187a80020 | 34 | //---------------------------------------------------------------------------- |
| mutech | 0:dff187a80020 | 35 | /** |
| mutech | 24:f93a61e727a3 | 36 | * WS281X |
| mutech | 24:f93a61e727a3 | 37 | */ |
| mutech | 0:dff187a80020 | 38 | class WS281X |
| mutech | 0:dff187a80020 | 39 | { |
| mutech | 0:dff187a80020 | 40 | public: |
| mutech | 0:dff187a80020 | 41 | /** |
| mutech | 0:dff187a80020 | 42 | Order of r, g and b bytes |
| mutech | 0:dff187a80020 | 43 | */ |
| mutech | 0:dff187a80020 | 44 | enum RGBOrder |
| mutech | 0:dff187a80020 | 45 | { |
| mutech | 0:dff187a80020 | 46 | RGB, RBG, GRB, GBR, BRG, BGR |
| mutech | 0:dff187a80020 | 47 | }; |
| mutech | 0:dff187a80020 | 48 | |
| mutech | 0:dff187a80020 | 49 | /** |
| mutech | 0:dff187a80020 | 50 | * Initializes the addressable led bus |
| mutech | 0:dff187a80020 | 51 | * |
| mutech | 0:dff187a80020 | 52 | * @param wirePin - The output pin on wich the addressable leds are connected |
| mutech | 0:dff187a80020 | 53 | * @param pinMode - The output pin mode PullUp, PullDown, PullNone, OpenDrain |
| mutech | 20:28fe0d0d081b | 54 | * @param maxPixels - Number of the addressable leds |
| mutech | 0:dff187a80020 | 55 | * @param RGBOrder - The order in wich the r, g and b bytes are expected |
| mutech | 0:dff187a80020 | 56 | */ |
| mutech | 20:28fe0d0d081b | 57 | WS281X(PinName wirePin, PinMode pinMode = PullNone, int maxPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB); |
| mutech | 20:28fe0d0d081b | 58 | WS281X(PinName wirePin, PinMode pinMode = PullNone, RGBColor *Buffer = 0, int maxPixels = 0, RGBOrder rgbOrder = WS281X::RGB); |
| mutech | 0:dff187a80020 | 59 | ~WS281X(); |
| mutech | 0:dff187a80020 | 60 | |
| mutech | 0:dff187a80020 | 61 | RGBOrder getRGBOrder() { return _rgbOrder; } |
| mutech | 0:dff187a80020 | 62 | void setRGBOrder(RGBOrder rgbOrder = WS281X::RGB); |
| mutech | 0:dff187a80020 | 63 | |
| mutech | 20:28fe0d0d081b | 64 | void setPixelBuffer(RGBColor *Buffer, int maxPixels); |
| mutech | 20:28fe0d0d081b | 65 | int getMaxPixels() { return _maxPixels; } |
| mutech | 20:28fe0d0d081b | 66 | void setNumPixels(int numPixels); |
| mutech | 9:087006b19049 | 67 | int getNumPixels() { return _numPixels; } |
| mutech | 9:087006b19049 | 68 | |
| mutech | 8:0617f524d67d | 69 | void setColor(int index, RGBColor *color, int len); |
| mutech | 21:77275089d837 | 70 | void setColor(int index, HSVColor *color, int len); |
| mutech | 8:0617f524d67d | 71 | void fillColor(int index, const RGBColor color, int len); |
| mutech | 0:dff187a80020 | 72 | void repeatBlock(int block_size); |
| mutech | 24:f93a61e727a3 | 73 | void repeatBlock(RGBColor *source, int size); |
| mutech | 24:f93a61e727a3 | 74 | void repeatBlock(HSVColor *source, int size); |
| mutech | 8:0617f524d67d | 75 | void clear(const RGBColor color); |
| mutech | 8:0617f524d67d | 76 | void clear(const int color = 0) { clear((RGBColor)color); } |
| mutech | 0:dff187a80020 | 77 | |
| mutech | 0:dff187a80020 | 78 | void show(); |
| mutech | 8:0617f524d67d | 79 | void show(const RGBColor color); |
| mutech | 0:dff187a80020 | 80 | |
| mutech | 2:cc8e091fd975 | 81 | RGBColor operator[](int index) const |
| mutech | 0:dff187a80020 | 82 | { |
| mutech | 0:dff187a80020 | 83 | if ((uint16_t)index < _numPixels) |
| mutech | 0:dff187a80020 | 84 | return _pixels[index]; |
| mutech | 0:dff187a80020 | 85 | return _dummyPixel; |
| mutech | 0:dff187a80020 | 86 | } |
| mutech | 0:dff187a80020 | 87 | |
| mutech | 2:cc8e091fd975 | 88 | RGBColor& operator[](int index) |
| mutech | 0:dff187a80020 | 89 | { |
| mutech | 0:dff187a80020 | 90 | if ((uint16_t)index < _numPixels) |
| mutech | 0:dff187a80020 | 91 | return _pixels[index]; |
| mutech | 0:dff187a80020 | 92 | return _dummyPixel; |
| mutech | 0:dff187a80020 | 93 | } |
| mutech | 0:dff187a80020 | 94 | |
| mutech | 0:dff187a80020 | 95 | protected: |
| mutech | 0:dff187a80020 | 96 | |
| mutech | 0:dff187a80020 | 97 | private: |
| mutech | 24:f93a61e727a3 | 98 | PinName _wirePin; |
| mutech | 24:f93a61e727a3 | 99 | gpio_t _gpio; |
| mutech | 0:dff187a80020 | 100 | RGBOrder _rgbOrder; |
| mutech | 0:dff187a80020 | 101 | int _1st, _2nd, _3rd; |
| mutech | 0:dff187a80020 | 102 | |
| mutech | 24:f93a61e727a3 | 103 | bool _buf_owner; |
| mutech | 20:28fe0d0d081b | 104 | uint16_t _maxPixels; |
| mutech | 0:dff187a80020 | 105 | uint16_t _numPixels; |
| mutech | 2:cc8e091fd975 | 106 | RGBColor *_pixels; |
| mutech | 0:dff187a80020 | 107 | RGBColor _dummyPixel; |
| mutech | 0:dff187a80020 | 108 | |
| mutech | 0:dff187a80020 | 109 | #if defined(TARGET_NXP) |
| mutech | 0:dff187a80020 | 110 | typedef uint32_t regsize_t; |
| mutech | 0:dff187a80020 | 111 | #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1) |
| mutech | 0:dff187a80020 | 112 | typedef uint32_t regsize_t; |
| mutech | 0:dff187a80020 | 113 | #elif defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 114 | typedef uint16_t regsize_t; |
| mutech | 0:dff187a80020 | 115 | #else |
| mutech | 0:dff187a80020 | 116 | #error "not supported CPU!!" |
| mutech | 0:dff187a80020 | 117 | #endif |
| mutech | 0:dff187a80020 | 118 | #if defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 119 | void pin_mode_ex(PinName pin, PinMode mode); |
| mutech | 0:dff187a80020 | 120 | #endif |
| mutech | 0:dff187a80020 | 121 | void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value); |
| mutech | 0:dff187a80020 | 122 | }; |
| mutech | 0:dff187a80020 | 123 | |
| mutech | 0:dff187a80020 | 124 | //---------------------------------------------------------------------------- |
| mutech | 0:dff187a80020 | 125 | #endif // end of WS281X_H |