i2c

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2017 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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
//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)
}
Output:

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
//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())
}
Output:

Directories

Path Synopsis
Package i2creg defines I²C bus registry to list buses present on the host.
Package i2creg defines I²C bus registry to list buses present on the host.
Package i2csmoketest is leveraged by periph-smoketest to verify that an I²C EEPROM device and a DS2483 device can be accessed on an I²C bus.
Package i2csmoketest is leveraged by periph-smoketest to verify that an I²C EEPROM device and a DS2483 device can be accessed on an I²C bus.
Package i2ctest is meant to be used to test drivers over a fake I²C bus.
Package i2ctest is meant to be used to test drivers over a fake I²C bus.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL