2016-09-12 23:27:06 +02:00
|
|
|
|
// Package mcp9808 is a driver for the MCP9808 temperature sensor
|
|
|
|
|
// and all code is based off of the documentation found here:
|
|
|
|
|
// http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf
|
|
|
|
|
package mcp9808
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// default I2C address for device
|
|
|
|
|
address = 0x18
|
|
|
|
|
|
|
|
|
|
// Register addresses.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
regConfig = iota // starts at 1, this is what we want
|
|
|
|
|
regUpperTemp
|
|
|
|
|
regLowerTemp
|
|
|
|
|
regCriticalTemp
|
|
|
|
|
regAmbientTemp
|
|
|
|
|
regManufID
|
|
|
|
|
regDeviceID
|
|
|
|
|
regResolution
|
2016-09-12 23:27:06 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
configAlertMode uint16 = 1 << iota
|
|
|
|
|
configAlertPolarity
|
|
|
|
|
configAlertSelect
|
|
|
|
|
configAlertControl
|
|
|
|
|
configAlertStatus
|
|
|
|
|
configInterruptClear
|
|
|
|
|
configWindowTempLock
|
|
|
|
|
configCriticalTempLock
|
|
|
|
|
configShutDown
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MCP9808 represents a MCP9808 temperature sensor.
|
|
|
|
|
type MCP9808 struct {
|
2016-09-21 19:49:09 +02:00
|
|
|
|
Bus embd.I2CBus
|
|
|
|
|
cmu sync.Mutex
|
2016-09-12 23:27:06 +02:00
|
|
|
|
config uint16
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New returns a handle to a MCP9808 sensor.
|
|
|
|
|
func New(bus embd.I2CBus) *MCP9808 {
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return &MCP9808{Bus: bus}
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ManufacturerID reads the device manufacturer ID
|
|
|
|
|
func (d *MCP9808) ManufacturerID() (uint16, error) {
|
|
|
|
|
d.cmu.Lock()
|
|
|
|
|
defer d.cmu.Unlock()
|
|
|
|
|
|
|
|
|
|
return d.Bus.ReadWordFromReg(address, regManufID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeviceID reads the device ID and revision
|
|
|
|
|
func (d *MCP9808) DeviceID() (uint8, uint8, error) {
|
|
|
|
|
d.cmu.Lock()
|
|
|
|
|
defer d.cmu.Unlock()
|
|
|
|
|
|
|
|
|
|
devIDRev, err := d.Bus.ReadWordFromReg(address, regDeviceID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uint8(devIDRev >> 8), uint8(devIDRev & 0xFF), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Config returns the current word value of the sensor config struct and whether
|
|
|
|
|
// that value reflects what is set on the sensor
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) Config() (uint16, error) {
|
|
|
|
|
return d.readConfig()
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadConfig reads the config word from the device and writes it to the config attribute
|
|
|
|
|
// this overwrites any changes that may have been made to the config attribute
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) readConfig() (uint16, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
config, err := d.Bus.ReadWordFromReg(address, regConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
d.config = config
|
|
|
|
|
return d.config, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteConfig writes the sensor's config word to the device and returns the resulting config
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) writeConfig() error {
|
|
|
|
|
return d.Bus.WriteWordToReg(address, regConfig, d.config)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// flipConfig bit sets (1, set = true) or clears (0, set = false) a bit within the config word
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) flipConfigBit(val uint16, set bool) error {
|
|
|
|
|
d.cmu.Lock()
|
|
|
|
|
defer d.cmu.Unlock()
|
2016-09-12 23:27:06 +02:00
|
|
|
|
if set {
|
|
|
|
|
d.config |= val
|
|
|
|
|
} else {
|
|
|
|
|
d.config &= ^val
|
|
|
|
|
}
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return d.writeConfig()
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) readConfigValue(val uint16) (bool, error) {
|
|
|
|
|
_, err := d.readConfig()
|
|
|
|
|
return !(d.config&(1<<val) == 0), err
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hysteresis applies for decreasing temperature only (hot to cold) or as temperature
|
|
|
|
|
// drifts below the specified limit.
|
|
|
|
|
type Hysteresis uint8
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// Zero hysteresis represents 0 degrees Celsius of hysteresis compensation
|
|
|
|
|
Zero Hysteresis = iota
|
|
|
|
|
// Plus1pt5 hysteresis represents +1.5 degrees Celsius of hysteresis compensation
|
|
|
|
|
Plus1pt5
|
|
|
|
|
// Plus3pt0 hysteresis represents +3.0 degrees Celsius of hysteresis compensation
|
|
|
|
|
Plus3pt0
|
|
|
|
|
// Plus6pt0 hysteresis represents +6.0 degrees Celsius of hysteresis compensation
|
|
|
|
|
Plus6pt0
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TempHysteresis - TUPPER and TLOWER Limit Hysteresis bits
|
|
|
|
|
// 00 = 0°C (power-up default)
|
|
|
|
|
// 01 = +1.5°C
|
|
|
|
|
// 10 = +3.0°C
|
|
|
|
|
// 11 = +6.0°C
|
|
|
|
|
// The hysteresis applies for decreasing temperature only (hot to cold) or as temperature
|
|
|
|
|
// drifts below the specified limit.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) TempHysteresis() (Hysteresis, error) {
|
|
|
|
|
_, err := d.readConfig()
|
|
|
|
|
return Hysteresis(d.config >> 9), err
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetTempHysteresis - TUPPER and TLOWER Limit Hysteresis bits
|
|
|
|
|
// 00 = 0°C (power-up default)
|
|
|
|
|
// 01 = +1.5°C
|
|
|
|
|
// 10 = +3.0°C
|
|
|
|
|
// 11 = +6.0°C
|
|
|
|
|
// The hysteresis applies for decreasing temperature only (hot to cold) or as temperature
|
|
|
|
|
// drifts below the specified limit.
|
|
|
|
|
// This bit can not be altered when either of the Lock bits are set (bit 6 and bit 7).
|
|
|
|
|
// Thi s bit can be programmed in Shutdown mode.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetTempHysteresis(val Hysteresis) error {
|
|
|
|
|
d.cmu.Lock()
|
|
|
|
|
defer d.cmu.Unlock()
|
|
|
|
|
|
2016-09-12 23:27:06 +02:00
|
|
|
|
d.config = d.config - d.config&^(d.config>>9) + uint16(val)<<9
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return d.writeConfig()
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ShutdownMode bit
|
|
|
|
|
// 0 (false) = Continuous conversion (power-up default)
|
|
|
|
|
// 1 (true) = Shutdown (Low-Power mode)
|
|
|
|
|
// In shutdown, all power-consuming activities are disabled, though all registers can be written to or read.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) ShutdownMode() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configShutDown)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetShutdownMode bit
|
|
|
|
|
// 0 (false) = Continuous conversion (power-up default)
|
|
|
|
|
// 1 (true) = Shutdown (Low-Power mode)
|
|
|
|
|
// In shutdown, all power-consuming activities are disabled, though all registers can be written to or read.
|
|
|
|
|
// This bit cannot be set to ‘1’ when either of the Lock bits is set (bit 6 and bit 7). However, it can be
|
|
|
|
|
// cleared to ‘0’ for continuous conversion while locked
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetShutdownMode(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configShutDown, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CriticalTempLock - TCRIT Lock bit
|
|
|
|
|
// 0 (false) = Unlocked. TCRIT register can be written (power-up default)
|
|
|
|
|
// 1 (true) = Locked. TCRIT register can not be written
|
|
|
|
|
// When enabled, this bit remains set to ‘1’ or locked until cleared by an internal Reset
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) CriticalTempLock() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configCriticalTempLock)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetCriticalTempLock - TCRIT Lock bit
|
|
|
|
|
// 0 (false) = Unlocked. TCRIT register can be written (power-up default)
|
|
|
|
|
// 1 (true) = Locked. TCRIT register can not be written
|
|
|
|
|
// When enabled, this bit remains set to ‘1’ or locked until cleared by an internal Reset
|
|
|
|
|
// This bit can be programmed in Shutdown mode.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) setCriticalTempLock(locked bool) error {
|
|
|
|
|
return d.flipConfigBit(configCriticalTempLock, locked)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WindowTempLock - TUPPER and TLOWER Window Lock bit
|
|
|
|
|
// 0 (false) = Unlocked; TUPPER and TLOWER registers can be written (power-up default)
|
|
|
|
|
// 1 (true) = Locked; TUPPER and TLOWER registers can not be written
|
|
|
|
|
// When enabled, this bit remains set to ‘1’ or locked until cleared by a Power-on Reset
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) WindowTempLock() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configWindowTempLock)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
// setWindowTempLock - TUPPER and TLOWER Window Lock bit
|
2016-09-12 23:27:06 +02:00
|
|
|
|
// 0 (false) = Unlocked; TUPPER and TLOWER registers can be written (power-up default)
|
|
|
|
|
// 1 (true) = Locked; TUPPER and TLOWER registers can not be written
|
|
|
|
|
// When enabled, this bit remains set to ‘1’ or locked until cleared by a Power-on Reset
|
|
|
|
|
// This bit can be programmed in Shutdown mode.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) setWindowTempLock(locked bool) error {
|
|
|
|
|
return d.flipConfigBit(configWindowTempLock, locked)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InterruptClear - Interrupt Clear bit
|
|
|
|
|
// 0 (false) = No effect (power-up default)
|
|
|
|
|
// 1 (true) = Clear interrupt output; when read, this bit returns to ‘0’
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) InterruptClear() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configInterruptClear)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetInterruptClear - Interrupt Clear bit
|
|
|
|
|
// 0 (false) = No effect (power-up default)
|
|
|
|
|
// 1 (true) = Clear interrupt output; when read, this bit returns to ‘0’
|
|
|
|
|
// This bit can not be set to ‘1’ in Shutdown mode, but it can be cleared after the device enters Shutdown
|
|
|
|
|
// mode.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetInterruptClear(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configInterruptClear, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AlertStatus Alert Output Status bit
|
|
|
|
|
// 0 (false) = Alert output is not asserted by the device (power-up default)
|
|
|
|
|
// 1 (true) = Alert output is asserted as a comparator/Interrupt or critical temperature output
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) AlertStatus() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configAlertStatus)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAlertStatus Alert Output Status bit
|
|
|
|
|
// 0 (false) = Alert output is not asserted by the device (power-up default)
|
|
|
|
|
// 1 (true) = Alert output is asserted as a comparator/Interrupt or critical temperature output
|
|
|
|
|
// This bit can not be set to ‘1’ or cleared to ‘0’ in Shutdown mode. However, if the Alert output is configured
|
|
|
|
|
// as Interrupt mode, and if the host controller clears to ‘0’, the interrupt, using bit 5 while the device
|
|
|
|
|
// is in Shutdown mode, then this bit will also be cleared ‘0’.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetAlertStatus(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configAlertStatus, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AlertControl - Alert Output Control bit
|
|
|
|
|
// 0 (false) = Disabled (power-up default)
|
|
|
|
|
// 1 (true) = Enabled
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) AlertControl() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configAlertControl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAlertControl - Alert Output Control bit
|
|
|
|
|
// 0 (false) = Disabled (power-up default)
|
|
|
|
|
// 1 (true) = Enabled
|
|
|
|
|
// This bit can not be altered when either of the Lock bits are set (bit 6 and bit 7).
|
|
|
|
|
// This bit can be programmed in Shutdown mode, but the Alert output will not assert or deassert.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetAlertControl(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configAlertControl, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AlertSelect - Alert Output Select bit
|
|
|
|
|
// 0 (false) = Alert output for TUPPER, TLOWER and TCRIT (power-up default)
|
|
|
|
|
// 1 (true) = TA > TCRIT only (TUPPER and TLOWER temperature boundaries are disabled)
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) AlertSelect() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configAlertSelect)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAlertSelect - Alert Output Select bit
|
|
|
|
|
// 0 (false) = Alert output for TUPPER, TLOWER and TCRIT (power-up default)
|
|
|
|
|
// 1 (true) = TA > TCRIT only (TUPPER and TLOWER temperature boundaries are disabled)
|
|
|
|
|
// When the Alarm Window Lock bit is set, this bit cannot be altered until unlocked (bit 6).
|
|
|
|
|
// This bit can be programmed in Shutdown mode, but the Alert output will not assert or deassert.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetAlertSelect(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configAlertSelect, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AlertPolarity - Alert Output Polarity bit
|
|
|
|
|
// 0 (false) = Active-low (power-up default; pull-up resistor required)
|
|
|
|
|
// 1 (true) = Active-high
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) AlertPolarity() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configAlertPolarity)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAlertPolarity - Alert Output Polarity bit
|
|
|
|
|
// 0 (false) = Active-low (power-up default; pull-up resistor required)
|
|
|
|
|
// 1 (true) = Active-high
|
|
|
|
|
// This bit cannot be altered when either of the Lock bits are set (bit 6 and bit 7).
|
|
|
|
|
// This bit can be programmed in Shutdown mode, but the Alert output will not assert or deassert.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetAlertPolarity(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configAlertPolarity, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AlertMode - Alert Output Mode bit
|
|
|
|
|
// 0 (false) = Comparator output (power-up default)
|
|
|
|
|
// 1 (true) = Interrupt output
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) AlertMode() (bool, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readConfigValue(configAlertMode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetAlertMode - Alert Output Mode bit
|
|
|
|
|
// 0 (false) = Comparator output (power-up default)
|
|
|
|
|
// 1 (true) = Interrupt output
|
|
|
|
|
// This bit cannot be altered when either of the Lock bits are set (bit 6 and bit 7).
|
|
|
|
|
// This bit can be programmed in Shutdown mode, but the Alert output will not assert or deassert.
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func (d *MCP9808) SetAlertMode(set bool) error {
|
|
|
|
|
return d.flipConfigBit(configAlertMode, set)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Temperature contains the ambient temperature along with alert values.
|
|
|
|
|
type Temperature struct {
|
|
|
|
|
CelsiusDeg float64
|
|
|
|
|
AboveCritical, AboveUpper, BelowLower bool
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
// readTempC reads from the reg temperature register and returns the current temperature value in celsius
|
2016-09-12 23:27:06 +02:00
|
|
|
|
func (d *MCP9808) readTempC(reg byte) (float64, error) {
|
|
|
|
|
temp, err := d.Bus.ReadWordFromReg(address, reg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return convertWordToTempC(temp), nil
|
|
|
|
|
}
|
2016-09-12 23:27:06 +02:00
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
func convertWordToTempC(temp uint16) float64 {
|
2016-09-26 06:01:22 +02:00
|
|
|
|
return float64(int16(temp<<3)>>3) / 16
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *MCP9808) setTemp(reg byte, newTemp float64) error {
|
2016-09-26 06:06:41 +02:00
|
|
|
|
return d.Bus.WriteWordToReg(address, reg, uint16(newTemp*16)&0x1fff)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
// AmbientTemp reads the current sensor value along with the flags denoting what boundaries the
|
|
|
|
|
// current temperature exceeds.
|
|
|
|
|
func (d *MCP9808) AmbientTemp() (*Temperature, error) {
|
|
|
|
|
temp, err := d.Bus.ReadWordFromReg(address, regAmbientTemp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tempResult := &Temperature{
|
2016-09-26 06:01:22 +02:00
|
|
|
|
AboveCritical: temp&0x8000 == 0x8000,
|
|
|
|
|
AboveUpper: temp&0x4000 == 0x4000,
|
|
|
|
|
BelowLower: temp&0x2000 == 0x2000}
|
2016-09-21 19:49:09 +02:00
|
|
|
|
tempResult.CelsiusDeg = convertWordToTempC(temp)
|
|
|
|
|
|
|
|
|
|
return tempResult, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-26 06:01:22 +02:00
|
|
|
|
// CriticalTemp reads the current temperature set in the critical temperature register.
|
|
|
|
|
func (d *MCP9808) CriticalTemp() (float64, error) {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return d.readTempC(regCriticalTemp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetCriticalTemp when the temperature goes above the set value the alert will be
|
|
|
|
|
// triggered if enabled.
|
|
|
|
|
func (d *MCP9808) SetCriticalTemp(newTemp float64) error {
|
2016-09-21 19:49:09 +02:00
|
|
|
|
if err := d.setCriticalTempLock(false); err != nil {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := d.setTemp(regCriticalTemp, newTemp); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return d.setCriticalTempLock(true)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WindowTempUpper reads the current temperature set in the upper window temperature register.
|
|
|
|
|
func (d *MCP9808) WindowTempUpper() (float64, error) {
|
|
|
|
|
return d.readTempC(regUpperTemp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetWindowTempUpper when the temperature goes above the set value the alert will be
|
|
|
|
|
// triggered if enabled.
|
|
|
|
|
func (d *MCP9808) SetWindowTempUpper(newTemp float64) error {
|
2016-09-21 19:49:09 +02:00
|
|
|
|
if err := d.setWindowTempLock(false); err != nil {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := d.setTemp(regUpperTemp, newTemp); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return d.setWindowTempLock(true)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WindowTempLower reads the current temperature set in the lower window temperature register.
|
|
|
|
|
func (d *MCP9808) WindowTempLower() (float64, error) {
|
|
|
|
|
return d.readTempC(regLowerTemp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetWindowTempLower when the temperature goes below the set value the alert will be
|
|
|
|
|
// triggered if enabled.
|
|
|
|
|
func (d *MCP9808) SetWindowTempLower(newTemp float64) error {
|
2016-09-21 19:49:09 +02:00
|
|
|
|
if err := d.setWindowTempLock(false); err != nil {
|
2016-09-12 23:27:06 +02:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := d.setTemp(regLowerTemp, newTemp); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 19:49:09 +02:00
|
|
|
|
return d.setWindowTempLock(true)
|
2016-09-12 23:27:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TempResolution reads the current temperature accuracy from the sensor (affects temperature read speed)
|
|
|
|
|
// 0 - +/- .5 degrees C (~30ms)
|
|
|
|
|
// 1 - +/- .25 degrees C (~65ms)
|
|
|
|
|
// 2 - +/- .125 degrees C (~130ms)
|
|
|
|
|
// 3 (default) - +/- .0625 degrees C (~250ms)
|
|
|
|
|
type TempResolution uint8
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
HalfC TempResolution = iota
|
|
|
|
|
QuarterC
|
|
|
|
|
Eighth
|
|
|
|
|
Sixteenth
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TempResolution reads the temperature resolution from the sensor.
|
|
|
|
|
func (d *MCP9808) TempResolution() (TempResolution, error) {
|
|
|
|
|
d.cmu.Lock()
|
|
|
|
|
defer d.cmu.Unlock()
|
|
|
|
|
|
|
|
|
|
res, err := d.Bus.ReadByteFromReg(address, regResolution)
|
|
|
|
|
return TempResolution(res), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetTempResolution writes a new temperature resolution to the sensor
|
|
|
|
|
func (d *MCP9808) SetTempResolution(res TempResolution) error {
|
|
|
|
|
d.cmu.Lock()
|
|
|
|
|
defer d.cmu.Unlock()
|
|
|
|
|
|
|
|
|
|
return d.Bus.WriteByteToReg(address, regResolution, byte(res))
|
|
|
|
|
}
|