-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp180.go
161 lines (133 loc) · 3.01 KB
/
bmp180.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
//Texas Instruments INA219 high side current sensor
package bmp180
import (
"fmt"
"github.com/NeuralSpaz/i2c"
)
const (
BMP180_ADDRESS = 0x77
BMP180_CALREG_AC1 = 0xAA
BMP180_CALREG_AC2 = 0xAC
BMP180_CALREG_AC3 = 0xAE
BMP180_CALREG_AC4 = 0xB0
BMP180_CALREG_AC5 = 0xB2
BMP180_CALREG_AC6 = 0xB4
BMP180_CALREG_B1 = 0xB6
BMP180_CALREG_B2 = 0xB8
BMP180_CALREG_MB = 0xBA
BMP180_CALREG_MC = 0xBC
BMP180_CALREG_MD = 0xBE
BMP180_CONTROL = 0xF4
BMP180_TEMP = 0xF6
BMP180_PRESSURE = 0xF6
BMP180_READTEMPCMD = 0x2E
BMP180_READPRESSURECMD = 0x34
)
type BMP180 struct {
Dev i2c.I2CBus
Init bool
Address uint8
oss uint
ac1, ac2, ac3 int16
ac4, ac5, ac6 uint16
b1, b2, mb, mc, md int16
b5 int32
Temp float64
Pressure float64
Altitude float64
}
func (d *BMP180) String() string {
return fmt.Sprintf("Temprature %f", d.Temp)
}
func New(deviceAdress uint8, i2cbus byte) *BMP180 {
deviceBus := i2c.NewI2CBus(i2cbus)
d := &BMP180{
Dev: deviceBus,
Address: deviceAdress,
}
return d
}
// Fetch all values from BMP180
func Fetch(d *BMP180) error {
if !d.Init {
err := calibration(d)
if err != nil {
return err
}
}
// Measure Temprature
if err := d.Dev.WriteByteToReg(d.Address, BMP180_TEMP, BMP180_READTEMPCMD); err != nil {
return err
}
// Get TEMP
ut, err := d.Dev.ReadWordFromReg(d.Address, BMP180_TEMP)
if err != nil {
return err
}
UT := int32(ut)
X1 := ((UT - int32(d.ac6)) * int32(d.ac5)) >> 15
X2 := (int32(d.mc) * 2048) / (X1 + int32(d.md))
B5 := X1 + X2
CT := (B5 + 8) >> 4
d.Temp = float64(CT) * 0.1
return nil
}
func calibration(d *BMP180) error {
ac1, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_AC1)
if err != nil {
return err
}
d.ac1 = int16(ac1)
ac2, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_AC2)
if err != nil {
return err
}
d.ac2 = int16(ac2)
ac3, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_AC3)
if err != nil {
return err
}
d.ac3 = int16(ac3)
ac4, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_AC4)
if err != nil {
return err
}
d.ac4 = uint16(ac4)
ac5, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_AC5)
if err != nil {
return err
}
d.ac5 = uint16(ac5)
ac6, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_AC6)
if err != nil {
return err
}
d.ac6 = uint16(ac6)
b1, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_B1)
if err != nil {
return err
}
d.b1 = int16(b1)
b2, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_B2)
if err != nil {
return err
}
d.b2 = int16(b2)
mb, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_MB)
if err != nil {
return err
}
d.mb = int16(mb)
mc, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_MC)
if err != nil {
return err
}
d.mc = int16(mc)
md, err := d.Dev.ReadWordFromReg(d.Address, BMP180_CALREG_MD)
if err != nil {
return err
}
d.md = int16(md)
d.Init = true
return nil
}