mirror of
https://github.com/kidoman/embd
synced 2024-06-06 11:07:48 +02:00
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// Package npa700 allows interfacing with GE NPA-700 pressure sensor. This sensor
|
|
// has the ability to provide compensated temperature and pressure readings.
|
|
package npa700
|
|
|
|
import (
|
|
"github.com/zlowred/embd"
|
|
"sync"
|
|
)
|
|
|
|
// NPA700 represents a Bosch BMP180 barometric sensor.
|
|
type NPA700 struct {
|
|
Bus embd.I2CBus
|
|
|
|
RawTemperature int16
|
|
RawPressure int16
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// New returns a handle to a BMP180 sensor.
|
|
func New(bus embd.I2CBus) *NPA700 {
|
|
return &NPA700{Bus: bus}
|
|
}
|
|
|
|
func (sensor *NPA700) Read() error {
|
|
sensor.mu.Lock()
|
|
defer sensor.mu.Unlock()
|
|
|
|
data, err := sensor.Bus.ReadBytes(0x28, 4)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sensor.RawPressure = (int16(data[0]) << 8) + int16(data[1])
|
|
sensor.RawTemperature = (int16(data[2]) << 3) + (int16(data[3]) >> 5)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sensor *NPA700) Celsius() float32 {
|
|
return float32(sensor.RawTemperature) * 200. / 2048. - 50.
|
|
}
|
|
|
|
func (sensor *NPA700) Fahrenheit() float32 {
|
|
return (float32(sensor.RawTemperature) * 200. / 2048. - 50.) * 1.8 + 32.
|
|
}
|
|
|
|
func (sensor *NPA700) Pascals(offset float32, minValue float32, maxValue float32, minPressure float32, maxPressure float32) float32 {
|
|
return minPressure + (float32(sensor.RawPressure) + offset - minValue) / (maxValue - minValue) * (maxPressure - minPressure)
|
|
}
|