Skip to content

Commit

Permalink
microbit: Add was_touched and get_touches to MicroBiTouchPin.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Mar 13, 2023
1 parent c9d6016 commit 9ddc573
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
2 changes: 2 additions & 0 deletions inc/genhdr/qstrdefs.generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ QDEF(MP_QSTR_gc, (const byte*)"\x61\x02" "gc")
QDEF(MP_QSTR_generator, (const byte*)"\x96\x09" "generator")
QDEF(MP_QSTR_get, (const byte*)"\x33\x03" "get")
QDEF(MP_QSTR_get_strength, (const byte*)"\x69\x0c" "get_strength")
QDEF(MP_QSTR_get_touches, (const byte*)"\x1f\x0b" "get_touches")
QDEF(MP_QSTR_getattr, (const byte*)"\xc0\x07" "getattr")
QDEF(MP_QSTR_globals, (const byte*)"\x9d\x07" "globals")
QDEF(MP_QSTR_hasattr, (const byte*)"\x8c\x07" "hasattr")
Expand Down Expand Up @@ -749,5 +750,6 @@ QDEF(MP_QSTR_utime, (const byte*)"\xe5\x05" "utime")
QDEF(MP_QSTR_value, (const byte*)"\x4e\x05" "value")
QDEF(MP_QSTR_values, (const byte*)"\x7d\x06" "values")
QDEF(MP_QSTR_version_info, (const byte*)"\x6e\x0c" "version_info")
QDEF(MP_QSTR_was_touched, (const byte*)"\xdb\x0b" "was_touched")
QDEF(MP_QSTR_wfi, (const byte*)"\x9d\x03" "wfi")
QDEF(MP_QSTR_zip, (const byte*)"\xe6\x03" "zip")
4 changes: 3 additions & 1 deletion inc/microbit/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ const microbit_pinmode_t *microbit_pin_get_mode(const microbit_pin_obj_t *pin);
bool microbit_obj_pin_can_be_acquired(const microbit_pin_obj_t *pin);
void pinmode_error(const microbit_pin_obj_t *pin);

bool microbit_pin_high_debounced(microbit_pin_obj_t *pin);
bool microbit_pin_debounce_is_high(const microbit_pin_obj_t *pin);
bool microbit_pin_debounce_was_pressed(const microbit_pin_obj_t *pin);
unsigned int microbit_pin_debounce_get_presses(const microbit_pin_obj_t *pin);

/****************************************************************/
// microbit.Image class
Expand Down
31 changes: 19 additions & 12 deletions source/microbit/microbitbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,20 @@ static inline debounce_state_t *get_debounce_state(const microbit_pin_obj_t *pin

mp_obj_t microbit_button_is_pressed(mp_obj_t self_in) {
microbit_button_obj_t *self = (microbit_button_obj_t*)self_in;
debounce_state_t *debounce = get_debounce_state(self->pin);
/* Button is pressed if pin is low */
return mp_obj_new_bool(!debounce->debounced_high);
return mp_obj_new_bool(!microbit_pin_debounce_is_high(self->pin));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_button_is_pressed_obj, microbit_button_is_pressed);

mp_obj_t microbit_button_get_presses(mp_obj_t self_in) {
microbit_button_obj_t *self = (microbit_button_obj_t*)self_in;
debounce_state_t *debounce = get_debounce_state(self->pin);
mp_obj_t n_presses = mp_obj_new_int(debounce->pressed >> 1);
debounce->pressed &= 1;
return n_presses;
return mp_obj_new_int(microbit_pin_debounce_get_presses(self->pin));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_button_get_presses_obj, microbit_button_get_presses);

mp_obj_t microbit_button_was_pressed(mp_obj_t self_in) {
microbit_button_obj_t *self = (microbit_button_obj_t*)self_in;
debounce_state_t *debounce = get_debounce_state(self->pin);
mp_int_t presses = debounce->pressed;
mp_obj_t result = mp_obj_new_bool(presses & 1);
debounce->pressed = presses & -2;
return result;
return mp_obj_new_bool(microbit_pin_debounce_was_pressed(self->pin));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_button_was_pressed_obj, microbit_button_was_pressed);

Expand Down Expand Up @@ -184,9 +176,24 @@ void microbit_button_tick(void) {
update(&microbit_p2_obj);
}

bool microbit_pin_high_debounced(microbit_pin_obj_t *pin) {
bool microbit_pin_debounce_is_high(const microbit_pin_obj_t *pin) {
debounce_state_t *debounce = get_debounce_state(pin);
return debounce->debounced_high;
}

bool microbit_pin_debounce_was_pressed(const microbit_pin_obj_t *pin) {
debounce_state_t *debounce = get_debounce_state(pin);
uint16_t presses = debounce->pressed;
bool result = presses & 1;
debounce->pressed = presses & -2;
return result;
}

unsigned int microbit_pin_debounce_get_presses(const microbit_pin_obj_t *pin) {
debounce_state_t *debounce = get_debounce_state(pin);
unsigned int n_presses = debounce->pressed >> 1;
debounce->pressed &= 1;
return n_presses;
}

}
31 changes: 25 additions & 6 deletions source/microbit/microbitpin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ const microbit_pin_obj_t microbit_p16_obj = {{&microbit_dig_pin_type}, 16, MICRO
const microbit_pin_obj_t microbit_p19_obj = {{&microbit_dig_pin_type}, 19, MICROBIT_PIN_P19, MODE_I2C};
const microbit_pin_obj_t microbit_p20_obj = {{&microbit_dig_pin_type}, 20, MICROBIT_PIN_P20, MODE_I2C};

static void microbit_pin_configure_touch_mode(microbit_pin_obj_t *self) {
const microbit_pinmode_t *mode = microbit_pin_get_mode(self);
if (mode != microbit_pin_mode_touch && mode != microbit_pin_mode_button) {
microbit_obj_pin_acquire(self, microbit_pin_mode_touch);
nrf_gpio_cfg_input(self->name, NRF_GPIO_PIN_NOPULL);
}
}

static mp_obj_t microbit_pin_get_mode_func(mp_obj_t self_in) {
microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in;
Expand Down Expand Up @@ -179,16 +186,26 @@ MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_get_analog_period_microseconds_obj, micro

mp_obj_t microbit_pin_is_touched(mp_obj_t self_in) {
microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in;
const microbit_pinmode_t *mode = microbit_pin_get_mode(self);
if (mode != microbit_pin_mode_touch && mode != microbit_pin_mode_button) {
microbit_obj_pin_acquire(self, microbit_pin_mode_touch);
nrf_gpio_cfg_input(self->name, NRF_GPIO_PIN_NOPULL);
}
microbit_pin_configure_touch_mode(self);
/* Pin is touched if it is low after debouncing */
return mp_obj_new_bool(!microbit_pin_high_debounced(self));
return mp_obj_new_bool(!microbit_pin_debounce_is_high(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_is_touched_obj, microbit_pin_is_touched);

mp_obj_t microbit_pin_was_touched(mp_obj_t self_in) {
microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in;
microbit_pin_configure_touch_mode(self);
return mp_obj_new_bool(microbit_pin_debounce_was_pressed(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_was_touched_obj, microbit_pin_was_touched);

mp_obj_t microbit_pin_get_touches(mp_obj_t self_in) {
microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in;
microbit_pin_configure_touch_mode(self);
return mp_obj_new_int(microbit_pin_debounce_get_presses(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_get_touches_obj, microbit_pin_get_touches);

#define PULL_CONSTANTS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(NRF_GPIO_PIN_PULLUP) }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(NRF_GPIO_PIN_PULLDOWN) }, \
Expand Down Expand Up @@ -230,6 +247,8 @@ STATIC const mp_map_elem_t microbit_touch_pin_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_analog_period_microseconds), (mp_obj_t)&microbit_pin_set_analog_period_microseconds_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_analog_period_microseconds), (mp_obj_t)&microbit_pin_get_analog_period_microseconds_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_touched), (mp_obj_t)&microbit_pin_is_touched_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_was_touched), (mp_obj_t)&microbit_pin_was_touched_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_touches), (mp_obj_t)&microbit_pin_get_touches_obj },
PULL_CONSTANTS,
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pull),(mp_obj_t)&microbit_pin_get_pull_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_pull),(mp_obj_t)&microbit_pin_set_pull_obj },
Expand Down

0 comments on commit 9ddc573

Please sign in to comment.