i2c

package module
v0.0.0-...-b77880f Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT Imports: 4 Imported by: 4

README

I2C under Linux in Go

This is a library to use the I2C bus e.g. on the Raspberry Pi in Go.

Disclaimer

I develop this library for myself and just for fun in my free time. If you find it useful, I'm happy to hear about that. If you have trouble using it, you have all the source code to fix the problem yourself (although pull requests are welcome).

Usage

// open communication with the device at address 0x60 on bus number 1
bus := i2c.Open(0x60, 1) 

// write one byte to register 0x1
bus.WriteReg(0x1, 0x23)

// read one byte from register 0x2
buf := make([]byte, 1)
bus.ReadReg(0x2, buf)

// get a writer that writes to register 0x3 and the following registers
w := bus.RegWriter(0x3)
someFancyFuncThatWritesToAWriter(w)

// read n bytes from register 0xa and the following registers
buf := make([]byte, n)
bus.ReadReg(0xa, buf)

// check if any error happened in the meantime
if bus.Err() != nil {
    log.Fatal(bus.Err())
}
Error Handling

The bus keeps the first error that happened when using any of the methods that can return an error. After this first error occurred, all following method invocations will do nothing and just return this error.

This allows to make several calls and have only one error check after the last call. Less error checking boilerplate.

License

This software is published under the MIT License.

Copyright Florian Thienel

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug = false

Debug indicates if the debug mode is enabled. In debug mode, all incoming and outgoing bytes are written to stdout.

Functions

This section is empty.

Types

type I2C

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

I2C bus.

func Open

func Open(address uint8, bus int) (*I2C, error)

Open a new I2C connection to the device with the given address on the given I2C bus.

func (*I2C) Address

func (b *I2C) Address() uint8

Address of the device this instance is communicating with.

func (*I2C) Bus

func (b *I2C) Bus() int

Bus on which this instance is communicating.

func (*I2C) Close

func (b *I2C) Close() error

Close the I2C communication

func (*I2C) Err

func (b *I2C) Err() error

Err returns the first error that happened when using this instance or nil if everything is fine. If Err returns an error != nil, all further invocations of Read and Write will also fail with this error. This instance should not be used anymore.

func (*I2C) Read

func (b *I2C) Read(p []byte) (int, error)

Read from the bus.

func (*I2C) ReadReg

func (b *I2C) ReadReg(reg uint8, p []byte) (int, error)

ReadReg reads len(p) bytes from the given register (and the following if len(p) > 1).

func (*I2C) RegWriter

func (b *I2C) RegWriter(reg uint8) io.Writer

RegWriter returns a writer that writes to the given register.

func (*I2C) Write

func (b *I2C) Write(p []byte) (int, error)

Write to the bus.

func (*I2C) WriteReg

func (b *I2C) WriteReg(reg uint8, values ...byte) (int, error)

WriteReg writes the given bytes to the given register (and the following if there is more than one byte given).

Jump to

Keyboard shortcuts

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