| 1 |
//-------------------------------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (c) 2018 Denis Dyakov |
| 4 |
// Portions Copyright (c) 2019 Iron Heart Consulting, LLC |
| 5 |
// |
| 6 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 7 |
// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 8 |
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 10 |
// subject to the following conditions: |
| 11 |
// |
| 12 |
// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 13 |
// portions of the Software. |
| 14 |
// |
| 15 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 16 |
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 |
// |
| 21 |
//-------------------------------------------------------------------------------------------------- |
| 22 |
|
| 23 |
package bsbmp |
| 24 |
|
| 25 |
import ( |
| 26 |
"bytes" |
| 27 |
"encoding/binary" |
| 28 |
"errors" |
| 29 |
"fmt" |
| 30 |
|
| 31 |
i2c "github.com/d2r2/go-i2c" |
| 32 |
) |
| 33 |
|
| 34 |
// BMP388 sensors memory map |
| 35 |
const ( |
| 36 |
// BMP388 general registers |
| 37 |
BMP388_ID_REG = 0x00 |
| 38 |
BMP388_STATUS_REG = 0x03 |
| 39 |
BMP388_ERR_REG = 0x02 |
| 40 |
// BMP388_CNTR_MEAS_REG = 0xF4 // No such reg in BMP388 |
| 41 |
BMP388_ODR_REG = 0x1D // Data Rate control |
| 42 |
BMP388_OSR_REG = 0x1D // Over sample rate control |
| 43 |
BMP388_PWR_CTRL_REG = 0x1B // enable/disable press or temp, set operating mode |
| 44 |
// CONFIG Register is used to set IIR Filter coefficent |
| 45 |
BMP388_CONFIG = 0x1F // TODO: support IIR filter settings |
| 46 |
// BMP388_RESET = 0xE0 // TODO: '388 doesn't have a reset register |
| 47 |
BMP388_CMD_REG = 0x7E |
| 48 |
// cmds - nop, extmode, clear FIFO, softreset |
| 49 |
// BMP388 specific compensation register's block |
| 50 |
BMP388_COEF_START = 0x31 |
| 51 |
BMP388_COEF_BYTES = 21 |
| 52 |
// BMP388 specific 3-byte reading out temprature and preassure |
| 53 |
BMP388_PRES_OUT_MSB_LSB_XLSB = 0x04 |
| 54 |
BMP388_TEMP_OUT_MSB_LSB_XLSB = 0x07 |
| 55 |
|
| 56 |
BMP388_PWR_MODE_SLEEP = 0 |
| 57 |
BMP388_PWR_MODE_FORCED = 1 |
| 58 |
BMP388_PWR_MODE_NORMAL = 3 |
| 59 |
|
| 60 |
// IIR Filter coefficent |
| 61 |
BMP388_coef_0 = 0 // bypass-mode |
| 62 |
BMP388_coef_1 = 0 |
| 63 |
BMP388_coef_3 = 0 |
| 64 |
BMP388_coef_7 = 0 |
| 65 |
BMP388_coef_15 = 0 |
| 66 |
BMP388_coef_31 = 0 |
| 67 |
BMP388_coef_63 = 0 |
| 68 |
BMP388_coef_127 = 0 |
| 69 |
) |
| 70 |
|
| 71 |
// Unique BMP388 calibration coefficients |
| 72 |
type CoeffBMP388 struct { |
| 73 |
// Registers storing unique calibration coefficients |
| 74 |
COEF_31 uint8 |
| 75 |
COEF_32 uint8 |
| 76 |
COEF_33 uint8 |
| 77 |
COEF_34 uint8 |
| 78 |
COEF_35 uint8 |
| 79 |
COEF_36 uint8 |
| 80 |
COEF_37 uint8 |
| 81 |
COEF_38 uint8 |
| 82 |
COEF_39 uint8 |
| 83 |
COEF_3A uint8 |
| 84 |
COEF_3B uint8 |
| 85 |
COEF_3C uint8 |
| 86 |
COEF_3D uint8 |
| 87 |
COEF_3E uint8 |
| 88 |
COEF_3F uint8 |
| 89 |
COEF_40 uint8 |
| 90 |
COEF_41 uint8 |
| 91 |
COEF_42 uint8 |
| 92 |
COEF_43 uint8 |
| 93 |
COEF_44 uint8 |
| 94 |
COEF_45 uint8 |
| 95 |
} |
| 96 |
|
| 97 |
func (v *CoeffBMP388) PAR_T1() uint16 { |
| 98 |
return uint16(v.COEF_32)<<8 | uint16(v.COEF_31) |
| 99 |
} |
| 100 |
|
| 101 |
func (v *CoeffBMP388) PAR_T2() uint16 { |
| 102 |
return uint16(uint16(v.COEF_34)<<8 | uint16(v.COEF_33)) |
| 103 |
} |
| 104 |
|
| 105 |
func (v *CoeffBMP388) PAR_T3() int8 { |
| 106 |
return int8(v.COEF_35) |
| 107 |
} |
| 108 |
|
| 109 |
func (v *CoeffBMP388) PAR_P1() int16 { |
| 110 |
return int16(uint16(v.COEF_37)<<8 | uint16(v.COEF_36)) |
| 111 |
} |
| 112 |
|
| 113 |
func (v *CoeffBMP388) PAR_P2() int16 { |
| 114 |
return int16(uint16(v.COEF_39)<<8 | uint16(v.COEF_38)) |
| 115 |
} |
| 116 |
|
| 117 |
func (v *CoeffBMP388) PAR_P3() int8 { |
| 118 |
return int8(uint16(v.COEF_3A)) |
| 119 |
} |
| 120 |
|
| 121 |
func (v *CoeffBMP388) PAR_P4() int8 { |
| 122 |
return int8(uint16(v.COEF_3B)) |
| 123 |
} |
| 124 |
|
| 125 |
func (v *CoeffBMP388) PAR_P5() uint16 { |
| 126 |
return uint16(uint16(v.COEF_3D)<<8 | uint16(v.COEF_3C)) |
| 127 |
} |
| 128 |
|
| 129 |
func (v *CoeffBMP388) PAR_P6() uint16 { |
| 130 |
return uint16(uint16(v.COEF_3F)<<8 | uint16(v.COEF_3E)) |
| 131 |
} |
| 132 |
|
| 133 |
func (v *CoeffBMP388) PAR_P7() int8 { |
| 134 |
return int8(uint16(v.COEF_40)) |
| 135 |
} |
| 136 |
|
| 137 |
func (v *CoeffBMP388) PAR_P8() int8 { |
| 138 |
return int8(uint16(v.COEF_41)) |
| 139 |
} |
| 140 |
|
| 141 |
func (v *CoeffBMP388) PAR_P9() int16 { |
| 142 |
return int16(uint16(v.COEF_43)<<8 | uint16(v.COEF_42)) |
| 143 |
} |
| 144 |
|
| 145 |
func (v *CoeffBMP388) PAR_P10() int8 { |
| 146 |
return int8(uint16(v.COEF_44)) |
| 147 |
} |
| 148 |
|
| 149 |
func (v *CoeffBMP388) PAR_P11() int8 { |
| 150 |
return int8(uint16(v.COEF_45)) |
| 151 |
} |
| 152 |
|
| 153 |
// SensorBMP388 specific type |
| 154 |
type SensorBMP388 struct { |
| 155 |
Coeff *CoeffBMP388 |
| 156 |
} |
| 157 |
|
| 158 |
// Static cast to verify at compile time |
| 159 |
// that type implement interface. |
| 160 |
var _ SensorInterface = &SensorBMP388{} |
| 161 |
|
| 162 |
// ReadSensorID reads sensor signature. It may be used for validation, |
| 163 |
// that proper code settings used for sensor data decoding. |
| 164 |
func (v *SensorBMP388) ReadSensorID(i2c *i2c.I2C) (uint8, error) { |
| 165 |
id, err := i2c.ReadRegU8(BMP388_ID_REG) |
| 166 |
if err != nil { |
| 167 |
return 0, err |
| 168 |
} |
| 169 |
return id, nil |
| 170 |
} |
| 171 |
|
| 172 |
// ReadCoefficients reads compensation coefficients, unique for each sensor. |
| 173 |
func (v *SensorBMP388) ReadCoefficients(i2c *i2c.I2C) error { |
| 174 |
_, err := i2c.WriteBytes([]byte{BMP388_COEF_START}) |
| 175 |
if err != nil { |
| 176 |
return err |
| 177 |
} |
| 178 |
var coef1 [BMP388_COEF_BYTES]byte |
| 179 |
err = readDataToStruct(i2c, BMP388_COEF_BYTES, |
| 180 |
binary.LittleEndian, &coef1) |
| 181 |
if err != nil { |
| 182 |
return err |
| 183 |
} |
| 184 |
buf := bytes.NewBuffer(coef1[:]) |
| 185 |
coeff := &CoeffBMP388{} |
| 186 |
err = binary.Read(buf, binary.LittleEndian, coeff) |
| 187 |
if err != nil { |
| 188 |
return err |
| 189 |
} |
| 190 |
v.Coeff = coeff |
| 191 |
return nil |
| 192 |
} |
| 193 |
|
| 194 |
// IsValidCoefficients verify that compensate registers |
| 195 |
// are not empty, and thus are valid. |
| 196 |
func (v *SensorBMP388) IsValidCoefficients() error { |
| 197 |
// TODO: research a better test for valid Coef. Refeence code doesn't check |
| 198 |
if v.Coeff != nil { |
| 199 |
err := checkCoefficient(uint16(v.Coeff.PAR_T1()), "PAR_T1") |
| 200 |
if err != nil { |
| 201 |
return err |
| 202 |
} |
| 203 |
err = checkCoefficient(uint16(v.Coeff.PAR_T2()), "PAR_T2") |
| 204 |
if err != nil { |
| 205 |
return err |
| 206 |
} |
| 207 |
err = checkCoefficient(uint16(v.Coeff.PAR_T3()), "PAR_T3") |
| 208 |
if err != nil { |
| 209 |
return err |
| 210 |
} |
| 211 |
err = checkCoefficient(uint16(v.Coeff.PAR_P1()), "PAR_P1") |
| 212 |
if err != nil { |
| 213 |
return err |
| 214 |
} |
| 215 |
err = checkCoefficient(uint16(v.Coeff.PAR_P2()), "PAR_P2") |
| 216 |
if err != nil { |
| 217 |
return err |
| 218 |
} |
| 219 |
err = checkCoefficient(uint16(v.Coeff.PAR_P3()), "PAR_P3") |
| 220 |
if err != nil { |
| 221 |
return err |
| 222 |
} |
| 223 |
// err = checkCoefficient(uint16(v.Coeff.PAR_P4()), "PAR_P4") |
| 224 |
// if err != nil { |
| 225 |
// return err |
| 226 |
// } |
| 227 |
err = checkCoefficient(uint16(v.Coeff.PAR_P5()), "PAR_P5") |
| 228 |
if err != nil { |
| 229 |
return err |
| 230 |
} |
| 231 |
err = checkCoefficient(uint16(v.Coeff.PAR_P6()), "PAR_P6") |
| 232 |
if err != nil { |
| 233 |
return err |
| 234 |
} |
| 235 |
err = checkCoefficient(uint16(v.Coeff.PAR_P7()), "PAR_P7") |
| 236 |
if err != nil { |
| 237 |
return err |
| 238 |
} |
| 239 |
err = checkCoefficient(uint16(v.Coeff.PAR_P8()), "PAR_P8") |
| 240 |
if err != nil { |
| 241 |
return err |
| 242 |
} |
| 243 |
err = checkCoefficient(uint16(v.Coeff.PAR_P9()), "PAR_P9") |
| 244 |
if err != nil { |
| 245 |
return err |
| 246 |
} |
| 247 |
err = checkCoefficient(uint16(v.Coeff.PAR_P10()), "PAR_P10") |
| 248 |
if err != nil { |
| 249 |
return err |
| 250 |
} |
| 251 |
err = checkCoefficient(uint16(v.Coeff.PAR_P11()), "PAR_P11") |
| 252 |
if err != nil { |
| 253 |
return err |
| 254 |
} |
| 255 |
} else { |
| 256 |
err := errors.New("CoeffBMP388 struct does not build") |
| 257 |
return err |
| 258 |
} |
| 259 |
lg.Debugf("PAR_T1:%v", v.Coeff.PAR_T1()) |
| 260 |
lg.Debugf("PAR_T2:%v", v.Coeff.PAR_T2()) |
| 261 |
lg.Debugf("PAR_T3:%v", v.Coeff.PAR_T3()) |
| 262 |
lg.Debugf("PAR_P1:%v", v.Coeff.PAR_P1()) |
| 263 |
lg.Debugf("PAR_P2:%v", v.Coeff.PAR_P2()) |
| 264 |
lg.Debugf("PAR_P3:%v", v.Coeff.PAR_P3()) |
| 265 |
lg.Debugf("PAR_P4:%v", v.Coeff.PAR_P4()) |
| 266 |
lg.Debugf("PAR_P5:%v", v.Coeff.PAR_P5()) |
| 267 |
lg.Debugf("PAR_P6:%v", v.Coeff.PAR_P6()) |
| 268 |
lg.Debugf("PAR_P7:%v", v.Coeff.PAR_P7()) |
| 269 |
lg.Debugf("PAR_P8:%v", v.Coeff.PAR_P8()) |
| 270 |
lg.Debugf("PAR_P9:%v", v.Coeff.PAR_P9()) |
| 271 |
lg.Debugf("PAR_P10:%v", v.Coeff.PAR_P10()) |
| 272 |
lg.Debugf("PAR_P11:%v", v.Coeff.PAR_P11()) |
| 273 |
return nil |
| 274 |
} |
| 275 |
|
| 276 |
// RecognizeSignature returns description of signature if it valid, |
| 277 |
// otherwise - error. |
| 278 |
func (v *SensorBMP388) RecognizeSignature(signature uint8) (string, error) { |
| 279 |
switch signature { |
| 280 |
case 0x50: |
| 281 |
return "BMP388", nil |
| 282 |
default: |
| 283 |
return "", errors.New(fmt.Sprintf("signature 0x%x doesn't belong to BMP388 series", signature)) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// IsBusy reads register 0xF3 for "busy" flag, |
| 288 |
// according to sensor specification. |
| 289 |
// BMP388 has three separate busy/done flags - pres, temp, and cmd |
| 290 |
// this routine is called by a 'waitFor Completion' shared by the other BMP parts, which all have a combined |
| 291 |
// busy/done bit. |
| 292 |
// for now - we return TRUE when any of the done bits go true |
| 293 |
// TODO: break out the busy polling |
| 294 |
func (v *SensorBMP388) IsBusy(i2c *i2c.I2C) (busy bool, err error) { |
| 295 |
// Check flag to know status of calculation, according |
| 296 |
// to specification about SCO (Start of conversion) flag |
| 297 |
b, err := i2c.ReadRegU8(BMP388_STATUS_REG) |
| 298 |
if err != nil { |
| 299 |
return false, err |
| 300 |
} |
| 301 |
lg.Debugf("Busy flag=0x%0X", b) |
| 302 |
b = b & 0x60 // ignore cmd done |
| 303 |
return b == 0, nil |
| 304 |
} |
| 305 |
|
| 306 |
func (v *SensorBMP388) getOversamplingRation(accuracy AccuracyMode) byte { |
| 307 |
var b byte |
| 308 |
switch accuracy { |
| 309 |
case ACCURACY_ULTRA_LOW: |
| 310 |
b = 0 |
| 311 |
case ACCURACY_LOW: |
| 312 |
b = 1 |
| 313 |
case ACCURACY_STANDARD: |
| 314 |
b = 2 |
| 315 |
case ACCURACY_HIGH: |
| 316 |
b = 3 |
| 317 |
case ACCURACY_ULTRA_HIGH: |
| 318 |
b = 4 |
| 319 |
case ACCURACY_HIGHEST: |
| 320 |
b = 5 |
| 321 |
default: |
| 322 |
// assign accuracy to lowest resolution by default |
| 323 |
b = 0 |
| 324 |
} |
| 325 |
return b |
| 326 |
} |
| 327 |
|
| 328 |
// readUncompTemprature reads uncompensated temprature from sensor. |
| 329 |
func (v *SensorBMP388) readUncompTemprature(i2c *i2c.I2C, accuracy AccuracyMode) (int32, error) { |
| 330 |
// set IIR filter to bypass |
| 331 |
err := i2c.WriteRegU8(BMP388_CONFIG, BMP388_coef_0<<1) |
| 332 |
if err != nil { |
| 333 |
return 0, err |
| 334 |
} |
| 335 |
// set over sample rate to 1x |
| 336 |
osrt := v.getOversamplingRation(accuracy) |
| 337 |
err = i2c.WriteRegU8(BMP388_OSR_REG, osrt<<3) |
| 338 |
if err != nil { |
| 339 |
return 0, err |
| 340 |
} |
| 341 |
// enable pres and temp measuremeent, start a measurment |
| 342 |
var power byte = (BMP388_PWR_MODE_FORCED << 4) | 3 // enable pres, temp, FORCED operating mode |
| 343 |
lg.Debugf("power=0x%0X", power) |
| 344 |
err = i2c.WriteRegU8(BMP388_PWR_CTRL_REG, power) |
| 345 |
if err != nil { |
| 346 |
return 0, err |
| 347 |
} |
| 348 |
_, err = waitForCompletion(v, i2c) |
| 349 |
if err != nil { |
| 350 |
return 0, err |
| 351 |
} |
| 352 |
buf, _, err := i2c.ReadRegBytes(BMP388_TEMP_OUT_MSB_LSB_XLSB, 3) |
| 353 |
if err != nil { |
| 354 |
return 0, err |
| 355 |
} |
| 356 |
ut := int32(uint32(buf[0]) + uint32(buf[1])<<8 + uint32(buf[2])<<16) |
| 357 |
return ut, nil |
| 358 |
} |
| 359 |
|
| 360 |
// readUncompPressure reads atmospheric uncompensated pressure from sensor. |
| 361 |
func (v *SensorBMP388) readUncompPressure(i2c *i2c.I2C, accuracy AccuracyMode) (int32, error) { |
| 362 |
var power byte = (BMP388_PWR_MODE_FORCED << 4) | 3 // enable pres, temp, FORCED operating mode |
| 363 |
err := i2c.WriteRegU8(BMP388_PWR_CTRL_REG, power) |
| 364 |
if err != nil { |
| 365 |
return 0, err |
| 366 |
} |
| 367 |
_, err = waitForCompletion(v, i2c) |
| 368 |
if err != nil { |
| 369 |
return 0, err |
| 370 |
} |
| 371 |
osrp := v.getOversamplingRation(accuracy) |
| 372 |
err = i2c.WriteRegU8(BMP388_OSR_REG, osrp) |
| 373 |
if err != nil { |
| 374 |
return 0, err |
| 375 |
} |
| 376 |
_, err = waitForCompletion(v, i2c) |
| 377 |
if err != nil { |
| 378 |
return 0, err |
| 379 |
} |
| 380 |
buf, _, err := i2c.ReadRegBytes(BMP388_PRES_OUT_MSB_LSB_XLSB, 3) |
| 381 |
if err != nil { |
| 382 |
return 0, err |
| 383 |
} |
| 384 |
up := int32(buf[0]) + int32(buf[1])<<8 + int32(buf[2])<<16 |
| 385 |
return up, nil |
| 386 |
} |
| 387 |
|
| 388 |
// readUncompTempratureAndPressure reads temprature and |
| 389 |
// atmospheric uncompensated pressure from sensor. |
| 390 |
// BMP388 allows to read temprature and pressure in one cycle, |
| 391 |
// BMP180 - doesn't. |
| 392 |
func (v *SensorBMP388) readUncompTempratureAndPressure(i2c *i2c.I2C, |
| 393 |
accuracy AccuracyMode) (temprature int32, pressure int32, err error) { |
| 394 |
var power byte = (BMP388_PWR_MODE_FORCED << 4) | 3 // enable pres, temp, FORCED operating mode |
| 395 |
err = i2c.WriteRegU8(BMP388_PWR_CTRL_REG, power) |
| 396 |
if err != nil { |
| 397 |
return 0, 0, err |
| 398 |
} |
| 399 |
_, err = waitForCompletion(v, i2c) |
| 400 |
if err != nil { |
| 401 |
return 0, 0, err |
| 402 |
} |
| 403 |
osrt := v.getOversamplingRation(ACCURACY_STANDARD) |
| 404 |
osrp := v.getOversamplingRation(accuracy) |
| 405 |
err = i2c.WriteRegU8(BMP388_OSR_REG, (osrt<<3)|osrp) |
| 406 |
if err != nil { |
| 407 |
return 0, 0, err |
| 408 |
} |
| 409 |
_, err = waitForCompletion(v, i2c) |
| 410 |
if err != nil { |
| 411 |
return 0, 0, err |
| 412 |
} |
| 413 |
buf, _, err := i2c.ReadRegBytes(BMP388_TEMP_OUT_MSB_LSB_XLSB, 3) |
| 414 |
if err != nil { |
| 415 |
return 0, 0, err |
| 416 |
} |
| 417 |
ut := int32(buf[0]) + int32(buf[1])<<8 + int32(buf[2])<<16 |
| 418 |
buf, _, err = i2c.ReadRegBytes(BMP388_PRES_OUT_MSB_LSB_XLSB, 3) |
| 419 |
if err != nil { |
| 420 |
return 0, 0, err |
| 421 |
} |
| 422 |
up := int32(buf[0]) + int32(buf[1])<<8 + int32(buf[2])<<16 |
| 423 |
return ut, up, nil |
| 424 |
} |
| 425 |
|
| 426 |
// ReadTemperatureMult100C reads and calculates temrature in C (celsius) multiplied by 100. |
| 427 |
// Multiplication approach allow to keep result as integer number. |
| 428 |
func (v *SensorBMP388) ReadTemperatureMult100C(i2c *i2c.I2C, accuracy AccuracyMode) (int32, error) { |
| 429 |
|
| 430 |
ut, err := v.readUncompTemprature(i2c, accuracy) |
| 431 |
if err != nil { |
| 432 |
return 0, err |
| 433 |
} |
| 434 |
err = v.ReadCoefficients(i2c) |
| 435 |
if err != nil { |
| 436 |
return 0, err |
| 437 |
} |
| 438 |
|
| 439 |
// comp formula - taken from BMP3 API on github |
| 440 |
partial_data1 := uint64(ut - int32(256*int32(v.Coeff.PAR_T1()))) |
| 441 |
partial_data2 := uint64(v.Coeff.PAR_T2()) * partial_data1 |
| 442 |
partial_data3 := partial_data1 * partial_data1 |
| 443 |
partial_data4 := int64(partial_data3) * int64(v.Coeff.PAR_T3()) |
| 444 |
partial_data5 := (int64(partial_data2*262144) + partial_data4) |
| 445 |
partial_data6 := partial_data5 / 4294967269 |
| 446 |
t := int32(partial_data6 * 25 / 16384) |
| 447 |
lg.Debugf("ut=%v", ut) |
| 448 |
lg.Debugf("d1=%v ", partial_data1) |
| 449 |
lg.Debugf("p_d2=%v ", partial_data2) |
| 450 |
lg.Debugf("p_d3=%v ", partial_data3) |
| 451 |
lg.Debugf("p_d4=%v ", partial_data4) |
| 452 |
lg.Debugf("p_d5=%v ", partial_data5) |
| 453 |
lg.Debugf("p_d6=%v ", partial_data6) |
| 454 |
return t, nil |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
// ReadPressureMult10Pa reads and calculates atmospheric pressure in Pa (Pascal) multiplied by 10. |
| 459 |
// Multiplication approach allow to keep result as integer number. |
| 460 |
func (v *SensorBMP388) ReadPressureMult10Pa(i2c *i2c.I2C, accuracy AccuracyMode) (uint32, error) { |
| 461 |
ut, up, err := v.readUncompTempratureAndPressure(i2c, accuracy) |
| 462 |
if err != nil { |
| 463 |
return 0, err |
| 464 |
} |
| 465 |
lg.Debugf("ut=%v, up=%v", ut, up) |
| 466 |
|
| 467 |
err = v.ReadCoefficients(i2c) |
| 468 |
if err != nil { |
| 469 |
return 0, err |
| 470 |
} |
| 471 |
|
| 472 |
// Comp temp for use in pressure comp |
| 473 |
// comp formula - taken from BMP3 API on github |
| 474 |
partial_data1_t := uint64(ut - int32(256*int32(v.Coeff.PAR_T1()))) |
| 475 |
partial_data2_t := uint64(v.Coeff.PAR_T2()) * partial_data1_t |
| 476 |
partial_data3_t := partial_data1_t * partial_data1_t |
| 477 |
partial_data4_t := int64(partial_data3_t) * int64(v.Coeff.PAR_T3()) |
| 478 |
partial_data5_t := (int64(partial_data2_t*262144) + partial_data4_t) |
| 479 |
partial_data6_t := partial_data5_t / 4294967269 |
| 480 |
t_lin := partial_data6_t |
| 481 |
lg.Debugf("t_lin=%v", t_lin) |
| 482 |
lg.Debugf("----------") |
| 483 |
|
| 484 |
// Compensate pressure - fixed point/integer arthmetic |
| 485 |
// taken form formulas written in github |
| 486 |
partial_data1 := t_lin * t_lin |
| 487 |
partial_data2 := partial_data1 / 64 |
| 488 |
partial_data3 := (partial_data2 * t_lin) / 256 |
| 489 |
partial_data4 := (int64(v.Coeff.PAR_P8()) * partial_data3) / 32 |
| 490 |
partial_data5 := (int64(v.Coeff.PAR_P7()) * partial_data1) * 16 |
| 491 |
partial_data6 := (int64(v.Coeff.PAR_P6()) * t_lin) * 4194304 |
| 492 |
offset := (int64(v.Coeff.PAR_P5()) * 140737488355328) + partial_data4 + partial_data5 + partial_data6 |
| 493 |
lg.Debugf("partial_data1=%v", partial_data1) |
| 494 |
lg.Debugf("partial_data2=%v", partial_data2) |
| 495 |
lg.Debugf("partial_data3=%v", partial_data3) |
| 496 |
lg.Debugf("partial_data4=%v", partial_data4) |
| 497 |
lg.Debugf("partial_data5=%v", partial_data5) |
| 498 |
lg.Debugf("partial_data6=%v", partial_data6) |
| 499 |
lg.Debugf("offset=%v", offset) |
| 500 |
lg.Debugf("----------") |
| 501 |
|
| 502 |
partial_data2 = (int64(v.Coeff.PAR_P4()) * partial_data3) / 32 |
| 503 |
partial_data4 = (int64(v.Coeff.PAR_P3()) * partial_data1) * 4 |
| 504 |
partial_data5 = (int64(v.Coeff.PAR_P2()) - 16384) * t_lin * 2097152 |
| 505 |
sensitivity := ((int64(v.Coeff.PAR_P1()) - 16384) * 70368744177664) + partial_data2 + partial_data4 + partial_data5 |
| 506 |
lg.Debugf("partial_data2=%v", partial_data2) |
| 507 |
lg.Debugf("partial_data4=%v", partial_data4) |
| 508 |
lg.Debugf("partial_data5=%v", partial_data5) |
| 509 |
lg.Debugf("sensitivity=%v", sensitivity) |
| 510 |
lg.Debugf("----------") |
| 511 |
|
| 512 |
partial_data1 = (sensitivity / 16777216) * int64(up) |
| 513 |
partial_data2 = int64(v.Coeff.PAR_P10()) * t_lin |
| 514 |
partial_data3 = partial_data2 + (65536 * int64(v.Coeff.PAR_P9())) |
| 515 |
partial_data4 = (partial_data3 * int64(up)) / 8192 |
| 516 |
partial_data5 = (partial_data4 * int64(up)) / 512 |
| 517 |
partial_data6 = int64(uint64(up) * uint64(up)) |
| 518 |
lg.Debugf("----------") |
| 519 |
lg.Debugf("partial_data1=%v", partial_data1) |
| 520 |
lg.Debugf("partial_data2=%v", partial_data2) |
| 521 |
lg.Debugf("partial_data3=%v", partial_data3) |
| 522 |
lg.Debugf("partial_data4=%v", partial_data4) |
| 523 |
lg.Debugf("partial_data5=%v", partial_data5) |
| 524 |
lg.Debugf("partial_data6=%v", partial_data6) |
| 525 |
lg.Debugf("----------") |
| 526 |
partial_data2 = (int64(v.Coeff.PAR_P11()) * partial_data6) / 65536 |
| 527 |
partial_data3 = (partial_data2 * int64(up)) / 128 |
| 528 |
partial_data4 = (offset / 4) + partial_data1 + partial_data5 + partial_data3 |
| 529 |
lg.Debugf("partial_data2=%v", partial_data2) |
| 530 |
lg.Debugf("partial_data3=%v", partial_data3) |
| 531 |
lg.Debugf("partial_data4=%v", partial_data4) |
| 532 |
comp_press := uint32((uint64(partial_data4) * 25) / 1099511627776) |
| 533 |
|
| 534 |
return comp_press, nil |
| 535 |
} |
| 536 |
|
| 537 |
// ReadHumidityMultQ2210 does nothing. Humidity function is not applicable for BMP388. |
| 538 |
func (v *SensorBMP388) ReadHumidityMultQ2210(i2c *i2c.I2C, accuracy AccuracyMode) (bool, uint32, error) { |
| 539 |
// Not supported |
| 540 |
return false, 0, nil |
| 541 |
} |