library for m3Dpi robot, based on the Pololu 3pi and m3pi. m3Dpi has multiple distance sensors, gyroscope, compass and accelerometer sensor to be fully aware of its environment. With the addition of xbee or nrf24n01 module it has wireless communication capabilities.

Dependencies:   m3pi ADXL345_I2C HMC5583L ITG3200 PCA9547 TLC59116 VL6180x RGB-fun xbee

Dependents:   m3Dpi-helloworld

lib/LedRing.cpp

Committer:
sillevl
Date:
2016-01-20
Revision:
12:4cb092df6958
Parent:
10:4200a8140b10

File content as of revision 12:4cb092df6958:

#include "LedRing.h"

LedRing::LedRing(I2C &i2c) : driver1(i2c, DRIVER_1_ADDRESS), driver2(i2c, DRIVER_2_ADDRESS)
{
    initialize();
}

LedRing::LedRing(PinName sda, PinName scl): driver1(sda, scl, DRIVER_1_ADDRESS), driver2(sda, scl, DRIVER_2_ADDRESS)
{
    initialize();
}

void LedRing::initialize()
{
    driver1.enable();
    driver2.enable();
    
    setBrightness(0.05);
}

void LedRing::setColor(int led, Color* color)
{
    led--; // zero indexed from now on
    led = led % 8;
    
    int channelMap[8] = {0, 3, 6, 0, 3, 6, 9, 9};
    int driverMap[8]  = {1, 1, 1, 2, 2, 2, 2, 1};
    
    TLC59116* driver = (driverMap[led] == 1) ? &driver1 : &driver2;
    int startChannel = channelMap[led];
        
    setLed(driver, startChannel, color);
}

void LedRing::setColor(int led, int color)
{
    Color* _color = new Color(color);
    setColor(led, _color);
    delete _color;
}

void LedRing::setAll(Color* color)
{
    for(int i = 0; i < 8; i++){
        setColor(i+1, color);
    }
}

void LedRing::setAll(int color)
{
    Color* _color = new Color(color);
    setAll(_color);
    delete _color;
}

void LedRing::setAll(int* colors)
{
    for(int i = 0; i < 8; i++){
        setColor(i+1, colors[i]);
    }
}

void LedRing::setLed(TLC59116* driver, int startChannel, Color* color)
{
    driver->setChannel(startChannel + 0, color->getRed() / 255.0);
    driver->setChannel(startChannel + 1, color->getGreen() / 255.0);
    driver->setChannel(startChannel + 2, color->getBlue() / 255.0);    
}

void LedRing::setBrightness(float brightness)
{
    driver1.setBrightness(brightness);
    driver2.setBrightness(brightness);
}

void LedRing::greenRedGradient(int led, int value)
{
    Color* _color = new Color(255 - value, value, 0);
    setColor(led, _color);
    delete _color;
}