A simple example to drive an APA-102 LED strip.

Dependencies:   mbed APA102b Ping SLCD

main.cpp

Committer:
rosienej
Date:
2015-03-14
Revision:
1:2d02f55d47c5
Parent:
0:afc41b8e2360
Child:
2:5a9388a7ac62

File content as of revision 1:2d02f55d47c5:

#include "mbed.h"
#include "Ping.h"
#include "APA102a.h"

//SPI spi(PTA16, PTA17, PTA15); // mosi, miso, sclk
Ping ping(PTA13); 

APA102a LEDs(PTA16, PTA17, PTA15,1000000); // mosi, miso, sclk, rate

// 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

    int rgb[3]; 
    unsigned char r,g,b;
    const int N=67;  // Number of APA-102 Elements +1
    int range;
    unsigned int Pixel;
    unsigned int Pixels[N];
    
    int colors=0x000000;
    LEDs.SetBuffer(Pixels,1,N, N,0, false,false); 
    
    while (true) {
        ping.Send();    
        wait_ms(50);  // update rate.
        range = ping.Read_cm();
        
        
        for(int i=0;i<N;i++)
        {   
            hsi2rgb(1.0*(i+colors), 0.8, 0.8,rgb);
            r = rgb[0];
            g = rgb[1];
            b = rgb[2];
            
            Pixel=LEDs.IRGB(7,r,g,b);
            Pixels[i]=Pixel;
           
            } 
        
        LEDs.Repaint();
        colors+=range/10;
    }
}