RGB LED Driver demo

Dependents:   NJU6063_HelloWorld

NJU6063.cpp

Committer:
og
Date:
2016-01-10
Revision:
2:137675ac48ab
Parent:
1:bbc915c814da
Child:
3:f83202c2ae59

File content as of revision 2:137675ac48ab:

#include "NJU6063.h"

NJU6063::NJU6063( PinName sda, PinName scl, PinName rst)
    : _i2c(sda, scl), _rst(rst)
{
    uint32_t i2cAddr = I2C_1;
    //_i2c.frequency(400000);
    _i2c_cr1 = (uint32_t *)i2cAddr;
    _i2c_dr  = (uint32_t *)(i2cAddr + 0x10);
    _i2c_sr1 = (uint32_t *)(i2cAddr + 0x14);
    _i2c_sr2 = (uint32_t *)(i2cAddr + 0x18);
}

void NJU6063::reset(void)
{
    //_i2c_type->CR1 &= CR1_ACK_Reset;
    //SetCR1(0x200);
    wait_ms(100);
    _rst = 0;
    wait_ms(100);
    _rst = 1;
}

uint8_t NJU6063::set_multi_device(uint8_t n)
{
    char data[3];
    data[0] = 0x00; // Initial chip addres
    data[1] = 0x0d; // multi device address
    for (uint8_t i=1; i<=n; i++) {
        data[2] = i;
        ack= _i2c.write(NJU6063_SLAVE, data, 3);
        wait_us(500*3);
        if (ack) {
            n = i-1;
        }
    }
    return(n);
}
void NJU6063::init(uint8_t chip_addr, uint8_t d)
{
    char data[3];
    data[0] = chip_addr;
    data[1] = 0x00;
    data[2] = d;
    ack = _i2c.write(NJU6063_SLAVE, data, 3);
    wait_us(500*3);
}

void NJU6063::set_iled(uint8_t chip_addr, uint8_t d1, uint8_t d2, uint8_t d3)
{
    char data[3];
    data[0] = chip_addr;
    data[1] = 0x01;
    data[2] = (0x03&d1) | (0x03&d2)<<2 | (0x03&d3) <<4;
    ack = _i2c.write(NJU6063_SLAVE, data, 3);
    wait_us(500*3);
}

void NJU6063::set_pwm(uint8_t chip_addr, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t loop, uint8_t son)
{
    char data[7];
    data[0] = chip_addr;
    data[1] = 0x02;
    data[2] = d1;
    data[3] = d2;
    data[4] = d3;
    data[5] = loop;
    data[6] = son;
    ack = _i2c.write(NJU6063_SLAVE, data, 7);
    wait_us(500*7);
}
void NJU6063::dim_start(uint8_t chip_addr)
{
    char data[3];
    data[0] = chip_addr;
    data[1] = 0x07; // reg addres
    data[2] = 0x01; // start
    ack = _i2c.write(NJU6063_SLAVE, data, 3);
    wait_us(500*3);
}

void NJU6063::dim_stop(uint8_t chip_addr)
{
    char data[3];
    data[0] = chip_addr;
    data[1] = 0x07; // reg addres
    data[2] = 0x02; // stop
    ack = _i2c.write(NJU6063_SLAVE, data, 3);
    wait_us(500*3);
}

void NJU6063::check_dim(void)
{
    uint8_t timeout = 0x0f;
    char data[3];
    data[0] = 0xff;
    data[1] = 0x0b;
    data[2] = 0x00;
    /*
    do {
        ack = _i2c.write(NJU6063_SLAVE, data, 3);
        wait_us(200*3);
        timeout--;
        if (timeout==0) ACK = 1;
    } while(!ack);
    */
    wait_ms(500);
}