Skip to content

Commit 54f1758

Browse files
authored
Merge pull request #38 from bcmi-labs/fix-backlight-api
Fix Backlight API: Backlight can be only turned on/off, no RGB background LED
2 parents def3aa9 + 11937b4 commit 54f1758

File tree

3 files changed

+68
-98
lines changed

3 files changed

+68
-98
lines changed

src/Braccio++.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BraccioClass::BraccioClass()
2929
, _is_motor_connected{false}
3030
, _motors_connected_mtx{}
3131
, _motors_connected_thd{}
32-
, _bl{}
32+
, _bl{i2c_mutex}
3333
, _gfx{}
3434
, _lvgl_disp_drv{}
3535
, _lvgl_indev_drv{}
@@ -66,15 +66,13 @@ bool BraccioClass::begin(voidFuncPtr custom_menu)
6666

6767
pinMode(1, INPUT_PULLUP);
6868

69-
SPI.begin();
70-
71-
i2c_mutex.lock();
7269
_bl.begin();
73-
if (_bl.getChipID() != 0xCE) {
70+
if (_bl.getChipID() != 0xCE)
7471
return false;
75-
}
76-
_bl.setColor(red);
72+
_bl.turnOn();
7773

74+
SPI.begin();
75+
i2c_mutex.lock();
7876
int ret = _expander.testConnection();
7977

8078
if (ret == false) {

src/lib/display/Backlight.cpp

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,26 @@
1111
1212
*/
1313

14-
#include <Arduino.h>
15-
#include <Wire.h>
14+
/**************************************************************************************
15+
* INCLUDE
16+
**************************************************************************************/
17+
1618
#include "Backlight.h"
1719

20+
/**************************************************************************************
21+
* CTOR/DTOR
22+
**************************************************************************************/
23+
24+
Backlight::Backlight(rtos::Mutex & wire_mtx)
25+
: _wire_mtx{wire_mtx}
26+
{
27+
28+
}
29+
30+
/**************************************************************************************
31+
* PUBLIC MEMBER FUNCTIONS
32+
**************************************************************************************/
33+
1834
void Backlight::begin()
1935
{
2036
reset();
@@ -27,62 +43,14 @@ void Backlight::end()
2743
powerDown();
2844
}
2945

30-
void Backlight::setColor(RGBColors color)
46+
void Backlight::turnOn()
3147
{
32-
if(color == off) {
33-
_blue = 0x00;
34-
_green = 0x00;
35-
_red = 0x00;
36-
}
37-
38-
if(color == green) {
39-
_blue = 0x00;
40-
_green = 0xFF;
41-
_red = 0x00;
42-
}
43-
44-
if(color == blue) {
45-
_blue = 0xFF;
46-
_green = 0x00;
47-
_red = 0x00;
48-
}
49-
50-
if(color == red) {
51-
_blue = 0x00;
52-
_green = 0x00;
53-
_red = 0xFF;
54-
}
55-
56-
if(color == cyan) {
57-
_blue = 0x20;
58-
_green = 0x20;
59-
_red = 0x00;
60-
}
61-
62-
if(color == magenta) {
63-
_blue = 0x20;
64-
_green = 0x00;
65-
_red = 0x20;
66-
}
67-
68-
if(color == yellow) {
69-
_blue = 0x00;
70-
_green = 0x20;
71-
_red = 0x20;
72-
}
73-
74-
setColor(_blue, _green, _red);
75-
48+
setColor(0xFF, 0xFF, 0xFF);
7649
}
7750

78-
void Backlight::setColor(uint8_t blue, uint8_t green, uint8_t red)
51+
void Backlight::turnOff()
7952
{
80-
// set rgb led current
81-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue); //maximum current
82-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green);
83-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red);
84-
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5); // write to color update register for changes to take effect
85-
53+
setColor(0, 0, 0);
8654
}
8755

8856
// Read the Chip ID register, this is a good test of communication
@@ -92,6 +60,9 @@ uint8_t Backlight::getChipID()
9260
return c;
9361
}
9462

63+
/**************************************************************************************
64+
* PRIVATE MEMBER FUNCTIONS
65+
**************************************************************************************/
9566

9667
void Backlight::reset()
9768
{
@@ -122,15 +93,19 @@ void Backlight::init()// configure rgb led function
12293
writeByte(IS31FL3194_ADDRESS, 0x32, 0xFF); // Max power on led R (OUT 3)
12394
}
12495

125-
void Backlight::ledBlink(RGBColors color, uint32_t duration)
96+
void Backlight::setColor(uint8_t blue, uint8_t green, uint8_t red)
12697
{
127-
setColor(color);
128-
delay(duration);
129-
setColor(off);
98+
// set rgb led current
99+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue); //maximum current
100+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green);
101+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red);
102+
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5); // write to color update register for changes to take effect
130103
}
131104

132105
void Backlight::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
133106
{
107+
mbed::ScopedLock<rtos::Mutex> lock(_wire_mtx);
108+
134109
Wire.beginTransmission(address);
135110
Wire.write(subAddress);
136111
Wire.write(data);
@@ -139,6 +114,8 @@ void Backlight::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
139114

140115
uint8_t Backlight::readByte(uint8_t address, uint8_t subAddress)
141116
{
117+
mbed::ScopedLock<rtos::Mutex> lock(_wire_mtx);
118+
142119
char response = 0xFF;
143120
Wire.beginTransmission(address);
144121
Wire.write(subAddress);

src/lib/display/Backlight.h

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#ifndef RGBled_h
22
#define RGBled_h
33

4+
/**************************************************************************************
5+
* INCLUDE
6+
**************************************************************************************/
7+
8+
#include <Arduino.h>
9+
10+
#include <Wire.h>
11+
12+
/**************************************************************************************
13+
* DEFINE
14+
**************************************************************************************/
15+
416
// Regsiter Map
517
// http://www.issi.com/WW/pdf/IS31FL3194.pdf
618
#define IS31FL3194_PRODUCT_ID 0x00 // should return 0xCE
@@ -125,54 +137,37 @@
125137
// light intensity (fraction of current max)
126138
#define Imax_frac 0x80 // Imax_frac/256 * Imax = current
127139

128-
enum RGBColors {
129-
off = 0,
130-
red = 1,
131-
green = 2,
132-
blue = 3,
133-
yellow = 4,
134-
magenta = 5,
135-
cyan = 6
136-
};
137-
138-
/*
139-
// allowed colors
140-
#define off 0
141-
#define red 1
142-
#define green 2
143-
#define blue 3
144-
#define yellow 4
145-
#define magenta 5
146-
#define cyan 6
147-
*/
148-
149-
#include "Wire.h"
140+
/**************************************************************************************
141+
* CLASS DECLARATION
142+
**************************************************************************************/
150143

151144
class Backlight
152145
{
153-
public:
154-
Backlight() {};
146+
public:
147+
148+
Backlight(rtos::Mutex & wire_mtx);
149+
155150

156151
void begin();
157152
void end();
158-
void setColor(RGBColors color);
159-
void setColor(uint8_t blue, uint8_t green, uint8_t red);
153+
154+
void turnOn();
155+
void turnOff();
156+
160157
uint8_t getChipID();
161158

159+
162160
private:
161+
162+
rtos::Mutex & _wire_mtx;
163+
163164
void init();
164165
void reset();
165166
void powerDown();
166167
void powerUp();
167-
void ledBlink(RGBColors color, uint32_t duration);
168-
void I2Cscan();
168+
void setColor(uint8_t blue, uint8_t green, uint8_t red);
169169
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
170170
uint8_t readByte(uint8_t address, uint8_t subAddress);
171-
172-
uint8_t _blue;
173-
uint8_t _green;
174-
uint8_t _red;
175-
176171
};
177172

178173
#endif

0 commit comments

Comments
 (0)