A simple example to drive an APA-102 LED strip.
Dependencies: mbed APA102b Ping SLCD
main.cpp
- Committer:
- rosienej
- Date:
- 2015-03-05
- Revision:
- 0:afc41b8e2360
- Child:
- 1:2d02f55d47c5
File content as of revision 0:afc41b8e2360:
#include "mbed.h" SPI spi(PTA16, PTA17, PTA15); // mosi, miso, sclk // This function was downloaded from: // http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi // Blog Post attributed to Brian Neltner // Function example takes H, S, I, and a pointer to the // returned RGB colorspace converted vector. It should // be initialized with: // // int rgb[3]; // // in the calling function. After calling hsi2rgb // the vector rgb will contain red, green, and blue // calculated values. // void hsi2rgb(float H, float S, float I, int* rgb) { int r, g, b; H = fmod(H,360); // cycle H around to 0-360 degrees H = 3.14159*H/(float)180; // Convert to radians. S = S>0?(S<1?S:1):0; // clamp S and I to interval [0,1] I = I>0?(I<1?I:1):0; // Math! Thanks in part to Kyle Miller. if(H < 2.09439) { r = 255*I/3*(1+S*cos(H)/cos(1.047196667-H)); g = 255*I/3*(1+S*(1-cos(H)/cos(1.047196667-H))); b = 255*I/3*(1-S); } else if(H < 4.188787) { H = H - 2.09439; g = 255*I/3*(1+S*cos(H)/cos(1.047196667-H)); b = 255*I/3*(1+S*(1-cos(H)/cos(1.047196667-H))); r = 255*I/3*(1-S); } else { H = H - 4.188787; b = 255*I/3*(1+S*cos(H)/cos(1.047196667-H)); r = 255*I/3*(1+S*(1-cos(H)/cos(1.047196667-H))); g = 255*I/3*(1-S); } rgb[0]=r; rgb[1]=g; rgb[2]=b; } int main() { // Quick example to drive an APA-102 LED Strip from a FRDM-KL46z // http://www.insomnialighting.com/catalog/index.php?main_page=product_info&products_id=61 // Wire the unit up to SPI, common ground and give it 5 volt power. // Shift through the spectrum, slowly rotate. // Setup the spi for 8 bit data, high steady state clock, // second edge capture, with a 1MHz clock rate spi.format(8,3); spi.frequency(1000000); int rgb[3]; const int N=288; // Number of APA-102 Elements int colors=0x000000; while (true) { spi.write(0X00); // Start spi.write(0X00); spi.write(0X00); spi.write(0X00); for(int i=0;i<N;i++) { spi.write(0XEF); // 0xE0 + 1F, Frame + max brightness hsi2rgb(1.0*(i+colors), 0.8, 0.8,rgb); spi.write(0XFF&rgb[0]); // 0xE0 + 1F, Frame + max brightness spi.write(0XFF&rgb[1]); // 0xE0 + 1F, Frame + max brightness spi.write(0XFF&rgb[2]); // 0xE0 + 1F, Frame + max brightness } spi.write(0XFF); // Stop spi.write(0XFF); spi.write(0XFF); spi.write(0XFF); colors++; } }