1
0
mirror of https://github.com/periph/conn.git synced 2026-10-23 05:28:52 +03:00
Files
conn/example_test.go
Marc-Antoine Ruel 744c52e072 Standardize development checks and CI tooling
Expose fix, verify, test, and help through consistent Make targets, with
locked uv tools, EditorConfig, versioned hooks, shared binary checks, and
generated agent file indexes. Apply safe lint fixes and formatting quietly
while preserving diagnostics and failure exit codes.

Require Go 1.26 and read CI toolchains from go.mod. Track lint and license
tools in the root module; run pinned actionlint separately because of YAML
dependency conflicts. Run vet through golangci-lint and retain distinct
compile/link and module-tidiness checks.

Refresh Actions and share setup where applicable. Ignore Codecov and
coverage artifacts on Unix and Windows while keeping plain Git status
checks for tracked changes and unexpected output.

Run portable runtime and static checks together on Ubuntu while retaining
downstream host, devices, and cmd consumer gates.

Validation: make fix and make verify pass. Applicable test, generation,
race, cross-build, and consumer checks passed during implementation.
Formatter diagnostic fixtures, ignore-rule fixtures, index checks, and
staged hooks pass. Native hosted OS and physical hardware execution remain
outside local validation.

Squashing preserves the validated file tree exactly.
2026-10-18 21:04:33 -04:00

47 lines
1.1 KiB
Go

// 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.
// Example implementing and using a connection interface.
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:])
}