i2cmux

package module
v0.0.0-...-9e2fcf4 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

README

i2cmux

A i2c mulitplexer/switch interface for periph.io interfaces

Documentation

Overview

Package i2cmux provides a API for using a I²C(i2c) multiplexer such as a NXP PCA9548A.

Example
package main

import (
	"fmt"
	"log"

	"github.com/NeuralSpaz/i2cmux"
	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/devices/bmxx80"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}
	mux, err := i2cmux.New("/dev/i2c-1", i2cmux.Address(0x40))
	if err != nil {
		log.Fatalln(err)
	}
	// register the channel with the mux
	ch, err := mux.RegisterChannel(0)
	if err != nil {
		log.Fatalln(err)
	}
	// use the mux channel like any other i2c.Bus example here taken from the
	// bmp180/280 example
	d, err := bmxx80.NewI2C(ch, 0x76, &bmxx80.DefaultOpts)
	if err != nil {
		log.Fatalf("failed to initialize bme280: %v", err)
	}
	e := physic.Env{}
	if err := d.Sense(&e); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%8s %10s %9s\n", e.Temperature, e.Pressure, e.Humidity)

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address(address uint16) func(*Mux) error

Address sets the i2c address in not using the default address of 0x40

func Channels

func Channels(channels uint8) func(*Mux) error

Channels sets the number of channels on the mux

func Debug

func Debug() func(*Mux) error

Debug sets the enables mux debug output

func Reset

func Reset(pin gpio.PinIO) func(*Mux) error

Reset use if you have a reset pin

Types

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

Channel implements the i2c.Bus interface through the Mux

func (Channel) Scan

func (c Channel) Scan() []uint16

Scan the channel for i2c devices, returns a slice of i2c addresses

func (Channel) SetSpeed

func (c Channel) SetSpeed(f physic.Frequency) error

SetSpeed sets the clock of the Mux, Changing the channel clock will also change the underlying i2c bus clock and is not supported on all hosts.

func (Channel) String

func (c Channel) String() string

String returns the channel number

func (Channel) Tx

func (c Channel) Tx(addr uint16, w, r []byte) error

Tx w or r can be omitted for one way communication

type Mux

type Mux struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Mux is i2c mux such NXP PCA9548A

func New

func New(name string, opts ...func(*Mux) error) (*Mux, error)

New creates a new Mux on an i2c bus given the bus name and mux i2c address

func (*Mux) RegisterChannel

func (m *Mux) RegisterChannel(channel uint8) (Channel, error)

RegisterChannel returns a i2c.Bus

Jump to

Keyboard shortcuts

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