Back to godoc.org

Package i2c

v1.0.0
Latest Go to latest

The highest tagged major version is .

Published: Aug 10, 2017 | License: Apache-2.0 | Module: periph.io/x/periph

Overview

Package i2c defines interface to an I²C bus and an I²C device.

It includes the adapter Dev to directly address an I²C device on a I²C bus without having to continuously specify the address when doing I/O. This enables the support of conn.Conn.

Index

Examples

type Bus

type Bus interface {
	Tx(addr uint16, w, r []byte) error
	// SetSpeed changes the bus speed, if supported.
	//
	// On linux due to the way the I²C sysfs driver is exposed in userland,
	// calling this function will likely affect *all* I²C buses on the host.
	SetSpeed(hz int64) error
}

Bus defines the interface a concrete I²C driver must implement.

This interface is consummed by a device driver for a device sitting on a bus.

This interface doesn't implement conn.Conn since a device address must be specified. Use i2cdev.Dev as an adapter to get a conn.Conn compatible object.

type BusCloser

type BusCloser interface {
	io.Closer
	Bus
}

BusCloser is an I²C bus that can be closed.

This interface is meant to be handled by the application and not the device driver. A device driver doesn't "own" a bus, hence it must operate on a Bus, not a BusCloser.

type Dev

type Dev struct {
	Bus  Bus
	Addr uint16
}

Dev is a device on a I²C bus.

It implements conn.Conn.

It saves from repeatedly specifying the device address.

Example

Code:

//b, err := i2creg.Open("")
//defer b.Close()
var b Bus

// Dev is a valid conn.Conn.
d := &Dev{Addr: 23, Bus: b}
var _ conn.Conn = d

// Send a command and expect a 5 bytes reply.
reply := [5]byte{}
if err := d.Tx([]byte("A command"), reply[:]); err != nil {
	log.Fatal(err)
}

func (*Dev) Duplex

func (d *Dev) Duplex() conn.Duplex

Duplex always return conn.Half for I²C.

func (*Dev) String

func (d *Dev) String() string

func (*Dev) Tx

func (d *Dev) Tx(w, r []byte) error

Tx does a transaction by adding the device's address to each command.

It's a wrapper for Bus.Tx().

func (*Dev) Write

func (d *Dev) Write(b []byte) (int, error)

Write writes to the I²C bus without reading, implementing io.Writer.

It's a wrapper for Tx()

type Pins

type Pins interface {
	// SCL returns the CLK (clock) pin.
	SCL() gpio.PinIO
	// SDA returns the DATA pin.
	SDA() gpio.PinIO
}

Pins defines the pins that an I²C bus interconnect is using on the host.

It is expected that a implementer of Bus also implement Pins but this is not a requirement.

Example

Code:

//b, err := i2creg.Open("")
//defer b.Close()
var b Bus

// Prints out the gpio pin used.
if p, ok := b.(Pins); ok {
	fmt.Printf("SDA: %s", p.SDA())
	fmt.Printf("SCL: %s", p.SCL())
}

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier