Added a SPI usage example

This commit is contained in:
Stian Eikeland 2018-11-29 17:29:11 +01:00 committed by GitHub
parent 0811a9f862
commit 23a42cad9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

49
examples/spi/spi.go Normal file
View File

@ -0,0 +1,49 @@
/*
SPI example
*/
package main
import (
"github.com/stianeikeland/go-rpio"
"fmt"
)
func main() {
if err := rpio.Open(); err != nil {
panic(err)
}
if err := rpio.SpiBegin(rpio.Spi0); err != nil {
panic(err)
}
rpio.SpiChipSelect(0) // Select CE0 slave
// Send
rpio.SpiTransmit(0xFF) // send single byte
rpio.SpiTransmit(0xDE, 0xAD, 0xBE) // send several bytes
data := []byte{'H', 'e', 'l', 'l', 'o', 0}
rpio.SpiTransmit(data...) // send slice of bytes
// Receive
received := rpio.SpiReceive(5) // receive 5 bytes, (sends 5 x 0s)
fmt.Println(received)
// Send & Receive
buffer := []byte{ 0xDE, 0xED, 0xBE, 0xEF }
rpio.SpiExchange(buffer) // buffer is populated with received data
fmt.Println(buffer)
rpio.SpiEnd(rpio.Spi0)
rpio.Close()
}