tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/aht20/main.go (about) 1 package main 2 3 import ( 4 "machine" 5 "time" 6 7 "tinygo.org/x/drivers/aht20" 8 ) 9 10 func main() { 11 machine.I2C0.Configure(machine.I2CConfig{}) 12 13 dev := aht20.New(machine.I2C0) 14 dev.Configure() 15 16 dev.Reset() 17 for { 18 time.Sleep(500 * time.Millisecond) 19 20 err := dev.Read() 21 if err != nil { 22 println("Error", err) 23 continue 24 } 25 26 println("temp ", fmtD(dev.DeciCelsius(), 3, 1), "C") 27 println("humidity", fmtD(dev.DeciRelHumidity(), 3, 1), "%") 28 } 29 } 30 31 func fmtD(val int32, i int, f int) string { 32 result := make([]byte, i+f+1) 33 neg := false 34 35 if val < 0 { 36 val = -val 37 neg = true 38 } 39 40 for p := len(result) - 1; p >= 0; p-- { 41 result[p] = byte(int32('0') + (val % 10)) 42 val = val / 10 43 44 if p == i+1 && p > 0 { 45 p-- 46 result[p] = '.' 47 } 48 } 49 50 if neg { 51 result[0] = '-' 52 } 53 54 return string(result) 55 }