From 606555ca7fde729cd24d20a80aa2a7477211069b Mon Sep 17 00:00:00 2001 From: Adam Bright Date: Mon, 26 Sep 2016 04:01:22 +0000 Subject: [PATCH] simplify temp set and read functions --- sensor/mcp9808/mcp9808.go | 48 ++++++--------------------------------- 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/sensor/mcp9808/mcp9808.go b/sensor/mcp9808/mcp9808.go index 9bb5725..fc0b2f0 100644 --- a/sensor/mcp9808/mcp9808.go +++ b/sensor/mcp9808/mcp9808.go @@ -4,7 +4,6 @@ package mcp9808 import ( - "math" "sync" "github.com/kidoman/embd" @@ -319,44 +318,11 @@ func (d *MCP9808) readTempC(reg byte) (float64, error) { } func convertWordToTempC(temp uint16) float64 { - wholeNum := float64(temp&0xFF0) / 16.0 - fraction := float64(temp&0xF) * .0625 - - tempRead := wholeNum + fraction - - // read sign bit - if temp>>12 == 1 { - tempRead *= -1 - } - return tempRead + return float64(int16(temp<<3)>>3) / 16 } func (d *MCP9808) setTemp(reg byte, newTemp float64) error { - d.cmu.Lock() - defer d.cmu.Unlock() - - var signBit uint16 - if newTemp < 0 { - newTemp *= -1 - signBit = 0x1000 - } - - wholeNum, fraction := math.Modf(newTemp) - var roundedFrac uint16 - switch { - case fraction < .125: - roundedFrac = 0 - case fraction < .375: - roundedFrac = 1 - case fraction < .625: - roundedFrac = 2 - case fraction < .875: - roundedFrac = 3 - default: - roundedFrac = 4 - } - newTempWord := signBit + uint16(wholeNum)*16 + roundedFrac*4 - return d.Bus.WriteWordToReg(address, reg, newTempWord) + return d.Bus.WriteWordToReg(address, reg, uint16((newTemp)*16)&0x1fff) } // AmbientTemp reads the current sensor value along with the flags denoting what boundaries the @@ -368,16 +334,16 @@ func (d *MCP9808) AmbientTemp() (*Temperature, error) { } tempResult := &Temperature{ - AboveCritical: !(temp&(1<<15) == 0), - AboveUpper: !(temp&(1<<14) == 0), - BelowLower: !(temp&(1<<13) == 0)} + AboveCritical: temp&0x8000 == 0x8000, + AboveUpper: temp&0x4000 == 0x4000, + BelowLower: temp&0x2000 == 0x2000} tempResult.CelsiusDeg = convertWordToTempC(temp) return tempResult, nil } -// CriticalTempUpper reads the current temperature set in the critical temperature register. -func (d *MCP9808) CriticalTempUpper() (float64, error) { +// CriticalTemp reads the current temperature set in the critical temperature register. +func (d *MCP9808) CriticalTemp() (float64, error) { return d.readTempC(regCriticalTemp) }