gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/helpers_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"sync"
     7  )
     8  
     9  var rgb = map[string]interface{}{
    10  	"red":   1.0,
    11  	"green": 1.0,
    12  	"blue":  1.0,
    13  }
    14  
    15  func castColor(color string) byte {
    16  	return byte(rgb[color].(float64))
    17  }
    18  
    19  var red = castColor("red")
    20  var green = castColor("green")
    21  var blue = castColor("blue")
    22  
    23  type i2cTestAdaptor struct {
    24  	name          string
    25  	bus           int
    26  	address       int
    27  	written       []byte
    28  	mtx           sync.Mutex
    29  	i2cConnectErr bool
    30  	i2cReadImpl   func([]byte) (int, error)
    31  	i2cWriteImpl  func([]byte) (int, error)
    32  }
    33  
    34  func (t *i2cTestAdaptor) Testi2cConnectErr(val bool) {
    35  	t.mtx.Lock()
    36  	defer t.mtx.Unlock()
    37  	t.i2cConnectErr = val
    38  }
    39  
    40  func (t *i2cTestAdaptor) Testi2cReadImpl(f func([]byte) (int, error)) {
    41  	t.mtx.Lock()
    42  	defer t.mtx.Unlock()
    43  	t.i2cReadImpl = f
    44  }
    45  
    46  func (t *i2cTestAdaptor) Testi2cWriteImpl(f func([]byte) (int, error)) {
    47  	t.mtx.Lock()
    48  	defer t.mtx.Unlock()
    49  	t.i2cWriteImpl = f
    50  }
    51  
    52  func (t *i2cTestAdaptor) Read(b []byte) (count int, err error) {
    53  	t.mtx.Lock()
    54  	defer t.mtx.Unlock()
    55  	return t.i2cReadImpl(b)
    56  }
    57  
    58  func (t *i2cTestAdaptor) Write(b []byte) (count int, err error) {
    59  	t.mtx.Lock()
    60  	defer t.mtx.Unlock()
    61  	t.written = append(t.written, b...)
    62  	return t.i2cWriteImpl(b)
    63  }
    64  
    65  func (t *i2cTestAdaptor) Close() error {
    66  	return nil
    67  }
    68  
    69  func (t *i2cTestAdaptor) ReadByte() (val byte, err error) {
    70  	t.mtx.Lock()
    71  	defer t.mtx.Unlock()
    72  	bytes := []byte{0}
    73  	if err = t.readBytes(bytes); err != nil {
    74  		return
    75  	}
    76  	val = bytes[0]
    77  	return
    78  }
    79  
    80  func (t *i2cTestAdaptor) ReadByteData(reg uint8) (val uint8, err error) {
    81  	t.mtx.Lock()
    82  	defer t.mtx.Unlock()
    83  	if err = t.writeBytes([]byte{reg}); err != nil {
    84  		return
    85  	}
    86  	bytes := []byte{0}
    87  	if err = t.readBytes(bytes); err != nil {
    88  		return
    89  	}
    90  	val = bytes[0]
    91  	return
    92  }
    93  
    94  func (t *i2cTestAdaptor) ReadWordData(reg uint8) (val uint16, err error) {
    95  	t.mtx.Lock()
    96  	defer t.mtx.Unlock()
    97  	if err = t.writeBytes([]byte{reg}); err != nil {
    98  		return
    99  	}
   100  	bytes := []byte{0, 0}
   101  	bytesRead, err := t.i2cReadImpl(bytes)
   102  	if err != nil {
   103  		return 0, err
   104  	}
   105  	if bytesRead != 2 {
   106  		return 0, fmt.Errorf("Buffer underrun")
   107  	}
   108  	low, high := bytes[0], bytes[1]
   109  	return (uint16(high) << 8) | uint16(low), err
   110  }
   111  
   112  func (t *i2cTestAdaptor) ReadBlockData(reg uint8, b []byte) (err error) {
   113  	t.mtx.Lock()
   114  	defer t.mtx.Unlock()
   115  	if err = t.writeBytes([]byte{reg}); err != nil {
   116  		return
   117  	}
   118  	return t.readBytes(b)
   119  }
   120  
   121  func (t *i2cTestAdaptor) WriteByte(val byte) error {
   122  	t.mtx.Lock()
   123  	defer t.mtx.Unlock()
   124  	return t.writeBytes([]byte{val})
   125  }
   126  
   127  func (t *i2cTestAdaptor) WriteByteData(reg uint8, val uint8) (err error) {
   128  	t.mtx.Lock()
   129  	defer t.mtx.Unlock()
   130  	bytes := []byte{reg, val}
   131  
   132  	return t.writeBytes(bytes)
   133  }
   134  
   135  func (t *i2cTestAdaptor) WriteWordData(reg uint8, val uint16) error {
   136  	t.mtx.Lock()
   137  	defer t.mtx.Unlock()
   138  	low := uint8(val & 0xff)
   139  	high := uint8((val >> 8) & 0xff)
   140  	bytes := []byte{reg, low, high}
   141  
   142  	return t.writeBytes(bytes)
   143  }
   144  
   145  func (t *i2cTestAdaptor) WriteBlockData(reg uint8, b []byte) error {
   146  	t.mtx.Lock()
   147  	defer t.mtx.Unlock()
   148  	if len(b) > 32 {
   149  		b = b[:32]
   150  	}
   151  	buf := make([]byte, len(b)+1)
   152  	copy(buf[1:], b)
   153  	buf[0] = reg
   154  
   155  	return t.writeBytes(buf)
   156  }
   157  
   158  func (t *i2cTestAdaptor) WriteBytes(b []byte) error {
   159  	t.mtx.Lock()
   160  	defer t.mtx.Unlock()
   161  	if len(b) > 32 {
   162  		b = b[:32]
   163  	}
   164  	return t.writeBytes(b)
   165  }
   166  
   167  func (t *i2cTestAdaptor) GetI2cConnection(address int, bus int) (connection Connection, err error) {
   168  	if t.i2cConnectErr {
   169  		return nil, errors.New("Invalid i2c connection")
   170  	}
   171  	t.bus = bus
   172  	t.address = address
   173  	return t, nil
   174  }
   175  
   176  func (t *i2cTestAdaptor) DefaultI2cBus() int {
   177  	return 0
   178  }
   179  
   180  func (t *i2cTestAdaptor) Name() string          { return t.name }
   181  func (t *i2cTestAdaptor) SetName(n string)      { t.name = n }
   182  func (t *i2cTestAdaptor) Connect() (err error)  { return }
   183  func (t *i2cTestAdaptor) Finalize() (err error) { return }
   184  
   185  func newI2cTestAdaptor() *i2cTestAdaptor {
   186  	return &i2cTestAdaptor{
   187  		i2cConnectErr: false,
   188  		i2cReadImpl: func(b []byte) (int, error) {
   189  			return len(b), nil
   190  		},
   191  		i2cWriteImpl: func(b []byte) (int, error) {
   192  			return len(b), nil
   193  		},
   194  	}
   195  }
   196  
   197  func (t *i2cTestAdaptor) readBytes(b []byte) error {
   198  	n, err := t.i2cReadImpl(b)
   199  	if err != nil {
   200  		return err
   201  	}
   202  	if n != len(b) {
   203  		return fmt.Errorf("Read %v bytes from device by i2c helpers, expected %v", n, len(b))
   204  	}
   205  	return nil
   206  }
   207  
   208  func (t *i2cTestAdaptor) writeBytes(b []byte) error {
   209  	t.written = append(t.written, b...)
   210  	// evaluation of count can be done in test
   211  	_, err := t.i2cWriteImpl(b)
   212  	if err != nil {
   213  		return err
   214  	}
   215  	return nil
   216  }