1
- from __future__ import print_function
2
-
3
1
import time
4
-
5
2
from numpy import int16
6
-
7
-
8
- def connect (route , ** args ):
9
- return BMP180 (route , ** args )
10
-
11
-
12
- class BMP180 :
13
- ADDRESS = 0x77
14
- REG_CONTROL = 0xF4
15
- REG_RESULT = 0xF6
16
- CMD_TEMP = 0x2E
17
- CMD_P0 = 0x34
18
- CMD_P1 = 0x74
19
- CMD_P2 = 0xB4
20
- CMD_P3 = 0xF4
3
+ from pslab .bus import I2CSlave
4
+
5
+ class BMP180 (I2CSlave ):
6
+ _ADDRESS = 0x77
7
+ _REG_CONTROL = 0xF4
8
+ _REG_RESULT = 0xF6
9
+ _CMD_TEMP = 0x2E
10
+ _CMD_P0 = 0x34
11
+ _CMD_P1 = 0x74
12
+ _CMD_P2 = 0xB4
13
+ _CMD_P3 = 0xF4
21
14
oversampling = 0
22
15
NUMPLOTS = 3
23
16
PLOTNAMES = ['Temperature' , 'Pressure' , 'Altitude' ]
24
- name = 'Altimeter BMP180'
25
-
26
- def __init__ (self , I2C , ** args ):
27
- self .ADDRESS = args .get ('address' , self .ADDRESS )
17
+ name = 'BMP180 Altimeter'
28
18
29
- self .I2C = I2C
19
+ def __init__ (self , ** args ):
20
+ self ._ADDRESS = args .get ('address' , self ._ADDRESS )
21
+ super ().__init__ (self ._ADDRESS )
30
22
31
23
self .MB = self .__readInt__ (0xBA )
32
24
@@ -50,26 +42,25 @@ def __init__(self, I2C, **args):
50
42
print ('calib:' , self .c3 , self .c4 , self .b1 , self .c5 , self .c6 , self .mc , self .md , self .x0 , self .x1 , self .x2 ,
51
43
self .y0 , self .y1 , self .p0 , self .p1 , self .p2 )
52
44
self .params = {'setOversampling' : [0 , 1 , 2 , 3 ]}
53
- self . name = 'BMP180 Altimeter'
45
+
54
46
self .initTemperature ()
55
- self .readTemperature ()
56
47
self .initPressure ()
57
48
self .baseline = self .readPressure ()
58
49
59
50
def __readInt__ (self , addr ):
60
- return int16 (self .__readUInt__ (addr ))
51
+ return int16 (self .read_byte (addr ))
61
52
62
53
def __readUInt__ (self , addr ):
63
- vals = self .I2C . readBulk ( self . ADDRESS , addr , 2 )
54
+ vals = self .read ( 2 , addr )
64
55
v = 1. * ((vals [0 ] << 8 ) | vals [1 ])
65
56
return v
66
57
67
58
def initTemperature (self ):
68
- self .I2C . writeBulk ( self . ADDRESS , [self .REG_CONTROL , self .CMD_TEMP ])
59
+ self .write ( [self ._REG_CONTROL , self ._CMD_TEMP ])
69
60
time .sleep (0.005 )
70
61
71
62
def readTemperature (self ):
72
- vals = self .I2C . readBulk ( self . ADDRESS , self .REG_RESULT , 2 )
63
+ vals = self .read ( 2 , self ._REG_RESULT )
73
64
if len (vals ) == 2 :
74
65
T = (vals [0 ] << 8 ) + vals [1 ]
75
66
a = self .c5 * (T - self .c6 )
@@ -84,11 +75,11 @@ def setOversampling(self, num):
84
75
def initPressure (self ):
85
76
os = [0x34 , 0x74 , 0xb4 , 0xf4 ]
86
77
delays = [0.005 , 0.008 , 0.014 , 0.026 ]
87
- self .I2C . writeBulk ( self . ADDRESS , [self .REG_CONTROL , os [self .oversampling ]])
78
+ self .write ( [self ._REG_CONTROL , os [self .oversampling ]])
88
79
time .sleep (delays [self .oversampling ])
89
80
90
81
def readPressure (self ):
91
- vals = self .I2C . readBulk ( self . ADDRESS , self .REG_RESULT , 3 )
82
+ vals = self .read ( 3 , self ._REG_RESULT )
92
83
if len (vals ) == 3 :
93
84
P = 1. * (vals [0 ] << 8 ) + vals [1 ] + (vals [2 ] / 256.0 )
94
85
s = self .T - 25.0
0 commit comments