Skip to content

Commit 5c90e4e

Browse files
laineeelaineeewmmc88
authored
add led matrix lib (#163)
* add led matrix lib * clang changes and test app * changed tabbing for clang formatting * clang formatting * more clang formatting * clang changes * include threading for flashing function * fix build errors * implement thread flashing and delete * fix build issues * fix logic and add destructor * implement flags * remove dynamic threads * add more tests * add led states * remove led state var * fix build errors * fix submodule * fix submodule stuff * add ledmatrix namespace * add led namespace * fix clang format and comments * fix clang formatting in hw bridge * change pin names and add comments * change pin names and add comments * add flag variables to class * fix clang formatting * updating submodule to latest * update hw bridge * refactor cmake * clang format * Update libs/led-matrix/include/LEDMatrix.h Co-authored-by: Melvin Wang <melvin.mc.wang@gmail.com> * add whitespace for readability Co-authored-by: laineee <ieitua@uwaterloo.ca> Co-authored-by: Melvin Wang <melvin.mc.wang@gmail.com>
1 parent 2ceb76a commit 5c90e4e

File tree

8 files changed

+226
-1
lines changed

8 files changed

+226
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ add_subdirectory(${LIBS_DIR}/encoder)
6969
add_subdirectory(${LIBS_DIR}/gamepad)
7070
add_subdirectory(${LIBS_DIR}/gpio)
7171
add_subdirectory(${LIBS_DIR}/controllers)
72+
add_subdirectory(${LIBS_DIR}/led-matrix)
7273
add_subdirectory(${LIBS_DIR}/neopixel)
7374
add_subdirectory(${LIBS_DIR}/pid)
7475
add_subdirectory(${LIBS_DIR}/sensors)
@@ -103,6 +104,7 @@ add_app_subdirectory(${TEST_APPS_DIR}/test-blinky)
103104
add_app_subdirectory(${TEST_APPS_DIR}/test-blockingneo)
104105
add_app_subdirectory(${TEST_APPS_DIR}/test-can)
105106
add_app_subdirectory(${TEST_APPS_DIR}/test-mae3)
107+
add_app_subdirectory(${TEST_APPS_DIR}/test-led-matrix)
106108
add_app_subdirectory(${TEST_APPS_DIR}/test-logger)
107109
add_app_subdirectory(${TEST_APPS_DIR}/test-lookup-table)
108110
add_app_subdirectory(${TEST_APPS_DIR}/test-pid)

libs/led-matrix/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_library(LEDMatrix STATIC)
2+
target_sources(LEDMatrix PRIVATE src/LEDMatrix.cpp)
3+
target_include_directories(LEDMatrix PUBLIC include)
4+
target_link_libraries(LEDMatrix
5+
PRIVATE
6+
uwrt-mars-rover-hw-bridge
7+
mbed-os
8+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include "hw_bridge.h"
3+
#include "mbed.h"
4+
5+
class LEDMatrix {
6+
public:
7+
// Define LED matrix colour channels by the pins it is connected to.
8+
LEDMatrix(PinName R, PinName G, PinName B);
9+
10+
// Terminate thread and clear lights.
11+
~LEDMatrix();
12+
13+
// Set the state of the LEDs.
14+
void setState(HWBRIDGE::LEDMATRIX::LEDMatrixState state);
15+
16+
// Periodically flash the colour given by R, G, B on the LED matrix.
17+
void setFlashColor(bool R, bool G, bool B);
18+
19+
// Set a solid colour given by the R, G, B on the LED matrix.
20+
void setSolidColor(bool R, bool G, bool B);
21+
22+
private:
23+
DigitalOut m_RChannel;
24+
DigitalOut m_GChannel;
25+
DigitalOut m_BChannel;
26+
27+
// Flags
28+
constexpr static uint32_t START_FLASH = (1U << 0); // 01
29+
constexpr static uint32_t ENDED_FLASH = (1U << 1); // 10
30+
31+
volatile bool m_flashing_red, m_flashing_green, m_flashing_blue;
32+
volatile bool m_continue_flashing;
33+
34+
EventFlags m_event_flags;
35+
Thread m_lightsThread;
36+
static constexpr auto PERIOD_DELAY = 500ms;
37+
38+
// Thread to take care of flashing colours on the LED matrix by setFlashColor().
39+
void flashing();
40+
41+
// Set the given R, G, B colour channels.
42+
void setColor(bool R, bool G, bool B);
43+
};

libs/led-matrix/src/LEDMatrix.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "LEDMatrix.h"
2+
3+
LEDMatrix::LEDMatrix(PinName R, PinName G, PinName B)
4+
: m_RChannel(R),
5+
m_GChannel(G),
6+
m_BChannel(B),
7+
m_flashing_red(false),
8+
m_flashing_green(false),
9+
m_flashing_blue(false),
10+
m_continue_flashing(false) {
11+
m_lightsThread.start(callback(this, &LEDMatrix::flashing));
12+
}
13+
14+
LEDMatrix::~LEDMatrix() {
15+
m_lightsThread.terminate();
16+
setSolidColor(0, 0, 0);
17+
}
18+
19+
void LEDMatrix::setState(HWBRIDGE::LEDMATRIX::LEDMatrixState state) {
20+
switch (state) {
21+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_RED:
22+
setSolidColor(1, 0, 0);
23+
break;
24+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_BLUE:
25+
setSolidColor(0, 0, 1);
26+
break;
27+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_GREEN:
28+
setSolidColor(0, 1, 0);
29+
break;
30+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_RED:
31+
setFlashColor(1, 0, 0);
32+
break;
33+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_BLUE:
34+
setFlashColor(0, 0, 1);
35+
break;
36+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_GREEN:
37+
setFlashColor(0, 1, 0);
38+
break;
39+
case HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF:
40+
setSolidColor(0, 0, 0);
41+
break;
42+
}
43+
}
44+
45+
void LEDMatrix::flashing() { // check https://os.mbed.com/docs/mbed-os/v6.8/apis/thisthread.html
46+
while (true) {
47+
m_event_flags.wait_all(START_FLASH); // yield until flag is set
48+
m_continue_flashing = true;
49+
50+
while (m_continue_flashing) {
51+
setColor(m_flashing_red, m_flashing_green, m_flashing_blue);
52+
ThisThread::sleep_for(PERIOD_DELAY);
53+
setColor(0, 0, 0);
54+
ThisThread::sleep_for(PERIOD_DELAY);
55+
}
56+
m_event_flags.set(ENDED_FLASH);
57+
}
58+
}
59+
60+
void LEDMatrix::setFlashColor(bool R, bool G, bool B) {
61+
m_flashing_red = R;
62+
m_flashing_green = G;
63+
m_flashing_blue = B;
64+
65+
if (!m_continue_flashing) {
66+
m_event_flags.set(START_FLASH);
67+
}
68+
}
69+
70+
void LEDMatrix::setSolidColor(bool R, bool G, bool B) {
71+
if (m_continue_flashing) {
72+
m_continue_flashing = false;
73+
m_event_flags.wait_all(ENDED_FLASH); // wait for while loop in flashing() to finish
74+
}
75+
setColor(R, G, B);
76+
}
77+
78+
void LEDMatrix::setColor(bool R, bool G, bool B) {
79+
m_RChannel = R;
80+
m_GChannel = G;
81+
m_BChannel = B;
82+
}

supported_build_configurations.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ test-mae3:
5151
# - SCIENCE_REV2
5252
- UWRT_NUCLEO
5353

54+
test-led-matrix:
55+
- UWRT_NUCLEO
56+
5457
test-logger:
5558
- GIMBAL_REV2
5659
- PDB_REV2
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_executable(test-led-matrix)
2+
target_sources(test-led-matrix PRIVATE src/main.cpp)
3+
target_link_libraries(test-led-matrix
4+
PRIVATE
5+
LEDMatrix
6+
uwrt-mars-rover-hw-bridge
7+
mbed-os
8+
)
9+
mbed_set_post_build(test-led-matrix)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "LEDMatrix.h"
2+
#include "hw_bridge.h"
3+
#include "mbed.h"
4+
5+
int main() {
6+
printf("####### STARTING LED MATRIX TEST #######\r\n");
7+
8+
LEDMatrix blinky(D8, D9, D10);
9+
10+
while (true) {
11+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_RED);
12+
ThisThread::sleep_for(5s);
13+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_BLUE);
14+
ThisThread::sleep_for(5s);
15+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_GREEN);
16+
ThisThread::sleep_for(5s);
17+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF);
18+
ThisThread::sleep_for(5s);
19+
20+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_RED);
21+
ThisThread::sleep_for(5s);
22+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_BLUE);
23+
ThisThread::sleep_for(5s);
24+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_GREEN);
25+
ThisThread::sleep_for(5s);
26+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF);
27+
ThisThread::sleep_for(5s);
28+
29+
blinky.setFlashColor(1, 0, 0); // red
30+
ThisThread::sleep_for(5s);
31+
blinky.setFlashColor(0, 1, 0); // green
32+
ThisThread::sleep_for(5s);
33+
blinky.setFlashColor(0, 0, 1); // blue
34+
ThisThread::sleep_for(5s);
35+
36+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF);
37+
ThisThread::sleep_for(5s);
38+
blinky.setSolidColor(1, 0, 0); // red
39+
ThisThread::sleep_for(5s);
40+
blinky.setSolidColor(0, 1, 0); // green
41+
ThisThread::sleep_for(5s);
42+
blinky.setSolidColor(0, 0, 1); // blue
43+
ThisThread::sleep_for(5s);
44+
45+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF);
46+
ThisThread::sleep_for(5s);
47+
blinky.setFlashColor(1, 0, 1); // purple
48+
ThisThread::sleep_for(5s);
49+
blinky.setFlashColor(1, 1, 0); // brownish
50+
ThisThread::sleep_for(5s);
51+
blinky.setFlashColor(0, 1, 1); // blue-green
52+
ThisThread::sleep_for(5s);
53+
54+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF);
55+
ThisThread::sleep_for(5s);
56+
blinky.setSolidColor(1, 0, 1); // purple
57+
ThisThread::sleep_for(5s);
58+
blinky.setSolidColor(1, 1, 0); // brownish
59+
ThisThread::sleep_for(5s);
60+
blinky.setSolidColor(0, 1, 1); // blue-green
61+
ThisThread::sleep_for(5s);
62+
63+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_RED);
64+
ThisThread::sleep_for(5s);
65+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_GREEN);
66+
ThisThread::sleep_for(5s);
67+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_BLUE);
68+
ThisThread::sleep_for(5s);
69+
70+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::FLASHING_GREEN);
71+
ThisThread::sleep_for(5s);
72+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_BLUE);
73+
ThisThread::sleep_for(5s);
74+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::SOLID_RED);
75+
ThisThread::sleep_for(5s);
76+
blinky.setState(HWBRIDGE::LEDMATRIX::LEDMatrixState::OFF);
77+
}
78+
}

0 commit comments

Comments
 (0)