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@2:cc8e091fd975, 2016-07-27 (annotated)
- Committer:
- mutech
- Date:
- Wed Jul 27 14:42:36 2016 +0000
- Revision:
- 2:cc8e091fd975
- Parent:
- 0:dff187a80020
- Child:
- 3:786b31c65e7a
WS2811/WS2812 Library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mutech | 0:dff187a80020 | 1 | /* WS281X.h (for LPC82X/STM32F0x/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 | 0:dff187a80020 | 30 | #define MAX_PIXELS 170 |
mutech | 0:dff187a80020 | 31 | |
mutech | 0:dff187a80020 | 32 | //---------------------------------------------------------------------------- |
mutech | 0:dff187a80020 | 33 | /** |
mutech | 0:dff187a80020 | 34 | WS281X |
mutech | 0:dff187a80020 | 35 | */ |
mutech | 0:dff187a80020 | 36 | class WS281X |
mutech | 0:dff187a80020 | 37 | { |
mutech | 0:dff187a80020 | 38 | public: |
mutech | 0:dff187a80020 | 39 | /** |
mutech | 0:dff187a80020 | 40 | Order of r, g and b bytes |
mutech | 0:dff187a80020 | 41 | */ |
mutech | 0:dff187a80020 | 42 | enum RGBOrder |
mutech | 0:dff187a80020 | 43 | { |
mutech | 0:dff187a80020 | 44 | RGB, RBG, GRB, GBR, BRG, BGR |
mutech | 0:dff187a80020 | 45 | }; |
mutech | 0:dff187a80020 | 46 | |
mutech | 0:dff187a80020 | 47 | /** |
mutech | 0:dff187a80020 | 48 | * Initializes the addressable led bus |
mutech | 0:dff187a80020 | 49 | * |
mutech | 0:dff187a80020 | 50 | * @param wirePin - The output pin on wich the addressable leds are connected |
mutech | 0:dff187a80020 | 51 | * @param pinMode - The output pin mode PullUp, PullDown, PullNone, OpenDrain |
mutech | 0:dff187a80020 | 52 | * @param numPixels - Number of the addressable leds |
mutech | 0:dff187a80020 | 53 | * @param RGBOrder - The order in wich the r, g and b bytes are expected |
mutech | 0:dff187a80020 | 54 | */ |
mutech | 0:dff187a80020 | 55 | WS281X(PinName wirePin, PinMode pinMode = PullNone, int numPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB); |
mutech | 0:dff187a80020 | 56 | ~WS281X(); |
mutech | 0:dff187a80020 | 57 | |
mutech | 0:dff187a80020 | 58 | RGBOrder getRGBOrder() { return _rgbOrder; } |
mutech | 0:dff187a80020 | 59 | void setRGBOrder(RGBOrder rgbOrder = WS281X::RGB); |
mutech | 0:dff187a80020 | 60 | |
mutech | 0:dff187a80020 | 61 | void repeatBlock(int block_size); |
mutech | 2:cc8e091fd975 | 62 | void clear(RGBColor color); |
mutech | 2:cc8e091fd975 | 63 | void clear(uint32_t color = 0) { clear((RGBColor)color); } |
mutech | 0:dff187a80020 | 64 | |
mutech | 0:dff187a80020 | 65 | void show(); |
mutech | 2:cc8e091fd975 | 66 | void show(RGBColor color); |
mutech | 2:cc8e091fd975 | 67 | void show(uint32_t color) { show((RGBColor)color); } |
mutech | 0:dff187a80020 | 68 | |
mutech | 2:cc8e091fd975 | 69 | RGBColor operator[](int index) const |
mutech | 0:dff187a80020 | 70 | { |
mutech | 0:dff187a80020 | 71 | if ((uint16_t)index < _numPixels) |
mutech | 0:dff187a80020 | 72 | return _pixels[index]; |
mutech | 0:dff187a80020 | 73 | return _dummyPixel; |
mutech | 0:dff187a80020 | 74 | } |
mutech | 0:dff187a80020 | 75 | |
mutech | 2:cc8e091fd975 | 76 | RGBColor& operator[](int index) |
mutech | 0:dff187a80020 | 77 | { |
mutech | 0:dff187a80020 | 78 | if ((uint16_t)index < _numPixels) |
mutech | 0:dff187a80020 | 79 | return _pixels[index]; |
mutech | 0:dff187a80020 | 80 | return _dummyPixel; |
mutech | 0:dff187a80020 | 81 | } |
mutech | 0:dff187a80020 | 82 | |
mutech | 0:dff187a80020 | 83 | protected: |
mutech | 0:dff187a80020 | 84 | |
mutech | 0:dff187a80020 | 85 | private: |
mutech | 0:dff187a80020 | 86 | PinName _wirePin; |
mutech | 0:dff187a80020 | 87 | gpio_t _gpio; |
mutech | 0:dff187a80020 | 88 | RGBOrder _rgbOrder; |
mutech | 0:dff187a80020 | 89 | int _1st, _2nd, _3rd; |
mutech | 0:dff187a80020 | 90 | |
mutech | 0:dff187a80020 | 91 | uint16_t _numPixels; |
mutech | 2:cc8e091fd975 | 92 | RGBColor *_pixels; |
mutech | 0:dff187a80020 | 93 | RGBColor _dummyPixel; |
mutech | 0:dff187a80020 | 94 | |
mutech | 0:dff187a80020 | 95 | #if defined(TARGET_NXP) |
mutech | 0:dff187a80020 | 96 | typedef uint32_t regsize_t; |
mutech | 0:dff187a80020 | 97 | #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1) |
mutech | 0:dff187a80020 | 98 | typedef uint32_t regsize_t; |
mutech | 0:dff187a80020 | 99 | #elif defined(TARGET_STM) |
mutech | 0:dff187a80020 | 100 | typedef uint16_t regsize_t; |
mutech | 0:dff187a80020 | 101 | #else |
mutech | 0:dff187a80020 | 102 | #error "not supported CPU!!" |
mutech | 0:dff187a80020 | 103 | #endif |
mutech | 0:dff187a80020 | 104 | #if defined(TARGET_STM) |
mutech | 0:dff187a80020 | 105 | void pin_mode_ex(PinName pin, PinMode mode); |
mutech | 0:dff187a80020 | 106 | #endif |
mutech | 0:dff187a80020 | 107 | #if defined(TARGET_STM32F7) |
mutech | 0:dff187a80020 | 108 | void _delay(int value); |
mutech | 0:dff187a80020 | 109 | #endif |
mutech | 0:dff187a80020 | 110 | void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value); |
mutech | 0:dff187a80020 | 111 | }; |
mutech | 0:dff187a80020 | 112 | |
mutech | 0:dff187a80020 | 113 | //---------------------------------------------------------------------------- |
mutech | 0:dff187a80020 | 114 | #endif // end of WS281X_H |