From 23a42cad9b661321b6484f1088adf476bf6c379f Mon Sep 17 00:00:00 2001 From: Stian Eikeland Date: Thu, 29 Nov 2018 17:29:11 +0100 Subject: [PATCH] Added a SPI usage example --- examples/spi/spi.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/spi/spi.go diff --git a/examples/spi/spi.go b/examples/spi/spi.go new file mode 100644 index 0000000..8611327 --- /dev/null +++ b/examples/spi/spi.go @@ -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() +}