Skip to content

Commit 4a4c4c4

Browse files
committed
codal_port/microbit_pinmode: Add microbit_obj_pin_acquire_and_free().
Signed-off-by: Damien George <damien@micropython.org>
1 parent fbf0652 commit 4a4c4c4

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/codal_port/microbit_pinmode.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinm
7373
}
7474
}
7575

76+
void microbit_obj_pin_acquire_and_free(const microbit_pin_obj_t **old_pin, const microbit_pin_obj_t *new_pin, const microbit_pinmode_t *new_mode) {
77+
microbit_obj_pin_acquire(new_pin, new_mode);
78+
if (*old_pin != new_pin) {
79+
microbit_obj_pin_free(*old_pin);
80+
*old_pin = new_pin;
81+
}
82+
}
83+
7684
static void noop(const microbit_pin_obj_t *pin) {
7785
(void)pin;
7886
}

src/codal_port/modmicrobit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ bool microbit_obj_pin_can_be_acquired(const microbit_pin_obj_t *pin);
220220
// Not safe to call in an interrupt as it may raise if pin can't be acquired.
221221
bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode);
222222

223+
// Acquire a new pin and free a previous one, for the given mode.
224+
void microbit_obj_pin_acquire_and_free(const microbit_pin_obj_t **old_pin, const microbit_pin_obj_t *new_pin, const microbit_pinmode_t *new_mode);
225+
223226
// Change the mode of a pin. This does not check the existing mode, nor release any
224227
// resources, and should only be used if pin resources are managed by the caller.
225228
void microbit_pin_set_mode(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode);

0 commit comments

Comments
 (0)