mirror of https://github.com/kidoman/embd
simplify temp set and read functions
This commit is contained in:
parent
afc8c08cff
commit
606555ca7f
|
@ -4,7 +4,6 @@
|
||||||
package mcp9808
|
package mcp9808
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/kidoman/embd"
|
"github.com/kidoman/embd"
|
||||||
|
@ -319,44 +318,11 @@ func (d *MCP9808) readTempC(reg byte) (float64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertWordToTempC(temp uint16) float64 {
|
func convertWordToTempC(temp uint16) float64 {
|
||||||
wholeNum := float64(temp&0xFF0) / 16.0
|
return float64(int16(temp<<3)>>3) / 16
|
||||||
fraction := float64(temp&0xF) * .0625
|
|
||||||
|
|
||||||
tempRead := wholeNum + fraction
|
|
||||||
|
|
||||||
// read sign bit
|
|
||||||
if temp>>12 == 1 {
|
|
||||||
tempRead *= -1
|
|
||||||
}
|
|
||||||
return tempRead
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *MCP9808) setTemp(reg byte, newTemp float64) error {
|
func (d *MCP9808) setTemp(reg byte, newTemp float64) error {
|
||||||
d.cmu.Lock()
|
return d.Bus.WriteWordToReg(address, reg, uint16((newTemp)*16)&0x1fff)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AmbientTemp reads the current sensor value along with the flags denoting what boundaries the
|
// 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{
|
tempResult := &Temperature{
|
||||||
AboveCritical: !(temp&(1<<15) == 0),
|
AboveCritical: temp&0x8000 == 0x8000,
|
||||||
AboveUpper: !(temp&(1<<14) == 0),
|
AboveUpper: temp&0x4000 == 0x4000,
|
||||||
BelowLower: !(temp&(1<<13) == 0)}
|
BelowLower: temp&0x2000 == 0x2000}
|
||||||
tempResult.CelsiusDeg = convertWordToTempC(temp)
|
tempResult.CelsiusDeg = convertWordToTempC(temp)
|
||||||
|
|
||||||
return tempResult, nil
|
return tempResult, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CriticalTempUpper reads the current temperature set in the critical temperature register.
|
// CriticalTemp reads the current temperature set in the critical temperature register.
|
||||||
func (d *MCP9808) CriticalTempUpper() (float64, error) {
|
func (d *MCP9808) CriticalTemp() (float64, error) {
|
||||||
return d.readTempC(regCriticalTemp)
|
return d.readTempC(regCriticalTemp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue