tester

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 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 = 255

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

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Command     []byte
	Mask        []byte
	Response    []byte
	Invocations int
}

Cmd represents a command sent via I2C to a device.

A command matches when (Command & Mask) == (Data & Mask). If a command is recognized, Response bytes is returned.

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

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

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 interface {
	// ReadRegister implements I2C.ReadRegister.
	ReadRegister(r uint8, buf []byte) error

	// WriteRegister implements I2C.WriteRegister.
	WriteRegister(r uint8, buf []byte) error

	// Tx implements I2C.Tx
	Tx(w, r []byte) error

	// Addr returns the Device address.
	Addr() uint8
}

type I2CDevice16

type I2CDevice16 struct {

	// Registers holds the device registers. It can be inspected
	// or changed as desired for testing.
	Registers map[uint8]uint16
	// 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 with 16-bit registers.

func NewI2CDevice16

func NewI2CDevice16(c Failer, addr uint8) *I2CDevice16

NewI2CDevice returns a new mock I2C device.

To use this mock, populate the Registers map with known / expected registers. Attempts by the code under test to write to a register that has not been populated into the map will be treated as an error.

func (*I2CDevice16) Addr

func (d *I2CDevice16) Addr() uint8

Addr returns the Device address.

func (*I2CDevice16) ReadRegister

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

ReadRegister implements I2C.ReadRegister.

func (*I2CDevice16) Tx

func (bus *I2CDevice16) Tx(w, r []byte) error

Tx implements I2C.Tx.

func (*I2CDevice16) WriteRegister

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

WriteRegister implements I2C.WriteRegister.

type I2CDevice8

type I2CDevice8 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 with 8-bit registers.

func NewI2CDevice

func NewI2CDevice(c Failer, addr uint8) *I2CDevice8

NewI2CDevice returns a new mock I2C device.

For compatibility, this creates an instance of NewI2CDevice8

func NewI2CDevice8

func NewI2CDevice8(c Failer, addr uint8) *I2CDevice8

NewI2CDevice8 returns a new mock I2C device.

func (*I2CDevice8) Addr

func (d *I2CDevice8) Addr() uint8

Addr returns the Device address.

func (*I2CDevice8) ReadRegister

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

ReadRegister implements I2C.ReadRegister.

func (*I2CDevice8) Tx

func (bus *I2CDevice8) Tx(w, r []byte) error

Tx implements I2C.Tx.

func (*I2CDevice8) WriteRegister

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

WriteRegister implements I2C.WriteRegister.

type I2CDeviceCmd

type I2CDeviceCmd struct {

	// Commands are the commands the device recognizes and responds to.
	Commands map[uint8]*Cmd

	// If Err is non-nil, it will be returned as the error from the
	// I2C methods.
	Err error
	// contains filtered or unexported fields
}

I2CDeviceCmd represents a mock I2C device that does not have 'registers', but has a command/response model.

Commands and canned responses are pre-loaded into the Commands member. For each command the mock receives it will lookup the command and return the corresponding canned response.

func NewI2CDeviceCmd

func NewI2CDeviceCmd(c Failer, addr uint8) *I2CDeviceCmd

NewI2CDeviceCmd returns a new mock I2C device.

func (*I2CDeviceCmd) Addr

func (d *I2CDeviceCmd) Addr() uint8

Addr returns the Device address.

func (*I2CDeviceCmd) FindCommand

func (d *I2CDeviceCmd) FindCommand(command []byte) *Cmd

func (*I2CDeviceCmd) ReadRegister

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

ReadRegister implements I2C.ReadRegister.

func (*I2CDeviceCmd) Tx

func (d *I2CDeviceCmd) Tx(w, r []byte) error

Tx implements I2C.Tx.

func (*I2CDeviceCmd) WriteRegister

func (d *I2CDeviceCmd) 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