tinygo-org / drivers

TinyGo drivers for sensors, displays, wireless adaptors, and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
608 stars 193 forks source link

Using Rotary Encoders with Blue Pill STM32F103 #195

Open olablt opened 4 years ago

olablt commented 4 years ago

I have Blue Pill board and Rotary Encoder connected. I can not find any info on how to implement interrupts on GPIO input pins.

Are there any attempts to make a driver for encoder?

Arduino library code: https://github.com/PaulStoffregen/Encoder/blob/master/Encoder.h

alankrantas commented 4 years ago

Well...this might work, except that machine.PinInputPullup has not been implemented on Bluepill yet (I only tested it on a BBC micro:bit). You would have to use external pull-ups.

package main

import (
    "machine"
    "time"
)

func main() {

    clk := machine.PB9
    dta := machine.PB8
    sw := machine.PB7
    clk.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
    dta.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
    sw.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
    var clkNow, clkPrv bool

    for {

        if !sw.Get() {
            println("Switch pressed")
            time.Sleep(time.Millisecond * 250)
        }

        clkNow = clk.Get()
        if clkNow != clkPrv && clkNow {
            if dta.Get() != clkNow {
                println("Turing clockwise")
            } else {
                println("Turing anti-clockwise")
            }
        }
        clkPrv = clkNow

        time.Sleep(time.Millisecond * 1)

    }

}
pavelanni commented 1 year ago

This code worked for me on RPi Pico (with minor changes in pin names). Clockwise and anti-clockwise were reversed in my case, but that's easy to change. tinygo version 0.27.0 linux/amd64 Thanks, @alankrantas !