-
Notifications
You must be signed in to change notification settings - Fork 13
/
example_test.go
44 lines (38 loc) · 1.03 KB
/
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
43
44
// 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/conn/v3/driver/driverreg"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
)
func Example() {
// Make sure periph is initialized.
// TODO: Use host.Init(). It is not used in this example to prevent circular
// go package import.
if _, err := driverreg.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:])
}