-
Notifications
You must be signed in to change notification settings - Fork 0
/
bme280.go
474 lines (441 loc) · 10.6 KB
/
bme280.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package bme280
import "io"
import "time"
import "github.com/pkg/errors"
type BME280 struct {
i2c io.ReadWriter
calib struct {
temp struct {
T1 uint16
T2 int16
T3 int16
}
press struct {
P1 uint16
P2 int16
P3 int16
P4 int16
P5 int16
P6 int16
P7 int16
P8 int16
P9 int16
}
hum struct {
H1 uint8
H2 int16
H3 uint8
H4 int16
H5 int16
H6 int8
}
}
raw [8]byte
}
type option func(*BME280) error
// Option sets the options specified.
// It returns an option to restore the last arg's previous value.
func (bme *BME280) Option(opts ...option) (err error) {
for _, opt := range opts {
err = opt(bme)
if err != nil {
return err
}
}
return nil
}
// OptHumOversampling can receive following values
// 0 -> off
// 1, 2, 4, 8, 16
func OptHumOversampling(sampling int) option {
return func(bme *BME280) error {
// read
var r byte
_, err := bme.read(reg_ctrl_hum, []byte{r})
if err != nil {
return err
}
// modify
r = r & ^byte(opt_hum_mask)
switch sampling {
case 0:
r = r | opt_hum_oversampling_skipped
case 1:
r = r | opt_hum_oversampling_x1
case 2:
r = r | opt_hum_oversampling_x2
case 4:
r = r | opt_hum_oversampling_x4
case 8:
r = r | opt_hum_oversampling_x8
case 16:
r = r | opt_hum_oversampling_x16
}
// write
_, err = bme.write(reg_ctrl_hum, []byte{r})
return err
}
}
// OptTempOversampling can receive following values
// 0 -> off
// 1, 2, 4, 8, 16
func OptTempOversampling(sampling int) option {
return func(bme *BME280) error {
// read
var r byte
_, err := bme.read(reg_ctrl_meas, []byte{r})
if err != nil {
return err
}
// modify
r = r & ^byte(opt_temp_mask)
switch sampling {
case 0:
r = r | opt_temp_oversampling_skipped
case 1:
r = r | opt_temp_oversampling_x1
case 2:
r = r | opt_temp_oversampling_x2
case 4:
r = r | opt_temp_oversampling_x4
case 8:
r = r | opt_temp_oversampling_x8
case 16:
r = r | opt_temp_oversampling_x16
}
// write
_, err = bme.write(reg_ctrl_meas, []byte{r})
return err
}
}
// OptPressOversampling can receive following values
// 0 -> off
// 1, 2, 4, 8, 16
func OptPressOversampling(sampling int) option {
return func(bme *BME280) error {
// read
var r byte
_, err := bme.read(reg_ctrl_meas, []byte{r})
if err != nil {
return err
}
// modify
r = r & ^byte(opt_press_mask)
switch sampling {
case 0:
r = r | opt_press_oversampling_skipped
case 1:
r = r | opt_press_oversampling_x1
case 2:
r = r | opt_press_oversampling_x2
case 4:
r = r | opt_press_oversampling_x4
case 8:
r = r | opt_press_oversampling_x8
case 16:
r = r | opt_press_oversampling_x16
}
// write
_, err = bme.write(reg_ctrl_meas, []byte{r})
return err
}
}
// OptMode can receive the following values
// "sleep", "forced", "normal"
func OptMode(mode string) option {
return func(bme *BME280) error {
// read
var r byte
_, err := bme.read(reg_ctrl_meas, []byte{r})
if err != nil {
return err
}
// modify
r = r & ^byte(opt_mode_mask)
switch mode {
case "sleep":
r = r | opt_mode_sleep
case "forced":
r = r | opt_mode_forced
case "normal":
r = r | opt_mode_normal
}
// write
_, err = bme.write(reg_ctrl_meas, []byte{r})
return err
}
}
// OptFilter can get following values:
// 0 -> off
// 2, 4, 8 ,16
func OptFilter(mode int) option {
return func(bme *BME280) error {
// read
var r byte
_, err := bme.read(reg_config, []byte{r})
if err != nil {
return err
}
// modify
r = r & ^byte(opt_config_filter_mask)
switch mode {
case 0:
r = r | opt_config_filter_off
case 2:
r = r | opt_config_filter_2
case 4:
r = r | opt_config_filter_4
case 8:
r = r | opt_config_filter_8
case 16:
r = r | opt_config_filter_16
}
// write
_, err = bme.write(reg_config, []byte{r})
return err
}
}
// OptStandbytime can get following values:
// 5 -> 5ms
// 625 -> 62.5 ms // carefull this one is special
// 125 -> 125 ms
// 250 -> 250ms
// 500, 1000, 10, 20...
func OptStandbytime(time int) option {
return func(bme *BME280) error {
// read
var r byte
_, err := bme.read(reg_config, []byte{r})
if err != nil {
return err
}
// modify
r = (r & ^byte(opt_config_standbytime_mask))
switch time {
case 5:
r = r | opt_config_standbytime_0_5
case 625:
r = r | opt_config_standbytime_62_5
case 125:
r = r | opt_config_standbytime_125
case 250:
r = r | opt_config_standbytime_250
case 500:
r = r | opt_config_standbytime_500
case 1000:
r = r | opt_config_standbytime_1000
case 10:
r = r | opt_config_standbytime_10
case 20:
r = r | opt_config_standbytime_20
}
// write
_, err = bme.write(reg_config, []byte{r})
return err
}
}
// OptReset will reset the sensor.
// Use WaitForPowerOnReset before using sensor again.
func OptReset() option {
return func(bme *BME280) error {
// write
_, err := bme.write(reg_reset, []byte{opt_reset})
return err
}
}
// Envdata saves temperature pressure and humidity
type Envdata struct {
Temp float64 `json:"temp"`
Press float64 `json:"press"`
Hum float64 `json:"hum"`
}
func (bme *BME280) read(reg byte, data []byte) (int, error) {
// first we have to write register adress
_, err := bme.i2c.Write([]byte{reg})
if err != nil {
return 0, err
}
// now we can read the data
return bme.i2c.Read(data)
}
func (bme *BME280) write(reg byte, data []byte) (int, error) {
var tdata []byte
tdata = append(tdata, reg)
tdata = append(tdata, data...)
return bme.i2c.Write(tdata)
}
// WaitForPowerOnReset polls the id-register of the bme280 sensor.
// There can be 3 types of errors.
// - Timeout after 3 seconds. This is very uncommon.
// - If the id-register has other values than 0x0 or 0x60.
// - If the read of the register fails.
// Returns nil if sensor has finished booting.
func (bme *BME280) WaitForPowerOnReset() (err error) {
var x [1]byte
timeoutchan := time.After(time.Second * 3)
for x[0] != 0x60 && err == nil {
select {
case <-timeoutchan:
return errors.New("Timeout reached.")
default:
_, err = bme.read(reg_id, x[:])
if x[0] != 0x0 && x[0] != 0x60 {
return errors.New("Register has some undefined value. Is this a bme280 sensor?")
}
}
time.Sleep(50 * time.Millisecond)
}
return err
}
// WaitForMeasurement polls the status-register of the bme280 sensor.
// Possible errors:
// - Timeout after 3 seconds. This is very uncommon.
// - If the read of the register fails.
// Returns nil if sensor has finished measurement
func (bme *BME280) WaitForMeasurement() (err error) {
var x [1]byte
timeoutchan := time.After(time.Second * 3)
for err == nil {
select {
case <-timeoutchan:
return errors.New("Timeout reached.")
default:
_, err = bme.read(reg_status, x[:])
x[0] = x[0] & opt_status_measure_mask
if x[0] == 0 {
return nil
}
}
time.Sleep(50 * time.Millisecond)
}
return err
}
func (bme *BME280) readCalibdata() (err error) {
// read calibration data
var calib1 [26]byte
var calib2 [16]byte
_, err = bme.read(reg_calib00, calib1[:])
if err != nil {
return err
}
_, err = bme.read(reg_calib26, calib2[:])
if err != nil {
return err
}
type tmpt struct {
idata []byte
odata interface{}
}
tconvert := []tmpt{
{calib1[0:6], &bme.calib.temp},
{calib1[6:24], &bme.calib.press},
{calib1[25:], &bme.calib.hum.H1},
{calib2[0:2], &bme.calib.hum.H2},
{calib2[2:3], &bme.calib.hum.H3},
{calib2[6:], &bme.calib.hum.H6}}
for _, value := range tconvert {
err = convert(value.idata, value.odata)
if err != nil {
return err
}
}
// H4 and H5 are a little bit tricky alligned.
bme.calib.hum.H4 = int16(calib2[3])<<4 | int16(calib2[4]&0x0F)
bme.calib.hum.H5 = int16(calib2[5])<<4 | int16(calib2[4]&0xF0)>>4
return err
}
func (bme *BME280) initialize(opts ...option) (err error) {
// wait for finished initialisation
err = bme.WaitForPowerOnReset()
if err != nil {
return err
}
// get calibrationdata
err = bme.readCalibdata()
if err != nil {
return err
}
// initialize bme
if len(opts) == 0 {
return bme.Option(OptHumOversampling(1), OptTempOversampling(1),
OptPressOversampling(1), OptStandbytime(1000), OptMode("normal"))
}
return bme.Option(opts...)
}
// latch all data in
func (bme *BME280) readRaw() (err error) {
_, err = bme.read(reg_press_msb, bme.raw[:])
return err
}
// Readenv starts the calculation of enviroment data
func (bme *BME280) Readenv() (env Envdata, err error) {
err = bme.readRaw()
traw := int32(bme.raw[3])<<12 | int32(bme.raw[4])<<4 | int32(bme.raw[5])>>4
praw := int32(bme.raw[0])<<12 | int32(bme.raw[1])<<4 | int32(bme.raw[2])>>4
hraw := int32(bme.raw[6])<<8 | int32(bme.raw[7])
t, tfine := bme.temp(traw)
p := bme.press(praw, tfine)
h := bme.hum(hraw, tfine)
env.Temp = t
env.Press = p / 100
env.Hum = h
return env, err
}
func (bme *BME280) temp(raw int32) (float64, int32) {
calt := bme.calib.temp
var v1, v2, t float64
var tfine int32
v1 = (float64(raw)/16384.0 - float64(calt.T1)/1024.0) *
float64(calt.T2)
v2 = (float64(raw)/131072.0 - float64(calt.T1)/8192.0) *
(float64(raw)/131072.0 - float64(calt.T1)/8192.0) *
float64(calt.T3)
tfine = int32(v1 + v2)
t = (v1 + v2) / 5120.0
return t, tfine
}
func (bme *BME280) press(raw int32, tfine int32) float64 {
calp := bme.calib.press
var v1, v2, p float64
v1 = float64(tfine)/2.0 - 64000.0
v2 = v1 * v1 * (float64(calp.P6) / 32768.0)
v2 = v2 + v1*(float64(calp.P5)*2.0)
v2 = v2/4.0 + (float64(calp.P4) * 65536.0)
v1 = (float64(calp.P3)*v1*v1/524288.0 + float64(calp.P2)*v1) / 524288.0
v1 = (1.0 + v1/32768.0) * float64(calp.P1)
if v1 == 0 {
return 0
}
p = 1048576.0 - float64(raw)
p = (p - v2/4096.0) * 6250.0 / v1
v1 = float64(calp.P9) * p * p / 2147483648.0
v2 = p * float64(calp.P8) / 32768.0
p = p + (v1+v2+float64(calp.P7))/16.0
return p
}
func (bme *BME280) hum(raw int32, tfine int32) float64 {
calh := bme.calib.hum
var h float64
h = float64(tfine) - 76800.0
h = (float64(raw) - float64(calh.H4)*64.0 +
float64(calh.H5)/16384.0*h) * float64(calh.H2) /
65536.0 * (1.0 + float64(calh.H6)/67108864.0*h*
(1.0+float64(calh.H3)/67108864.0*h))
h = h * (1.0 - float64(calh.H1)*h/524288.0)
if h > 100.0 {
h = 100.0
} else if h < 0.0 {
h = 0.0
}
return h
}
// NewI2CDriver initializes the bme280 device to use the i2c-bus for communication.
// It is expecting the i2c bus as a ReadWriter-Interface.
// If no options given it will be initialized with:
// OptHumOversampling(1), OptTempOversampling(1), OptPressOversampling(1), OptStandbytime(1000), OptMode("normal")
func NewI2CDriver(i2c io.ReadWriter, ops ...option) (*BME280, error) {
bme := BME280{
i2c: i2c,
}
return &bme, bme.initialize(ops...)
}