Skip to content

Commit 2b0f6c9

Browse files
committed
pybricks/util_mp: Optimize array test.
Using a utility saves a few more bytes on MoveHub and makes the code easier to follow.
1 parent e2c465c commit 2b0f6c9

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

pybricks/common/pb_type_ble.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ STATIC mp_obj_t pb_module_ble_broadcast(size_t n_args, const mp_obj_t *pos_args,
276276
mp_obj_t *objs;
277277
size_t n_objs;
278278
size_t index;
279-
if (mp_obj_is_type(data_in, &mp_type_tuple) || mp_obj_is_type(data_in, &mp_type_list)) {
279+
if (pb_obj_is_array(data_in)) {
280280
index = 0;
281281
mp_obj_get_array(data_in, &n_objs, &objs);
282282
} else {

pybricks/common/pb_type_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void unpack_acceleration_value(mp_obj_t accel_in, int32_t *acceleration, int32_t
3333
}
3434

3535
// If single value is given for acceleration, use it for deceleration too.
36-
if (mp_obj_is_int(accel_in) || mp_obj_is_float(accel_in)) {
36+
if (!pb_obj_is_array(accel_in)) {
3737
*acceleration = pb_obj_get_int(accel_in);
3838
*deceleration = *acceleration;
3939
return;

pybricks/robotics/pb_type_car.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ STATIC mp_obj_t pb_type_Car_make_new(const mp_obj_type_t *type, size_t n_args, s
7878
// Should be one motor for steering.
7979
self->srv_steer = pb_type_motor_get_servo(steer_motor_in);
8080

81-
if (mp_obj_is_type(drive_motors_in, &mp_type_tuple) || mp_obj_is_type(drive_motors_in, &mp_type_list)) {
81+
if (pb_obj_is_array(drive_motors_in)) {
8282
// Unpack the drive motors if multiple are given.
8383
mp_obj_t *drive_motors;
8484
mp_obj_get_array(drive_motors_in, &self->n_drive, &drive_motors);

pybricks/util_mp/pb_obj_helper.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ 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+
* Tests if an object is a tuple or list.
129+
*
130+
* @param obj_in [in] A MicroPython object
131+
* @return True if @p obj_in is a tuple or list, otherwise false.
132+
*/
133+
bool pb_obj_is_array(mp_obj_t obj_in) {
134+
return mp_obj_is_type(obj_in, &mp_type_tuple) || mp_obj_is_type(obj_in, &mp_type_list);
135+
}
136+
127137
/**
128138
* Populates an array of percentages from a single number or tuple.
129139
*
@@ -134,7 +144,7 @@ mp_int_t pb_obj_get_default_abs_int(mp_obj_t obj, mp_int_t default_val) {
134144
* Raises exception if @p obj_in is not a number or a tuple of @p num values.
135145
*/
136146
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)) {
147+
if (pb_obj_is_array(obj_in)) {
138148
mp_obj_t *items;
139149
size_t len;
140150
mp_obj_get_array(obj_in, &len, &items);

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+
// Tests if an object is a tuple or list
34+
bool pb_obj_is_array(mp_obj_t obj_in);
35+
3336
// Get array of percentages from single value or tuple
3437
void pb_obj_get_pct_or_array(mp_obj_t obj_in, size_t num, int8_t *values);
3538

0 commit comments

Comments
 (0)