Skip to content

all: introduce a temperature type #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,6 @@ NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 micropho
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))

unit-test:
@go test -v $(addprefix ./,$(TESTS))
@go test -v . $(addprefix ./,$(TESTS))

test: clean fmt-check unit-test smoke-test
15 changes: 2 additions & 13 deletions adt7410/adt7410.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,8 @@ func (d *Device) Connected() bool {
}

// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
func (d *Device) ReadTemperature() (temperature int32, err error) {
return (int32(d.readUint16(RegTempValueMSB)) * 1000) / 128, nil
}

// ReadTempC returns the value in the temperature value register, in Celsius.
func (d *Device) ReadTempC() float32 {
t := d.readUint16(RegTempValueMSB)
return float32(int(t)) / 128.0
}

// ReadTempF returns the value in the temperature value register, in Fahrenheit.
func (d *Device) ReadTempF() float32 {
return d.ReadTempC()*1.8 + 32.0
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) {
Expand Down
4 changes: 2 additions & 2 deletions bme280/bme280.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ func (d *Device) Reset() {
}

// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
func (d *Device) ReadTemperature() (int32, error) {
func (d *Device) ReadTemperature() (drivers.Temperature, error) {
data, err := d.readData()
if err != nil {
return 0, err
}

temp, _ := d.calculateTemp(data)
return temp, nil
return drivers.Temperature(temp), nil
}

// ReadPressure returns the pressure in milli pascals mPa
Expand Down
4 changes: 2 additions & 2 deletions bmi160/bmi160.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (d *DeviceSPI) Reset() error {
}

// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
func (d *DeviceSPI) ReadTemperature() (temperature drivers.Temperature, err error) {
data := d.buf[:3]
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
Expand Down Expand Up @@ -109,7 +109,7 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
// rawTemperature * 1000 * 64 / 0x8000 + 23000
// rawTemperature * 64000 / 0x8000 + 23000
// rawTemperature * 125 / 64 + 23000
temperature = int32(rawTemperature)*125/64 + 23000
temperature = drivers.Temperature(rawTemperature)*125/64 + 23000
return
}

Expand Down
4 changes: 2 additions & 2 deletions bmp180/bmp180.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ func (d *Device) Configure() {
}

// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
func (d *Device) ReadTemperature() (temperature int32, err error) {
func (d *Device) ReadTemperature() (temperature drivers.Temperature, err error) {
rawTemp, err := d.rawTemp()
if err != nil {
return
}
b5 := d.calculateB5(rawTemp)
t := (b5 + 8) >> 4
return 100 * t, nil
return drivers.Temperature(100 * t), nil
}

// ReadPressure returns the pressure in milli pascals (mPa).
Expand Down
4 changes: 2 additions & 2 deletions bmp280/bmp280.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (d *Device) PrintCali() {
}

// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
func (d *Device) ReadTemperature() (temperature int32, err error) {
func (d *Device) ReadTemperature() (temperature drivers.Temperature, err error) {
data, err := d.readData(REG_TEMP, 3)
if err != nil {
return
Expand All @@ -150,7 +150,7 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {

// Convert from degrees to milli degrees by multiplying by 10.
// Will output 30250 milli degrees celsius for 30.25 degrees celsius
temperature = 10 * ((tFine*5 + 128) >> 8)
temperature = drivers.Temperature(10 * ((tFine*5 + 128) >> 8))
return
}

Expand Down
8 changes: 4 additions & 4 deletions bmp388/bmp388.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ func (d *Device) tlinCompensate() (int64, error) {

}

// ReadTemperature returns the temperature in centicelsius, i.e 2426 / 100 = 24.26 C
func (d *Device) ReadTemperature() (int32, error) {
// ReadTemperature returns the temperature in milli degrees Celsius, i.e 24260 / 1000 = 24.26°C.
func (d *Device) ReadTemperature() (drivers.Temperature, error) {

tlin, err := d.tlinCompensate()
if err != nil {
return 0, err
}

temp := (tlin * 25) / 16384
return int32(temp), nil
temp := (tlin * 125) / 8192
return drivers.Temperature(temp), nil
}

// ReadPressure returns the pressure in centipascals, i.e 10132520 / 100 = 101325.20 Pa
Expand Down
15 changes: 0 additions & 15 deletions dht/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ func (d DeviceType) extractData(buf []byte) (temp int16, hum uint16) {
return
}

// Celsius and Fahrenheit temperature scales
type TemperatureScale uint8

func (t TemperatureScale) convertToFloat(temp int16) float32 {
if t == C {
return float32(temp) / 10
} else {
// Fahrenheit
return float32(temp)*(9.0/50.) + 32.
}
}

// All functions return ErrorCode instance as error. This class can be used for more efficient error processing
type ErrorCode uint8

Expand All @@ -57,9 +45,6 @@ const (
DHT11 DeviceType = iota
DHT22

C TemperatureScale = iota
F

ChecksumError ErrorCode = iota
NoSignalError
NoDataError
Expand Down
21 changes: 6 additions & 15 deletions dht/thermometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ package dht // import "tinygo.org/x/drivers/dht"
import (
"machine"
"time"

"tinygo.org/x/drivers"
)

// DummyDevice provides a basic interface for DHT devices.
type DummyDevice interface {
ReadMeasurements() error
Measurements() (temperature int16, humidity uint16, err error)
Temperature() (int16, error)
TemperatureFloat(scale TemperatureScale) (float32, error)
Temperature() (drivers.Temperature, error)
Humidity() (uint16, error)
HumidityFloat() (float32, error)
}
Expand Down Expand Up @@ -49,23 +50,13 @@ func (t *device) ReadMeasurements() error {
return err
}

// Getter for temperature. Temperature method returns temperature as it is sent by device.
// The temperature is measured temperature in Celsius multiplied by 10.
// If no successful measurements for this device was performed, returns UninitializedDataError.
func (t *device) Temperature() (int16, error) {
if !t.initialized {
return 0, UninitializedDataError
}
return t.temperature, nil
}

// Getter for temperature. TemperatureFloat returns temperature in a given scale.
// Getter for temperature. The temperature is returned in milli degrees Celsius.
// If no successful measurements for this device was performed, returns UninitializedDataError.
func (t *device) TemperatureFloat(scale TemperatureScale) (float32, error) {
func (t *device) Temperature() (drivers.Temperature, error) {
if !t.initialized {
return 0, UninitializedDataError
}
return scale.convertToFloat(t.temperature), nil
return drivers.Temperature(t.temperature) * 100, nil
}

// Getter for humidity. Humidity returns humidity as it is sent by device.
Expand Down
17 changes: 4 additions & 13 deletions dht/timesafethermometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package dht // import "tinygo.org/x/drivers/dht"
import (
"machine"
"time"

"tinygo.org/x/drivers"
)

// Device interface provides main functionality of the DHTXX sensors.
Expand All @@ -35,10 +37,9 @@ func (m *managedDevice) Measurements() (temperature int16, humidity uint16, err
return m.t.Measurements()
}

// Getter for temperature. Temperature method returns temperature as it is sent by device.
// The temperature is measured temperature in Celsius multiplied by 10.
// Getter for temperature. The temperature is returned in milli degrees Celsius.
// Depending on the UpdatePolicy of the device may update cached measurements.
func (m *managedDevice) Temperature() (temp int16, err error) {
func (m *managedDevice) Temperature() (temp drivers.Temperature, err error) {
err = m.checkForUpdateOnDataRequest()
if err != nil {
return 0, err
Expand All @@ -64,16 +65,6 @@ func (m *managedDevice) checkForUpdateOnDataRequest() (err error) {
return err
}

// Getter for temperature. TemperatureFloat returns temperature in a given scale.
// Depending on the UpdatePolicy of the device may update cached measurements.
func (m *managedDevice) TemperatureFloat(scale TemperatureScale) (float32, error) {
err := m.checkForUpdateOnDataRequest()
if err != nil {
return 0, err
}
return m.t.TemperatureFloat(scale)
}

// Getter for humidity. Humidity returns humidity as it is sent by device.
// The humidity is measured in percentages multiplied by 10.
// Depending on the UpdatePolicy of the device may update cached measurements.
Expand Down
4 changes: 2 additions & 2 deletions ds3231/ds3231.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ func (d *Device) ReadTime() (dt time.Time, err error) {
}

// ReadTemperature returns the temperature in millicelsius (mC)
func (d *Device) ReadTemperature() (int32, error) {
func (d *Device) ReadTemperature() (drivers.Temperature, error) {
data := make([]uint8, 2)
err := d.bus.ReadRegister(uint8(d.Address), REG_TEMP, data)
if err != nil {
return 0, err
}
return int32(data[0])*1000 + int32((data[1]>>6)*25)*10, nil
return drivers.Temperature(int32(data[0])*1000 + int32((data[1]>>6)*25)*10), nil
}

// uint8ToBCD converts a byte to BCD for the DS3231
Expand Down
4 changes: 2 additions & 2 deletions examples/adt7410/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func main() {
sensor.Configure()

for {
temp := sensor.ReadTempF()
fmt.Printf("temperature: %f\r\n", temp)
temp, _ := sensor.ReadTemperature()
fmt.Printf("temperature: %f°C\r\n", temp.Celsius())
time.Sleep(time.Second)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/bme280/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

for {
temp, _ := sensor.ReadTemperature()
println("Temperature:", strconv.FormatFloat(float64(temp)/1000, 'f', 2, 64), "°C")
println("Temperature:", strconv.FormatFloat(float64(temp.Celsius()), 'f', 2, 64), "°C")
press, _ := sensor.ReadPressure()
println("Pressure:", strconv.FormatFloat(float64(press)/100000, 'f', 2, 64), "hPa")
hum, _ := sensor.ReadHumidity()
Expand Down
2 changes: 1 addition & 1 deletion examples/bmi160/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
println("Error reading temperature", err)
continue
}
fmt.Printf("Temperature: %.2f °C\n", float32(t)/1000)
fmt.Printf("Temperature: %.2f °C\n", t.Celsius())

accelX, accelY, accelZ, err := sensor.ReadAcceleration()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/bmp180/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

for {
temp, _ := sensor.ReadTemperature()
println("Temperature:", float32(temp)/1000, "°C")
println("Temperature:", temp.Celsius(), "°C")

pressure, _ := sensor.ReadPressure()
println("Pressure", float32(pressure)/100000, "hPa")
Expand Down
2 changes: 1 addition & 1 deletion examples/bmp280/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
println("Error reading temperature")
}
// Temperature in degrees Celsius
fmt.Printf("Temperature: %.2f °C\n", float32(t)/1000)
fmt.Printf("Temperature: %.2f °C\n", t.Celsius())

p, err := sensor.ReadPressure()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions examples/bmp388/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func main() {
}

for {
temp, err := sensor.ReadTemperature() // returns the temperature in centicelsius
temp, err := sensor.ReadTemperature() // returns the temperature in millicelsius
press, err := sensor.ReadPressure() // returns the pressure in centipascals

if err != nil {
println(err)
} else {
println("Temperature: " + strconv.FormatInt(int64(temp), 10) + " cC")
println("Temperature:", temp/1000, "C")
println("Pressure: " + strconv.FormatInt(int64(press), 10) + " cPa\n")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ds3231/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
fmt.Printf("Date: %d/%s/%02d %02d:%02d:%02d \r\n", dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second())
}
temp, _ := rtc.ReadTemperature()
fmt.Printf("Temperature: %.2f °C \r\n", float32(temp)/1000)
fmt.Printf("Temperature: %.2f °C \r\n", temp.Celsius())

time.Sleep(time.Second * 1)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/lsm6ds3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func main() {
println("Acceleration:", float32(x)/1000000, float32(y)/1000000, float32(z)/1000000)
x, y, z = accel.ReadRotation()
println("Gyroscope:", float32(x)/1000000, float32(y)/1000000, float32(z)/1000000)
x, _ = accel.ReadTemperature()
println("Degrees C", float32(x)/1000, "\n\n")
t, _ := accel.ReadTemperature()
println("Degrees C", t.Celsius(), "\n\n")
time.Sleep(time.Millisecond * 1000)
}
}
5 changes: 3 additions & 2 deletions examples/lsm6dsox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"machine"
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/lsm6dsox"
)

Expand Down Expand Up @@ -76,15 +77,15 @@ func calibrateGyro(device *lsm6dsox.Device) {
}

// Arduino IDE's Serial Plotter
func printPlotter(ax, ay, az, gx, gy, gz, t int32) {
func printPlotter(ax, ay, az, gx, gy, gz int32, t drivers.Temperature) {
if SHOW_ACCELERATION {
fmt.Printf("AX:%f, AY:%f, AZ:%f,", axis(ax, 0), axis(ay, 0), axis(az, 0))
}
if SHOW_ROTATION {
fmt.Printf("GX:%f, GY:%f, GZ:%f,", axis(gx, cal[0]), axis(gy, cal[1]), axis(gz, cal[2]))
}
if SHOW_TEMPERATURE {
fmt.Printf("T:%f", float32(t)/1000)
fmt.Printf("T:%f", t.Celsius())
}
println()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mag3110/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
println("Magnetic readings:", x, y, z)

c, _ := mag.ReadTemperature()
println("Temperature:", float32(c)/1000, "°C")
println("Temperature:", c.Celsius(), "°C")

time.Sleep(time.Millisecond * 100)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tmp102/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {

temp, _ := thermo.ReadTemperature()

print(fmt.Sprintf("%.2f°C\r\n", float32(temp)/1000.0))
print(fmt.Sprintf("%.2f°C\r\n", temp.Celsius()))

time.Sleep(time.Millisecond * 1000)
}
Expand Down
4 changes: 2 additions & 2 deletions lsm303agr/lsm303agr.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ func (d *Device) ReadCompass() (h int32) {
}

// ReadTemperature returns the temperature in Celsius milli degrees (°C/1000)
func (d *Device) ReadTemperature() (c int32, e error) {
func (d *Device) ReadTemperature() (c drivers.Temperature, e error) {

data1, data2 := []byte{0}, []byte{0}
d.bus.ReadRegister(uint8(d.AccelAddress), OUT_TEMP_H_A, data1)
d.bus.ReadRegister(uint8(d.AccelAddress), OUT_TEMP_L_A, data2)

t := int16((uint16(data1[0])<<8 | uint16(data2[0]))) >> 4 // temperature offsef from 25 °C
c = int32((float32(25) + float32(t)/8) * 1000)
c = drivers.Temperature(t)*125 + 25000
e = nil
return
}
Loading