| 1 |
//-------------------------------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (c) 2018 Denis Dyakov |
| 4 |
// |
| 5 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 6 |
// associated documentation files (the "Software"), to deal in the Software without restriction, |
| 7 |
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 |
// subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in all copies or substantial |
| 12 |
// portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 15 |
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 16 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 17 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
// |
| 20 |
//-------------------------------------------------------------------------------------------------- |
| 21 |
|
| 22 |
package bsbmp |
| 23 |
|
| 24 |
import ( |
| 25 |
"bytes" |
| 26 |
"encoding/binary" |
| 27 |
"errors" |
| 28 |
"fmt" |
| 29 |
|
| 30 |
i2c "github.com/d2r2/go-i2c" |
| 31 |
) |
| 32 |
|
| 33 |
// BMP180 sensors memory map |
| 34 |
const ( |
| 35 |
// BMP180 general registers |
| 36 |
BMP180_ID_REG = 0xD0 |
| 37 |
BMP180_CNTR_MEAS_REG = 0xF4 |
| 38 |
BMP180_RESET = 0xE0 |
| 39 |
// BMP180 specific compensation register's block |
| 40 |
BMP180_COEF_START = 0xAA |
| 41 |
BMP180_COEF_BYTES = 22 |
| 42 |
// BMP180 specific 3-byte reading out temprature and preassure |
| 43 |
BMP180_OUT_MSB_LSB_XLSB = 0xF6 |
| 44 |
) |
| 45 |
|
| 46 |
// Unique BMP180 calibration coefficients |
| 47 |
type CoeffBMP180 struct { |
| 48 |
// Registers storing unique calibration coefficients |
| 49 |
COEF_AA uint8 |
| 50 |
COEF_AB uint8 |
| 51 |
COEF_AC uint8 |
| 52 |
COEF_AD uint8 |
| 53 |
COEF_AE uint8 |
| 54 |
COEF_AF uint8 |
| 55 |
COEF_B0 uint8 |
| 56 |
COEF_B1 uint8 |
| 57 |
COEF_B2 uint8 |
| 58 |
COEF_B3 uint8 |
| 59 |
COEF_B4 uint8 |
| 60 |
COEF_B5 uint8 |
| 61 |
COEF_B6 uint8 |
| 62 |
COEF_B7 uint8 |
| 63 |
COEF_B8 uint8 |
| 64 |
COEF_B9 uint8 |
| 65 |
COEF_BA uint8 |
| 66 |
COEF_BB uint8 |
| 67 |
COEF_BC uint8 |
| 68 |
COEF_BD uint8 |
| 69 |
COEF_BE uint8 |
| 70 |
COEF_BF uint8 |
| 71 |
} |
| 72 |
|
| 73 |
func (v *CoeffBMP180) dig_AC1() int16 { |
| 74 |
return int16(uint16(v.COEF_AA)<<8 | uint16(v.COEF_AB)) |
| 75 |
} |
| 76 |
|
| 77 |
func (v *CoeffBMP180) dig_AC2() int16 { |
| 78 |
return int16(uint16(v.COEF_AC)<<8 | uint16(v.COEF_AD)) |
| 79 |
} |
| 80 |
|
| 81 |
func (v *CoeffBMP180) dig_AC3() int16 { |
| 82 |
return int16(uint16(v.COEF_AE)<<8 | uint16(v.COEF_AF)) |
| 83 |
} |
| 84 |
|
| 85 |
func (v *CoeffBMP180) dig_AC4() uint16 { |
| 86 |
return uint16(v.COEF_B0)<<8 | uint16(v.COEF_B1) |
| 87 |
} |
| 88 |
|
| 89 |
func (v *CoeffBMP180) dig_AC5() uint16 { |
| 90 |
return uint16(v.COEF_B2)<<8 | uint16(v.COEF_B3) |
| 91 |
} |
| 92 |
|
| 93 |
func (v *CoeffBMP180) dig_AC6() uint16 { |
| 94 |
return uint16(v.COEF_B4)<<8 | uint16(v.COEF_B5) |
| 95 |
} |
| 96 |
|
| 97 |
func (v *CoeffBMP180) dig_B1() int16 { |
| 98 |
return int16(uint16(v.COEF_B6)<<8 | uint16(v.COEF_B7)) |
| 99 |
} |
| 100 |
|
| 101 |
func (v *CoeffBMP180) dig_B2() int16 { |
| 102 |
return int16(uint16(v.COEF_B8)<<8 | uint16(v.COEF_B9)) |
| 103 |
} |
| 104 |
|
| 105 |
func (v *CoeffBMP180) dig_MB() int16 { |
| 106 |
return int16(uint16(v.COEF_BA)<<8 | uint16(v.COEF_BB)) |
| 107 |
} |
| 108 |
|
| 109 |
func (v *CoeffBMP180) dig_MC() int16 { |
| 110 |
return int16(uint16(v.COEF_BC)<<8 | uint16(v.COEF_BD)) |
| 111 |
} |
| 112 |
|
| 113 |
func (v *CoeffBMP180) dig_MD() int16 { |
| 114 |
return int16(uint16(v.COEF_BE)<<8 | uint16(v.COEF_BF)) |
| 115 |
} |
| 116 |
|
| 117 |
// SensorBMP180 specific type |
| 118 |
type SensorBMP180 struct { |
| 119 |
Coeff *CoeffBMP180 |
| 120 |
} |
| 121 |
|
| 122 |
// Static cast to verify at compile time |
| 123 |
// that type implement interface. |
| 124 |
var _ SensorInterface = &SensorBMP180{} |
| 125 |
|
| 126 |
// ReadSensorID reads sensor signature. It may be used for validation, |
| 127 |
// that proper code settings used for sensor data decoding. |
| 128 |
func (v *SensorBMP180) ReadSensorID(i2c *i2c.I2C) (uint8, error) { |
| 129 |
id, err := i2c.ReadRegU8(BMP180_ID_REG) |
| 130 |
if err != nil { |
| 131 |
return 0, err |
| 132 |
} |
| 133 |
return id, nil |
| 134 |
} |
| 135 |
|
| 136 |
// ReadCoefficients reads compensation coefficients, unique for each sensor. |
| 137 |
func (v *SensorBMP180) ReadCoefficients(i2c *i2c.I2C) error { |
| 138 |
_, err := i2c.WriteBytes([]byte{BMP180_COEF_START}) |
| 139 |
if err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
var coef1 [BMP180_COEF_BYTES]byte |
| 143 |
err = readDataToStruct(i2c, BMP180_COEF_BYTES, |
| 144 |
binary.LittleEndian, &coef1) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
buf := bytes.NewBuffer(coef1[:]) |
| 149 |
coeff := &CoeffBMP180{} |
| 150 |
err = binary.Read(buf, binary.LittleEndian, coeff) |
| 151 |
if err != nil { |
| 152 |
return err |
| 153 |
} |
| 154 |
v.Coeff = coeff |
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
// IsValidCoefficients verify that compensate registers |
| 159 |
// are not empty, and thus are valid. |
| 160 |
func (v *SensorBMP180) IsValidCoefficients() error { |
| 161 |
if v.Coeff != nil { |
| 162 |
err := checkCoefficient(uint16(v.Coeff.dig_AC1()), "AC1") |
| 163 |
if err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
err = checkCoefficient(uint16(v.Coeff.dig_AC2()), "AC2") |
| 167 |
if err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
err = checkCoefficient(uint16(v.Coeff.dig_AC3()), "AC3") |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
err = checkCoefficient(v.Coeff.dig_AC4(), "AC4") |
| 175 |
if err != nil { |
| 176 |
return err |
| 177 |
} |
| 178 |
err = checkCoefficient(v.Coeff.dig_AC5(), "AC5") |
| 179 |
if err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
err = checkCoefficient(v.Coeff.dig_AC6(), "AC6") |
| 183 |
if err != nil { |
| 184 |
return err |
| 185 |
} |
| 186 |
err = checkCoefficient(uint16(v.Coeff.dig_B1()), "B1") |
| 187 |
if err != nil { |
| 188 |
return err |
| 189 |
} |
| 190 |
err = checkCoefficient(uint16(v.Coeff.dig_B2()), "B2") |
| 191 |
if err != nil { |
| 192 |
return err |
| 193 |
} |
| 194 |
err = checkCoefficient(uint16(v.Coeff.dig_MB()), "MB") |
| 195 |
if err != nil { |
| 196 |
return err |
| 197 |
} |
| 198 |
err = checkCoefficient(uint16(v.Coeff.dig_MC()), "MC") |
| 199 |
if err != nil { |
| 200 |
return err |
| 201 |
} |
| 202 |
err = checkCoefficient(uint16(v.Coeff.dig_MD()), "MD") |
| 203 |
if err != nil { |
| 204 |
return err |
| 205 |
} |
| 206 |
} else { |
| 207 |
err := errors.New("CoeffBMP180 struct does not built") |
| 208 |
return err |
| 209 |
} |
| 210 |
return nil |
| 211 |
} |
| 212 |
|
| 213 |
// RecognizeSignature returns description of signature if it valid, |
| 214 |
// otherwise - error. |
| 215 |
func (v *SensorBMP180) RecognizeSignature(signature uint8) (string, error) { |
| 216 |
switch signature { |
| 217 |
case 0x55: |
| 218 |
return "BMP180", nil |
| 219 |
default: |
| 220 |
return "", errors.New(fmt.Sprintf("signature 0x%x doesn't belong to BMP180 series", signature)) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// IsBusy reads register 0xF4 for "busy" flag, |
| 225 |
// according to sensor specification. |
| 226 |
func (v *SensorBMP180) IsBusy(i2c *i2c.I2C) (busy bool, err error) { |
| 227 |
// Check flag to know status of calculation, according |
| 228 |
// to specification about SCO (Start of conversion) flag |
| 229 |
b, err := i2c.ReadRegU8(BMP180_CNTR_MEAS_REG) |
| 230 |
if err != nil { |
| 231 |
return false, err |
| 232 |
} |
| 233 |
b = b & 0x20 |
| 234 |
lg.Debugf("Busy flag=0x%0X", b) |
| 235 |
return b != 0, nil |
| 236 |
} |
| 237 |
|
| 238 |
// readUncompTemp reads uncompensated temprature from sensor. |
| 239 |
func (v *SensorBMP180) readUncompTemp(i2c *i2c.I2C) (int32, error) { |
| 240 |
err := i2c.WriteRegU8(BMP180_CNTR_MEAS_REG, 0x2F) |
| 241 |
if err != nil { |
| 242 |
return 0, err |
| 243 |
} |
| 244 |
_, err = waitForCompletion(v, i2c) |
| 245 |
if err != nil { |
| 246 |
return 0, err |
| 247 |
} |
| 248 |
w, err := i2c.ReadRegU16BE(BMP180_OUT_MSB_LSB_XLSB) |
| 249 |
if err != nil { |
| 250 |
return 0, err |
| 251 |
} |
| 252 |
return int32(w), nil |
| 253 |
} |
| 254 |
|
| 255 |
func (v *SensorBMP180) getOversamplingRation(accuracy AccuracyMode) byte { |
| 256 |
var b byte |
| 257 |
switch accuracy { |
| 258 |
case ACCURACY_LOW, ACCURACY_ULTRA_LOW: |
| 259 |
b = 0 |
| 260 |
case ACCURACY_STANDARD: |
| 261 |
b = 1 |
| 262 |
case ACCURACY_HIGH: |
| 263 |
b = 2 |
| 264 |
case ACCURACY_ULTRA_HIGH: |
| 265 |
b = 3 |
| 266 |
default: |
| 267 |
// assign accuracy to lowest resolution by default |
| 268 |
b = 0 |
| 269 |
} |
| 270 |
return b |
| 271 |
} |
| 272 |
|
| 273 |
// readUncompPressure reads atmospheric uncompensated pressure from sensor. |
| 274 |
func (v *SensorBMP180) readUncompPressure(i2c *i2c.I2C, accuracy AccuracyMode) (int32, error) { |
| 275 |
oss := v.getOversamplingRation(accuracy) |
| 276 |
lg.Debugf("oss=%v", oss) |
| 277 |
err := i2c.WriteRegU8(BMP180_CNTR_MEAS_REG, 0x34+(oss<<6)) |
| 278 |
if err != nil { |
| 279 |
return 0, err |
| 280 |
} |
| 281 |
_, err = waitForCompletion(v, i2c) |
| 282 |
if err != nil { |
| 283 |
return 0, err |
| 284 |
} |
| 285 |
buf, _, err := i2c.ReadRegBytes(BMP180_OUT_MSB_LSB_XLSB, 3) |
| 286 |
if err != nil { |
| 287 |
return 0, err |
| 288 |
} |
| 289 |
up := (int32(buf[0])<<16 + int32(buf[1])<<8 + int32(buf[2])) >> (8 - oss) |
| 290 |
return up, nil |
| 291 |
} |
| 292 |
|
| 293 |
// ReadTemperatureMult100C reads and calculates temprature in C (celsius) multiplied by 100. |
| 294 |
// Multiplication approach allow to keep result as integer number. |
| 295 |
func (v *SensorBMP180) ReadTemperatureMult100C(i2c *i2c.I2C, mode AccuracyMode) (int32, error) { |
| 296 |
ut, err := v.readUncompTemp(i2c) |
| 297 |
if err != nil { |
| 298 |
return 0, err |
| 299 |
} |
| 300 |
err = v.ReadCoefficients(i2c) |
| 301 |
if err != nil { |
| 302 |
return 0, err |
| 303 |
} |
| 304 |
// Calculate temperature according to sensor specification |
| 305 |
x1 := ((ut - int32(v.Coeff.dig_AC6())) * int32(v.Coeff.dig_AC5())) >> 15 |
| 306 |
lg.Debugf("x1=%v", x1) |
| 307 |
x2 := (int32(v.Coeff.dig_MC()) << 11) / (x1 + int32(v.Coeff.dig_MD())) |
| 308 |
lg.Debugf("x2=%v", x2) |
| 309 |
b5 := x1 + x2 |
| 310 |
lg.Debugf("b5=%v", b5) |
| 311 |
t := ((b5 + 8) >> 4) * 10 |
| 312 |
lg.Debugf("t=%v", t) |
| 313 |
return t, nil |
| 314 |
} |
| 315 |
|
| 316 |
// ReadPressureMult10Pa reads and calculates atmospheric pressure in Pa (Pascal) multiplied by 10. |
| 317 |
// Multiplication approach allow to keep result as integer number. |
| 318 |
func (v *SensorBMP180) ReadPressureMult10Pa(i2c *i2c.I2C, accuracy AccuracyMode) (uint32, error) { |
| 319 |
oss := v.getOversamplingRation(accuracy) |
| 320 |
ut, err := v.readUncompTemp(i2c) |
| 321 |
if err != nil { |
| 322 |
return 0, err |
| 323 |
} |
| 324 |
lg.Debugf("ut=%v", ut) |
| 325 |
|
| 326 |
up, err := v.readUncompPressure(i2c, accuracy) |
| 327 |
if err != nil { |
| 328 |
return 0, err |
| 329 |
} |
| 330 |
lg.Debugf("up=%v", up) |
| 331 |
|
| 332 |
err = v.ReadCoefficients(i2c) |
| 333 |
if err != nil { |
| 334 |
return 0, err |
| 335 |
} |
| 336 |
|
| 337 |
// Calculate pressure according to sensor specification |
| 338 |
x1 := ((ut - int32(v.Coeff.dig_AC6())) * int32(v.Coeff.dig_AC5())) >> 15 |
| 339 |
lg.Debugf("x1=%v", x1) |
| 340 |
x2 := (int32(v.Coeff.dig_MC()) << 11) / (x1 + int32(v.Coeff.dig_MD())) |
| 341 |
lg.Debugf("x2=%v", x2) |
| 342 |
b5 := x1 + x2 |
| 343 |
lg.Debugf("b5=%v", b5) |
| 344 |
b6 := b5 - 4000 |
| 345 |
lg.Debugf("b6=%v", b6) |
| 346 |
x1 = (int32(v.Coeff.dig_B2()) * ((b6 * b6) >> 12)) >> 11 |
| 347 |
lg.Debugf("x1=%v", x1) |
| 348 |
x2 = (int32(v.Coeff.dig_AC2()) * b6) >> 11 |
| 349 |
lg.Debugf("x2=%v", x2) |
| 350 |
x3 := x1 + x2 |
| 351 |
lg.Debugf("x3=%v", x3) |
| 352 |
b3 := (((int32(v.Coeff.dig_AC1())*4 + x3) << uint32(oss)) + 2) / 4 |
| 353 |
lg.Debugf("b3=%v", b3) |
| 354 |
x1 = (int32(v.Coeff.dig_AC3()) * b6) >> 13 |
| 355 |
lg.Debugf("x1=%v", x1) |
| 356 |
x2 = ((int32(v.Coeff.dig_B1()) * (b6 * b6)) >> 12) >> 16 |
| 357 |
lg.Debugf("x2=%v", x2) |
| 358 |
x3 = ((x1 + x2) + 2) >> 2 |
| 359 |
lg.Debugf("x3=%v", x3) |
| 360 |
b4 := (uint32(v.Coeff.dig_AC4()) * uint32(x3+32768)) >> 15 |
| 361 |
lg.Debugf("b4=%v", b4) |
| 362 |
b7 := (uint32(up) - uint32(b3)) * (50000 >> uint32(oss)) |
| 363 |
lg.Debugf("b7=%v", b7) |
| 364 |
var p1 int32 |
| 365 |
if b7 < 0x80000000 { |
| 366 |
p1 = int32((b7 * 2) / b4) |
| 367 |
} else { |
| 368 |
p1 = int32((b7 / b4) * 2) |
| 369 |
} |
| 370 |
lg.Debugf("p=%v", p1) |
| 371 |
x1 = (p1 >> 8) * (p1 >> 8) |
| 372 |
lg.Debugf("x1=%v", x1) |
| 373 |
x1 = (x1 * 3038) >> 16 |
| 374 |
lg.Debugf("x1=%v", x1) |
| 375 |
x2 = (-7357 * p1) >> 16 |
| 376 |
lg.Debugf("x2=%v", x2) |
| 377 |
p1 += (x1 + x2 + 3791) >> 4 |
| 378 |
lg.Debugf("p=%v", p1) |
| 379 |
p := uint32(p1) * 10 |
| 380 |
return p, nil |
| 381 |
} |
| 382 |
|
| 383 |
// ReadHumidityMultQ2210 does nothing. Humidity function is not applicable for BMP180. |
| 384 |
func (v *SensorBMP180) ReadHumidityMultQ2210(i2c *i2c.I2C, accuracy AccuracyMode) (bool, uint32, error) { |
| 385 |
// Not supported |
| 386 |
return false, 0, nil |
| 387 |
} |