Skip to content

Commit 390a002

Browse files
committed
Pass wire mutex via constructor, now back light can be used without explicit locking.
1 parent 25e5c64 commit 390a002

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/Braccio++.cpp

Lines changed: 4 additions & 6 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-
}
7672
_bl.on();
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
* INCLUDE
1616
**************************************************************************************/
1717

18-
#include <Arduino.h>
19-
#include <Wire.h>
2018
#include "Backlight.h"
2119

20+
/**************************************************************************************
21+
* CTOR/DTOR
22+
**************************************************************************************/
23+
24+
Backlight::Backlight(rtos::Mutex & wire_mtx)
25+
: _wire_mtx{wire_mtx}
26+
{
27+
28+
}
29+
2230
/**************************************************************************************
2331
* PUBLIC MEMBER FUNCTIONS
2432
**************************************************************************************/
@@ -96,6 +104,8 @@ void Backlight::setColor(uint8_t blue, uint8_t green, uint8_t red)
96104

97105
void Backlight::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
98106
{
107+
mbed::ScopedLock<rtos::Mutex> lock(_wire_mtx);
108+
99109
Wire.beginTransmission(address);
100110
Wire.write(subAddress);
101111
Wire.write(data);
@@ -104,6 +114,8 @@ void Backlight::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
104114

105115
uint8_t Backlight::readByte(uint8_t address, uint8_t subAddress)
106116
{
117+
mbed::ScopedLock<rtos::Mutex> lock(_wire_mtx);
118+
107119
char response = 0xFF;
108120
Wire.beginTransmission(address);
109121
Wire.write(subAddress);

src/lib/display/Backlight.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* INCLUDE
66
**************************************************************************************/
77

8+
#include <Arduino.h>
9+
810
#include <Wire.h>
911

1012
/**************************************************************************************
@@ -143,7 +145,8 @@ class Backlight
143145
{
144146
public:
145147

146-
Backlight() { }
148+
Backlight(rtos::Mutex & wire_mtx);
149+
147150

148151
void begin();
149152
void end();
@@ -156,6 +159,8 @@ class Backlight
156159

157160
private:
158161

162+
rtos::Mutex & _wire_mtx;
163+
159164
void init();
160165
void reset();
161166
void powerDown();

0 commit comments

Comments
 (0)