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+ //
15package adt7410 // import "tinygo.org/x/drivers/adt7410"
26
37import (
@@ -22,70 +26,67 @@ func (e Error) Error() string {
2226}
2327
2428type Device struct {
25- bus drivers.I2C
26- buf []byte
27- addr uint8
29+ bus drivers.I2C
30+ buf []byte
31+ Address uint8
2832}
2933
30- // New returns ADT7410 device for the provided I2C bus and address. The ADT7410
31- // has a default address of 0x48 (1001000). The last 2 bits of the address
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
3236// can be set using by connecting to the A1 and A0 pins to VDD or GND (for a
3337// total of up to 4 devices on a I2C bus). Also note that 10k pullups are
3438// recommended for the SDA and SCL lines.
35- func New (i2c drivers.I2C , addressBits uint8 ) * Device {
39+ func New (i2c drivers.I2C ) * Device {
3640 return & Device {
37- bus : i2c ,
38- buf : make ([]byte , 2 ),
39- addr : Address | ( addressBits & 0x3 ) ,
41+ bus : i2c ,
42+ buf : make ([]byte , 2 ),
43+ Address : Address ,
4044 }
4145}
4246
43- func (dev * Device ) Configure () (err error ) {
44-
45- // verify the chip ID
46- // TODO: According to datasheet, the check below should work; however
47- // this does not seem to be working right, but is not exactly
48- // necessary, so can revisit later to see if there is a bug
49- //id := dev.ReadByte(RegID) & 0xF8
50- //if id != 0xC8 {
51- // err = ErrInvalidID
52- //}
53-
47+ // Configure the ADT7410 device.
48+ func (d * Device ) Configure () (err error ) {
5449 // reset the chip
55- dev .writeByte (RegReset , 0xFF )
50+ d .writeByte (RegReset , 0xFF )
5651 time .Sleep (10 * time .Millisecond )
5752 return
53+ }
5854
55+ // Connected returns whether sensor has been found.
56+ func (d * Device ) Connected () bool {
57+ data := []byte {0 }
58+ d .bus .ReadRegister (uint8 (d .Address ), RegID , data )
59+ return data [0 ]& 0xF8 == 0xC8
5960}
6061
6162// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
6263func (d * Device ) ReadTemperature () (temperature int32 , err error ) {
6364 return (int32 (d .readUint16 (RegTempValueMSB )) * 1000 ) / 128 , nil
6465}
6566
66- // ReadTempC returns the value in the temperature value register, in Celcius
67+ // ReadTempC returns the value in the temperature value register, in Celsius.
6768func (d * Device ) ReadTempC () float32 {
6869 t := d .readUint16 (RegTempValueMSB )
6970 return float32 (int (t )) / 128.0
7071}
7172
72- // ReadTempF returns the value in the temperature value register, in Fahrenheit
73+ // ReadTempF returns the value in the temperature value register, in Fahrenheit.
7374func (d * Device ) ReadTempF () float32 {
7475 return d .ReadTempC ()* 1.8 + 32.0
7576}
7677
7778func (d * Device ) writeByte (reg uint8 , data byte ) {
7879 d .buf [0 ] = reg
7980 d .buf [1 ] = data
80- d .bus .Tx (uint16 (d .addr ), d .buf , nil )
81+ d .bus .Tx (uint16 (d .Address ), d .buf , nil )
8182}
8283
8384func (d * Device ) readByte (reg uint8 ) byte {
84- d .bus .ReadRegister (d .addr , reg , d .buf )
85+ d .bus .ReadRegister (d .Address , reg , d .buf )
8586 return d .buf [0 ]
8687}
8788
8889func (d * Device ) readUint16 (reg uint8 ) uint16 {
89- d .bus .ReadRegister (d .addr , reg , d .buf )
90+ d .bus .ReadRegister (d .Address , reg , d .buf )
9091 return uint16 (d .buf [0 ])<< 8 | uint16 (d .buf [1 ])
9192}
0 commit comments