Skip to content

Commit

Permalink
i2c: bmp280/bme280 raw reading in place, just need the conversions
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Mar 31, 2017
1 parent c9b992a commit f523fc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
8 changes: 4 additions & 4 deletions drivers/i2c/bme280_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,21 @@ func (d *BME280Driver) initHumidity() (err error) {
return nil
}

func (d *BME280Driver) rawHumidity() (int16, error) {
func (d *BME280Driver) rawHumidity() (int32, error) {
ret, err := d.read(bme280RegisterHumidityMSB, 2)
if err != nil {
return 0, err
}
buf := bytes.NewBuffer(ret)
var rawH int16
var rawH int32
binary.Read(buf, binary.BigEndian, &rawH)
return rawH, nil
}

// Adapted from https://github.com/BoschSensortec/BME280_driver/blob/master/bme280.c
// function bme280_compensate_humidity_double(s32 v_uncom_humidity_s32)
func (d *BME280Driver) calculateHumidity(rawH int16) float32 {
var rawT int16
func (d *BME280Driver) calculateHumidity(rawH int32) float32 {
var rawT int32
var err error
var h float32
var fine int32
Expand Down
47 changes: 32 additions & 15 deletions drivers/i2c/bmp280_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

const bmp280RegisterCalib00 = 0x88
const bme280RegisterPressureMSB = 0xf7

type bmp280CalibrationCoefficients struct {
t1 uint16
Expand Down Expand Up @@ -82,16 +83,10 @@ func (d *BMP280Driver) Start() (err error) {
return err
}

// TODO: set sleep mode here...

if err := d.initialization(); err != nil {
return err
}

// TODO: set usage mode here...

// TODO: set default sea level here

return nil
}

Expand All @@ -102,14 +97,20 @@ func (d *BMP280Driver) Halt() (err error) {

// Temperature returns the current temperature, in celsius degrees.
func (d *BMP280Driver) Temperature() (temp float32, err error) {
// TODO: implement this
return 0, nil
var rawT int32
if rawT, _, err = d.rawTempPress(); err != nil {
return 0.0, err
}
return d.calculateTemp(rawT), nil
}

// Pressure returns the current barometric pressure, in Pa
func (d *BMP280Driver) Pressure() (press float32, err error) {
// TODO: implement this
return 0, nil
var rawP int32
if _, rawP, err = d.rawTempPress(); err != nil {
return 0.0, err
}
return d.calculatePress(rawP), nil
}

// initialization reads the calibration coefficients.
Expand Down Expand Up @@ -140,18 +141,34 @@ func (d *BMP280Driver) initialization() (err error) {
return nil
}

// TODO: implement
func (d *BMP280Driver) rawTempPress() (temp int16, press int16, err error) {
return 0, 0, nil
func (d *BMP280Driver) rawTempPress() (temp int32, press int32, err error) {
var data []byte
var tp0, tp1, tp2, tp3, tp4, tp5 byte

if data, err = d.read(bme280RegisterPressureMSB, 6); err != nil {
return 0, 0, err
}
buf := bytes.NewBuffer(data)
binary.Read(buf, binary.LittleEndian, &tp0)
binary.Read(buf, binary.LittleEndian, &tp1)
binary.Read(buf, binary.LittleEndian, &tp2)
binary.Read(buf, binary.LittleEndian, &tp3)
binary.Read(buf, binary.LittleEndian, &tp4)
binary.Read(buf, binary.LittleEndian, &tp5)

temp = ((int32(tp5) >> 4) | (int32(tp4) << 4) | (int32(tp3) << 12))
press = ((int32(tp2) >> 4) | (int32(tp1) << 4) | (int32(tp0) << 12))

return
}

// TODO: implement
func (d *BMP280Driver) calculateTemp(rawTemp int16) float32 {
func (d *BMP280Driver) calculateTemp(rawTemp int32) float32 {
return 0
}

// TODO: implement
func (d *BMP280Driver) calculatePress(rawPress int16) float32 {
func (d *BMP280Driver) calculatePress(rawPress int32) float32 {
return 0
}

Expand Down
3 changes: 3 additions & 0 deletions examples/firmata_bme280.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func main() {
//fmt.Println("Pressure", mpl115a2.Pressure())
t, _ := bme280.Temperature()
fmt.Println("Temperature", t)

p, _ := bme280.Pressure()
fmt.Println("Pressure", p)
})
}

Expand Down

0 comments on commit f523fc6

Please sign in to comment.