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.
apa102.h
00001 /* 00002 Copyright (c) 2011 Thomas Olsson. 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #ifndef APA102_H 00024 #define APA102_H 00025 00026 #include "mbed.h" 00027 #include "HTML_color.h" 00028 00029 #define APA102_MAX_LEVEL 100 00030 00031 /** APA102 class, controlling LED strips or arrays like this http://www.sparkfun.com/products/10312 00032 * 00033 * the APA102 IC's are 5V but works well connected directly to mbed IO pins. 00034 * 00035 * You need a 5V power supply capable of about 2A to light up a 32 led strip, do NOT power from mbed(usb). 00036 * 00037 * the required reset delay is a blocking wait(), it can be lowered (or set to 0) if you want to use the 00038 * time for executing code instead, data sheet says 500us but i found it need at least 800us. 00039 * 00040 * Example: 00041 * @code 00042 #include "mbed.h" 00043 #include "apa102.h" 00044 00045 #define STRIP_LENGTH 32 00046 00047 apa102 mystrip(p9, p10, STRIP_LENGTH); 00048 00049 int dir=1, level=10; 00050 int rainbow[] = {0xff00ff,0xff00cc,0xff0099,0xff0066,0xff0033,0xff0000,0xff3300,0xff6600, 00051 0xff9900,0xffcc00,0xffff00,0xccff00,0x99ff00,0x66ff00,0x33ff00,0x00ff00, 00052 0x00ff33,0x00ff66,0x00ff99,0x00ffcc,0x00ffff,0x00ccff,0x0099ff,0x0066ff, 00053 0x0033ff,0x0000ff,0x3300ff,0x6600ff,0x9900ff,0xcc00ff,0x9900ff,0x6600ff 00054 }; 00055 00056 void move(void){ 00057 int temp = rainbow[31]; 00058 for (int x = (STRIP_LENGTH - 1) ; x > 0 ; x--) rainbow[x] = rainbow[x - 1]; 00059 rainbow[0] = temp; 00060 } 00061 00062 void pulse(void){ 00063 if(dir) 00064 { 00065 mystrip.level(level+=2); 00066 if(level >= 100)dir = 0; 00067 } 00068 else if(!dir) 00069 { 00070 mystrip.level(level--); 00071 if(level <= 5)dir = 1; 00072 } 00073 } 00074 00075 int main() { 00076 mystrip.level(level); 00077 while(1) 00078 { 00079 move(); 00080 pulse(); 00081 mystrip.post(rainbow); 00082 wait_ms(100); 00083 } 00084 } 00085 * @endcode 00086 */ 00087 class apa102 00088 { 00089 public: 00090 /** Create a new apa102 object 00091 * 00092 * 00093 * @param CKI clock pin 00094 * @param SDI data pin 00095 * @param STRIP_LENGTH number of apa102 IC's i strip or array defaults to 32 00096 * @param reset_delay delay in us to allow latching data defaults to 800 00097 * @returns nothing 00098 */ 00099 apa102(PinName CKI, PinName SDI, int STRIP_LENGTH = 32, int reset_delay = 800); 00100 /** write RGB color data to strip or array 00101 * 00102 * color data for each LED is 3 bytes int the order of rrggbb. 00103 * array must have STRIP_LENGTH number of elements 00104 * 00105 * @param strip_colors array of color data 00106 */ 00107 void post(int *strip_colors); 00108 /** clears the array or strip (all off) 00109 */ 00110 void clear(void); 00111 /** set level of the entire array 0-100% 00112 * 00113 * at low levels the colors may change because R,G or B 00114 * reaches 0. 00115 * 00116 * @param level level in percent 00117 * @returns current level 00118 */ 00119 int level(int level=0); 00120 /** get/set reset/write delay 00121 * 00122 * mainly for experimenting with reset values without recompiling, 00123 * leave at default to begin with & then lower to get faster execution. 00124 * 00125 * @param delay delay in us 00126 * @returns delay in us 00127 */ 00128 int delay(uint32_t reset_delay=100); 00129 00130 private: 00131 DigitalOut _CKI; 00132 DigitalOut _SDI; 00133 int _STRIP_LENGTH; 00134 int _level; 00135 int _reset_delay; 00136 inline void write_hi(); 00137 inline void write_lo(); 00138 inline void write_bit(bool bit); 00139 inline void write_word(uint32_t word, int bits = 32); 00140 inline void write_done(); 00141 inline void preamble(); 00142 inline void afterword(); 00143 }; 00144 00145 #endif
Generated on Thu Jul 14 2022 22:02:19 by
1.7.2