tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/aht20/aht20_test.go (about)

     1  package aht20
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/frankban/quicktest"
     7  	"tinygo.org/x/drivers/tester"
     8  )
     9  
    10  func TestDefaultI2CAddress(t *testing.T) {
    11  	c := qt.New(t)
    12  	bus := tester.NewI2CBus(c)
    13  	dev := New(bus)
    14  	c.Assert(uint8(dev.Address), qt.Equals, uint8(Address))
    15  }
    16  
    17  func TestInitialization(t *testing.T) {
    18  	c := qt.New(t)
    19  	bus := tester.NewI2CBus(c)
    20  	fdev := tester.NewI2CDeviceCmd(c, Address)
    21  	fdev.Commands = defaultCommands()
    22  	bus.AddDevice(fdev)
    23  
    24  	// Set status to uninitialized to force initialization
    25  	fdev.Commands[CMD_STATUS].Response[0] = 0x0C
    26  
    27  	dev := New(bus)
    28  	dev.Configure()
    29  
    30  	// Check initialization command invoked
    31  	c.Assert(fdev.Commands[CMD_INITIALIZE].Invocations > 0, qt.Equals, true)
    32  }
    33  
    34  func TestRead(t *testing.T) {
    35  	c := qt.New(t)
    36  	bus := tester.NewI2CBus(c)
    37  	fdev := tester.NewI2CDeviceCmd(c, Address)
    38  	fdev.Commands = defaultCommands()
    39  	bus.AddDevice(fdev)
    40  
    41  	dev := New(bus)
    42  	dev.Read()
    43  
    44  	// Should be 25deg (250 decidegrees)
    45  	c.Assert(dev.DeciCelsius(), qt.Equals, int32(250))
    46  
    47  	// Should be 36.3% (363 decipercent)
    48  	c.Assert(dev.DeciRelHumidity(), qt.Equals, int32(363))
    49  }
    50  
    51  func defaultCommands() map[uint8]*tester.Cmd {
    52  	return map[uint8]*tester.Cmd{
    53  		CMD_INITIALIZE: {
    54  			Command:  []byte{0xBE},
    55  			Mask:     []byte{0xFF},
    56  			Response: []byte{},
    57  		},
    58  		CMD_TRIGGER: {
    59  			Command:  []byte{0xAC, 0x33, 0x00},
    60  			Mask:     []byte{0xFF, 0xFF, 0xFF},
    61  			Response: []byte{0x1C, 0x5D, 0x10, 0x66, 0x01, 0xD2, 0x93},
    62  		},
    63  		CMD_SOFTRESET: {
    64  			Command:  []byte{0xBA},
    65  			Mask:     []byte{0xFF},
    66  			Response: []byte{},
    67  		},
    68  		CMD_STATUS: {
    69  			Command:  []byte{0x71},
    70  			Mask:     []byte{0xFF},
    71  			Response: []byte{0x1C},
    72  		},
    73  	}
    74  }