Skip to content

Commit 8364035

Browse files
committed
codal_port/modmicrobit: Add helper funcs to query button/pin types.
Signed-off-by: Damien George <[email protected]>
1 parent 7067aa7 commit 8364035

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/codal_port/microbit_button.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ STATIC const mp_map_elem_t microbit_button_locals_dict_table[] = {
6464

6565
STATIC MP_DEFINE_CONST_DICT(microbit_button_locals_dict, microbit_button_locals_dict_table);
6666

67-
STATIC const mp_obj_type_t microbit_button_type = {
67+
const mp_obj_type_t microbit_button_type = {
6868
{ &mp_type_type },
6969
.name = MP_QSTR_MicroBitButton,
7070
.locals_dict = (mp_obj_dict_t *)&microbit_button_locals_dict,
@@ -81,3 +81,8 @@ const microbit_button_obj_t microbit_button_b_obj = {
8181
.pin = &microbit_p11_obj,
8282
.button_id = 1,
8383
};
84+
85+
// This function assumes "button" is of type microbit_button_type.
86+
uint8_t microbit_obj_get_button_id(mp_obj_t button) {
87+
return ((microbit_button_obj_t *)MP_OBJ_TO_PTR(button))->button_id;
88+
}

src/codal_port/microbit_pin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ const mp_obj_type_t microbit_touch_only_pin_type = {
266266

267267
const microbit_pin_obj_t *microbit_obj_get_pin(mp_const_obj_t o) {
268268
const mp_obj_type_t *type = mp_obj_get_type(o);
269-
if (type == &microbit_touch_pin_type || type == &microbit_ad_pin_type || type == &microbit_dig_pin_type) {
269+
if (microbit_obj_type_is_pin(type)) {
270270
return (microbit_pin_obj_t*)o;
271271
} else {
272272
mp_raise_TypeError(MP_ERROR_TEXT("expecting a pin"));

src/codal_port/modmicrobit.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef struct _microbit_soundevent_obj_t microbit_soundevent_obj_t;
8080

8181
extern const microbit_pinmode_t microbit_pinmodes[];
8282

83+
extern const mp_obj_type_t microbit_button_type;
8384
extern const mp_obj_type_t microbit_ad_pin_type;
8485
extern const mp_obj_type_t microbit_dig_pin_type;
8586
extern const mp_obj_type_t microbit_touch_pin_type;
@@ -192,6 +193,17 @@ extern const struct _microbit_microphone_obj_t microbit_microphone_obj;
192193
extern const struct _microbit_button_obj_t microbit_button_a_obj;
193194
extern const struct _microbit_button_obj_t microbit_button_b_obj;
194195

196+
static inline bool microbit_obj_type_is_button(const mp_obj_type_t *type) {
197+
return type == &microbit_button_type;
198+
}
199+
200+
static inline bool microbit_obj_type_is_pin(const mp_obj_type_t *type) {
201+
return type == &microbit_touch_pin_type || type == &microbit_ad_pin_type || type == &microbit_dig_pin_type;
202+
}
203+
204+
// This function assumes "button" is of type microbit_button_type.
205+
uint8_t microbit_obj_get_button_id(mp_obj_t button);
206+
195207
const microbit_pin_obj_t *microbit_obj_get_pin(mp_const_obj_t o);
196208
uint8_t microbit_obj_get_pin_name(mp_obj_t o);
197209

0 commit comments

Comments
 (0)