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.cpp
00001 #include "apa102.h" 00002 #include "mbed.h" 00003 00004 #define DO_N(N, F) for( int repetier = 0; repetier < N; repetier++) { F; } 00005 00006 //------------------------------------------------------------------------------------------------------------ 00007 apa102::apa102(PinName CKI, PinName SDI, int STRIP_LENGTH, int reset_delay) 00008 : _CKI(CKI), _SDI(SDI), _STRIP_LENGTH(STRIP_LENGTH), _reset_delay(reset_delay) { 00009 00010 _level=100; 00011 this->clear(); 00012 wait_us(_reset_delay); 00013 } 00014 //------------------------------------------------------------------------------------------------------------ 00015 void apa102::post(int *strip_colors) { 00016 this->preamble(); 00017 00018 int scaled_level = 0xE0 | (_level * 31 / 100); 00019 00020 for(int LED_number = 0 ; LED_number < _STRIP_LENGTH ; LED_number++) { 00021 this->write_word(scaled_level, 8); 00022 this->write_word(strip_colors[LED_number], 24); 00023 } 00024 00025 this->afterword(); 00026 wait_us(_reset_delay); //Wait for 1ms to go into reset 00027 } 00028 //------------------------------------------------------------------------------------------------------------ 00029 void apa102::clear(void) { 00030 this->preamble(); 00031 DO_N(_STRIP_LENGTH, this->write_word(0xE0000000, 32)); 00032 this->afterword(); 00033 } 00034 //------------------------------------------------------------------------------------------------------------ 00035 int apa102::level(int level) 00036 { 00037 if((level <= 100) && level) 00038 { 00039 _level = level; 00040 return _level; 00041 } 00042 return 0; 00043 } 00044 00045 //------------------------------------------------------------------------------------------------------------ 00046 int apa102::delay(uint32_t reset_delay) 00047 { 00048 if(reset_delay <= 0xffffffff)_reset_delay = reset_delay; 00049 return _reset_delay; 00050 } 00051 //------------------------------------------------------------------------------------------------------------ 00052 inline void apa102::write_hi() { 00053 this->write_bit(1); 00054 } 00055 inline void apa102::write_lo() { 00056 this->write_bit(0); 00057 } 00058 inline void apa102::write_bit(bool bit) { 00059 _CKI = 0; 00060 _SDI = bit; 00061 _CKI = 1; 00062 } 00063 inline void apa102::write_word(uint32_t word, int bits) { 00064 for(char color_bit = bits - 1 ; color_bit != 255 ; color_bit--) { 00065 this->write_bit( 00066 (word & (1 << color_bit)) ? 1 : 0 00067 ); 00068 } 00069 } 00070 inline void apa102::write_done() { 00071 _CKI = 0; 00072 } 00073 inline void apa102::preamble() { 00074 DO_N(32, this->write_lo()); 00075 } 00076 inline void apa102::afterword() { 00077 DO_N(32, this->write_hi()); 00078 this->write_done(); 00079 } 00080 //---------EOF---------------EOF------------------------------------------------------------------------------
Generated on Thu Jul 14 2022 22:02:19 by
 1.7.2
 1.7.2