Skip to content

Commit ecf1342

Browse files
committed
pbio/sys: Begin simplifying light_matrix.
There are three files named light_matrix.h, and several structures with a single field defined in one of the other ones. Some were directly including from the ../src/ directory. There were also funcs types, but they had only one function, and they always use a led_array. In practice, there will be only one light matrix. It has only one function to set one pixel on one led array. This simplifies it by cutting out some of the abstractions. And it still allows having more than one light matrix if we ever need it. We can remove the pbio/sys/light_matrix abstraction in the next commit.
1 parent e951375 commit ecf1342

File tree

8 files changed

+68
-84
lines changed

8 files changed

+68
-84
lines changed

lib/pbio/include/pbio/light_matrix.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@ typedef struct _pbio_light_matrix_t pbio_light_matrix_t;
2020

2121
#if PBIO_CONFIG_LIGHT_MATRIX
2222

23+
pbio_error_t pbio_light_matrix_get_dev(uint8_t index, uint8_t size, pbio_light_matrix_t **light_matrix);
2324
uint8_t pbio_light_matrix_get_size(pbio_light_matrix_t *light_matrix);
2425
void pbio_light_matrix_set_orientation(pbio_light_matrix_t *light_matrix, pbio_geometry_side_t up_side);
2526
pbio_error_t pbio_light_matrix_clear(pbio_light_matrix_t *light_matrix);
2627
pbio_error_t pbio_light_matrix_set_rows(pbio_light_matrix_t *light_matrix, const uint8_t *rows);
27-
pbio_error_t pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness);
28+
pbio_error_t pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness, bool clear_animation);
2829
pbio_error_t pbio_light_matrix_set_image(pbio_light_matrix_t *light_matrix, const uint8_t *image);
2930
void pbio_light_matrix_start_animation(pbio_light_matrix_t *light_matrix, const uint8_t *cells, uint8_t num_cells, uint16_t interval);
3031
void pbio_light_matrix_stop_animation(pbio_light_matrix_t *light_matrix);
3132

3233
#else // PBIO_CONFIG_LIGHT_MATRIX
3334

35+
static inline pbio_error_t pbio_light_matrix_get_dev(uint8_t index, uint8_t size, pbio_light_matrix_t **light_matrix) {
36+
return PBIO_ERROR_NOT_SUPPORTED;
37+
}
38+
3439
static inline uint8_t pbio_light_matrix_get_size(pbio_light_matrix_t *light_matrix) {
3540
return 0;
3641
}
@@ -46,7 +51,7 @@ static inline pbio_error_t pbio_light_matrix_set_rows(pbio_light_matrix_t *light
4651
return PBIO_ERROR_NOT_SUPPORTED;
4752
}
4853

49-
static inline pbio_error_t pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness) {
54+
static inline pbio_error_t pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness, bool clear_animation) {
5055
return PBIO_ERROR_NOT_SUPPORTED;
5156
}
5257

lib/pbio/platform/ev3/pbioconfig.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#define PBIO_CONFIG_IMAGE (1)
99
#define PBIO_CONFIG_IMU (0)
1010
#define PBIO_CONFIG_LIGHT (1)
11-
#define PBIO_CONFIG_LIGHT_MATRIX (1)
1211
#define PBIO_CONFIG_LOGGER (1)
1312
#define PBIO_CONFIG_MOTOR_PROCESS (1)
1413
#define PBIO_CONFIG_PORT (1)

lib/pbio/platform/prime_hub/pbioconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define PBIO_CONFIG_LIGHT (1)
1010
#define PBIO_CONFIG_LOGGER (1)
1111
#define PBIO_CONFIG_LIGHT_MATRIX (1)
12+
#define PBIO_CONFIG_LIGHT_MATRIX_NUM_DEV (1)
1213
#define PBIO_CONFIG_MOTOR_PROCESS (1)
1314
#define PBIO_CONFIG_PORT (1)
1415
#define PBIO_CONFIG_PORT_NUM_DEV (6)

lib/pbio/src/light/light_matrix.c

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@
1414
#include "animation.h"
1515
#include "light_matrix.h"
1616

17+
static pbio_light_matrix_t light_matrices[PBIO_CONFIG_LIGHT_MATRIX_NUM_DEV];
18+
19+
/**
20+
* Gets and initializes the light matrix.
21+
*
22+
* Skips initialization if already initialized.
23+
*
24+
* @param [in] index Device index.
25+
* @param [in] size The size of the square light matrix.
26+
* @param [out] light_matrix The light matrix
27+
* @return ::PBIO_SUCCESS if device is available and initialization succeeds or completed previously.
28+
* ::PBIO_ERROR_NO_DEV if this index is invalid.
29+
*/
30+
pbio_error_t pbio_light_matrix_get_dev(uint8_t index, uint8_t size, pbio_light_matrix_t **light_matrix) {
31+
if (index >= PBIO_CONFIG_LIGHT_MATRIX_NUM_DEV) {
32+
return PBIO_ERROR_NO_DEV;
33+
}
34+
pbio_light_matrix_t *dev = &light_matrices[index];
35+
*light_matrix = dev;
36+
37+
if (dev->led_array_dev) {
38+
// Already configured.
39+
return PBIO_SUCCESS;
40+
}
41+
42+
pbio_error_t err = pbdrv_led_array_get_dev(index, &dev->led_array_dev);
43+
if (err != PBIO_SUCCESS) {
44+
return err;
45+
}
46+
47+
dev->size = size;
48+
pbio_light_animation_init(&dev->animation, NULL);
49+
return PBIO_SUCCESS;
50+
}
51+
1752
/**
1853
* Sets the pixel to a given brightness.
1954
*
@@ -54,23 +89,12 @@ static pbio_error_t _pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matr
5489
}
5590
}
5691

57-
// Set the pixel brightness
58-
return light_matrix->funcs->set_pixel(light_matrix, row, col, brightness);
59-
}
92+
if (!light_matrix->led_array_dev) {
93+
return PBIO_ERROR_NO_DEV;
94+
}
6095

61-
/**
62-
* Initializes the required fields in a ::pbio_light_matrix_t.
63-
*
64-
* This function must be called before using the ::pbio_light_matrix_t.
65-
*
66-
* @param [in] light_matrix The struct to initialize.
67-
* @param [in] size The size of the light matrix.
68-
* @param [in] funcs The instance-specific callback functions.
69-
*/
70-
void pbio_light_matrix_init(pbio_light_matrix_t *light_matrix, uint8_t size, const pbio_light_matrix_funcs_t *funcs) {
71-
light_matrix->size = size;
72-
light_matrix->funcs = funcs;
73-
pbio_light_animation_init(&light_matrix->animation, NULL);
96+
// Set the pixel brightness
97+
return pbdrv_led_array_set_brightness(light_matrix->led_array_dev, row * light_matrix->size + col, brightness);
7498
}
7599

76100
/**
@@ -173,12 +197,13 @@ pbio_error_t pbio_light_matrix_set_rows(pbio_light_matrix_t *light_matrix, const
173197
* @param [in] row Row index (0 to size-1)
174198
* @param [in] col Column index (0 to size-1)
175199
* @param [in] brightness Brightness (0 to 100)
200+
* @param [in] clear_animation Whether to clear the matrix if an animation was active.
176201
* @return ::PBIO_SUCCESS on success or an
177202
* implementation-specific error on failure.
178203
*/
179-
pbio_error_t pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness) {
204+
pbio_error_t pbio_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness, bool clear_animation) {
180205

181-
if (pbio_light_animation_is_started(&light_matrix->animation)) {
206+
if (clear_animation && pbio_light_animation_is_started(&light_matrix->animation)) {
182207
pbio_light_matrix_clear(light_matrix);
183208
}
184209
return _pbio_light_matrix_set_pixel(light_matrix, row, col, brightness);

lib/pbio/src/light/light_matrix.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <stdint.h>
55

6+
#include <pbdrv/led.h>
7+
68
#include <pbio/error.h>
79
#include <pbio/light_matrix.h>
810

@@ -11,25 +13,9 @@
1113
#ifndef _PBIO_LIGHT_LIGHT_MATRIX_H_
1214
#define _PBIO_LIGHT_LIGHT_MATRIX_H_
1315

14-
/** Implementation-specific callbacks for a light matrix. */
15-
typedef struct {
16-
/**
17-
* Sets the light at @p row, @p col to @p brightness.
18-
*
19-
* @param [in] light_matrix The light matrix instance.
20-
* @param [in] row The row index (0 to size-1).
21-
* @param [in] col The column index (0 to size-1).
22-
* @param [in] brightness The apparent brightness (0 to 100).
23-
* @return Success/failure of the operation.
24-
*/
25-
pbio_error_t (*set_pixel)(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness);
26-
} pbio_light_matrix_funcs_t;
27-
2816
struct _pbio_light_matrix_t {
2917
/** Animation instance for background animation. */
3018
pbio_light_animation_t animation;
31-
/** Implementation specific callback functions. */
32-
const pbio_light_matrix_funcs_t *funcs;
3319
/** Animation cell data. */
3420
const uint8_t *animation_cells;
3521
/** The number of cells in @p animation_cells */
@@ -42,8 +28,8 @@ struct _pbio_light_matrix_t {
4228
uint8_t size;
4329
/** Orientation of the matrix: which side is "up". */
4430
pbio_geometry_side_t up_side;
31+
/** The driver for this light matrix. */
32+
pbdrv_led_array_dev_t *led_array_dev;
4533
};
4634

47-
void pbio_light_matrix_init(pbio_light_matrix_t *light_matrix, uint8_t size, const pbio_light_matrix_funcs_t *funcs);
48-
4935
#endif // _PBIO_LIGHT_LIGHT_MATRIX_H_

lib/pbio/sys/light_matrix.c

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,8 @@
2323

2424
#if PBSYS_CONFIG_HUB_LIGHT_MATRIX
2525

26-
typedef struct {
27-
/** Struct for PBIO light matrix implementation. */
28-
pbio_light_matrix_t light_matrix;
29-
} pbsys_hub_light_matrix_t;
30-
31-
static pbsys_hub_light_matrix_t pbsys_hub_light_matrix_instance;
32-
33-
/** The hub built-in light matrix instance. */
34-
pbio_light_matrix_t *pbsys_hub_light_matrix = &pbsys_hub_light_matrix_instance.light_matrix;
35-
36-
static pbio_error_t pbsys_hub_light_matrix_set_pixel(pbio_light_matrix_t *light_matrix, uint8_t row, uint8_t col, uint8_t brightness) {
37-
#if PBSYS_CONFIG_HUB_LIGHT_MATRIX_LED_ARRAY
38-
// REVISIT: currently hub light matrix is hard-coded as LED array at index 0
39-
// on all platforms
40-
pbdrv_led_array_dev_t *array;
41-
if (pbdrv_led_array_get_dev(0, &array) == PBIO_SUCCESS) {
42-
return pbdrv_led_array_set_brightness(array, row * light_matrix->size + col, brightness);
43-
}
44-
#elif PBSYS_CONFIG_HUB_LIGHT_MATRIX_DISPLAY
45-
pbio_image_t *display = pbdrv_display_get_image();
46-
uint8_t value = brightness * (pbdrv_display_get_max_value() + 1) / 100;
47-
const uint32_t size = PBDRV_CONFIG_DISPLAY_NUM_ROWS / PBSYS_CONFIG_HMI_NUM_SLOTS;
48-
const uint32_t width = size * 4 / 5;
49-
const uint32_t offset = (PBDRV_CONFIG_DISPLAY_NUM_COLS - (PBSYS_CONFIG_HMI_NUM_SLOTS * size)) / 2;
50-
pbio_image_fill_rect(display, col * size + offset, row * size, width, width, value);
51-
pbdrv_display_update();
52-
return PBIO_SUCCESS;
53-
#endif
54-
return PBIO_ERROR_NOT_SUPPORTED;
55-
}
56-
57-
static const pbio_light_matrix_funcs_t pbsys_hub_light_matrix_funcs = {
58-
.set_pixel = pbsys_hub_light_matrix_set_pixel,
59-
};
26+
// revisit, we will drop this in the next commit
27+
pbio_light_matrix_t *pbsys_hub_light_matrix;
6028

6129
/**
6230
* Displays the idle UI. Has a square stop sign and selected slot on bottom row.
@@ -70,7 +38,7 @@ static void pbsys_hub_light_matrix_show_idle_ui(uint8_t brightness) {
7038
#if PBSYS_CONFIG_HMI_NUM_SLOTS
7139
is_on |= (r == 4 && c == pbsys_status_get_selected_slot());
7240
#endif
73-
pbsys_hub_light_matrix_set_pixel(pbsys_hub_light_matrix, r, c, is_on ? brightness : 0);
41+
pbio_light_matrix_set_pixel(pbsys_hub_light_matrix, r, c, is_on ? brightness : 0, false);
7442
}
7543
}
7644
}
@@ -109,7 +77,7 @@ static void pbsys_hub_light_matrix_start_power_animation(void) {
10977
}
11078

11179
void pbsys_hub_light_matrix_init(void) {
112-
pbio_light_matrix_init(pbsys_hub_light_matrix, 5, &pbsys_hub_light_matrix_funcs);
80+
pbio_light_matrix_get_dev(0, 5, &pbsys_hub_light_matrix);
11381
pbsys_hub_light_matrix_start_power_animation();
11482
}
11583

@@ -124,7 +92,7 @@ void pbsys_hub_light_matrix_deinit(void) {
12492
static void pbsys_hub_light_matrix_user_program_animation_clear(void) {
12593
for (uint8_t r = 0; r < 3; r++) {
12694
for (uint8_t c = 1; c < 4; c++) {
127-
pbsys_hub_light_matrix_set_pixel(pbsys_hub_light_matrix, r, c, 0);
95+
pbio_light_matrix_set_pixel(pbsys_hub_light_matrix, r, c, 0, true);
12896
}
12997
}
13098
}
@@ -144,7 +112,7 @@ static uint32_t pbsys_hub_light_matrix_user_program_animation_next(pbio_light_an
144112
uint8_t brightness = offset > 200 ? 0 : (offset < 100 ? offset : 200 - offset);
145113

146114
// Set the brightness for this pixel
147-
pbsys_hub_light_matrix_set_pixel(pbsys_hub_light_matrix, indexes[i] / 5, indexes[i] % 5, brightness);
115+
pbio_light_matrix_set_pixel(pbsys_hub_light_matrix, indexes[i] / 5, indexes[i] % 5, brightness, false);
148116
}
149117
// This increment controls the speed of the pattern
150118
cycle += 9;

lib/pbio/test/src/test_light_matrix.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ static PT_THREAD(test_light_matrix(struct pt *pt)) {
5858

5959
// set pixel should only set one pixel
6060
test_light_matrix_reset();
61-
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix, 0, 0, 100), ==, PBIO_SUCCESS);
61+
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix, 0, 0, 100, true), ==, PBIO_SUCCESS);
6262
tt_want_light_matrix_data(100, 0, 0, 0, 0, 0, 0, 0, 0);
6363

6464
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix,
65-
MATRIX_SIZE - 1, MATRIX_SIZE - 1, 100), ==, PBIO_SUCCESS);
65+
MATRIX_SIZE - 1, MATRIX_SIZE - 1, 100, true), ==, PBIO_SUCCESS);
6666
tt_want_light_matrix_data(100, 0, 0, 0, 0, 0, 0, 0, 100);
6767

6868
// out of bounds checking
69-
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix, MATRIX_SIZE, 0, 100), ==, PBIO_SUCCESS);
69+
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix, MATRIX_SIZE, 0, 100, true), ==, PBIO_SUCCESS);
7070
tt_want_light_matrix_data(100, 0, 0, 0, 0, 0, 0, 0, 100);
71-
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix, 0, MATRIX_SIZE, 100), ==, PBIO_SUCCESS);
71+
tt_want_uint_op(pbio_light_matrix_set_pixel(&test_light_matrix, 0, MATRIX_SIZE, 100, true), ==, PBIO_SUCCESS);
7272
tt_want_light_matrix_data(100, 0, 0, 0, 0, 0, 0, 0, 100);
7373

7474
// bitwise mapping

pybricks/common/pb_type_lightmatrix.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static mp_obj_t common_LightMatrix_on(size_t n_args, const mp_obj_t *pos_args, m
160160

161161
for (uint8_t i = 0; i < size; i++) {
162162
for (uint8_t j = 0; j < size; j++) {
163-
pb_assert(pbio_light_matrix_set_pixel(self->light_matrix, i, j, brightness));
163+
pb_assert(pbio_light_matrix_set_pixel(self->light_matrix, i, j, brightness, true));
164164
}
165165
}
166166

@@ -218,7 +218,7 @@ void pb_type_LightMatrix_display_number(pbio_light_matrix_t *light_matrix, mp_ob
218218

219219
// Display one faint dot in the middle to indicate negative
220220
if (negative) {
221-
pb_assert(pbio_light_matrix_set_pixel(light_matrix, 2, 2, 50));
221+
pb_assert(pbio_light_matrix_set_pixel(light_matrix, 2, 2, 50, true));
222222
}
223223
}
224224

@@ -276,7 +276,7 @@ static mp_obj_t common_LightMatrix_pixel(size_t n_args, const mp_obj_t *pos_args
276276
PB_ARG_DEFAULT_INT(brightness, 100));
277277

278278
// Set pixel at the given brightness
279-
pb_assert(pbio_light_matrix_set_pixel(self->light_matrix, pb_obj_get_int(row_in), pb_obj_get_int(column_in), pb_obj_get_pct(brightness_in)));
279+
pb_assert(pbio_light_matrix_set_pixel(self->light_matrix, pb_obj_get_int(row_in), pb_obj_get_int(column_in), pb_obj_get_pct(brightness_in), true));
280280

281281
return mp_const_none;
282282
}

0 commit comments

Comments
 (0)