-
Notifications
You must be signed in to change notification settings - Fork 231
rpio: basic implementation of Pins and SPI interfaces for Raspberry Pi #828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ysoldak
wants to merge
1
commit into
dev
Choose a base branch
from
rpio
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package main | ||
|
|
||
| // Example program for the ST7735 display (Waveshare 1.44" LCD HAT) using the rpio driver on a Raspberry Pi. | ||
| // Build command: | ||
| // GOOS=linux GOARCH=arm64 go build -o rpio-example ./examples/rpio/ | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "image/color" | ||
| "time" | ||
|
|
||
| go_rpio "github.com/stianeikeland/go-rpio/v4" | ||
| "tinygo.org/x/drivers" | ||
| "tinygo.org/x/drivers/rpio" | ||
| "tinygo.org/x/drivers/st7735" | ||
| ) | ||
|
|
||
| var ( | ||
| black = color.RGBA{0, 0, 0, 255} | ||
| colors = [...]color.RGBA{ | ||
| {255, 0, 0, 255}, // red | ||
| {0, 255, 0, 255}, // green | ||
| {0, 0, 255, 255}, // blue | ||
| {255, 255, 0, 255}, // yellow | ||
| } | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := go_rpio.Open(); err != nil { | ||
| fmt.Println("Error opening GPIO:", err) | ||
| return | ||
| } | ||
| defer go_rpio.Close() | ||
|
|
||
| // Initialize SPI and pins | ||
| spi := rpio.NewSPI() | ||
| resetPin := rpio.NewPin(27) | ||
| dcPin := rpio.NewPin(25) | ||
| csPin := rpio.NewPin(8) | ||
| blPin := rpio.NewPin(24) | ||
|
|
||
| rotatePin := rpio.NewPin(20) // middle button on the HAT | ||
| rotatePin.Pin.PullUp() // enable pull-up resistor, our wrapper sets only input mode automatically | ||
|
|
||
| // Initialize display | ||
| device := st7735.New(spi, resetPin, dcPin, csPin, blPin) | ||
| device.Configure(st7735.Config{ | ||
| Width: 128, | ||
| Height: 128, | ||
| Model: st7735.GREENTAB, | ||
| RowOffset: 3, | ||
| ColumnOffset: 2, | ||
| }) | ||
| device.InvertColors(false) | ||
| device.EnableBacklight(true) | ||
| device.IsBGR(true) // no effect w/o rotation! | ||
| device.SetRotation(st7735.NO_ROTATION) | ||
|
|
||
| width, height := device.Size() | ||
|
|
||
| // Clear display | ||
| device.FillScreen(black) | ||
|
|
||
| // Draw rectangles in a loop, clockwise rotation of colors | ||
| pos := 0 | ||
| for { | ||
| device.FillRectangle(0, 0, width/2, height/2, colors[(pos+0)%len(colors)]) // top left | ||
| device.FillRectangle(0, height/2, width/2, height/2, colors[(pos+1)%len(colors)]) // bottom left | ||
| device.FillRectangle(width/2, height/2, width/2, height/2, colors[(pos+2)%len(colors)]) // bottom right | ||
| device.FillRectangle(width/2, 0, width/2, height/2, colors[(pos+3)%len(colors)]) // top right | ||
| device.FillRectangle(0, 0, 10, 10, black) // rotation marker | ||
| pos++ | ||
|
|
||
| timeout := time.Now().Add(1 * time.Second) | ||
| for time.Now().Before(timeout) { | ||
| // check button pressed | ||
| if !rotatePin.Get() { | ||
| currentRotation := device.Rotation() | ||
| newRotation := (currentRotation + 1) % 4 | ||
| device.SetRotation(drivers.Rotation(newRotation)) | ||
| // wait for button release | ||
| for !rotatePin.Get() { | ||
| time.Sleep(10 * time.Millisecond) | ||
| } | ||
| break | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Implemenmts drivers.SPI interface for the Raspberry Pi. | ||
| // Depends on the go-rpio library. | ||
|
|
||
| package rpio // import "tinygo.org/x/drivers/rpio" | ||
|
|
||
| import go_rpio "github.com/stianeikeland/go-rpio/v4" | ||
|
|
||
| type SPI struct { | ||
| } | ||
|
|
||
| func NewSPI() (s *SPI) { | ||
| if err := go_rpio.SpiBegin(go_rpio.Spi0); err != nil { | ||
| panic(err) | ||
| } | ||
| go_rpio.SpiSpeed(25_000_000) // 25 MHz | ||
| go_rpio.SpiChipSelect(0) | ||
| return &SPI{} | ||
| } | ||
|
|
||
| func (s *SPI) Tx(w, r []byte) error { | ||
| data := make([]byte, len(w)) | ||
| copy(data, w) | ||
| go_rpio.SpiExchange(data) | ||
| copy(r, data) | ||
| return nil | ||
| } | ||
|
|
||
| func (s *SPI) Transfer(b byte) (byte, error) { | ||
| w := []byte{b} | ||
| r := make([]byte, len(w)) | ||
| err := s.Tx(w, r) | ||
| return r[0], err | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't want to go as far as hiding even this (do automatically) in the
rpiopackage.Leaving full control to user,
rpiopackage is just a convenience wrapper implementing our interfaces.