| 1 |
//-------------------------------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (c) 2018 Denis Dyakov |
| 4 |
// poritons 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 |
// go-bsbmp package implements reading sensors values and providing compensating the readings, based on a table of coefficents stored in the device. |
| 24 |
// Sensors supported: |
| 25 |
// BMP180 - Abs Press, Temp. (Not recommeneded for new designs) |
| 26 |
// BMP280 - Abs Press, Tewp. |
| 27 |
// BME280 - ABs Press, Temp, Relative Humidity |
| 28 |
// BMP388 - Abs Press, Temp. |
| 29 |
// Note: the BMP300 device was never produced |
| 30 |
package bsbmp |
| 31 |
|
| 32 |
import ( |
| 33 |
"math" |
| 34 |
|
| 35 |
"github.com/d2r2/go-i2c" |
| 36 |
) |
| 37 |
|
| 38 |
// SensorType identify which Bosch Sensortec |
| 39 |
// temperature and pressure sensor is used. |
| 40 |
// BMP180 and BMP280 are supported. |
| 41 |
type SensorType int |
| 42 |
|
| 43 |
// Implement Stringer interface. |
| 44 |
func (v SensorType) String() string { |
| 45 |
if v == BMP180 { |
| 46 |
return "BMP180" |
| 47 |
} else if v == BMP280 { |
| 48 |
return "BMP280" |
| 49 |
} else if v == BME280 { |
| 50 |
return "BME280" |
| 51 |
} else if v == BMP388 { |
| 52 |
return "BMP388" |
| 53 |
} else { |
| 54 |
return "!!! unknown !!!" |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
const ( |
| 59 |
// Bosch Sensortec pressure and temperature sensor model BMP180. |
| 60 |
BMP180 SensorType = iota |
| 61 |
// Bosch Sensortec pressure and temperature sensor model BMP280. |
| 62 |
BMP280 |
| 63 |
// Bosch Sensortec pressure, temperature and relative humidity sensor model BME280. |
| 64 |
BME280 |
| 65 |
// Bosch Sensortec pressure and temperature sensor model BMP388. |
| 66 |
BMP388 |
| 67 |
) |
| 68 |
|
| 69 |
// Accuracy mode for calculation of atmospheric pressure and temprature. |
| 70 |
// Impact to value accuracy, calculation time frame and power consumption. |
| 71 |
type AccuracyMode int |
| 72 |
|
| 73 |
const ( |
| 74 |
ACCURACY_ULTRA_LOW AccuracyMode = iota // x1 sample |
| 75 |
ACCURACY_LOW // x2 samples |
| 76 |
ACCURACY_STANDARD // x4 samples |
| 77 |
ACCURACY_HIGH // x8 samples |
| 78 |
ACCURACY_ULTRA_HIGH // x16 samples |
| 79 |
ACCURACY_HIGHEST // x32 samples - added in BMP388 |
| 80 |
) |
| 81 |
|
| 82 |
// Abstract BMPx sensor interface |
| 83 |
// to control and gather data. |
| 84 |
type SensorInterface interface { |
| 85 |
// ReadSensorID read sensor identifier unuque for each sensor type. |
| 86 |
ReadSensorID(i2c *i2c.I2C) (uint8, error) |
| 87 |
// ReadCoefficients read coefficient's block unique for each sensor. |
| 88 |
ReadCoefficients(i2c *i2c.I2C) error |
| 89 |
// IsValidCoefficients verify that coefficient values are not empty. |
| 90 |
IsValidCoefficients() error |
| 91 |
// Verify, that specific sensor can own signature identifier and |
| 92 |
// return text description of this specific id. |
| 93 |
RecognizeSignature(signature uint8) (string, error) |
| 94 |
// IsBusy check via status register that sensor ready for data exchange. |
| 95 |
IsBusy(i2c *i2c.I2C) (bool, error) |
| 96 |
// Divide by 10 to get float temperature value in celsius. |
| 97 |
ReadTemperatureMult100C(i2c *i2c.I2C, mode AccuracyMode) (temperature int32, erro error) |
| 98 |
// Divide by 10 to get float preasure value in pascal. |
| 99 |
ReadPressureMult10Pa(i2c *i2c.I2C, mode AccuracyMode) (pressure uint32, erro error) |
| 100 |
// Divide by 1024 to get float humidity value in range [0..100]%. |
| 101 |
ReadHumidityMultQ2210(i2c *i2c.I2C, mode AccuracyMode) (supported bool, humidity uint32, erro error) |
| 102 |
} |
| 103 |
|
| 104 |
// BMP represent both sensors BMP180 and BMP280 |
| 105 |
// implementing same approach to control and gather data. |
| 106 |
type BMP struct { |
| 107 |
sensorType SensorType |
| 108 |
i2c *i2c.I2C |
| 109 |
bmp SensorInterface |
| 110 |
} |
| 111 |
|
| 112 |
// NewBMP creates new sensor object. |
| 113 |
func NewBMP(sensorType SensorType, i2c *i2c.I2C) (*BMP, error) { |
| 114 |
v := &BMP{sensorType: sensorType, i2c: i2c} |
| 115 |
switch sensorType { |
| 116 |
case BMP180: |
| 117 |
v.bmp = &SensorBMP180{} |
| 118 |
case BMP280: |
| 119 |
v.bmp = &SensorBMP280{} |
| 120 |
case BME280: |
| 121 |
v.bmp = &SensorBME280{} |
| 122 |
case BMP388: |
| 123 |
v.bmp = &SensorBMP388{} |
| 124 |
} |
| 125 |
|
| 126 |
id, err := v.ReadSensorID() |
| 127 |
if err != nil { |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
_, err = v.bmp.RecognizeSignature(id) |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
err = v.bmp.ReadCoefficients(i2c) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
return v, nil |
| 139 |
} |
| 140 |
|
| 141 |
// ReadSensorID reads sensor signature. It may be used for validation, |
| 142 |
// that proper code settings used for sensor data decoding. |
| 143 |
func (v *BMP) ReadSensorID() (uint8, error) { |
| 144 |
id, err := v.bmp.ReadSensorID(v.i2c) |
| 145 |
return id, err |
| 146 |
} |
| 147 |
|
| 148 |
func (v *BMP) IsValidCoefficients() error { |
| 149 |
return v.bmp.IsValidCoefficients() |
| 150 |
} |
| 151 |
|
| 152 |
// ReadTemperatureMult100C reads and calculates temrature in C (celsius) multiplied by 100. |
| 153 |
// Multiplication approach allow to keep result as integer amount. |
| 154 |
func (v *BMP) ReadTemperatureMult100C(accuracy AccuracyMode) (int32, error) { |
| 155 |
t, err := v.bmp.ReadTemperatureMult100C(v.i2c, accuracy) |
| 156 |
return t, err |
| 157 |
} |
| 158 |
|
| 159 |
// ReadTemperatureC reads and calculates temrature in C (celsius). |
| 160 |
func (v *BMP) ReadTemperatureC(accuracy AccuracyMode) (float32, error) { |
| 161 |
t, err := v.bmp.ReadTemperatureMult100C(v.i2c, accuracy) |
| 162 |
if err != nil { |
| 163 |
return 0, err |
| 164 |
} |
| 165 |
return float32(t) / 100, nil |
| 166 |
} |
| 167 |
|
| 168 |
// ReadPressureMult10Pa reads and calculates atmospheric pressure in Pa (Pascal) multiplied by 10. |
| 169 |
// Multiplication approach allow to keep result as integer amount. |
| 170 |
func (v *BMP) ReadPressureMult10Pa(accuracy AccuracyMode) (uint32, error) { |
| 171 |
p, err := v.bmp.ReadPressureMult10Pa(v.i2c, accuracy) |
| 172 |
return p, err |
| 173 |
} |
| 174 |
|
| 175 |
// ReadPressurePa reads and calculates atmospheric pressure in Pa (Pascal). |
| 176 |
func (v *BMP) ReadPressurePa(accuracy AccuracyMode) (float32, error) { |
| 177 |
p, err := v.bmp.ReadPressureMult10Pa(v.i2c, accuracy) |
| 178 |
if err != nil { |
| 179 |
return 0, err |
| 180 |
} |
| 181 |
return float32(p) / 10, err |
| 182 |
} |
| 183 |
|
| 184 |
// ReadPressureMmHg reads and calculates atmospheric pressure in mmHg (millimeter of mercury). |
| 185 |
func (v *BMP) ReadPressureMmHg(accuracy AccuracyMode) (float32, error) { |
| 186 |
p, err := v.bmp.ReadPressureMult10Pa(v.i2c, accuracy) |
| 187 |
if err != nil { |
| 188 |
return 0, err |
| 189 |
} |
| 190 |
// Amount of Pa in 1 mmHg |
| 191 |
var mmHg float32 = 133.322 |
| 192 |
// Round up to 2 decimals after point |
| 193 |
p2 := float32(int(float32(p)/mmHg*10)) / 100 |
| 194 |
return p2, nil |
| 195 |
} |
| 196 |
|
| 197 |
// ReadHumidityRH reads and calculate humidity %RH. |
| 198 |
func (v *BMP) ReadHumidityRH(accuracy AccuracyMode) (bool, float32, error) { |
| 199 |
supported, h, err := v.bmp.ReadHumidityMultQ2210(v.i2c, accuracy) |
| 200 |
if !supported { |
| 201 |
return supported, 0, nil |
| 202 |
} |
| 203 |
if err != nil { |
| 204 |
return supported, 0, err |
| 205 |
} |
| 206 |
h2 := float32(h) / 1024 |
| 207 |
return supported, h2, nil |
| 208 |
} |
| 209 |
|
| 210 |
// ReadAltitude reads and calculates altitude above sea level, if we assume |
| 211 |
// that pressure at sea level is equal to 101325 Pa. |
| 212 |
func (v *BMP) ReadAltitude(accuracy AccuracyMode) (float32, error) { |
| 213 |
p, err := v.bmp.ReadPressureMult10Pa(v.i2c, accuracy) |
| 214 |
if err != nil { |
| 215 |
return 0, err |
| 216 |
} |
| 217 |
// Approximate atmospheric pressure at sea level in Pa |
| 218 |
p0 := 1013250.0 |
| 219 |
a := 44330 * (1 - math.Pow(float64(p)/p0, 1/5.255)) |
| 220 |
// Round up to 2 decimals after point |
| 221 |
a2 := float32(int(a*100)) / 100 |
| 222 |
return a2, nil |
| 223 |
} |