Skip to content

Commit

Permalink
codal_port/microbit_pinmode: Add microbit_obj_pin_acquire_and_free().
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Mar 25, 2024
1 parent fbf0652 commit 4a4c4c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/codal_port/microbit_pinmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinm
}
}

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) {
microbit_obj_pin_acquire(new_pin, new_mode);
if (*old_pin != new_pin) {
microbit_obj_pin_free(*old_pin);
*old_pin = new_pin;
}
}

static void noop(const microbit_pin_obj_t *pin) {
(void)pin;
}
Expand Down
3 changes: 3 additions & 0 deletions src/codal_port/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ bool microbit_obj_pin_can_be_acquired(const microbit_pin_obj_t *pin);
// Not safe to call in an interrupt as it may raise if pin can't be acquired.
bool microbit_obj_pin_acquire(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode);

// Acquire a new pin and free a previous one, for the given mode.
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);

// Change the mode of a pin. This does not check the existing mode, nor release any
// resources, and should only be used if pin resources are managed by the caller.
void microbit_pin_set_mode(const microbit_pin_obj_t *pin, const microbit_pinmode_t *mode);
Expand Down

0 comments on commit 4a4c4c4

Please sign in to comment.