Skip to content

Fix Backlight API: Backlight can be only turned on/off, no RGB background LED #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/Braccio++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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) {
Expand Down
93 changes: 35 additions & 58 deletions src/lib/display/Backlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@

*/

#include <Arduino.h>
#include <Wire.h>
/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include "Backlight.h"

/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/

Backlight::Backlight(rtos::Mutex & wire_mtx)
: _wire_mtx{wire_mtx}
{

}

/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/

void Backlight::begin()
{
reset();
Expand All @@ -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
Expand All @@ -92,6 +60,9 @@ uint8_t Backlight::getChipID()
return c;
}

/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/

void Backlight::reset()
{
Expand Down Expand Up @@ -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<rtos::Mutex> lock(_wire_mtx);

Wire.beginTransmission(address);
Wire.write(subAddress);
Wire.write(data);
Expand All @@ -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<rtos::Mutex> lock(_wire_mtx);

char response = 0xFF;
Wire.beginTransmission(address);
Wire.write(subAddress);
Expand Down
61 changes: 28 additions & 33 deletions src/lib/display/Backlight.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#ifndef RGBled_h
#define RGBled_h

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <Arduino.h>

#include <Wire.h>

/**************************************************************************************
* DEFINE
**************************************************************************************/

// Regsiter Map
// http://www.issi.com/WW/pdf/IS31FL3194.pdf
#define IS31FL3194_PRODUCT_ID 0x00 // should return 0xCE
Expand Down Expand Up @@ -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