forked from fossasia/pslab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBH1750.py
More file actions
66 lines (51 loc) · 1.71 KB
/
BH1750.py
File metadata and controls
66 lines (51 loc) · 1.71 KB
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
from __future__ import print_function
def connect(route, **args):
return BRIDGE(route, **args)
class BRIDGE():
POWER_ON = 0x01
RESET = 0x07
RES_1000mLx = 0x10
RES_500mLx = 0x11
RES_4000mLx = 0x13
gain_choices = [RES_500mLx, RES_1000mLx, RES_4000mLx]
gain_literal_choices = ['500mLx', '1000mLx', '4000mLx']
gain = 0
scaling = [2, 1, .25]
# --------------Parameters--------------------
# This must be defined in order to let GUIs automatically create menus
# for changing various options of this sensor
# It's a dictionary of the string representations of functions matched with an array
# of options that each one can accept
params = {'init': None,
'setRange': gain_literal_choices,
}
NUMPLOTS = 1
PLOTNAMES = ['Lux']
ADDRESS = 0x23
name = 'Luminosity'
def __init__(self, I2C, **args):
self.I2C = I2C
self.ADDRESS = args.get('address', 0x23)
self.init()
def init(self):
self.I2C.writeBulk(self.ADDRESS, [self.RES_500mLx])
def setRange(self, g):
self.gain = self.gain_literal_choices.index(g)
self.I2C.writeBulk(self.ADDRESS, [self.gain_choices[self.gain]])
def getVals(self, numbytes):
vals = self.I2C.simpleRead(self.ADDRESS, numbytes)
return vals
def getRaw(self):
vals = self.getVals(2)
if vals:
if len(vals) == 2:
return [(vals[0] << 8 | vals[1]) / 1.2] # /self.scaling[self.gain]
else:
return False
else:
return False
if __name__ == "__main__":
from PSL import sciencelab
I = sciencelab.connect()
A = connect(I.I2C)
print(A.getRaw())