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

     1  // Package adt7410 provides a driver for the adt7410 I2C Temperature Sensor.
     2  //
     3  // Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADT7410.pdf
     4  package adt7410 // import "tinygo.org/x/drivers/adt7410"
     5  
     6  import (
     7  	"time"
     8  
     9  	"tinygo.org/x/drivers"
    10  	"tinygo.org/x/drivers/internal/legacy"
    11  )
    12  
    13  type Error uint8
    14  
    15  const (
    16  	ErrInvalidID Error = 0x1
    17  )
    18  
    19  func (e Error) Error() string {
    20  	switch e {
    21  	case ErrInvalidID:
    22  		return "Invalid chip ID"
    23  	default:
    24  		return "Unknown error"
    25  	}
    26  }
    27  
    28  type Device struct {
    29  	bus     drivers.I2C
    30  	buf     []byte
    31  	Address uint8
    32  }
    33  
    34  // New returns ADT7410 device for the provided I2C bus using default address.
    35  // of 0x48 (1001000).  To use multiple ADT7410 devices, the last 2 bits of the address
    36  // can be set using by connecting to the A1 and A0 pins to VDD or GND (for a
    37  // total of up to 4 devices on a I2C bus).  Also note that 10k pullups are
    38  // recommended for the SDA and SCL lines.
    39  func New(i2c drivers.I2C) *Device {
    40  	return &Device{
    41  		bus:     i2c,
    42  		buf:     make([]byte, 2),
    43  		Address: Address,
    44  	}
    45  }
    46  
    47  // Configure the ADT7410 device.
    48  func (d *Device) Configure() (err error) {
    49  	// reset the chip
    50  	d.writeByte(RegReset, 0xFF)
    51  	time.Sleep(10 * time.Millisecond)
    52  	return
    53  }
    54  
    55  // Connected returns whether sensor has been found.
    56  func (d *Device) Connected() bool {
    57  	data := []byte{0}
    58  	legacy.ReadRegister(d.bus, uint8(d.Address), RegID, data)
    59  	return data[0]&0xF8 == 0xC8
    60  }
    61  
    62  // ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
    63  func (d *Device) ReadTemperature() (temperature int32, err error) {
    64  	return (int32(d.readUint16(RegTempValueMSB)) * 1000) / 128, nil
    65  }
    66  
    67  // ReadTempC returns the value in the temperature value register, in Celsius.
    68  func (d *Device) ReadTempC() float32 {
    69  	t := d.readUint16(RegTempValueMSB)
    70  	return float32(int(t)) / 128.0
    71  }
    72  
    73  // ReadTempF returns the value in the temperature value register, in Fahrenheit.
    74  func (d *Device) ReadTempF() float32 {
    75  	return d.ReadTempC()*1.8 + 32.0
    76  }
    77  
    78  func (d *Device) writeByte(reg uint8, data byte) {
    79  	d.buf[0] = reg
    80  	d.buf[1] = data
    81  	d.bus.Tx(uint16(d.Address), d.buf, nil)
    82  }
    83  
    84  func (d *Device) readByte(reg uint8) byte {
    85  	legacy.ReadRegister(d.bus, d.Address, reg, d.buf)
    86  	return d.buf[0]
    87  }
    88  
    89  func (d *Device) readUint16(reg uint8) uint16 {
    90  	legacy.ReadRegister(d.bus, d.Address, reg, d.buf)
    91  	return uint16(d.buf[0])<<8 | uint16(d.buf[1])
    92  }