Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

Committer:
pvaibhav
Date:
Tue Jan 13 11:23:01 2015 +0000
Revision:
0:943820483318
Child:
1:c279bc3af90c
Initial commit with working LED and motor drivers.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pvaibhav 0:943820483318 1 #include "LEDDriver.h"
pvaibhav 0:943820483318 2 #include "mbed.h"
pvaibhav 0:943820483318 3 #define DEBUG "LEDDriver.cpp"
pvaibhav 0:943820483318 4 #include "Logger.h"
pvaibhav 0:943820483318 5 #include <cassert>
pvaibhav 0:943820483318 6
pvaibhav 0:943820483318 7 LEDDriver::LEDDriver(I2C &i2c) : I2CPeripheral(i2c, 0x60 /* I2C address of the LED controller */) {
pvaibhav 0:943820483318 8 // cf. LP5562TME/NOPB datasheet page 14
pvaibhav 0:943820483318 9 // Supply 3.6V to VDD (hardware - done)
pvaibhav 0:943820483318 10 // Supply 1.8V to EN (hardware - done)
pvaibhav 0:943820483318 11 wait_ms(1); // wait 1 ms
pvaibhav 0:943820483318 12 write_reg(0x00, 0xC0); // chip_en to 1, log_en to 1
pvaibhav 0:943820483318 13 wait_us(500); // wait 500 usec
pvaibhav 0:943820483318 14 write_reg(0x08, 0x61); // set PWM freq high, enable power save mode, start internal clock
pvaibhav 0:943820483318 15 write_reg(0x70, 0x00); // configure all LEDs to be controlled from I2C registers
pvaibhav 0:943820483318 16
pvaibhav 0:943820483318 17 setColor(Colors::black);
pvaibhav 0:943820483318 18 }
pvaibhav 0:943820483318 19
pvaibhav 0:943820483318 20 void LEDDriver::setColor(const uint8_t r, const uint8_t g, const uint8_t b) {
pvaibhav 0:943820483318 21 write_reg(0x04, r);
pvaibhav 0:943820483318 22 write_reg(0x03, g);
pvaibhav 0:943820483318 23 write_reg(0x02, b);
pvaibhav 0:943820483318 24 }
pvaibhav 0:943820483318 25
pvaibhav 0:943820483318 26 void LEDDriver::setColor(const float r, const float g, const float b)
pvaibhav 0:943820483318 27 {
pvaibhav 0:943820483318 28 setColor(r * 255, g * 255, b * 255);
pvaibhav 0:943820483318 29 }
pvaibhav 0:943820483318 30
pvaibhav 0:943820483318 31 void LEDDriver::setColor(const Color color)
pvaibhav 0:943820483318 32 {
pvaibhav 0:943820483318 33 const uint8_t r = (color >> 16) & 0xff;
pvaibhav 0:943820483318 34 const uint8_t g = (color >> 8) & 0xff;
pvaibhav 0:943820483318 35 const uint8_t b = (color >> 0) & 0xff;
pvaibhav 0:943820483318 36 setColor(r, g, b);
pvaibhav 0:943820483318 37 }
pvaibhav 0:943820483318 38
pvaibhav 0:943820483318 39 LEDDriver& LEDDriver::operator=(const Color& c) {
pvaibhav 0:943820483318 40 setColor(c);
pvaibhav 0:943820483318 41 return *this;
pvaibhav 0:943820483318 42 }
pvaibhav 0:943820483318 43
pvaibhav 0:943820483318 44 void LEDDriver::setOutputCurrent(const float mA) {
pvaibhav 0:943820483318 45 assert(mA <= 25.5);
pvaibhav 0:943820483318 46 assert(mA >= 0.1);
pvaibhav 0:943820483318 47 write_reg(0x05, mA * 10);
pvaibhav 0:943820483318 48 write_reg(0x06, mA * 10);
pvaibhav 0:943820483318 49 write_reg(0x07, mA * 10);
pvaibhav 0:943820483318 50 INFO("LED output current => %0.1f mA", mA);
pvaibhav 0:943820483318 51 }