Skip to content

Commit 6dd2afa

Browse files
committed
pybricks.common.LightMatrix: Share num/char setters.
This lets us use the same code to create a hub menu.
1 parent a4c20fd commit 6dd2afa

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

pybricks/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ mp_obj_t common_LightArray_obj_make_new(pb_type_device_obj_base_t *sensor, uint8
6363
extern const uint8_t pb_digits_5x2[10][5];
6464
extern const uint8_t pb_font_5x5[95][5];
6565
mp_obj_t pb_type_LightMatrix_obj_new(pbio_light_matrix_t *light_matrix);
66+
void pb_type_LightMatrix_display_number(pbio_light_matrix_t *light_matrix, mp_obj_t number_in);
67+
void pb_type_LightMatrix_display_char(pbio_light_matrix_t *light_matrix, mp_obj_t char_in);
6668
#endif
6769

6870
#if PYBRICKS_PY_COMMON_KEYPAD

pybricks/common/pb_type_lightmatrix.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ STATIC mp_obj_t common_LightMatrix_orientation(size_t n_args, const mp_obj_t *po
5353
}
5454
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_LightMatrix_orientation_obj, 1, common_LightMatrix_orientation);
5555

56-
// pybricks._common.LightMatrix.char
57-
STATIC mp_obj_t common_LightMatrix_char(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
58-
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
59-
common_LightMatrix_obj_t, self,
60-
PB_ARG_REQUIRED(char));
61-
56+
void pb_type_LightMatrix_display_char(pbio_light_matrix_t *light_matrix, mp_obj_t char_in) {
6257
// Argument must be a qstr or string
6358
if (!mp_obj_is_qstr(char_in)) {
6459
pb_assert_type(char_in, &mp_type_str);
@@ -71,7 +66,16 @@ STATIC mp_obj_t common_LightMatrix_char(size_t n_args, const mp_obj_t *pos_args,
7166
}
7267

7368
// Pick corresponding icon and display it
74-
pb_assert(pbio_light_matrix_set_rows(self->light_matrix, pb_font_5x5[text[0] - 32]));
69+
pb_assert(pbio_light_matrix_set_rows(light_matrix, pb_font_5x5[text[0] - 32]));
70+
}
71+
72+
// pybricks._common.LightMatrix.char
73+
STATIC mp_obj_t common_LightMatrix_char(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
74+
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
75+
common_LightMatrix_obj_t, self,
76+
PB_ARG_REQUIRED(char));
77+
78+
pb_type_LightMatrix_display_char(self->light_matrix, char_in);
7579

7680
return mp_const_none;
7781
}
@@ -163,33 +167,27 @@ STATIC mp_obj_t common_LightMatrix_off(mp_obj_t self_in) {
163167
}
164168
STATIC MP_DEFINE_CONST_FUN_OBJ_1(common_LightMatrix_off_obj, common_LightMatrix_off);
165169

166-
// pybricks._common.LightMatrix.number
167-
STATIC mp_obj_t common_LightMatrix_number(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
168-
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
169-
common_LightMatrix_obj_t, self,
170-
PB_ARG_REQUIRED(number));
170+
void pb_type_LightMatrix_display_number(pbio_light_matrix_t *light_matrix, mp_obj_t number_in) {
171171

172+
mp_int_t number = pb_obj_get_int(number_in);
172173

173-
uint8_t size = pbio_light_matrix_get_size(self->light_matrix);
174+
uint8_t size = pbio_light_matrix_get_size(light_matrix);
174175

175176
// Currently numbers are only implemented for 5x5 matrices
176177
if (size != 5) {
177178
pb_assert(PBIO_ERROR_NOT_IMPLEMENTED);
178179
}
179180

180-
// Get the number
181-
mp_int_t number = pb_obj_get_int(number_in);
182-
183181
// > 99 gets displayed as >
184182
if (number > 99) {
185-
pb_assert(pbio_light_matrix_set_rows(self->light_matrix, pb_font_5x5['>' - 32]));
186-
return mp_const_none;
183+
pb_assert(pbio_light_matrix_set_rows(light_matrix, pb_font_5x5['>' - 32]));
184+
return;
187185
}
188186

189187
// < -99 gets displayed as <
190188
if (number < -99) {
191-
pb_assert(pbio_light_matrix_set_rows(self->light_matrix, pb_font_5x5['<' - 32]));
192-
return mp_const_none;
189+
pb_assert(pbio_light_matrix_set_rows(light_matrix, pb_font_5x5['<' - 32]));
190+
return;
193191
}
194192

195193
// Remember sign but make value positive
@@ -205,13 +203,22 @@ STATIC mp_obj_t common_LightMatrix_number(size_t n_args, const mp_obj_t *pos_arg
205203
}
206204

207205
// Display the result
208-
pb_assert(pbio_light_matrix_set_rows(self->light_matrix, composite));
206+
pb_assert(pbio_light_matrix_set_rows(light_matrix, composite));
209207

210208
// Display one faint dot in the middle to indicate negative
211209
if (negative) {
212-
pb_assert(pbio_light_matrix_set_pixel(self->light_matrix, 2, 2, 50));
210+
pb_assert(pbio_light_matrix_set_pixel(light_matrix, 2, 2, 50));
213211
}
212+
}
213+
214+
// pybricks._common.LightMatrix.number
215+
STATIC mp_obj_t common_LightMatrix_number(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
216+
PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
217+
common_LightMatrix_obj_t, self,
218+
PB_ARG_REQUIRED(number));
214219

220+
// Display the number
221+
pb_type_LightMatrix_display_number(self->light_matrix, number_in);
215222
return mp_const_none;
216223
}
217224
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_LightMatrix_number_obj, 1, common_LightMatrix_number);

0 commit comments

Comments
 (0)