-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample_test.go
76 lines (65 loc) · 1.85 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 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 spi_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)
}
// Use spireg SPI port registry to find the first available SPI bus.
p, err := spireg.Open("")
if err != nil {
log.Fatal(err)
}
defer p.Close()
// Convert the spi.Port into a spi.Conn so it can be used for communication.
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:])
}
func ExamplePins() {
// 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)
}
// Use spireg SPI port registry to find the first available SPI bus.
p, err := spireg.Open("")
if err != nil {
log.Fatal(err)
}
defer p.Close()
// Convert the spi.Port into a spi.Conn so it can be used for communication.
c, err := p.Connect(physic.MegaHertz, spi.Mode3, 8)
if err != nil {
log.Fatal(err)
}
// Prints out the gpio pin used.
if p, ok := c.(spi.Pins); ok {
fmt.Printf(" CLK : %s", p.CLK())
fmt.Printf(" MOSI: %s", p.MOSI())
fmt.Printf(" MISO: %s", p.MISO())
fmt.Printf(" CS : %s", p.CS())
}
}