TLIGHT_PRODUCTS / WS281X
Committer:
mutech
Date:
Sat Aug 20 19:10:59 2016 +0000
Revision:
16:01e073c662d7
Parent:
9:087006b19049
Child:
20:28fe0d0d081b
WS2811/WS2812 Library;

Who changed what in which revision?

UserRevisionLine numberNew 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 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 0:dff187a80020 36 WS281X
mutech 0:dff187a80020 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 0:dff187a80020 54 * @param numPixels - 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 0:dff187a80020 57 WS281X(PinName wirePin, PinMode pinMode = PullNone, int numPixels = MAX_PIXELS, RGBOrder rgbOrder = WS281X::RGB);
mutech 9:087006b19049 58 WS281X(PinName wirePin, PinMode pinMode = PullNone, RGBColor *Buffer = 0, int numPixels = 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 9:087006b19049 64 void setPixelBuffer(RGBColor *Buffer, int numPixels);
mutech 9:087006b19049 65 int getNumPixels() { return _numPixels; }
mutech 9:087006b19049 66
mutech 8:0617f524d67d 67 void setColor(int index, RGBColor *color, int len);
mutech 8:0617f524d67d 68 void fillColor(int index, const RGBColor color, int len);
mutech 0:dff187a80020 69 void repeatBlock(int block_size);
mutech 8:0617f524d67d 70 void clear(const RGBColor color);
mutech 8:0617f524d67d 71 void clear(const int color = 0) { clear((RGBColor)color); }
mutech 0:dff187a80020 72
mutech 0:dff187a80020 73 void show();
mutech 8:0617f524d67d 74 void show(const RGBColor color);
mutech 0:dff187a80020 75
mutech 2:cc8e091fd975 76 RGBColor operator[](int index) const
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 2:cc8e091fd975 83 RGBColor& operator[](int index)
mutech 0:dff187a80020 84 {
mutech 0:dff187a80020 85 if ((uint16_t)index < _numPixels)
mutech 0:dff187a80020 86 return _pixels[index];
mutech 0:dff187a80020 87 return _dummyPixel;
mutech 0:dff187a80020 88 }
mutech 0:dff187a80020 89
mutech 0:dff187a80020 90 protected:
mutech 0:dff187a80020 91
mutech 0:dff187a80020 92 private:
mutech 0:dff187a80020 93 PinName _wirePin;
mutech 0:dff187a80020 94 gpio_t _gpio;
mutech 0:dff187a80020 95 RGBOrder _rgbOrder;
mutech 0:dff187a80020 96 int _1st, _2nd, _3rd;
mutech 0:dff187a80020 97
mutech 9:087006b19049 98 bool _buf_owner;
mutech 0:dff187a80020 99 uint16_t _numPixels;
mutech 2:cc8e091fd975 100 RGBColor *_pixels;
mutech 0:dff187a80020 101 RGBColor _dummyPixel;
mutech 0:dff187a80020 102
mutech 0:dff187a80020 103 #if defined(TARGET_NXP)
mutech 0:dff187a80020 104 typedef uint32_t regsize_t;
mutech 0:dff187a80020 105 #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1)
mutech 0:dff187a80020 106 typedef uint32_t regsize_t;
mutech 0:dff187a80020 107 #elif defined(TARGET_STM)
mutech 0:dff187a80020 108 typedef uint16_t regsize_t;
mutech 0:dff187a80020 109 #else
mutech 0:dff187a80020 110 #error "not supported CPU!!"
mutech 0:dff187a80020 111 #endif
mutech 0:dff187a80020 112 #if defined(TARGET_STM)
mutech 0:dff187a80020 113 void pin_mode_ex(PinName pin, PinMode mode);
mutech 0:dff187a80020 114 #endif
mutech 0:dff187a80020 115 void writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value);
mutech 0:dff187a80020 116 };
mutech 0:dff187a80020 117
mutech 0:dff187a80020 118 //----------------------------------------------------------------------------
mutech 0:dff187a80020 119 #endif // end of WS281X_H