rpio: basic impl of Pins and SPI interfaces for Raspberry Pi

This commit is contained in:
Yurii Soldak
2025-12-21 23:41:51 +01:00
parent 2a42fa7cbb
commit b990fc887f
5 changed files with 164 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
// Implements internal.pin.[Input,Output] interfaces for the Raspberry Pi GPIO pins.
// Depends on the go-rpio library.
// Configures pin modes automatically when Get or Set methods are called.
package rpio // import "tinygo.org/x/drivers/rpio"
import go_rpio "github.com/stianeikeland/go-rpio/v4"
type Pin struct {
modeSet bool
Mode go_rpio.Mode
Pin go_rpio.Pin
}
func NewPin(pinNumber int) *Pin {
return &Pin{Pin: go_rpio.Pin(pinNumber)}
}
func (p *Pin) Get() bool {
if !p.modeSet || p.Mode != go_rpio.Input {
p.Pin.Input()
p.Mode = go_rpio.Input
}
return p.Pin.Read() == go_rpio.High
}
func (p *Pin) Set(high bool) {
if !p.modeSet || p.Mode != go_rpio.Output {
p.Pin.Output()
p.Mode = go_rpio.Output
}
state := go_rpio.Low
if high {
state = go_rpio.High
}
p.Pin.Write(state)
}