forked from google/periph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
42 lines (36 loc) · 945 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package conn_test
import (
"fmt"
"log"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spireg"
"periph.io/x/periph/host"
)
func Example() {
// Make sure periph is initialized.
if _, err := host.Init(); err != nil {
log.Fatal(err)
}
// Using SPI as an example. See package ./spi/spireg for more details.
p, err := spireg.Open("")
if err != nil {
log.Fatal(err)
}
defer p.Close()
c, err := p.Connect(physic.MegaHertz, spi.Mode3, 8)
if err != nil {
log.Fatal(err)
}
// Write 0x10 to the device, and read a byte right after.
write := []byte{0x10, 0x00}
read := make([]byte, len(write))
if err := c.Tx(write, read); err != nil {
log.Fatal(err)
}
// Use read.
fmt.Printf("%v\n", read[1:])
}