From 19c9fe6db379bd0a4e2ba34ddc957f93abd7385c Mon Sep 17 00:00:00 2001 From: sago35 Date: Sun, 14 Jul 2024 13:37:13 +0900 Subject: [PATCH] Add error handling --- dht20/dht20.go | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/dht20/dht20.go b/dht20/dht20.go index c692ea7..a34c0b9 100644 --- a/dht20/dht20.go +++ b/dht20/dht20.go @@ -5,11 +5,16 @@ package dht20 import ( + "errors" "time" "tinygo.org/x/drivers" ) +var ( + errUpdateCalledTooSoon = errors.New("Update() called within 80ms is invalid") +) + // Device wraps an I2C connection to a DHT20 device. type Device struct { bus drivers.I2C @@ -63,31 +68,35 @@ func (d *Device) initRegisters() { } // Update reads data from the sensor and updates the temperature and humidity values. +// Note that the values obtained by this function are from the previous call to Update. +// If you want to use the most recent values, shorten the interval at which Update is called. func (d *Device) Update(which drivers.Measurement) error { // Check if 80ms have passed since the last access - if d.prevAccessTime.Add(80 * time.Millisecond).Before(time.Now()) { - // Check the status word Bit[7] - d.data[0] = 0x71 - d.bus.Tx(d.Address, d.data[:1], d.data[:1]) - if (d.data[0] & 0x80) == 0 { - // Read 7 bytes of data from the sensor - d.bus.Tx(d.Address, nil, d.data[:7]) - rawHumidity := uint32(d.data[1])<<12 | uint32(d.data[2])<<4 | uint32(d.data[3])>>4 - rawTemperature := uint32(d.data[3]&0x0F)<<16 | uint32(d.data[4])<<8 | uint32(d.data[5]) + if time.Since(d.prevAccessTime) < 80*time.Millisecond { + return errUpdateCalledTooSoon + } - // Convert raw values to human-readable values - d.humidity = float32(rawHumidity) / 1048576.0 * 100 - d.temperature = float32(rawTemperature)/1048576.0*200 - 50 + // Check the status word Bit[7] + d.data[0] = 0x71 + d.bus.Tx(d.Address, d.data[:1], d.data[:1]) + if (d.data[0] & 0x80) == 0 { + // Read 7 bytes of data from the sensor + d.bus.Tx(d.Address, nil, d.data[:7]) + rawHumidity := uint32(d.data[1])<<12 | uint32(d.data[2])<<4 | uint32(d.data[3])>>4 + rawTemperature := uint32(d.data[3]&0x0F)<<16 | uint32(d.data[4])<<8 | uint32(d.data[5]) - // Trigger the next measurement - d.data[0] = 0xAC - d.data[1] = 0x33 - d.data[2] = 0x00 - d.bus.Tx(d.Address, d.data[:3], nil) + // Convert raw values to human-readable values + d.humidity = float32(rawHumidity) / 1048576.0 * 100 + d.temperature = float32(rawTemperature)/1048576.0*200 - 50 - // Update the previous access time to the current time - d.prevAccessTime = time.Now() - } + // Trigger the next measurement + d.data[0] = 0xAC + d.data[1] = 0x33 + d.data[2] = 0x00 + d.bus.Tx(d.Address, d.data[:3], nil) + + // Update the previous access time to the current time + d.prevAccessTime = time.Now() } return nil }