Implemented first Hangar-Service
Dependencies: CalibrateMagneto QuaternionMath
Fork of SML2 by
LEDDriver.cpp@33:bd56fc8aeb0a, 2015-05-05 (annotated)
- Committer:
- pvaibhav
- Date:
- Tue May 05 09:59:11 2015 +0000
- Revision:
- 33:bd56fc8aeb0a
- Parent:
- 25:abb0f208e6a9
- Child:
- 35:fb6e4601adf3
White LED mirrors blue LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pvaibhav | 0:943820483318 | 1 | #include "LEDDriver.h" |
pvaibhav | 0:943820483318 | 2 | #include "mbed.h" |
pvaibhav | 1:c279bc3af90c | 3 | #define DEBUG "LEDDriver" |
pvaibhav | 0:943820483318 | 4 | #include "Logger.h" |
pvaibhav | 0:943820483318 | 5 | |
pvaibhav | 12:1632d7391453 | 6 | LEDDriver::LEDDriver(I2C &i2c) : I2CPeripheral(i2c, 0x60 /* I2C address of the LED controller */) |
pvaibhav | 12:1632d7391453 | 7 | { |
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 | 12:1632d7391453 | 16 | |
pvaibhav | 0:943820483318 | 17 | setColor(Colors::black); |
pvaibhav | 12:1632d7391453 | 18 | |
pvaibhav | 4:e759b8c756da | 19 | INFO("LED driver ready"); |
pvaibhav | 0:943820483318 | 20 | } |
pvaibhav | 0:943820483318 | 21 | |
pvaibhav | 12:1632d7391453 | 22 | void LEDDriver::setColor(const uint8_t r, const uint8_t g, const uint8_t b) |
pvaibhav | 12:1632d7391453 | 23 | { |
pvaibhav | 33:bd56fc8aeb0a | 24 | write_reg(0x0e, b); |
pvaibhav | 25:abb0f208e6a9 | 25 | write_reg(0x04, g); |
pvaibhav | 25:abb0f208e6a9 | 26 | write_reg(0x03, r); |
pvaibhav | 0:943820483318 | 27 | write_reg(0x02, b); |
pvaibhav | 0:943820483318 | 28 | } |
pvaibhav | 0:943820483318 | 29 | |
pvaibhav | 0:943820483318 | 30 | void LEDDriver::setColor(const float r, const float g, const float b) |
pvaibhav | 0:943820483318 | 31 | { |
pvaibhav | 0:943820483318 | 32 | setColor(r * 255, g * 255, b * 255); |
pvaibhav | 0:943820483318 | 33 | } |
pvaibhav | 0:943820483318 | 34 | |
pvaibhav | 0:943820483318 | 35 | void LEDDriver::setColor(const Color color) |
pvaibhav | 0:943820483318 | 36 | { |
pvaibhav | 0:943820483318 | 37 | const uint8_t r = (color >> 16) & 0xff; |
pvaibhav | 0:943820483318 | 38 | const uint8_t g = (color >> 8) & 0xff; |
pvaibhav | 0:943820483318 | 39 | const uint8_t b = (color >> 0) & 0xff; |
pvaibhav | 0:943820483318 | 40 | setColor(r, g, b); |
pvaibhav | 0:943820483318 | 41 | } |
pvaibhav | 0:943820483318 | 42 | |
pvaibhav | 33:bd56fc8aeb0a | 43 | void LEDDriver::setWhiteLed(const float w) |
pvaibhav | 33:bd56fc8aeb0a | 44 | { |
pvaibhav | 33:bd56fc8aeb0a | 45 | const uint8_t value = (uint8_t) (w * 255); |
pvaibhav | 33:bd56fc8aeb0a | 46 | write_reg(0x0e, value); |
pvaibhav | 33:bd56fc8aeb0a | 47 | } |
pvaibhav | 33:bd56fc8aeb0a | 48 | |
pvaibhav | 12:1632d7391453 | 49 | LEDDriver& LEDDriver::operator=(const Color& c) |
pvaibhav | 12:1632d7391453 | 50 | { |
pvaibhav | 0:943820483318 | 51 | setColor(c); |
pvaibhav | 0:943820483318 | 52 | return *this; |
pvaibhav | 0:943820483318 | 53 | } |
pvaibhav | 0:943820483318 | 54 | |
pvaibhav | 18:f51b1a94a6e2 | 55 | void LEDDriver::setOutputCurrent(float mA) |
pvaibhav | 12:1632d7391453 | 56 | { |
pvaibhav | 18:f51b1a94a6e2 | 57 | if (mA < 0.1) |
pvaibhav | 18:f51b1a94a6e2 | 58 | mA = 0.1; |
pvaibhav | 18:f51b1a94a6e2 | 59 | if (mA > 25.5) |
pvaibhav | 18:f51b1a94a6e2 | 60 | mA = 25.5; |
pvaibhav | 0:943820483318 | 61 | write_reg(0x05, mA * 10); |
pvaibhav | 0:943820483318 | 62 | write_reg(0x06, mA * 10); |
pvaibhav | 0:943820483318 | 63 | write_reg(0x07, mA * 10); |
pvaibhav | 33:bd56fc8aeb0a | 64 | write_reg(0x0f, mA * 10); |
pvaibhav | 1:c279bc3af90c | 65 | INFO("Output current => %0.1f mA", mA); |
pvaibhav | 0:943820483318 | 66 | } |