From 9717fd235d31b576855a0c0061b7834c0b0f70a9 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 3 May 2022 15:28:42 -0400 Subject: [PATCH] make Pin hashable --- locale/circuitpython.pot | 2 +- shared-bindings/microcontroller/Pin.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 3bec8b622b..db06147fe0 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -623,7 +623,7 @@ msgstr "" msgid "Buffer length must be a multiple of 512" msgstr "" -#: ports/stm/common-hal/sdioio/SDCard.c +#: ports/stm/common-hal/sdioio/SDCard.c shared-bindings/floppyio/__init__.c msgid "Buffer must be a multiple of 512 bytes" msgstr "" diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index ee8336fa8a..80c8c1d33c 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -78,10 +78,28 @@ STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki } } +//| def __hash__(self) -> int: +//| """Returns a hash for the Pin.""" +//| ... +//| +STATIC mp_obj_t mcu_pin_unary_op(mp_unary_op_t op, mp_obj_t self_in) { + switch (op) { + case MP_UNARY_OP_HASH: { + return mp_obj_id(self_in); + } + default: + return MP_OBJ_NULL; // op not supported + } +} + const mp_obj_type_t mcu_pin_type = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_EXTENDED, .name = MP_QSTR_Pin, - .print = mcu_pin_print + .print = mcu_pin_print, + MP_TYPE_EXTENDED_FIELDS( + .unary_op = mcu_pin_unary_op, + ) }; const mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj) {