Dependencies:   PCA9955A_library_prototype mbed

main.cpp

Committer:
okano
Date:
2014-07-17
Revision:
5:78bfbf11bb2f
Parent:
4:75a2c3550bdc
Child:
6:4da1c7f560b8

File content as of revision 5:78bfbf11bb2f:

#include    "mbed.h"
#include    "PCA9955A.h"

BusOut      leds( LED4, LED3, LED2, LED1 );
DigitalOut  pin( p21 );
AnalogIn    ain( p20 );
AnalogOut   aout( p18 );

PCA9955A    led_driver( p28, p27, 0x02 );   // SDA, SCL, I2C_slave_sddress

Ticker  led_maintenance;
Ticker  sample_timing;

int     led_change  = 0;
int     sampling    = 0;


void    set_color( int v );
void    led_cntl();
void    ad_cntl();




int main()
{
    float   in;
    float   coef    = 26.4;
    float   peak    = 0;
    int     shift;

    led_driver.set_all_intensity( 0xFF );
    led_driver.set_all_intensity( 0x20, true );

    set_color( 1 );

    led_maintenance.attach( &led_cntl, 0.02 );
    sample_timing.attach( &ad_cntl, 0.01 );

    while(1) {

        if ( sampling ) {
            sampling    = 0;

            pin =0;
            in      = (float)ain;
            peak    = in < peak ? peak : in;
            shift  = (int)(coef * peak);
            leds    = 0x1 << shift;
            peak    *= 0.99;
            pin =1;
            aout    = peak;

            if ( shift > 2 ) {
                led_change  = 1;
            }
        }
    }

}

void ad_cntl()
{
    sampling    = 1;
}


void led_cntl()
{
    static int      count       = 0;
    static float    intensity   = 1.0;

    if ( led_change ) {
        set_color( count++ & 0x3 );
        led_change  = 0;
        intensity   = 1.0;
    }

    led_driver.set_all_intensity( (char)(intensity * (float)0xFF) );

    intensity   *= 0.90;
}

void set_color( int v )
{
    v   = 0x1 << v;
    led_driver  = (v << 12) | (v << 8) | (v << 4) | v;
}