mirror of https://github.com/kidoman/embd
NPA-700 sensor support - requires https://github.com/kidoman/embd/pull/37
This commit is contained in:
parent
7054cdbeea
commit
fef92bd936
|
@ -0,0 +1,35 @@
|
|||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"github.com/zlowred/embd"
|
||||
"github.com/zlowred/embd/sensor/npa700"
|
||||
|
||||
_ "github.com/zlowred/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if err := embd.InitI2C(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer embd.CloseI2C()
|
||||
|
||||
bus := embd.NewI2CBus(1)
|
||||
|
||||
sensor := npa700.New(bus)
|
||||
|
||||
err := sensor.Read()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Temp is %fC\n", sensor.Celsius())
|
||||
fmt.Printf("Temp is %fF\n", sensor.Fahrenheit())
|
||||
fmt.Printf("Pres is %fPa\n", sensor.Pascals(0, 1638, 14745, -6894.76, 6894.76))
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
// 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)
|
||||
}
|
Loading…
Reference in New Issue