Kenji Arai / WS2812

Dependents:   WS2812_out_of_specification_demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WS2812.h Source File

WS2812.h

00001 /* Copyright (c) 2012 cstyles, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy
00004  * of this software and associated documentation files (the "Software"),
00005  * to deal in the Software without restriction, including without limitation
00006  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00007  * and/or sell copies of the Software, and to permit persons to whom
00008  * the Software is furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included
00011  * in all copies or substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00016  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00019  * IN THE SOFTWARE.
00020  */
00021 
00022 /*
00023     Modified by Kenji Arai / JH1PJL
00024         March 20th, 2020
00025 
00026     Original & Refrence
00027         https://os.mbed.com/users/chris/code/ChrisRGB-Ring/
00028         https://os.mbed.com/users/chris/code/WS2812/
00029         https://os.mbed.com/users/chris/code/PixelArray/
00030 
00031         https://os.mbed.com/users/bridadan/code/WS2812_Example/
00032         https://os.mbed.com/users/bridadan/code/WS2812/
00033         https://os.mbed.com/users/chris/code/PixelArray/
00034 
00035  */
00036 
00037 /*
00038     !!!!! This library does NOT follow specification value of the timing !!!!
00039         https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf
00040         http://akizukidenshi.com/download/ds/adafruit/WS2812B.pdf
00041 
00042     WS2812 -> Interpretation of control timing (Not Grantee but works)
00043         Specification   
00044                     T0H     200 to 500ns
00045                     T1H     550 to 850ns
00046                     T0L     650 to 950ns
00047                     T1L     450 to 750ns
00048                     Reset   50us
00049         Interpretation
00050                     T0H     same as above
00051                     T1H     same as above
00052                     T0L     less than 10us
00053                     T1L     Less than 10us
00054                     Reset   over 50uS
00055 
00056     Please refer following web page
00057         https://wp.josh.com/2014/05/13/
00058                 ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
00059  */
00060 
00061 #ifndef WS2812_H
00062 #define WS2812_H
00063 
00064 #include "mbed.h"
00065 
00066 #if \
00067     defined(TARGET_NUCLEO_F042K6)\
00068  || defined(TARGET_NUCLEO_F334R8)\
00069  || defined(TARGET_NUCLEO_F401RE)\
00070  || defined(TARGET_NUCLEO_F411RE)\
00071  || defined(TARGET_NUCLEO_F446RE)\
00072  || defined(TARGET_NUCLEO_L053R8)\
00073  || defined(TARGET_NUCLEO_L073RZ)\
00074  || defined(TARGET_NUCLEO_L152RE)\
00075  || defined(TARGET_NUCLEO_L476RG)
00076 #   define WS1 0x0f
00077 #   define WS0 0x0c
00078 #   define SPICLK 8000000
00079 #   define SPIBPF 4
00080 #elif \
00081     defined(TARGET_RZ_A1H)\
00082  || defined(TARGET_GR_LYCHEE)\
00083  || defined(TARGET_GR_MANGO)
00084 #   define WS1 0x0f
00085 #   define WS0 0x0c
00086 #   define SPICLK 5000000
00087 #   define SPIBPF 8
00088 #endif
00089 
00090 //!Library for the WS2812 RGB LED with integrated controller
00091 /*!
00092 The WS2812 is controller that is built into a range of LEDs
00093 */
00094 class WS2812
00095 {
00096 public:
00097     enum BrightnessControl {OFF, GLOBAL, PER_PIXEL};
00098 
00099     //!Creates an instance of the class.
00100     /*!
00101     Connect WS2812 using SPI MOSI pin(Only use) and MISO&SCLK(NC)
00102     */
00103     WS2812(PinName mosi, PinName miso, PinName sclk, int size);
00104 
00105     /*!
00106     Destroys instance.
00107     */
00108     ~WS2812 ();
00109 
00110     void write(int buf[]);
00111     void write_offsets(int buf[], int r_offset = 0,
00112                        int g_offset = 0, int b_offset = 0);
00113     void setAll(int color);
00114 
00115     void set_brightness_mode(BrightnessControl mode);
00116     void set_brightness(unsigned char br);
00117 
00118 private:
00119     SPI __spi;
00120 
00121     void __write (int color);
00122 
00123     int __size;
00124     unsigned char __br;
00125     BrightnessControl __mode;
00126 
00127 };
00128 
00129 #endif