49
49
50
50
# Register addresses:
51
51
# 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 )
67
67
68
68
# Register value constants:
69
69
RANGE_16_G = const (0b11 ) # +/- 16g
@@ -94,22 +94,22 @@ class LIS3DH:
94
94
"""Driver base for the LIS3DH accelerometer."""
95
95
def __init__ (self , int1 = None , int2 = None ):
96
96
# Check device ID.
97
- device_id = self ._read_register_byte (REG_WHOAMI )
97
+ device_id = self ._read_register_byte (_REG_WHOAMI )
98
98
if device_id != 0x33 :
99
99
raise RuntimeError ('Failed to find LIS3DH!' )
100
100
# Reboot
101
- self ._write_register_byte (REG_CTRL5 , 0x80 )
101
+ self ._write_register_byte (_REG_CTRL5 , 0x80 )
102
102
time .sleep (0.01 ) # takes 5ms
103
103
# Enable all axes, normal mode.
104
- self ._write_register_byte (REG_CTRL1 , 0x07 )
104
+ self ._write_register_byte (_REG_CTRL1 , 0x07 )
105
105
# Set 400Hz data rate.
106
106
self .data_rate = DATARATE_400_HZ
107
107
# High res & BDU enabled.
108
- self ._write_register_byte (REG_CTRL4 , 0x88 )
108
+ self ._write_register_byte (_REG_CTRL4 , 0x88 )
109
109
# Enable ADCs.
110
- self ._write_register_byte (REG_TEMPCFG , 0x80 )
110
+ self ._write_register_byte (_REG_TEMPCFG , 0x80 )
111
111
# Latch interrupt for INT1
112
- self ._write_register_byte (REG_CTRL5 , 0x08 )
112
+ self ._write_register_byte (_REG_CTRL5 , 0x08 )
113
113
114
114
# Initialise interrupt pins
115
115
self ._int1 = int1
@@ -124,29 +124,29 @@ def data_rate(self):
124
124
DATA_RATE_100_HZ, DATA_RATE_50_HZ, DATA_RATE_25_HZ, DATA_RATE_10_HZ,
125
125
DATA_RATE_1_HZ, DATA_RATE_POWERDOWN, DATA_RATE_LOWPOWER_1K6HZ, or
126
126
DATA_RATE_LOWPOWER_5KHZ."""
127
- ctl1 = self ._read_register_byte (REG_CTRL1 )
127
+ ctl1 = self ._read_register_byte (_REG_CTRL1 )
128
128
return (ctl1 >> 4 ) & 0x0F
129
129
130
130
@data_rate .setter
131
131
def data_rate (self , rate ):
132
- ctl1 = self ._read_register_byte (REG_CTRL1 )
132
+ ctl1 = self ._read_register_byte (_REG_CTRL1 )
133
133
ctl1 &= ~ (0xF0 )
134
134
ctl1 |= rate << 4
135
- self ._write_register_byte (REG_CTRL1 , ctl1 )
135
+ self ._write_register_byte (_REG_CTRL1 , ctl1 )
136
136
137
137
@property
138
138
def range (self ):
139
139
"""The range of the accelerometer. Can be RANGE_2_G, RANGE_4_G, RANGE_8_G, or
140
140
RANGE_16_G."""
141
- ctl4 = self ._read_register_byte (REG_CTRL4 )
141
+ ctl4 = self ._read_register_byte (_REG_CTRL4 )
142
142
return (ctl4 >> 4 ) & 0x03
143
143
144
144
@range .setter
145
145
def range (self , range_value ):
146
- ctl4 = self ._read_register_byte (REG_CTRL4 )
146
+ ctl4 = self ._read_register_byte (_REG_CTRL4 )
147
147
ctl4 &= ~ 0x30
148
148
ctl4 |= range_value << 4
149
- self ._write_register_byte (REG_CTRL4 , ctl4 )
149
+ self ._write_register_byte (_REG_CTRL4 , ctl4 )
150
150
151
151
@property
152
152
def acceleration (self ):
@@ -162,7 +162,7 @@ def acceleration(self):
162
162
elif accel_range == RANGE_2_G :
163
163
divider = 16380
164
164
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 ))
166
166
167
167
# convert from Gs to m / s ^ 2 and adjust for the range
168
168
x = (x / divider ) * STANDARD_GRAVITY
@@ -209,7 +209,7 @@ def read_adc_raw(self, adc):
209
209
if adc < 1 or adc > 3 :
210
210
raise ValueError ('ADC must be a value 1 to 3!' )
211
211
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 ]
213
213
214
214
def read_adc_mV (self , adc ): # pylint: disable=invalid-name
215
215
"""Read the specified analog to digital converter value in millivolts.
@@ -252,7 +252,7 @@ def tapped(self):
252
252
"""
253
253
if self ._int1 and not self ._int1 .value :
254
254
return False
255
- raw = self ._read_register_byte (REG_CLICKSRC )
255
+ raw = self ._read_register_byte (_REG_CLICKSRC )
256
256
return raw & 0x40 > 0
257
257
258
258
def set_tap (self , tap , threshold , * ,
@@ -280,26 +280,26 @@ def set_tap(self, tap, threshold, *,
280
280
if threshold > 127 or threshold < 0 :
281
281
raise ValueError ('Threshold out of range (0-127)' )
282
282
283
- ctrl3 = self ._read_register_byte (REG_CTRL3 )
283
+ ctrl3 = self ._read_register_byte (_REG_CTRL3 )
284
284
if tap == 0 and click_cfg is None :
285
285
# 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 )
288
288
return
289
289
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
291
291
292
292
if click_cfg is None :
293
293
if tap == 1 :
294
294
click_cfg = 0x15 # Turn on all axes & singletap.
295
295
if tap == 2 :
296
296
click_cfg = 0x2A # Turn on all axes & doubletap.
297
297
# 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 )
303
303
304
304
def _read_register_byte (self , register ):
305
305
# Read a byte register value and return it.
@@ -360,7 +360,7 @@ def _read_register(self, register, length):
360
360
return self ._buffer
361
361
362
362
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.
364
364
self ._buffer [1 ] = value & 0xFF
365
365
with self ._spi as spi :
366
366
spi .write (self ._buffer , start = 0 , end = 2 )
0 commit comments