i2c

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2020 License: MIT Imports: 6 Imported by: 0

README

i2c

Build Status Go Report Card GoDoc MIT License

This project is a set of Go bindings for the I²C bus, focused on sensors compatible with Raspberry Pi (and RPi clones). This project was forked from github.com/d2r2/go-i2c, which itself was forked from github.com/davecheney/i2c.

License

i2c is licensed under MIT License.

Compatibility

Pre-fork this project was tested on Raspberry Pi 1 (Model B), Raspberry Pi 3 (Model B+), Banana Pi (model M1), Orange Pi Zero, Orange Pi One.

Usage

This section is a work in progress. Ideally the GoDoc page provides enough context to get started.

Troubleshooting

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Documentation

Overview

Package i2c provides low level interactions with the Linux I²C bus.

Before usage you should load the i2c-dev kernel module

sudo modprobe i2c-dev

Each I²C bus can address 127 independent I²C devices, and most Linux systems contain several buses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDebugf added in v0.2.0

func DefaultDebugf(string, ...interface{})

DefaultDebugf is a no-op formatted debug printf function used by the Device type by default. This is exported so that you can use it to toggle debug logging back off using Device.SetDebugf().

Types

type Device

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

Device is a connection to a device on the I²C bus. It contains a file handle to a specific device address on a numbered I²C bus.

func New

func New(bus int, addr uint8) (*Device, error)

New opens a new file handle on the provided I²C bus, making an ioctl call to request read/write access to the device at the specified address.

Most interactions start with either reads or writes at a specific register address. See ReadReg and WriteReg.

func (*Device) Addr

func (d *Device) Addr() uint8

Addr returns the device's address on the I²C bus.

func (*Device) Bus

func (d *Device) Bus() int

Bus return bus number to create this device.

func (*Device) Close

func (d *Device) Close() error

Close I²C file handle.

func (*Device) Read

func (d *Device) Read(p []byte) (int, error)

Read satisfies io.Reader, reading data from the I2C device.

func (*Device) ReadReg

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

ReadReg reads I2C device data at the specified register address into the buffer provided. This expects you to right-size the buffer so that it only reads the appropriate amount of data.

func (*Device) SetDebugf

func (d *Device) SetDebugf(debugf func(format string, args ...interface{}))

SetDebugf sets a formatted debug function, which can be used to hook in to your logging system.

func (*Device) Write

func (d *Device) Write(p []byte) (int, error)

Write satisfies io.Writer, sending data to the I2C device.

func (*Device) WriteByte

func (d *Device) WriteByte(b byte) (int, error)

WriteByte writes a single byte to the I2C device.

func (*Device) WriteReg

func (d *Device) WriteReg(p []byte, reg byte) (int, error)

WriteReg writes a series of bytes to a specific register address.

Jump to

Keyboard shortcuts

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