tester

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2021 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package tester contains mock structs to make it easier to test I2C devices.

TODO: info on how to use this.

Index

Constants

View Source
const MaxRegisters = 200

MaxRegisters is the maximum number of registers supported for a Device.

Variables

This section is empty.

Functions

This section is empty.

Types

type Failer

type Failer interface {
	// Fatalf prints the Printf-formatted message and exits the current
	// goroutine.
	Fatalf(f string, a ...interface{})
}

Failer is used by the I2CDevice type to abort when it's used in unexpected ways, such as reading an out-of-range register.

type I2CBus

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

I2CBus implements the I2C interface in memory for testing.

func NewI2CBus

func NewI2CBus(c Failer) *I2CBus

NewI2CBus returns an I2CBus mock I2C instance that uses c to flag errors if they happen. After creating a I2C instance, add devices to it with addDevice before using NewI2CBus interface.

func (*I2CBus) AddDevice

func (bus *I2CBus) AddDevice(d *I2CDevice)

AddDevice adds a new mock device to the mock I2C bus. It panics if a device with the same address is added more than once.

func (*I2CBus) FindDevice

func (bus *I2CBus) FindDevice(addr uint8) *I2CDevice

FindDevice returns the device with the given address.

func (*I2CBus) NewDevice added in v0.15.1

func (bus *I2CBus) NewDevice(addr uint8) *I2CDevice

NewDevice creates a new device with the given address and adds it to the mock I2C bus.

func (*I2CBus) ReadRegister

func (bus *I2CBus) ReadRegister(addr uint8, r uint8, buf []byte) error

ReadRegister implements I2C.ReadRegister.

func (*I2CBus) Tx

func (bus *I2CBus) Tx(addr uint16, w, r []byte) error

Tx implements I2C.Tx.

func (*I2CBus) WriteRegister

func (bus *I2CBus) WriteRegister(addr uint8, r uint8, buf []byte) error

WriteRegister implements I2C.WriteRegister.

type I2CDevice

type I2CDevice struct {

	// Registers holds the device registers. It can be inspected
	// or changed as desired for testing.
	Registers [MaxRegisters]uint8
	// If Err is non-nil, it will be returned as the error from the
	// I2C methods.
	Err error
	// contains filtered or unexported fields
}

I2CDevice represents a mock I2C device on a mock I2C bus.

func NewI2CDevice

func NewI2CDevice(c Failer, addr uint8) *I2CDevice

NewI2CDevice returns a new mock I2C device.

func (*I2CDevice) Addr

func (d *I2CDevice) Addr() uint8

Addr returns the Device address.

func (*I2CDevice) ReadRegister

func (d *I2CDevice) ReadRegister(r uint8, buf []byte) error

ReadRegister implements I2C.ReadRegister.

func (*I2CDevice) WriteRegister

func (d *I2CDevice) WriteRegister(r uint8, buf []byte) error

WriteRegister implements I2C.WriteRegister.

Jump to

Keyboard shortcuts

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