Skip to content

Commit 6080999

Browse files
committed
pybricks/parameters/pb_type_color: add get_distance_to method
exposes the pbio_color_get_bicone_squared_distance function used in the sensor.color() method.
1 parent a17fb1a commit 6080999

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pybricks/parameters/pb_type_color.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ STATIC void pb_type_Color_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
217217
default:
218218
break;
219219
}
220+
dest[1] = MP_OBJ_SENTINEL;
220221
return;
221222
}
222223

@@ -307,6 +308,20 @@ STATIC mp_obj_t pb_type_Color_call(mp_obj_t self_in, size_t n_args, size_t n_kw,
307308
return pb_type_Color_make_new_helper(pb_obj_get_int(h_in), pb_obj_get_int(s_in), pb_obj_get_int(v_in));
308309
}
309310

311+
STATIC mp_obj_t pb_type_Color_get_distance(mp_obj_t self_in, mp_obj_t other_in) {
312+
const pbio_color_hsv_t *self = pb_type_Color_get_hsv(self_in);
313+
const pbio_color_hsv_t *other = pb_type_Color_get_hsv(other_in);
314+
return mp_obj_new_int(pbio_color_get_bicone_squared_distance(self, other));
315+
}
316+
317+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(get_distance_obj, pb_type_Color_get_distance);
318+
319+
STATIC const mp_rom_map_elem_t pb_type_Color_locals_dict_table[] = {
320+
{ MP_ROM_QSTR(MP_QSTR_get_distance_to), MP_ROM_PTR(&get_distance_obj)},
321+
};
322+
323+
STATIC MP_DEFINE_CONST_DICT(pb_type_Color_locals_dict, pb_type_Color_locals_dict_table);
324+
310325
MP_DEFINE_CONST_OBJ_TYPE(pb_type_Color,
311326
MP_QSTR_Color,
312327
MP_TYPE_FLAG_ITER_IS_GETITER,
@@ -316,7 +331,8 @@ MP_DEFINE_CONST_OBJ_TYPE(pb_type_Color,
316331
unary_op, mp_generic_unary_op,
317332
binary_op, pb_type_Color_binary_op,
318333
subscr, pb_type_Color_subscr,
319-
iter, pb_type_Color_getiter);
334+
iter, pb_type_Color_getiter,
335+
locals_dict, &pb_type_Color_locals_dict);
320336

321337
// We expose an instance instead of the type. This lets us provide class
322338
// attributes via the attribute handler for more flexibility.

0 commit comments

Comments
 (0)