tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.21k stars 895 forks source link

SetInterrupt Callback with PinChange #2452

Open fiurthorn opened 2 years ago

fiurthorn commented 2 years ago

I would find it interesting to have the event trigger passed along with it.

var (
    led    = machine.GP2
    button = machine.GP3
)

button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
led.Configure(machine.PinConfig{Mode: machine.PinOutput})

var start time.Time 
button.SetInterrupt(machine.PinFalling|machine.PinRising, func(p machine.Pin, c machine.PinChange) {
    if c == machine.PinFalling {
        start = time.Now()
    } else if c == machine.PinRising {
        d := time.Since(start).Seconds()
        if d > 5 {
            led.High()
        }
    }
})
bmentink commented 2 years ago

I vote for this enhancement as well 👍 It is almost essential for the user case I have ...

(My use case is a sensorless BLDC motor control, where the phase BEMF interrupts can arrive in any order, so I need to have the interrupt set for both edges, then in the handler I need to know which edge it was. It is not enough to sense the state of the pin, as it could bounce quite a bit..)

aykevl commented 2 years ago

It does seem reasonable, but there are two issues:

So, not sure about this.

My use case is a sensorless BLDC motor control

Oof, that's a rather heavy duty use case! It may be reasonable to do this by directly manipulating registers, just so you know exactly what's happening. In fact, I believe some chips have direct support for these motors but it's certainly out of my area of expertise so can't really help you with this :)

bmentink commented 2 years ago

@aykevl It's not a very heavy duty use case, if it can be implemented on an 8-bit Arduino Uno in C ... :)

The RP Pico I am using is 32-bit and 4 times the clock speed ..

fiurthorn commented 1 year ago

it the normal use case of an JK latch (Flipflop).