diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index 508f980..35750e8 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -29,7 +29,7 @@ BraccioClass::BraccioClass() , _is_motor_connected{false} , _motors_connected_mtx{} , _motors_connected_thd{} -, _bl{} +, _bl{i2c_mutex} , _gfx{} , _lvgl_disp_drv{} , _lvgl_indev_drv{} @@ -66,15 +66,13 @@ bool BraccioClass::begin(voidFuncPtr custom_menu) pinMode(1, INPUT_PULLUP); - SPI.begin(); - - i2c_mutex.lock(); _bl.begin(); - if (_bl.getChipID() != 0xCE) { + if (_bl.getChipID() != 0xCE) return false; - } - _bl.setColor(red); + _bl.turnOn(); + SPI.begin(); + i2c_mutex.lock(); int ret = _expander.testConnection(); if (ret == false) { diff --git a/src/lib/display/Backlight.cpp b/src/lib/display/Backlight.cpp index 84da88a..b3ec74f 100644 --- a/src/lib/display/Backlight.cpp +++ b/src/lib/display/Backlight.cpp @@ -11,10 +11,26 @@ */ -#include -#include +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + #include "Backlight.h" +/************************************************************************************** + * CTOR/DTOR + **************************************************************************************/ + +Backlight::Backlight(rtos::Mutex & wire_mtx) +: _wire_mtx{wire_mtx} +{ + +} + +/************************************************************************************** + * PUBLIC MEMBER FUNCTIONS + **************************************************************************************/ + void Backlight::begin() { reset(); @@ -27,62 +43,14 @@ void Backlight::end() powerDown(); } -void Backlight::setColor(RGBColors color) +void Backlight::turnOn() { - if(color == off) { - _blue = 0x00; - _green = 0x00; - _red = 0x00; - } - - if(color == green) { - _blue = 0x00; - _green = 0xFF; - _red = 0x00; - } - - if(color == blue) { - _blue = 0xFF; - _green = 0x00; - _red = 0x00; - } - - if(color == red) { - _blue = 0x00; - _green = 0x00; - _red = 0xFF; - } - - if(color == cyan) { - _blue = 0x20; - _green = 0x20; - _red = 0x00; - } - - if(color == magenta) { - _blue = 0x20; - _green = 0x00; - _red = 0x20; - } - - if(color == yellow) { - _blue = 0x00; - _green = 0x20; - _red = 0x20; - } - - setColor(_blue, _green, _red); - + setColor(0xFF, 0xFF, 0xFF); } -void Backlight::setColor(uint8_t blue, uint8_t green, uint8_t red) +void Backlight::turnOff() { - // set rgb led current - writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue); //maximum current - writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green); - writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red); - writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5); // write to color update register for changes to take effect - + setColor(0, 0, 0); } // Read the Chip ID register, this is a good test of communication @@ -92,6 +60,9 @@ uint8_t Backlight::getChipID() return c; } +/************************************************************************************** + * PRIVATE MEMBER FUNCTIONS + **************************************************************************************/ void Backlight::reset() { @@ -122,15 +93,19 @@ void Backlight::init()// configure rgb led function writeByte(IS31FL3194_ADDRESS, 0x32, 0xFF); // Max power on led R (OUT 3) } -void Backlight::ledBlink(RGBColors color, uint32_t duration) +void Backlight::setColor(uint8_t blue, uint8_t green, uint8_t red) { - setColor(color); - delay(duration); - setColor(off); + // set rgb led current + writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue); //maximum current + writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green); + writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red); + writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5); // write to color update register for changes to take effect } void Backlight::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) { + mbed::ScopedLock lock(_wire_mtx); + Wire.beginTransmission(address); Wire.write(subAddress); Wire.write(data); @@ -139,6 +114,8 @@ void Backlight::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) uint8_t Backlight::readByte(uint8_t address, uint8_t subAddress) { + mbed::ScopedLock lock(_wire_mtx); + char response = 0xFF; Wire.beginTransmission(address); Wire.write(subAddress); diff --git a/src/lib/display/Backlight.h b/src/lib/display/Backlight.h index a8af973..e458d1d 100644 --- a/src/lib/display/Backlight.h +++ b/src/lib/display/Backlight.h @@ -1,6 +1,18 @@ #ifndef RGBled_h #define RGBled_h +/************************************************************************************** + * INCLUDE + **************************************************************************************/ + +#include + +#include + +/************************************************************************************** + * DEFINE + **************************************************************************************/ + // Regsiter Map // http://www.issi.com/WW/pdf/IS31FL3194.pdf #define IS31FL3194_PRODUCT_ID 0x00 // should return 0xCE @@ -125,54 +137,37 @@ // light intensity (fraction of current max) #define Imax_frac 0x80 // Imax_frac/256 * Imax = current -enum RGBColors { - off = 0, - red = 1, - green = 2, - blue = 3, - yellow = 4, - magenta = 5, - cyan = 6 -}; - -/* -// allowed colors -#define off 0 -#define red 1 -#define green 2 -#define blue 3 -#define yellow 4 -#define magenta 5 -#define cyan 6 -*/ - -#include "Wire.h" +/************************************************************************************** + * CLASS DECLARATION + **************************************************************************************/ class Backlight { -public: - Backlight() {}; +public: + + Backlight(rtos::Mutex & wire_mtx); + void begin(); void end(); - void setColor(RGBColors color); - void setColor(uint8_t blue, uint8_t green, uint8_t red); + + void turnOn(); + void turnOff(); + uint8_t getChipID(); + private: + + rtos::Mutex & _wire_mtx; + void init(); void reset(); void powerDown(); void powerUp(); - void ledBlink(RGBColors color, uint32_t duration); - void I2Cscan(); + void setColor(uint8_t blue, uint8_t green, uint8_t red); void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); uint8_t readByte(uint8_t address, uint8_t subAddress); - - uint8_t _blue; - uint8_t _green; - uint8_t _red; - }; #endif