@@ -29,86 +29,86 @@ def __init__(self, i2c, address=0x29):
2929 self .default_settings ()
3030 self .init ()
3131
32- def i2c_write (self , register , regValue ):
32+ def _i2c_write (self , register , regValue ):
3333 return self .i2c .writeto_mem (self ._address , register , bytearray ([regValue ]), addrsize = 16 ), 'big'
3434
35- def i2c_read (self , register , nb_bytes = 1 ):
35+ def _i2c_read (self , register , nb_bytes = 1 ):
3636 value = int .from_bytes (
3737 self .i2c .readfrom_mem (self ._address , register , nb_bytes , addrsize = 16 ),
3838 'big'
3939 )
4040 return value & 0xFFFF
4141
4242 def init (self ):
43- if self .i2c_read (0x0016 ) != 1 :
43+ if self ._i2c_read (0x0016 ) != 1 :
4444 raise RuntimeError ("Failure reset" )
4545
4646 # Recommended setup from the datasheet
47- self .i2c_write (0x0207 , 0x01 )
48- self .i2c_write (0x0208 , 0x01 )
49- self .i2c_write (0x0096 , 0x00 )
50- self .i2c_write (0x0097 , 0xfd )
51- self .i2c_write (0x00e3 , 0x00 )
52- self .i2c_write (0x00e4 , 0x04 )
53- self .i2c_write (0x00e5 , 0x02 )
54- self .i2c_write (0x00e6 , 0x01 )
55- self .i2c_write (0x00e7 , 0x03 )
56- self .i2c_write (0x00f5 , 0x02 )
57- self .i2c_write (0x00d9 , 0x05 )
58- self .i2c_write (0x00db , 0xce )
59- self .i2c_write (0x00dc , 0x03 )
60- self .i2c_write (0x00dd , 0xf8 )
61- self .i2c_write (0x009f , 0x00 )
62- self .i2c_write (0x00a3 , 0x3c )
63- self .i2c_write (0x00b7 , 0x00 )
64- self .i2c_write (0x00bb , 0x3c )
65- self .i2c_write (0x00b2 , 0x09 )
66- self .i2c_write (0x00ca , 0x09 )
67- self .i2c_write (0x0198 , 0x01 )
68- self .i2c_write (0x01b0 , 0x17 )
69- self .i2c_write (0x01ad , 0x00 )
70- self .i2c_write (0x00ff , 0x05 )
71- self .i2c_write (0x0100 , 0x05 )
72- self .i2c_write (0x0199 , 0x05 )
73- self .i2c_write (0x01a6 , 0x1b )
74- self .i2c_write (0x01ac , 0x3e )
75- self .i2c_write (0x01a7 , 0x1f )
76- self .i2c_write (0x0030 , 0x00 )
47+ self ._i2c_write (0x0207 , 0x01 )
48+ self ._i2c_write (0x0208 , 0x01 )
49+ self ._i2c_write (0x0096 , 0x00 )
50+ self ._i2c_write (0x0097 , 0xfd )
51+ self ._i2c_write (0x00e3 , 0x00 )
52+ self ._i2c_write (0x00e4 , 0x04 )
53+ self ._i2c_write (0x00e5 , 0x02 )
54+ self ._i2c_write (0x00e6 , 0x01 )
55+ self ._i2c_write (0x00e7 , 0x03 )
56+ self ._i2c_write (0x00f5 , 0x02 )
57+ self ._i2c_write (0x00d9 , 0x05 )
58+ self ._i2c_write (0x00db , 0xce )
59+ self ._i2c_write (0x00dc , 0x03 )
60+ self ._i2c_write (0x00dd , 0xf8 )
61+ self ._i2c_write (0x009f , 0x00 )
62+ self ._i2c_write (0x00a3 , 0x3c )
63+ self ._i2c_write (0x00b7 , 0x00 )
64+ self ._i2c_write (0x00bb , 0x3c )
65+ self ._i2c_write (0x00b2 , 0x09 )
66+ self ._i2c_write (0x00ca , 0x09 )
67+ self ._i2c_write (0x0198 , 0x01 )
68+ self ._i2c_write (0x01b0 , 0x17 )
69+ self ._i2c_write (0x01ad , 0x00 )
70+ self ._i2c_write (0x00ff , 0x05 )
71+ self ._i2c_write (0x0100 , 0x05 )
72+ self ._i2c_write (0x0199 , 0x05 )
73+ self ._i2c_write (0x01a6 , 0x1b )
74+ self ._i2c_write (0x01ac , 0x3e )
75+ self ._i2c_write (0x01a7 , 0x1f )
76+ self ._i2c_write (0x0030 , 0x00 )
7777
7878 def default_settings (self ):
7979 # Enables polling for ‘New Sample ready’ when measurement completes
80- self .i2c_write (0x0011 , 0x10 )
81- self .i2c_write (0x010A , 0x30 ) # Set Avg sample period
82- self .i2c_write (0x003f , 0x46 ) # Set the ALS gain
83- self .i2c_write (0x0031 , 0xFF ) # Set auto calibration period
80+ self ._i2c_write (0x0011 , 0x10 )
81+ self ._i2c_write (0x010A , 0x30 ) # Set Avg sample period
82+ self ._i2c_write (0x003f , 0x46 ) # Set the ALS gain
83+ self ._i2c_write (0x0031 , 0xFF ) # Set auto calibration period
8484 # (Max = 255)/(OFF = 0)
85- self .i2c_write (0x0040 , 0x63 ) # Set ALS integration time to 100ms
85+ self ._i2c_write (0x0040 , 0x63 ) # Set ALS integration time to 100ms
8686 # perform a single temperature calibration
87- self .i2c_write (0x002E , 0x01 )
87+ self ._i2c_write (0x002E , 0x01 )
8888
8989 # Optional settings from datasheet
90- self .i2c_write (0x001B , 0x09 ) # Set default ranging inter-measurement
90+ self ._i2c_write (0x001B , 0x09 ) # Set default ranging inter-measurement
9191 # period to 100ms
92- self .i2c_write (0x003E , 0x0A ) # Set default ALS inter-measurement
92+ self ._i2c_write (0x003E , 0x0A ) # Set default ALS inter-measurement
9393 # period to 100ms
94- self .i2c_write (0x0014 , 0x24 ) # Configures interrupt on ‘New Sample
94+ self ._i2c_write (0x0014 , 0x24 ) # Configures interrupt on ‘New Sample
9595 # Ready threshold event’
9696
9797 # Additional settings defaults from community
98- self .i2c_write (0x001C , 0x32 ) # Max convergence time
99- self .i2c_write (0x002D , 0x10 | 0x01 ) # Range check enables
100- self .i2c_write (0x0022 , 0x7B ) # Early convergence estimate
101- self .i2c_write (0x0120 , 0x01 ) # Firmware result scaler
98+ self ._i2c_write (0x001C , 0x32 ) # Max convergence time
99+ self ._i2c_write (0x002D , 0x10 | 0x01 ) # Range check enables
100+ self ._i2c_write (0x0022 , 0x7B ) # Early convergence estimate
101+ self ._i2c_write (0x0120 , 0x01 ) # Firmware result scaler
102102
103- def _range (self ):
103+ def _read_range_single (self ):
104104 """Measure the distance in millimeters. Takes 0.01s."""
105- self .i2c_write (0x0018 , 0x01 ) # Sysrange start
105+ self ._i2c_write (0x0018 , 0x01 ) # Sysrange start
106106 time .sleep (0.01 )
107- return self .i2c_read (0x0062 ) # Result range value import ustruct
107+ return self ._i2c_read (0x0062 ) # Result range value import ustruct
108108
109109 @property
110110 def range (self ):
111- return self ._range ()
111+ return self ._read_range_single ()
112112
113113 def read_lux (self , gain = 0x06 ):
114114 """Read the lux (light value) from the sensor and return it. Must
@@ -130,30 +130,30 @@ def read_lux(self, gain=0x06):
130130 :param int gain: The gain value to use
131131 """
132132
133- reg = self .i2c_read (0x0014 )
133+ reg = self ._i2c_read (0x0014 )
134134 reg &= ~ 0x38
135135 reg |= 0x4 << 3 # IRQ on ALS ready
136- self .i2c_write (0x0014 , reg )
136+ self ._i2c_write (0x0014 , reg )
137137 # 100 ms integration period
138- self .i2c_write (0x0040 , 0 )
139- self .i2c_write (0x0041 , 100 )
138+ self ._i2c_write (0x0040 , 0 )
139+ self ._i2c_write (0x0041 , 100 )
140140 # analog gain
141141 gain = min (gain , 0x07 )
142- self .i2c_write (0x003F , 0x40 | gain )
142+ self ._i2c_write (0x003F , 0x40 | gain )
143143 # start ALS
144- self .i2c_write (0x0038 , 1 )
144+ self ._i2c_write (0x0038 , 1 )
145145 # Poll until "New Sample Ready threshold event" is set
146146 while (
147- (self .i2c_read (0x004F ) >> 3 ) & 0x7
147+ (self ._i2c_read (0x004F ) >> 3 ) & 0x7
148148 ) != 4 :
149149 pass
150150 # read lux!
151- lux = self .i2c_read (0x0050 , 2 )
151+ lux = self ._i2c_read (0x0050 , 2 )
152152 # clear interrupt
153- self .i2c_write (0x0015 , 0x07 )
153+ self ._i2c_write (0x0015 , 0x07 )
154154 lux *= 0.32 # calibrated count/lux
155155
156- if gain == 0x06 : # ALS_GAIN_1:
156+ if gain == 0x06 : # ALS_GAIN_1:
157157 pass
158158 elif gain == 0x05 : # ALS_GAIN_1_25:
159159 lux /= 1.25
0 commit comments