Skip to content

Commit 957e810

Browse files
committed
codal_app: Add was_touched and get_touches to touch-capable pins.
Pins with these new methods are: pin0, pin1, pin2, pin_logo. Fixes issue #149. Signed-off-by: Damien George <damien@micropython.org>
1 parent d425ef2 commit 957e810

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/codal_app/microbithal.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static const PullMode pin_pull_mode_mapping[] = {
7878
};
7979

8080
static uint8_t pin_pull_state[32 + 6];
81+
static uint16_t touch_state[4];
8182
static uint16_t button_state[2];
8283

8384
extern "C" {
@@ -202,11 +203,32 @@ void microbit_hal_pin_write_analog_u10(int pin, int value) {
202203
pin_obj[pin]->setAnalogValue(value);
203204
}
204205

205-
int microbit_hal_pin_is_touched(int pin) {
206-
if (pin == MICROBIT_HAL_PIN_LOGO) {
207-
// For touch on the logo pin, delegate to the TouchButton instance.
208-
return uBit.logo.buttonActive();
206+
int microbit_hal_pin_touch_state(int pin, int *was_touched, int *num_touches) {
207+
if (was_touched != NULL || num_touches != NULL) {
208+
int pin_state_index;
209+
if (pin == MICROBIT_HAL_PIN_LOGO) {
210+
pin_state_index = 3;
211+
} else {
212+
pin_state_index = pin; // pin0/1/2
213+
}
214+
int t = pin_obj[pin]->wasTouched();
215+
uint16_t state = touch_state[pin_state_index];
216+
if (t) {
217+
// Update state based on number of touches since last call.
218+
// Low bit is "was touched at least once", upper bits are "number of touches".
219+
state = (state + (t << 1)) | 1;
220+
}
221+
if (was_touched != NULL) {
222+
*was_touched = state & 1;
223+
state &= ~1;
224+
}
225+
if (num_touches != NULL) {
226+
*num_touches = state >> 1;
227+
state &= 1;
228+
}
229+
touch_state[pin_state_index] = state;
209230
}
231+
210232
return pin_obj[pin]->isTouched();
211233
}
212234

src/codal_app/microbithal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ int microbit_hal_pin_read(int pin);
123123
void microbit_hal_pin_write(int pin, int value);
124124
int microbit_hal_pin_read_analog_u10(int pin);
125125
void microbit_hal_pin_write_analog_u10(int pin, int value);
126-
int microbit_hal_pin_is_touched(int pin);
126+
int microbit_hal_pin_touch_state(int pin, int *was_touched, int *num_touches);
127127
void microbit_hal_pin_write_ws2812(int pin, const uint8_t *buf, size_t len);
128128

129129
int microbit_hal_i2c_init(int scl, int sda, int freq);

src/codal_port/microbit_pin.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,35 @@ mp_obj_t microbit_pin_get_analog_period_microseconds(mp_obj_t self_in) {
155155
}
156156
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_get_analog_period_microseconds_obj, microbit_pin_get_analog_period_microseconds);
157157

158-
mp_obj_t microbit_pin_is_touched(mp_obj_t self_in) {
158+
static int microbit_pin_get_touch_state(mp_obj_t self_in, int *was_touched, int *num_touches) {
159159
microbit_pin_obj_t *self = (microbit_pin_obj_t*)self_in;
160160
const microbit_pinmode_t *mode = microbit_pin_get_mode(self);
161161
if (mode != microbit_pin_mode_touch && mode != microbit_pin_mode_button) {
162162
microbit_obj_pin_acquire(self, microbit_pin_mode_touch);
163163
// set NO_PULL on pin
164164
}
165-
return mp_obj_new_bool(microbit_hal_pin_is_touched(self->name));
165+
return microbit_hal_pin_touch_state(self->name, was_touched, num_touches);
166+
}
167+
168+
mp_obj_t microbit_pin_is_touched(mp_obj_t self_in) {
169+
return mp_obj_new_bool(microbit_pin_get_touch_state(self_in, NULL, NULL));
166170
}
167171
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_is_touched_obj, microbit_pin_is_touched);
168172

173+
mp_obj_t microbit_pin_was_touched(mp_obj_t self_in) {
174+
int was_touched;
175+
microbit_pin_get_touch_state(self_in, &was_touched, NULL);
176+
return mp_obj_new_bool(was_touched);
177+
}
178+
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_was_touched_obj, microbit_pin_was_touched);
179+
180+
mp_obj_t microbit_pin_get_touches(mp_obj_t self_in) {
181+
int num_touches;
182+
microbit_pin_get_touch_state(self_in, NULL, &num_touches);
183+
return MP_OBJ_NEW_SMALL_INT(num_touches);
184+
}
185+
MP_DEFINE_CONST_FUN_OBJ_1(microbit_pin_get_touches_obj, microbit_pin_get_touches);
186+
169187
mp_obj_t microbit_pin_set_touch_mode(mp_obj_t self_in, mp_obj_t mode_in) {
170188
microbit_pin_obj_t *self = (microbit_pin_obj_t *)self_in;
171189
const microbit_pinmode_t *mode = microbit_pin_get_mode(self);
@@ -238,6 +256,8 @@ STATIC const mp_rom_map_elem_t microbit_touch_pin_locals_dict_table[] = {
238256
{ MP_ROM_QSTR(MP_QSTR_set_analog_period_microseconds), MP_ROM_PTR(&microbit_pin_set_analog_period_microseconds_obj) },
239257
{ MP_ROM_QSTR(MP_QSTR_get_analog_period_microseconds), MP_ROM_PTR(&microbit_pin_get_analog_period_microseconds_obj) },
240258
{ MP_ROM_QSTR(MP_QSTR_is_touched), MP_ROM_PTR(&microbit_pin_is_touched_obj) },
259+
{ MP_ROM_QSTR(MP_QSTR_was_touched), MP_ROM_PTR(&microbit_pin_was_touched_obj) },
260+
{ MP_ROM_QSTR(MP_QSTR_get_touches), MP_ROM_PTR(&microbit_pin_get_touches_obj) },
241261
{ MP_ROM_QSTR(MP_QSTR_get_pull), MP_ROM_PTR(&microbit_pin_get_pull_obj) },
242262
{ MP_ROM_QSTR(MP_QSTR_set_pull), MP_ROM_PTR(&microbit_pin_set_pull_obj) },
243263
{ MP_ROM_QSTR(MP_QSTR_get_mode), MP_ROM_PTR(&microbit_pin_get_mode_obj) },
@@ -256,6 +276,8 @@ MP_DEFINE_CONST_OBJ_TYPE(
256276

257277
STATIC const mp_rom_map_elem_t microbit_touch_only_pin_locals_dict_table[] = {
258278
{ MP_ROM_QSTR(MP_QSTR_is_touched), MP_ROM_PTR(&microbit_pin_is_touched_obj) },
279+
{ MP_ROM_QSTR(MP_QSTR_was_touched), MP_ROM_PTR(&microbit_pin_was_touched_obj) },
280+
{ MP_ROM_QSTR(MP_QSTR_get_touches), MP_ROM_PTR(&microbit_pin_get_touches_obj) },
259281
{ MP_ROM_QSTR(MP_QSTR_set_touch_mode), MP_ROM_PTR(&microbit_pin_set_touch_mode_obj) },
260282
TOUCH_CONSTANTS,
261283
};

0 commit comments

Comments
 (0)