From 6f213e97c347079f737e511c17943a9c57005610 Mon Sep 17 00:00:00 2001 From: Yannis Huber <32066446+yannishuber@users.noreply.github.com> Date: Fri, 3 Apr 2020 13:11:46 +0200 Subject: [PATCH] Add driver for TMP102 low-power digital temperature sensor (#141) * tmp102: add driver and example --- README.md | 3 ++- examples/tmp102/main.go | 28 ++++++++++++++++++++ tmp102/registers.go | 18 +++++++++++++ tmp102/tmp102.go | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 examples/tmp102/main.go create mode 100644 tmp102/registers.go create mode 100644 tmp102/tmp102.go diff --git a/README.md b/README.md index 767bcd2..b7a8f3e 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ func main() { ## Currently supported devices -The following 45 devices are supported. +The following 46 devices are supported. | Device Name | Interface Type | |----------|-------------| @@ -101,6 +101,7 @@ The following 45 devices are supported. | [Waveshare 2.13" e-paper display](https://www.waveshare.com/w/upload/e/e6/2.13inch_e-Paper_Datasheet.pdf) | SPI | | [Waveshare 2.13" (B & C) e-paper display](https://www.waveshare.com/w/upload/d/d3/2.13inch-e-paper-b-Specification.pdf) | SPI | | [WS2812 RGB LED](https://cdn-shop.adafruit.com/datasheets/WS2812.pdf) | GPIO | +| [TMP102 I2C Temperature Sensor](https://download.mikroe.com/documents/datasheets/tmp102-data-sheet.pdf) | I2C | ## Contributing diff --git a/examples/tmp102/main.go b/examples/tmp102/main.go new file mode 100644 index 0000000..5d764b4 --- /dev/null +++ b/examples/tmp102/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "machine" + "time" + + "tinygo.org/x/drivers/tmp102" +) + +func main() { + machine.I2C0.Configure(machine.I2CConfig{ + Frequency: machine.TWI_FREQ_400KHZ, + }) + + thermo := tmp102.New(machine.I2C0) + thermo.Configure(tmp102.Config{}) + + for { + + temp, _ := thermo.ReadTemperature() + + print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0)) + + time.Sleep(time.Millisecond * 1000) + } + +} diff --git a/tmp102/registers.go b/tmp102/registers.go new file mode 100644 index 0000000..644a9d5 --- /dev/null +++ b/tmp102/registers.go @@ -0,0 +1,18 @@ +package tmp102 + +const ( + // Default I2C address + Address = 0x48 + + // Temperature register address + RegTemperature = 0x00 + + // Configuration register address + RegConfiguration = 0x01 + + // Low limit register address + RegLimitLow = 0x02 + + // High limit register address + RegLimitHigh = 0x03 +) diff --git a/tmp102/tmp102.go b/tmp102/tmp102.go new file mode 100644 index 0000000..5c568d4 --- /dev/null +++ b/tmp102/tmp102.go @@ -0,0 +1,58 @@ +// Package tmp102 implements a driver for the TMP102 digital temperature sensor. +// +// Datasheet: https://download.mikroe.com/documents/datasheets/tmp102-data-sheet.pdf + +package tmp102 // import "tinygo.org/x/drivers/tmp102" + +import ( + "machine" +) + +// Device holds the already configured I2C bus and the address of the sensor. +type Device struct { + bus machine.I2C + address uint8 +} + +// Config is the configuration for the TMP102. +type Config struct { + Address uint8 +} + +// New creates a new TMP102 connection. The I2C bus must already be configured. +func New(bus machine.I2C) Device { + return Device{ + bus: bus, + } +} + +// Configure initializes the sensor with the given parameters. +func (d *Device) Configure(cfg Config) { + if cfg.Address == 0 { + cfg.Address = Address + } + + d.address = cfg.Address +} + +// Reads the temperature from the sensor and returns it in celsius milli degrees (°C/1000). +func (d *Device) ReadTemperature() (temperature int32, err error) { + + tmpData := make([]byte, 2) + + err = d.bus.ReadRegister(d.address, RegTemperature, tmpData) + + if err != nil { + return + } + + temperatureSum := int32((int16(tmpData[0])<<8 | int16(tmpData[1])) >> 4) + + if (temperatureSum & int32(1<<11)) == int32(1<<11) { + temperatureSum |= int32(0xf800) + } + + temperature = temperatureSum * 625 + + return temperature / 10, nil +}