gobot.io/x/gobot@v1.16.0/drivers/i2c/th02_driver_test.go (about) 1 package i2c 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 "time" 8 9 "gobot.io/x/gobot" 10 "gobot.io/x/gobot/gobottest" 11 ) 12 13 var _ gobot.Driver = (*TH02Driver)(nil) 14 15 // // --------- HELPERS 16 func initTestTH02Driver() *SHT3xDriver { 17 driver, _ := initTestSHT3xDriverWithStubbedAdaptor() 18 return driver 19 } 20 21 func initTestTH02DriverWithStubbedAdaptor() (*TH02Driver, *i2cTestAdaptor) { 22 adaptor := newI2cTestAdaptor() 23 return NewTH02Driver(adaptor), adaptor 24 } 25 26 // --------- TESTS 27 28 func TestNewTH02Driver(t *testing.T) { 29 i2cd := newI2cTestAdaptor() 30 defer i2cd.Close() 31 // Does it return a pointer to an instance of SHT3xDriver? 32 var iface interface{} = NewTH02Driver(i2cd) 33 _, ok := iface.(*TH02Driver) 34 if !ok { 35 t.Errorf("NewTH02Driver() should have returned a *NewTH02Driver") 36 } 37 b := NewTH02Driver(i2cd, func(Config) {}) 38 gobottest.Refute(t, b.Connection(), nil) 39 40 //cover some basically useless protions the Interface demands 41 if name := b.Name(); name != b.name { 42 t.Errorf("Didnt return the proper name. Got %q wanted %q", name, b.name) 43 } 44 45 if b.SetName("42"); b.name != "42" { 46 t.Errorf("yikes - didnt set name.") 47 } 48 } 49 50 func TestTH02Driver_Accuracy(t *testing.T) { 51 i2cd := newI2cTestAdaptor() 52 defer i2cd.Close() 53 b := NewTH02Driver(i2cd) 54 55 if b.SetAddress(0x42); b.addr != 0x42 { 56 t.Error("Didnt set address as expected") 57 } 58 59 if b.SetAccuracy(0x42); b.accuracy != TH02HighAccuracy { 60 t.Error("Setting an invalid accuracy should resolve to TH02HighAccuracy") 61 } 62 63 if b.SetAccuracy(TH02LowAccuracy); b.accuracy != TH02LowAccuracy { 64 t.Error("Expected setting low accuracy to actually set to low accuracy") 65 } 66 67 if acc := b.Accuracy(); acc != TH02LowAccuracy { 68 t.Errorf("Accuract() didnt return what was expected") 69 } 70 } 71 72 func TestTH022DriverStart(t *testing.T) { 73 b, _ := initTestTH02DriverWithStubbedAdaptor() 74 gobottest.Assert(t, b.Start(), nil) 75 } 76 77 func TestTH02StartConnectError(t *testing.T) { 78 d, adaptor := initTestTH02DriverWithStubbedAdaptor() 79 adaptor.Testi2cConnectErr(true) 80 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 81 } 82 83 func TestTH02DriverHalt(t *testing.T) { 84 sht3x := initTestTH02Driver() 85 gobottest.Assert(t, sht3x.Halt(), nil) 86 } 87 88 func TestTH02DriverOptions(t *testing.T) { 89 d := NewTH02Driver(newI2cTestAdaptor(), WithBus(2)) 90 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 91 d.Halt() 92 } 93 94 func TestTH02Driver_ReadData(t *testing.T) { 95 d, i2cd := initTestTH02DriverWithStubbedAdaptor() 96 gobottest.Assert(t, d.Start(), nil) 97 98 type x struct { 99 rd, wr func([]byte) (int, error) 100 rtn uint16 101 errNil bool 102 } 103 104 tests := map[string]x{ 105 "example RH": x{ 106 rd: func(b []byte) (int, error) { 107 copy(b, []byte{0x00, 0x07, 0xC0}) 108 return 3, nil 109 }, 110 wr: func([]byte) (int, error) { 111 return 1, nil 112 }, 113 errNil: true, 114 rtn: 1984, 115 }, 116 "example T": x{ 117 rd: func(b []byte) (int, error) { 118 copy(b, []byte{0x00, 0x12, 0xC0}) 119 return 3, nil 120 }, 121 wr: func([]byte) (int, error) { 122 return 1, nil 123 }, 124 errNil: true, 125 rtn: 4800, 126 }, 127 "timeout - no wait for ready": x{ 128 rd: func(b []byte) (int, error) { 129 time.Sleep(200 * time.Millisecond) 130 copy(b, []byte{0x01}) 131 return 1, fmt.Errorf("nope") 132 }, 133 wr: func([]byte) (int, error) { 134 return 1, nil 135 }, 136 errNil: false, 137 rtn: 0, 138 }, 139 "unable to write status register": x{ 140 rd: func(b []byte) (int, error) { 141 copy(b, []byte{0x00}) 142 return 0, nil 143 }, 144 wr: func([]byte) (int, error) { 145 return 0, fmt.Errorf("Nope") 146 }, 147 errNil: false, 148 rtn: 0, 149 }, 150 "unable to read doesnt provide enought data": x{ 151 rd: func(b []byte) (int, error) { 152 copy(b, []byte{0x00, 0x01}) 153 return 2, nil 154 }, 155 wr: func([]byte) (int, error) { 156 return 1, nil 157 }, 158 errNil: false, 159 rtn: 0, 160 }, 161 } 162 163 for name, x := range tests { 164 t.Log("Running", name) 165 i2cd.i2cReadImpl = x.rd 166 i2cd.i2cWriteImpl = x.wr 167 got, err := d.readData() 168 gobottest.Assert(t, err == nil, x.errNil) 169 gobottest.Assert(t, got, x.rtn) 170 } 171 } 172 173 func TestTH02Driver_waitForReady(t *testing.T) { 174 d, i2cd := initTestTH02DriverWithStubbedAdaptor() 175 gobottest.Assert(t, d.Start(), nil) 176 177 i2cd.i2cReadImpl = func(b []byte) (int, error) { 178 time.Sleep(50 * time.Millisecond) 179 copy(b, []byte{0x01, 0x00}) 180 return 3, nil 181 } 182 183 i2cd.i2cWriteImpl = func([]byte) (int, error) { 184 return 1, nil 185 } 186 187 timeout := 10 * time.Microsecond 188 if err := d.waitForReady(&timeout); err == nil { 189 t.Error("Expected a timeout error") 190 } 191 } 192 193 func TestTH02Driver_WriteRegister(t *testing.T) { 194 d, i2cd := initTestTH02DriverWithStubbedAdaptor() 195 gobottest.Assert(t, d.Start(), nil) 196 197 i2cd.i2cWriteImpl = func([]byte) (int, error) { 198 return 1, nil 199 } 200 201 if err := d.writeRegister(0x00, 0x00); err != nil { 202 t.Errorf("expected a nil error write") 203 } 204 } 205 206 func TestTH02Driver_Heater(t *testing.T) { 207 d, i2cd := initTestTH02DriverWithStubbedAdaptor() 208 gobottest.Assert(t, d.Start(), nil) 209 210 i2cd.i2cReadImpl = func(b []byte) (int, error) { 211 copy(b, []byte{0xff}) 212 return 1, nil 213 } 214 215 i2cd.i2cWriteImpl = func([]byte) (int, error) { 216 return 1, nil 217 } 218 219 on, err := d.Heater() 220 gobottest.Assert(t, on, true) 221 gobottest.Assert(t, err, nil) 222 } 223 func TestTH02Driver_SerialNumber(t *testing.T) { 224 d, i2cd := initTestTH02DriverWithStubbedAdaptor() 225 gobottest.Assert(t, d.Start(), nil) 226 227 i2cd.i2cReadImpl = func(b []byte) (int, error) { 228 copy(b, []byte{0x42}) 229 return 1, nil 230 } 231 232 i2cd.i2cWriteImpl = func([]byte) (int, error) { 233 return 1, nil 234 } 235 236 sn, err := d.SerialNumber() 237 238 gobottest.Assert(t, sn, uint32((0x42)>>4)) 239 gobottest.Assert(t, err, nil) 240 } 241 242 func TestTH02Driver_ApplySettings(t *testing.T) { 243 d := &TH02Driver{} 244 245 type x struct { 246 acc, base, out byte 247 heating bool 248 } 249 250 tests := map[string]x{ 251 "low acc, heating": x{acc: TH02LowAccuracy, base: 0x00, heating: true, out: 0x01}, 252 "high acc, no heating": x{acc: TH02HighAccuracy, base: 0x00, heating: false, out: 0x23}, 253 } 254 255 for name, x := range tests { 256 t.Log(name) 257 d.accuracy = x.acc 258 d.heating = x.heating 259 got := d.applysettings(x.base) 260 gobottest.Assert(t, x.out, got) 261 } 262 } 263 264 func TestTH02Driver_Sample(t *testing.T) { 265 d, i2cd := initTestTH02DriverWithStubbedAdaptor() 266 gobottest.Assert(t, d.Start(), nil) 267 268 i2cd.i2cReadImpl = func(b []byte) (int, error) { 269 copy(b, []byte{0x00, 0x00, 0x07, 0xC0}) 270 return 4, nil 271 } 272 273 i2cd.i2cWriteImpl = func([]byte) (int, error) { 274 return 1, nil 275 } 276 277 temp, rh, _ := d.Sample() 278 279 gobottest.Assert(t, temp, float32(0)) 280 gobottest.Assert(t, rh, float32(0)) 281 282 }