Library for the WS2812 LED Driver. Uses bit banging and nops for precise timing. Number of nops executed are configurable at run time.
Dependents: WS2812_Example WS2812_Example_fade Lamp_03 Lamp_04 ... more
WS2812.h
00001 /* Copyright (c) 2012 cstyles, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef WS2812_H 00020 #define WS2812_H 00021 00022 #include "mbed.h" 00023 00024 #define FRAME_SIZE 24 00025 00026 //!Library for the WS2812 RGB LED with integrated controller 00027 /*! 00028 The WS2812 is controller that is built into a range of LEDs 00029 */ 00030 class WS2812 00031 { 00032 public: 00033 enum BrightnessControl { OFF, GLOBAL, PER_PIXEL }; 00034 00035 /** 00036 * Constructor 00037 * 00038 * @param pin Output pin. Connect to "Din" on the first WS2812 in the strip 00039 * @param size Number of LEDs in your strip 00040 * @param zeroHigh How many NOPs to insert to ensure TOH is properly generated. See library description for more information. 00041 * @param zeroLow How many NOPs to insert to ensure TOL is properly generated. See library description for more information. 00042 * @param oneHigh How many NOPs to insert to ensure T1H is properly generated. See library description for more information. 00043 * @param oneLow How many NOPs to insert to ensure T1L is properly generated. See library description for more information. 00044 * 00045 */ 00046 WS2812(PinName pin, int size, int zeroHigh, int zeroLow, int oneHigh, int oneLow); 00047 00048 /*! 00049 Destroys instance. 00050 */ 00051 ~WS2812 (); 00052 00053 /** 00054 * Sets the timing parameters for the bit-banged signal 00055 * 00056 * @param zeroHigh How many NOPs to insert to ensure TOH is properly generated. See library description for more information. 00057 * @param zeroLow How many NOPs to insert to ensure TOL is properly generated. See library description for more information. 00058 * @param oneHigh How many NOPs to insert to ensure T1H is properly generated. See library description for more information. 00059 * @param oneLow How many NOPs to insert to ensure T1L is properly generated. See library description for more information. 00060 * 00061 */ 00062 void setDelays(int zeroHigh, int zeroLow, int oneHigh, int oneLow); 00063 00064 /** 00065 * Writes the given buffer to the LED strip with the given offsets. 00066 * NOTE: This function is timing critical, therefore interrupts are disabled during the transmission section. 00067 * 00068 * @param buf Pointer to the PixelArray buffer 00069 * @param r_offset The offset where each each pixel pulls its red component. Wraps to beginning if end is reached. 00070 * @param g_offset The offset where each each pixel pulls its green component. Wraps to beginning if end is reached. 00071 * @param b_offset The offset where each each pixel pulls its blue component. Wraps to beginning if end is reached. 00072 * 00073 */ 00074 void write_offsets(int buf[], int r_offset = 0, int g_offset = 0, int b_offset = 0); 00075 00076 00077 /** 00078 * Writes the given buffer to the LED strip 00079 * NOTE: This function is timing critical, therefore interrupts are disabled during the transmission section. 00080 * 00081 * @param buf Pointer to the PixelArray buffer 00082 * 00083 */ 00084 void write(int buf[]); 00085 00086 /** 00087 * Sets the brightness mode 00088 * 00089 * @param bc The brightness control. Defaults to OFF. Possible values include OFF, GLOBAL, and PER_PIXEL 00090 * 00091 */ 00092 void useII(BrightnessControl bc); 00093 00094 /** 00095 * Sets the global brightness level. 00096 * 00097 * @param II The brightness level. Possible values include 0 - 255 (0x00 - 0xFF). 00098 * 00099 */ 00100 void setII(unsigned char II); 00101 00102 00103 00104 private: 00105 00106 int __size; 00107 int __zeroHigh, __zeroLow, __oneHigh, __oneLow; 00108 unsigned char __II; 00109 BrightnessControl __use_II; 00110 bool *__transmitBuf; 00111 void __loadBuf(int buf[],int r_offset=0, int g_offset=0, int b_offset=0); 00112 PinName __outPin; 00113 DigitalOut __gpo; 00114 }; 00115 00116 #endif
Generated on Tue Jul 12 2022 22:53:55 by 1.7.2