diff --git a/aht20/aht20_test.go b/aht20/aht20_test.go index ef833bd..15efe42 100644 --- a/aht20/aht20_test.go +++ b/aht20/aht20_test.go @@ -7,13 +7,80 @@ import ( "testing" ) +const byteStatusInitialized = bitInitialized | 0x10 + +func TestNewI2C(t *testing.T) { + type TestCase struct { + name string + ops []i2ctest.IO + } + + testCases := []TestCase{ + { + name: "device already initialized", + ops: []i2ctest.IO{ + // Read status + {Addr: deviceAddress, W: []byte{cmdStatus}, R: []byte{byteStatusInitialized}}, + }, + }, + { + name: "device not initialized", + ops: []i2ctest.IO{ + // Read status + {Addr: deviceAddress, W: []byte{cmdStatus}, R: []byte{0x00}}, + // Initialize + {Addr: deviceAddress, W: argsInitialize}, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + bus := i2ctest.Playback{Ops: tc.ops} + if dev, err := NewI2C(&bus, nil); err != nil { + t.Fatal(err) + } else if dev == nil { + t.Fatal("expected device") + } + }) + } +} + +func TestDev_IsInitialized(t *testing.T) { + bus := i2ctest.Playback{ + Ops: []i2ctest.IO{ + // Read status + {Addr: deviceAddress, W: []byte{cmdStatus}, R: []byte{byteStatusInitialized}}, + }, + } + dev := Dev{d: &i2c.Dev{Bus: &bus, Addr: deviceAddress}, opts: DefaultOpts} + if err, initialized := dev.IsInitialized(); err != nil { + t.Fatal(err) + } else if !initialized { + t.Fatal("expected initialized") + } +} + +func TestDev_Initialize(t *testing.T) { + bus := i2ctest.Playback{ + Ops: []i2ctest.IO{ + // Initialize + {Addr: deviceAddress, W: argsInitialize}, + }, + } + dev := Dev{d: &i2c.Dev{Bus: &bus, Addr: deviceAddress}, opts: DefaultOpts} + if err := dev.Initialize(); err != nil { + t.Fatal(err) + } +} + func TestDev_Sense(t *testing.T) { bus := i2ctest.Playback{ Ops: []i2ctest.IO{ // Trigger measurement {Addr: deviceAddress, W: argsMeasure}, // Read measurement - {Addr: deviceAddress, R: []byte{0x18, 0x75, 0x52, 0x05, 0x8E, 0x40, 0x7F}}, + {Addr: deviceAddress, R: []byte{byteStatusInitialized, 0x75, 0x52, 0x05, 0x8E, 0x40, 0x7F}}, }, } dev := Dev{d: &i2c.Dev{Bus: &bus, Addr: deviceAddress}, opts: DefaultOpts} @@ -34,3 +101,16 @@ func TestDev_Sense(t *testing.T) { t.Fatal(err) } } + +func TestDev_SoftReset(t *testing.T) { + bus := i2ctest.Playback{ + Ops: []i2ctest.IO{ + // Soft reset + {Addr: deviceAddress, W: []byte{cmdSoftReset}}, + }, + } + dev := Dev{d: &i2c.Dev{Bus: &bus, Addr: deviceAddress}, opts: DefaultOpts} + if err := dev.SoftReset(); err != nil { + t.Fatal(err) + } +}