gobot.io/x/gobot@v1.16.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 written []byte 26 mtx sync.Mutex 27 i2cConnectErr bool 28 i2cReadImpl func([]byte) (int, error) 29 i2cWriteImpl func([]byte) (int, error) 30 } 31 32 func (t *i2cTestAdaptor) Testi2cConnectErr(val bool) { 33 t.mtx.Lock() 34 defer t.mtx.Unlock() 35 t.i2cConnectErr = val 36 } 37 38 func (t *i2cTestAdaptor) Testi2cReadImpl(f func([]byte) (int, error)) { 39 t.mtx.Lock() 40 defer t.mtx.Unlock() 41 t.i2cReadImpl = f 42 } 43 44 func (t *i2cTestAdaptor) Testi2cWriteImpl(f func([]byte) (int, error)) { 45 t.mtx.Lock() 46 defer t.mtx.Unlock() 47 t.i2cWriteImpl = f 48 } 49 50 func (t *i2cTestAdaptor) Read(b []byte) (count int, err error) { 51 t.mtx.Lock() 52 defer t.mtx.Unlock() 53 return t.i2cReadImpl(b) 54 } 55 56 func (t *i2cTestAdaptor) Write(b []byte) (count int, err error) { 57 t.mtx.Lock() 58 defer t.mtx.Unlock() 59 t.written = append(t.written, b...) 60 return t.i2cWriteImpl(b) 61 } 62 63 func (t *i2cTestAdaptor) Close() error { 64 return nil 65 } 66 67 func (t *i2cTestAdaptor) ReadByte() (val byte, err error) { 68 t.mtx.Lock() 69 defer t.mtx.Unlock() 70 bytes := []byte{0} 71 bytesRead, err := t.i2cReadImpl(bytes) 72 if err != nil { 73 return 0, err 74 } 75 if bytesRead != 1 { 76 return 0, fmt.Errorf("Buffer underrun") 77 } 78 val = bytes[0] 79 return 80 } 81 82 func (t *i2cTestAdaptor) ReadByteData(reg uint8) (val uint8, err error) { 83 if err = t.WriteByte(reg); err != nil { 84 return 85 } 86 t.mtx.Lock() 87 defer t.mtx.Unlock() 88 bytes := []byte{0} 89 bytesRead, err := t.i2cReadImpl(bytes) 90 if err != nil { 91 return 0, err 92 } 93 if bytesRead != 1 { 94 return 0, fmt.Errorf("Buffer underrun") 95 } 96 val = bytes[0] 97 return 98 } 99 100 func (t *i2cTestAdaptor) ReadWordData(reg uint8) (val uint16, err error) { 101 if err = t.WriteByte(reg); err != nil { 102 return 103 } 104 t.mtx.Lock() 105 defer t.mtx.Unlock() 106 bytes := []byte{0, 0} 107 bytesRead, err := t.i2cReadImpl(bytes) 108 if err != nil { 109 return 0, err 110 } 111 if bytesRead != 2 { 112 return 0, fmt.Errorf("Buffer underrun") 113 } 114 low, high := bytes[0], bytes[1] 115 return (uint16(high) << 8) | uint16(low), err 116 } 117 118 func (t *i2cTestAdaptor) WriteByte(val byte) (err error) { 119 t.mtx.Lock() 120 defer t.mtx.Unlock() 121 t.written = append(t.written, val) 122 bytes := []byte{val} 123 _, err = t.i2cWriteImpl(bytes) 124 return 125 } 126 127 func (t *i2cTestAdaptor) WriteByteData(reg uint8, val uint8) (err error) { 128 t.mtx.Lock() 129 defer t.mtx.Unlock() 130 t.written = append(t.written, reg) 131 t.written = append(t.written, val) 132 bytes := []byte{val} 133 _, err = t.i2cWriteImpl(bytes) 134 return 135 } 136 137 func (t *i2cTestAdaptor) WriteWordData(reg uint8, val uint16) (err error) { 138 t.mtx.Lock() 139 defer t.mtx.Unlock() 140 t.written = append(t.written, reg) 141 low := uint8(val & 0xff) 142 high := uint8((val >> 8) & 0xff) 143 t.written = append(t.written, low) 144 t.written = append(t.written, high) 145 bytes := []byte{low, high} 146 _, err = t.i2cWriteImpl(bytes) 147 return 148 } 149 150 func (t *i2cTestAdaptor) WriteBlockData(reg uint8, b []byte) (err error) { 151 t.mtx.Lock() 152 defer t.mtx.Unlock() 153 t.written = append(t.written, reg) 154 t.written = append(t.written, b...) 155 _, err = t.i2cWriteImpl(b) 156 return 157 } 158 159 func (t *i2cTestAdaptor) GetConnection( /* address */ int /* bus */, int) (connection Connection, err error) { 160 if t.i2cConnectErr { 161 return nil, errors.New("Invalid i2c connection") 162 } 163 return t, nil 164 } 165 166 func (t *i2cTestAdaptor) GetDefaultBus() int { 167 return 0 168 } 169 170 func (t *i2cTestAdaptor) Name() string { return t.name } 171 func (t *i2cTestAdaptor) SetName(n string) { t.name = n } 172 func (t *i2cTestAdaptor) Connect() (err error) { return } 173 func (t *i2cTestAdaptor) Finalize() (err error) { return } 174 175 func newI2cTestAdaptor() *i2cTestAdaptor { 176 return &i2cTestAdaptor{ 177 i2cConnectErr: false, 178 i2cReadImpl: func(b []byte) (int, error) { 179 return len(b), nil 180 }, 181 i2cWriteImpl: func([]byte) (int, error) { 182 return 0, nil 183 }, 184 } 185 }