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@9:087006b19049, 2016-08-12 (annotated)
- Committer:
- mutech
- Date:
- Fri Aug 12 20:19:06 2016 +0000
- Revision:
- 9:087006b19049
- Parent:
- 8:0617f524d67d
- Child:
- 16:01e073c662d7
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 | 9:087006b19049 | 56 | WS281X(PinName wirePin, PinMode pinMode = PullNone, RGBColor *Buffer = 0, int numPixels = 0, RGBOrder rgbOrder = WS281X::RGB); |
mutech | 0:dff187a80020 | 57 | ~WS281X(); |
mutech | 0:dff187a80020 | 58 | |
mutech | 0:dff187a80020 | 59 | RGBOrder getRGBOrder() { return _rgbOrder; } |
mutech | 0:dff187a80020 | 60 | void setRGBOrder(RGBOrder rgbOrder = WS281X::RGB); |
mutech | 0:dff187a80020 | 61 | |
mutech | 9:087006b19049 | 62 | void setPixelBuffer(RGBColor *Buffer, int numPixels); |
mutech | 9:087006b19049 | 63 | int getNumPixels() { return _numPixels; } |
mutech | 9:087006b19049 | 64 | |
mutech | 8:0617f524d67d | 65 | void setColor(int index, RGBColor *color, int len); |
mutech | 8:0617f524d67d | 66 | void fillColor(int index, const RGBColor color, int len); |
mutech | 0:dff187a80020 | 67 | void repeatBlock(int block_size); |
mutech | 8:0617f524d67d | 68 | void clear(const RGBColor color); |
mutech | 8:0617f524d67d | 69 | void clear(const int color = 0) { clear((RGBColor)color); } |
mutech | 0:dff187a80020 | 70 | |
mutech | 0:dff187a80020 | 71 | void show(); |
mutech | 8:0617f524d67d | 72 | void show(const RGBColor color); |
mutech | 0:dff187a80020 | 73 | |
mutech | 2:cc8e091fd975 | 74 | RGBColor operator[](int index) const |
mutech | 0:dff187a80020 | 75 | { |
mutech | 0:dff187a80020 | 76 | if ((uint16_t)index < _numPixels) |
mutech | 0:dff187a80020 | 77 | return _pixels[index]; |
mutech | 0:dff187a80020 | 78 | return _dummyPixel; |
mutech | 0:dff187a80020 | 79 | } |
mutech | 0:dff187a80020 | 80 | |
mutech | 2:cc8e091fd975 | 81 | RGBColor& operator[](int index) |
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 | 0:dff187a80020 | 88 | protected: |
mutech | 0:dff187a80020 | 89 | |
mutech | 0:dff187a80020 | 90 | private: |
mutech | 0:dff187a80020 | 91 | PinName _wirePin; |
mutech | 0:dff187a80020 | 92 | gpio_t _gpio; |
mutech | 0:dff187a80020 | 93 | RGBOrder _rgbOrder; |
mutech | 0:dff187a80020 | 94 | int _1st, _2nd, _3rd; |
mutech | 0:dff187a80020 | 95 | |
mutech | 9:087006b19049 | 96 | bool _buf_owner; |
mutech | 0:dff187a80020 | 97 | uint16_t _numPixels; |
mutech | 2:cc8e091fd975 | 98 | RGBColor *_pixels; |
mutech | 0:dff187a80020 | 99 | RGBColor _dummyPixel; |
mutech | 0:dff187a80020 | 100 | |
mutech | 0:dff187a80020 | 101 | #if defined(TARGET_NXP) |
mutech | 0:dff187a80020 | 102 | typedef uint32_t regsize_t; |
mutech | 0:dff187a80020 | 103 | #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1) |
mutech | 0:dff187a80020 | 104 | typedef uint32_t regsize_t; |
mutech | 0:dff187a80020 | 105 | #elif defined(TARGET_STM) |
mutech | 0:dff187a80020 | 106 | typedef uint16_t regsize_t; |
mutech | 0:dff187a80020 | 107 | #else |
mutech | 0:dff187a80020 | 108 | #error "not supported CPU!!" |
mutech | 0:dff187a80020 | 109 | #endif |
mutech | 0:dff187a80020 | 110 | #if defined(TARGET_STM) |
mutech | 0:dff187a80020 | 111 | void pin_mode_ex(PinName pin, PinMode mode); |
mutech | 0:dff187a80020 | 112 | #endif |
mutech | 0:dff187a80020 | 113 | void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value); |
mutech | 0:dff187a80020 | 114 | }; |
mutech | 0:dff187a80020 | 115 | |
mutech | 0:dff187a80020 | 116 | //---------------------------------------------------------------------------- |
mutech | 0:dff187a80020 | 117 | #endif // end of WS281X_H |