Skip to content

Commit e2c465c

Browse files
committed
pybricks/util_mp: Utility for percentage array.
This will be useful in several places, so make a utility function. Now it also accepts a single floating point value for consistency with the rest of the API. Fixes pybricks/support#1547.
1 parent d6ebec5 commit e2c465c

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
## [Unreleased]
66

7+
### Changed
8+
- Allow single floating point value for brightness array ([support#1547]).
9+
10+
[support#1547]: https://github.com/pybricks/support/issues/1547
11+
712
## [3.4.0] - 2024-03-11
813

914
### Changed

pybricks/common/pb_type_lightarray.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,7 @@ STATIC mp_obj_t common_LightArray_on(size_t n_args, const mp_obj_t *pos_args, mp
3232
PB_ARG_DEFAULT_INT(brightness, 100));
3333

3434
int8_t brightness_values[4];
35-
36-
// Given an integer, make all lights the same brightness.
37-
if (mp_obj_is_int(brightness_in)) {
38-
int32_t b = pb_obj_get_pct(brightness_in);
39-
for (uint8_t i = 0; i < self->number_of_lights; i++) {
40-
brightness_values[i] = b;
41-
}
42-
}
43-
// Otherwise, get each brightness value from list or tuple.
44-
else {
45-
mp_obj_t *brightness_objects;
46-
size_t num_values;
47-
mp_obj_get_array(brightness_in, &num_values, &brightness_objects);
48-
if (num_values != self->number_of_lights) {
49-
pb_assert(PBIO_ERROR_INVALID_ARG);
50-
}
51-
for (uint8_t i = 0; i < self->number_of_lights; i++) {
52-
brightness_values[i] = pb_obj_get_pct(brightness_objects[i]);
53-
}
54-
}
35+
pb_obj_get_pct_or_array(brightness_in, self->number_of_lights, brightness_values);
5536

5637
// Set the brightness values and wait or await it.
5738
return pb_type_device_set_data(self->sensor, self->light_mode, brightness_values, self->number_of_lights);

pybricks/util_mp/pb_obj_helper.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,34 @@ mp_int_t pb_obj_get_default_abs_int(mp_obj_t obj, mp_int_t default_val) {
124124
return value > 0 ? value: -value;
125125
}
126126

127+
/**
128+
* Populates an array of percentages from a single number or tuple.
129+
*
130+
* @param obj_in [in] A MicroPython object
131+
* @param num [in] The number of values to populate
132+
* @param values [out] The array to populate
133+
*
134+
* Raises exception if @p obj_in is not a number or a tuple of @p num values.
135+
*/
136+
void pb_obj_get_pct_or_array(mp_obj_t obj_in, size_t num, int8_t *values) {
137+
if (mp_obj_is_type(obj_in, &mp_type_tuple) || mp_obj_is_type(obj_in, &mp_type_list)) {
138+
mp_obj_t *items;
139+
size_t len;
140+
mp_obj_get_array(obj_in, &len, &items);
141+
if (len != num) {
142+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("expected 1 number or tuple of %d values"), num);
143+
}
144+
for (size_t i = 0; i < num; i++) {
145+
values[i] = pb_obj_get_pct(items[i]);
146+
}
147+
} else {
148+
mp_int_t pct = pb_obj_get_pct(obj_in);
149+
for (size_t i = 0; i < num; i++) {
150+
values[i] = pct;
151+
}
152+
}
153+
}
154+
127155
mp_obj_t pb_obj_get_base_class_obj(mp_obj_t obj, const mp_obj_type_t *type) {
128156

129157
// If it equals the base type then return as is

pybricks/util_mp/pb_obj_helper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ mp_obj_t pb_obj_new_fraction(int32_t numerator, int32_t denominator);
3030
// Get absolute value if object is not none, else return default
3131
mp_int_t pb_obj_get_default_abs_int(mp_obj_t obj, mp_int_t default_val);
3232

33+
// Get array of percentages from single value or tuple
34+
void pb_obj_get_pct_or_array(mp_obj_t obj_in, size_t num, int8_t *values);
35+
3336
// Get base instance if object is instance of subclass of type
3437
mp_obj_t pb_obj_get_base_class_obj(mp_obj_t obj, const mp_obj_type_t *type);
3538

0 commit comments

Comments
 (0)