lancaster-university / codal-microbit-v2

CODAL target for the micro:bit v2.x series of devices
MIT License
43 stars 52 forks source link

Quickly switching between resistive and capacitive pin touch and checking state doesn't work #415

Open microbit-carlos opened 6 months ago

microbit-carlos commented 6 months ago

Easier to visualise in MakeCode:

Works Doesn't work
image image
input.onButtonPressed(Button.A, function () {
    basic.showString("r")
    pins.touchSetMode(
        TouchTarget.LOGO,
        TouchTargetMode.Resistive)
})
input.onButtonPressed(Button.B, function () {
    basic.showString("c")
    pins.touchSetMode(
        TouchTarget.LOGO,
        TouchTargetMode.Capacitive)
})
basic.forever(function () {
    if (input.logoIsPressed()) {
        basic.showIcon(IconNames.Heart)
    } else {
        basic.showIcon(IconNames.Sad)
    }
})
basic.forever(function () {
    pins.touchSetMode(
        TouchTarget.P0,
        TouchTargetMode.Resistive)
    if (input.pinIsPressed(TouchPin.P0)) {
        basic.showString("r")
    } else {
        basic.showIcon(IconNames.Sad)
    }
    pins.touchSetMode(
        TouchTarget.P0,
        TouchTargetMode.Capacitive)
    if (input.pinIsPressed(TouchPin.P0)) {
        basic.showString("c")
    } else {
        basic.showIcon(IconNames.Sad)
    }
})

Replicable in CODAL v0.2.66, also adding an extra isTouched() call in case it's needed:

#include "MicroBit.h"

MicroBit uBit;

int main() {
    uBit.init();

    while (true) {
        uBit.io.P0.isTouched(TouchMode::Capacitative);
        uBit.sleep(1);
        if (uBit.io.P0.isTouched(TouchMode::Capacitative)) {
            uBit.display.print('C');
        } else {
            uBit.display.clear();
        }
        uBit.sleep(300);

        uBit.io.P0.isTouched(TouchMode::Resistive);
        uBit.sleep(1);
        if (uBit.io.P0.isTouched(TouchMode::Resistive)) {
            uBit.display.print('R');
        } else {
            uBit.display.clear();
        }
        uBit.sleep(300);
    }
}

Related to this issue, but not quite the same. That issue is about only the uBit.logo (TouchButton) not working after setting the type to resistive, this is about none of the NRF52Pin touch pins working when switching modes quickly.