Back to

package i2c

v0.0.0 (0022984)
Latest Go to latest
Published: May 13, 2020 | License: BSD-3-Clause | Module:


Package i2c allows users to read from and write to a slave I2C device.


This package is not maintained anymore. An actively supported cross-platform alternative is



func TenBit

func TenBit(addr int) int

TenBit marks an I2C address as a 10-bit address.

type Devfs

type Devfs struct {
	// Dev is the I2C bus device, e.g. /dev/i2c-1. Required.
	Dev string

Devfs is an I2C driver that works against the devfs. You need to load the "i2c-dev" kernel module to use this driver.

func (*Devfs) Open

func (d *Devfs) Open(addr int, tenbit bool) (driver.Conn, error)

type Device

type Device struct {
	// contains filtered or unexported fields

Device represents an I2C device. Devices must be closed once they are no longer in use.

func Open

func Open(o driver.Opener, addr int) (*Device, error)

Open opens a connection to an I2C device. All devices must be closed once they are no longer in use. For devices that use 10-bit I2C addresses, addr can be marked as a 10-bit address with TenBit.



d, err := i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, 0x39)
if err != nil {

// opens a 10-bit address
d, err = i2c.Open(&i2c.Devfs{Dev: "/dev/i2c-1"}, i2c.TenBit(0x78))
if err != nil {

_ = d

func (*Device) Close

func (d *Device) Close() error

Close closes the device and releases the underlying sources.

func (*Device) Read

func (d *Device) Read(buf []byte) error

Read reads len(buf) bytes from the device.

func (*Device) ReadReg

func (d *Device) ReadReg(reg byte, buf []byte) error

ReadReg is similar to Read but it reads from a register.

func (*Device) Write

func (d *Device) Write(buf []byte) (err error)

Write writes the buffer to the device. If it is required to write to a specific register, the register should be passed as the first byte in the given buffer.

func (*Device) WriteReg

func (d *Device) WriteReg(reg byte, buf []byte) (err error)

WriteReg is similar to Write but writes to a register.

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

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier