2014-04-28 09:54:41 +08:00
|
|
|
package i2c
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
import "fmt"
|
|
|
|
|
2014-09-12 04:38:08 +08: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 18:11:51 +08:00
|
|
|
|
2014-06-16 08:22:50 +08:00
|
|
|
type i2cTestAdaptor struct {
|
2014-12-23 05:36:52 +08:00
|
|
|
name string
|
2016-12-30 05:42:00 +08:00
|
|
|
written []byte
|
2017-02-06 07:19:42 +08:00
|
|
|
i2cReadImpl func([]byte) (int, error)
|
|
|
|
i2cWriteImpl func([]byte) (int, error)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
func (t *i2cTestAdaptor) Read(b []byte) (count int, err error) {
|
|
|
|
return t.i2cReadImpl(b)
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
2017-01-20 07:17:41 +08:00
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
func (t *i2cTestAdaptor) Write(b []byte) (count int, err error) {
|
|
|
|
t.written = append(t.written, b...)
|
|
|
|
return t.i2cWriteImpl(b)
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
2017-01-20 07:17:41 +08:00
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
func (t *i2cTestAdaptor) Close() error {
|
|
|
|
return nil
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2017-01-20 07:17:41 +08:00
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadByte() (val uint8, err error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
bytes := []byte{0}
|
|
|
|
bytesRead, err := t.i2cReadImpl(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if bytesRead != 1 {
|
|
|
|
return 0, fmt.Errorf("Buffer underrun")
|
|
|
|
}
|
|
|
|
val = bytes[0]
|
2017-01-20 07:17:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadByteData(reg uint8) (val uint8, err error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
bytes := []byte{0}
|
|
|
|
bytesRead, err := t.i2cReadImpl(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if bytesRead != 1 {
|
|
|
|
return 0, fmt.Errorf("Buffer underrun")
|
|
|
|
}
|
|
|
|
val = bytes[0]
|
|
|
|
return
|
2017-01-20 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) ReadWordData(reg uint8) (val uint16, err error) {
|
2017-02-06 07:19:42 +08:00
|
|
|
bytes := []byte{0, 0}
|
|
|
|
bytesRead, err := t.i2cReadImpl(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if bytesRead != 2 {
|
|
|
|
return 0, fmt.Errorf("Buffer underrun")
|
|
|
|
}
|
|
|
|
low, high := bytes[0], bytes[1]
|
2017-01-20 07:17:41 +08:00
|
|
|
return (uint16(high) << 8) | uint16(low), err
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
func (t *i2cTestAdaptor) ReadBlockData(_ uint8, b []byte) (n int, err error) {
|
|
|
|
bytes := make([]byte, 32)
|
|
|
|
bytesRead, err := t.i2cReadImpl(bytes)
|
|
|
|
copy(b, bytes[:bytesRead])
|
|
|
|
return bytesRead, err
|
2017-01-20 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) WriteByte(val uint8) (err error) {
|
|
|
|
t.written = append(t.written, val)
|
2017-02-06 07:19:42 +08:00
|
|
|
bytes := []byte{val}
|
|
|
|
_, err = t.i2cWriteImpl(bytes)
|
|
|
|
return
|
2017-01-20 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) WriteByteData(reg uint8, val uint8) (err error) {
|
|
|
|
t.written = append(t.written, reg)
|
|
|
|
t.written = append(t.written, val)
|
2017-02-06 07:19:42 +08:00
|
|
|
bytes := []byte{val}
|
|
|
|
_, err = t.i2cWriteImpl(bytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) WriteWordData(reg uint8, val uint16) (err error) {
|
|
|
|
t.written = append(t.written, reg)
|
|
|
|
low := uint8(val & 0xff)
|
|
|
|
high := uint8((val >> 8) & 0xff)
|
|
|
|
t.written = append(t.written, low)
|
|
|
|
t.written = append(t.written, high)
|
|
|
|
bytes := []byte{low, high}
|
|
|
|
_, err = t.i2cWriteImpl(bytes)
|
|
|
|
return
|
2017-01-20 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
func (t *i2cTestAdaptor) WriteBlockData(reg uint8, b []byte) (err error) {
|
|
|
|
t.written = append(t.written, reg)
|
2017-01-20 07:17:41 +08:00
|
|
|
t.written = append(t.written, b...)
|
2017-02-06 07:19:42 +08:00
|
|
|
_, err = t.i2cWriteImpl(b)
|
|
|
|
return
|
2017-01-20 07:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *i2cTestAdaptor) I2cGetConnection( /* address */ int /* bus */, int) (connection I2cConnection, err error) {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
func (t *i2cTestAdaptor) I2cGetDefaultBus() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:55:21 +08: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 18:11:51 +08:00
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
func newI2cTestAdaptor() *i2cTestAdaptor {
|
2014-06-16 08:22:50 +08:00
|
|
|
return &i2cTestAdaptor{
|
2017-02-06 07:19:42 +08:00
|
|
|
i2cReadImpl: func([]byte) (int, error) {
|
|
|
|
return 0, nil
|
2014-12-23 05:36:52 +08:00
|
|
|
},
|
2017-02-06 07:19:42 +08:00
|
|
|
i2cWriteImpl: func([]byte) (int, error) {
|
|
|
|
return 0, nil
|
2014-09-12 04:38:08 +08:00
|
|
|
},
|
2014-06-16 08:22:50 +08:00
|
|
|
}
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|