2014-04-27 18:54:41 -07:00
|
|
|
package i2c
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-09-11 15:38:08 -05:00
|
|
|
var rgb = map[string]interface{}{
|
|
|
|
"red": 1.0,
|
|
|
|
"green": 1.0,
|
|
|
|
"blue": 1.0,
|
|
|
|
}
|
|
|
|
|
|
|
|
func castColor(color string) byte {
|
|
|
|
return byte(rgb[color].(float64))
|
|
|
|
}
|
|
|
|
|
|
|
|
var red = castColor("red")
|
|
|
|
var green = castColor("green")
|
|
|
|
var blue = castColor("blue")
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-06-15 17:22:50 -07:00
|
|
|
type i2cTestAdaptor struct {
|
2014-12-22 13:36:52 -08:00
|
|
|
name string
|
2016-12-29 15:42:00 -06:00
|
|
|
written []byte
|
2017-01-20 00:17:41 +01:00
|
|
|
pos int
|
2014-12-22 13:36:52 -08:00
|
|
|
i2cReadImpl func() ([]byte, error)
|
|
|
|
i2cWriteImpl func() error
|
|
|
|
i2cStartImpl func() error
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
|
|
|
|
2015-07-03 18:57:29 -07:00
|
|
|
func (t *i2cTestAdaptor) I2cStart(int) (err error) {
|
2017-01-20 00:17:41 +01:00
|
|
|
t.pos = 0
|
2014-12-22 13:36:52 -08:00
|
|
|
return t.i2cStartImpl()
|
|
|
|
}
|
2017-01-20 00:17:41 +01:00
|
|
|
|
2016-12-29 15:44:48 -06:00
|
|
|
func (t *i2cTestAdaptor) I2cRead(int, int) (data []byte, err error) {
|
2014-12-22 13:36:52 -08:00
|
|
|
return t.i2cReadImpl()
|
|
|
|
}
|
2017-01-20 00:17:41 +01:00
|
|
|
|
2016-12-29 15:42:00 -06:00
|
|
|
func (t *i2cTestAdaptor) I2cWrite(address int, b []byte) (err error) {
|
|
|
|
t.written = append(t.written, b...)
|
2014-12-22 13:36:52 -08:00
|
|
|
return t.i2cWriteImpl()
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|
2017-01-20 00:17:41 +01:00
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadByte() (val uint8, err error) {
|
|
|
|
bytes, err := t.i2cReadImpl()
|
|
|
|
val = bytes[t.pos]
|
|
|
|
t.pos++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadByteData(reg uint8) (val uint8, err error) {
|
|
|
|
bytes, err := t.i2cReadImpl()
|
|
|
|
return bytes[reg], err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadWordData(reg uint8) (val uint16, err error) {
|
|
|
|
bytes, err := t.i2cReadImpl()
|
|
|
|
low, high := bytes[reg], bytes[reg + 1]
|
|
|
|
return (uint16(high) << 8) | uint16(low), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadBlockData(b []byte) (n int, err error) {
|
|
|
|
reg := b[0]
|
|
|
|
bytes, err := t.i2cReadImpl()
|
|
|
|
copy(b, bytes[reg:])
|
|
|
|
return len(b), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) WriteByte(val uint8) (err error) {
|
|
|
|
t.pos = int(val)
|
|
|
|
t.written = append(t.written, val)
|
|
|
|
return t.i2cWriteImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) WriteByteData(reg uint8, val uint8) (err error) {
|
|
|
|
t.pos = int(reg)
|
|
|
|
t.written = append(t.written, reg)
|
|
|
|
t.written = append(t.written, val)
|
|
|
|
return t.i2cWriteImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) WriteBlockData(b []byte) (err error) {
|
|
|
|
t.pos = int(b[0])
|
|
|
|
t.written = append(t.written, b...)
|
|
|
|
return t.i2cWriteImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) I2cGetConnection( /* address */ int /* bus */, int) (connection I2cConnection, err error) {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:55:21 +01:00
|
|
|
func (t *i2cTestAdaptor) Name() string { return t.name }
|
|
|
|
func (t *i2cTestAdaptor) SetName(n string) { t.name = n }
|
|
|
|
func (t *i2cTestAdaptor) Connect() (err error) { return }
|
|
|
|
func (t *i2cTestAdaptor) Finalize() (err error) { return }
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2016-09-25 14:08:18 +02:00
|
|
|
func newI2cTestAdaptor() *i2cTestAdaptor {
|
2014-06-15 17:22:50 -07:00
|
|
|
return &i2cTestAdaptor{
|
2014-12-22 13:36:52 -08:00
|
|
|
i2cReadImpl: func() ([]byte, error) {
|
|
|
|
return []byte{}, nil
|
|
|
|
},
|
|
|
|
i2cWriteImpl: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
i2cStartImpl: func() error {
|
|
|
|
return nil
|
2014-09-11 15:38:08 -05:00
|
|
|
},
|
2014-06-15 17:22:50 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
}
|