4949
5050# Register addresses:
5151# pylint: disable=bad-whitespace
52- REG_OUTADC1_L = const (0x08 )
53- REG_WHOAMI = const (0x0F )
54- REG_TEMPCFG = const (0x1F )
55- REG_CTRL1 = const (0x20 )
56- REG_CTRL3 = const (0x22 )
57- REG_CTRL4 = const (0x23 )
58- REG_CTRL5 = const (0x24 )
59- REG_OUT_X_L = const (0x28 )
60- REG_INT1SRC = const (0x31 )
61- REG_CLICKCFG = const (0x38 )
62- REG_CLICKSRC = const (0x39 )
63- REG_CLICKTHS = const (0x3A )
64- REG_TIMELIMIT = const (0x3B )
65- REG_TIMELATENCY = const (0x3C )
66- REG_TIMEWINDOW = const (0x3D )
52+ _REG_OUTADC1_L = const (0x08 )
53+ _REG_WHOAMI = const (0x0F )
54+ _REG_TEMPCFG = const (0x1F )
55+ _REG_CTRL1 = const (0x20 )
56+ _REG_CTRL3 = const (0x22 )
57+ _REG_CTRL4 = const (0x23 )
58+ _REG_CTRL5 = const (0x24 )
59+ _REG_OUT_X_L = const (0x28 )
60+ _REG_INT1SRC = const (0x31 )
61+ _REG_CLICKCFG = const (0x38 )
62+ _REG_CLICKSRC = const (0x39 )
63+ _REG_CLICKTHS = const (0x3A )
64+ _REG_TIMELIMIT = const (0x3B )
65+ _REG_TIMELATENCY = const (0x3C )
66+ _REG_TIMEWINDOW = const (0x3D )
6767
6868# Register value constants:
6969RANGE_16_G = const (0b11 ) # +/- 16g
@@ -94,22 +94,22 @@ class LIS3DH:
9494 """Driver base for the LIS3DH accelerometer."""
9595 def __init__ (self , int1 = None , int2 = None ):
9696 # Check device ID.
97- device_id = self ._read_register_byte (REG_WHOAMI )
97+ device_id = self ._read_register_byte (_REG_WHOAMI )
9898 if device_id != 0x33 :
9999 raise RuntimeError ('Failed to find LIS3DH!' )
100100 # Reboot
101- self ._write_register_byte (REG_CTRL5 , 0x80 )
101+ self ._write_register_byte (_REG_CTRL5 , 0x80 )
102102 time .sleep (0.01 ) # takes 5ms
103103 # Enable all axes, normal mode.
104- self ._write_register_byte (REG_CTRL1 , 0x07 )
104+ self ._write_register_byte (_REG_CTRL1 , 0x07 )
105105 # Set 400Hz data rate.
106106 self .data_rate = DATARATE_400_HZ
107107 # High res & BDU enabled.
108- self ._write_register_byte (REG_CTRL4 , 0x88 )
108+ self ._write_register_byte (_REG_CTRL4 , 0x88 )
109109 # Enable ADCs.
110- self ._write_register_byte (REG_TEMPCFG , 0x80 )
110+ self ._write_register_byte (_REG_TEMPCFG , 0x80 )
111111 # Latch interrupt for INT1
112- self ._write_register_byte (REG_CTRL5 , 0x08 )
112+ self ._write_register_byte (_REG_CTRL5 , 0x08 )
113113
114114 # Initialise interrupt pins
115115 self ._int1 = int1
@@ -124,29 +124,29 @@ def data_rate(self):
124124 DATA_RATE_100_HZ, DATA_RATE_50_HZ, DATA_RATE_25_HZ, DATA_RATE_10_HZ,
125125 DATA_RATE_1_HZ, DATA_RATE_POWERDOWN, DATA_RATE_LOWPOWER_1K6HZ, or
126126 DATA_RATE_LOWPOWER_5KHZ."""
127- ctl1 = self ._read_register_byte (REG_CTRL1 )
127+ ctl1 = self ._read_register_byte (_REG_CTRL1 )
128128 return (ctl1 >> 4 ) & 0x0F
129129
130130 @data_rate .setter
131131 def data_rate (self , rate ):
132- ctl1 = self ._read_register_byte (REG_CTRL1 )
132+ ctl1 = self ._read_register_byte (_REG_CTRL1 )
133133 ctl1 &= ~ (0xF0 )
134134 ctl1 |= rate << 4
135- self ._write_register_byte (REG_CTRL1 , ctl1 )
135+ self ._write_register_byte (_REG_CTRL1 , ctl1 )
136136
137137 @property
138138 def range (self ):
139139 """The range of the accelerometer. Can be RANGE_2_G, RANGE_4_G, RANGE_8_G, or
140140 RANGE_16_G."""
141- ctl4 = self ._read_register_byte (REG_CTRL4 )
141+ ctl4 = self ._read_register_byte (_REG_CTRL4 )
142142 return (ctl4 >> 4 ) & 0x03
143143
144144 @range .setter
145145 def range (self , range_value ):
146- ctl4 = self ._read_register_byte (REG_CTRL4 )
146+ ctl4 = self ._read_register_byte (_REG_CTRL4 )
147147 ctl4 &= ~ 0x30
148148 ctl4 |= range_value << 4
149- self ._write_register_byte (REG_CTRL4 , ctl4 )
149+ self ._write_register_byte (_REG_CTRL4 , ctl4 )
150150
151151 @property
152152 def acceleration (self ):
@@ -162,7 +162,7 @@ def acceleration(self):
162162 elif accel_range == RANGE_2_G :
163163 divider = 16380
164164
165- x , y , z = struct .unpack ('<hhh' , self ._read_register (REG_OUT_X_L | 0x80 , 6 ))
165+ x , y , z = struct .unpack ('<hhh' , self ._read_register (_REG_OUT_X_L | 0x80 , 6 ))
166166
167167 # convert from Gs to m / s ^ 2 and adjust for the range
168168 x = (x / divider ) * STANDARD_GRAVITY
@@ -209,7 +209,7 @@ def read_adc_raw(self, adc):
209209 if adc < 1 or adc > 3 :
210210 raise ValueError ('ADC must be a value 1 to 3!' )
211211
212- return struct .unpack ('<h' , self ._read_register ((REG_OUTADC1_L + ((adc - 1 )* 2 )) | 0x80 , 2 ))[0 ]
212+ return struct .unpack ('<h' , self ._read_register ((_REG_OUTADC1_L + ((adc - 1 )* 2 )) | 0x80 , 2 ))[0 ]
213213
214214 def read_adc_mV (self , adc ): # pylint: disable=invalid-name
215215 """Read the specified analog to digital converter value in millivolts.
@@ -252,7 +252,7 @@ def tapped(self):
252252 """
253253 if self ._int1 and not self ._int1 .value :
254254 return False
255- raw = self ._read_register_byte (REG_CLICKSRC )
255+ raw = self ._read_register_byte (_REG_CLICKSRC )
256256 return raw & 0x40 > 0
257257
258258 def set_tap (self , tap , threshold , * ,
@@ -280,26 +280,26 @@ def set_tap(self, tap, threshold, *,
280280 if threshold > 127 or threshold < 0 :
281281 raise ValueError ('Threshold out of range (0-127)' )
282282
283- ctrl3 = self ._read_register_byte (REG_CTRL3 )
283+ ctrl3 = self ._read_register_byte (_REG_CTRL3 )
284284 if tap == 0 and click_cfg is None :
285285 # Disable click interrupt.
286- self ._write_register_byte (REG_CTRL3 , ctrl3 & ~ (0x80 )) # Turn off I1_CLICK.
287- self ._write_register_byte (REG_CLICKCFG , 0 )
286+ self ._write_register_byte (_REG_CTRL3 , ctrl3 & ~ (0x80 )) # Turn off I1_CLICK.
287+ self ._write_register_byte (_REG_CLICKCFG , 0 )
288288 return
289289 else :
290- self ._write_register_byte (REG_CTRL3 , ctrl3 | 0x80 ) # Turn on int1 click output
290+ self ._write_register_byte (_REG_CTRL3 , ctrl3 | 0x80 ) # Turn on int1 click output
291291
292292 if click_cfg is None :
293293 if tap == 1 :
294294 click_cfg = 0x15 # Turn on all axes & singletap.
295295 if tap == 2 :
296296 click_cfg = 0x2A # Turn on all axes & doubletap.
297297 # Or, if a custom click configuration register value specified, use it.
298- self ._write_register_byte (REG_CLICKCFG , click_cfg )
299- self ._write_register_byte (REG_CLICKTHS , 0x80 | threshold )
300- self ._write_register_byte (REG_TIMELIMIT , time_limit )
301- self ._write_register_byte (REG_TIMELATENCY , time_latency )
302- self ._write_register_byte (REG_TIMEWINDOW , time_window )
298+ self ._write_register_byte (_REG_CLICKCFG , click_cfg )
299+ self ._write_register_byte (_REG_CLICKTHS , 0x80 | threshold )
300+ self ._write_register_byte (_REG_TIMELIMIT , time_limit )
301+ self ._write_register_byte (_REG_TIMELATENCY , time_latency )
302+ self ._write_register_byte (_REG_TIMEWINDOW , time_window )
303303
304304 def _read_register_byte (self , register ):
305305 # Read a byte register value and return it.
@@ -360,7 +360,7 @@ def _read_register(self, register, length):
360360 return self ._buffer
361361
362362 def _write_register_byte (self , register , value ):
363- self ._buffer [0 ] = ( register & ( ~ 0x80 & 0xFF )) & 0xFF # Write, bit 7 low.
363+ self ._buffer [0 ] = register & 0x7F # Write, bit 7 low.
364364 self ._buffer [1 ] = value & 0xFF
365365 with self ._spi as spi :
366366 spi .write (self ._buffer , start = 0 , end = 2 )
0 commit comments