mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-08 08:53:40 +00:00
6763521eff
This type should be used whenever a sensor (or actuator?) works with a
temperature. For example, this commit changes the signature:
ReadTemperature() (int32, error)
to the following:
ReadTemperature() (drivers.Temperature, error)
I believe this is much clearer in intent. It also makes it trivial to
introduce common conversions. For example, there are already Celsius()
and Fahrenheit() methods to convert to the given units, as a floating
point. More units could be added as needed, for example a CelsiusInt().
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
// Package adt7410 provides a driver for the adt7410 I2C Temperature Sensor.
|
|
//
|
|
// Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADT7410.pdf
|
|
//
|
|
package adt7410 // import "tinygo.org/x/drivers/adt7410"
|
|
|
|
import (
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers"
|
|
)
|
|
|
|
type Error uint8
|
|
|
|
const (
|
|
ErrInvalidID Error = 0x1
|
|
)
|
|
|
|
func (e Error) Error() string {
|
|
switch e {
|
|
case ErrInvalidID:
|
|
return "Invalid chip ID"
|
|
default:
|
|
return "Unknown error"
|
|
}
|
|
}
|
|
|
|
type Device struct {
|
|
bus drivers.I2C
|
|
buf []byte
|
|
Address uint8
|
|
}
|
|
|
|
// New returns ADT7410 device for the provided I2C bus using default address.
|
|
// of 0x48 (1001000). To use multiple ADT7410 devices, the last 2 bits of the address
|
|
// can be set using by connecting to the A1 and A0 pins to VDD or GND (for a
|
|
// total of up to 4 devices on a I2C bus). Also note that 10k pullups are
|
|
// recommended for the SDA and SCL lines.
|
|
func New(i2c drivers.I2C) *Device {
|
|
return &Device{
|
|
bus: i2c,
|
|
buf: make([]byte, 2),
|
|
Address: Address,
|
|
}
|
|
}
|
|
|
|
// Configure the ADT7410 device.
|
|
func (d *Device) Configure() (err error) {
|
|
// reset the chip
|
|
d.writeByte(RegReset, 0xFF)
|
|
time.Sleep(10 * time.Millisecond)
|
|
return
|
|
}
|
|
|
|
// Connected returns whether sensor has been found.
|
|
func (d *Device) Connected() bool {
|
|
data := []byte{0}
|
|
d.bus.ReadRegister(uint8(d.Address), RegID, data)
|
|
return data[0]&0xF8 == 0xC8
|
|
}
|
|
|
|
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
|
|
func (d *Device) ReadTemperature() (temperature drivers.Temperature, err error) {
|
|
return (drivers.Temperature(d.readUint16(RegTempValueMSB)) * 1000) / 128, nil
|
|
}
|
|
|
|
func (d *Device) writeByte(reg uint8, data byte) {
|
|
d.buf[0] = reg
|
|
d.buf[1] = data
|
|
d.bus.Tx(uint16(d.Address), d.buf, nil)
|
|
}
|
|
|
|
func (d *Device) readByte(reg uint8) byte {
|
|
d.bus.ReadRegister(d.Address, reg, d.buf)
|
|
return d.buf[0]
|
|
}
|
|
|
|
func (d *Device) readUint16(reg uint8) uint16 {
|
|
d.bus.ReadRegister(d.Address, reg, d.buf)
|
|
return uint16(d.buf[0])<<8 | uint16(d.buf[1])
|
|
}
|