A simple example to drive an APA-102 LED strip.
Dependencies: mbed APA102b Ping SLCD
Revision 0:afc41b8e2360, committed 2015-03-05
- Comitter:
- rosienej
- Date:
- Thu Mar 05 19:02:07 2015 +0000
- Child:
- 1:2d02f55d47c5
- Commit message:
- A simple example to drive an APA-102 Led Strip
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 05 19:02:07 2015 +0000 @@ -0,0 +1,89 @@ +#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++; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Mar 05 19:02:07 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac \ No newline at end of file