4545
4646from micropython import const
4747from adafruit_bus_device import i2c_device
48+
4849try :
4950 from struct import unpack
5051except ImportError :
5152 from ustruct import unpack
5253__version__ = "0.0.0-auto.0"
5354__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADXL34x.git"
54- #pylint: disable=bad-whitespace
55- _ADXL345_DEFAULT_ADDRESS = const (0x53 ) # Assumes ALT address pin low
55+ # pylint: disable=bad-whitespace
56+ _ADXL345_DEFAULT_ADDRESS = const (0x53 ) # Assumes ALT address pin low
5657
5758# Conversion factors
58- _ADXL345_MG2G_MULTIPLIER = 0.004 # 4mg per lsb
59- _STANDARD_GRAVITY = 9.80665 # earth standard gravity
60-
61- _REG_DEVID = const (0x00 ) # Device ID
62- _REG_THRESH_TAP = const (0x1D ) # Tap threshold
63- _REG_OFSX = const (0x1E ) # X-axis offset
64- _REG_OFSY = const (0x1F ) # Y-axis offset
65- _REG_OFSZ = const (0x20 ) # Z-axis offset
66- _REG_DUR = const (0x21 ) # Tap duration
67- _REG_LATENT = const (0x22 ) # Tap latency
68- _REG_WINDOW = const (0x23 ) # Tap window
69- _REG_THRESH_ACT = const (0x24 ) # Activity threshold
70- _REG_THRESH_INACT = const (0x25 ) # Inactivity threshold
71- _REG_TIME_INACT = const (0x26 ) # Inactivity time
72- _REG_ACT_INACT_CTL = const (0x27 ) # Axis enable control for [in]activity detection
73- _REG_THRESH_FF = const (0x28 ) # Free-fall threshold
74- _REG_TIME_FF = const (0x29 ) # Free-fall time
75- _REG_TAP_AXES = const (0x2A ) # Axis control for single/double tap
76- _REG_ACT_TAP_STATUS = const (0x2B ) # Source for single/double tap
77- _REG_BW_RATE = const (0x2C ) # Data rate and power mode control
78- _REG_POWER_CTL = const (0x2D ) # Power-saving features control
79- _REG_INT_ENABLE = const (0x2E ) # Interrupt enable control
80- _REG_INT_MAP = const (0x2F ) # Interrupt mapping control
81- _REG_INT_SOURCE = const (0x30 ) # Source of interrupts
82- _REG_DATA_FORMAT = const (0x31 ) # Data format control
83- _REG_DATAX0 = const (0x32 ) # X-axis data 0
84- _REG_DATAX1 = const (0x33 ) # X-axis data 1
85- _REG_DATAY0 = const (0x34 ) # Y-axis data 0
86- _REG_DATAY1 = const (0x35 ) # Y-axis data 1
87- _REG_DATAZ0 = const (0x36 ) # Z-axis data 0
88- _REG_DATAZ1 = const (0x37 ) # Z-axis data 1
89- _REG_FIFO_CTL = const (0x38 ) # FIFO control
90- _REG_FIFO_STATUS = const (0x39 ) # FIFO status
91- _INT_SINGLE_TAP = const (0b01000000 ) # SINGLE_TAP bit
92- _INT_DOUBLE_TAP = const (0b00100000 ) # DOUBLE_TAP bit
93- _INT_ACT = const (0b00010000 ) # ACT bit
94- _INT_INACT = const (0b00001000 ) # INACT bit
95- _INT_FREE_FALL = const (0b00000100 ) # FREE_FALL bit
96-
97- class DataRate : #pylint: disable=too-few-public-methods
59+ _ADXL345_MG2G_MULTIPLIER = 0.004 # 4mg per lsb
60+ _STANDARD_GRAVITY = 9.80665 # earth standard gravity
61+
62+ _REG_DEVID = const (0x00 ) # Device ID
63+ _REG_THRESH_TAP = const (0x1D ) # Tap threshold
64+ _REG_OFSX = const (0x1E ) # X-axis offset
65+ _REG_OFSY = const (0x1F ) # Y-axis offset
66+ _REG_OFSZ = const (0x20 ) # Z-axis offset
67+ _REG_DUR = const (0x21 ) # Tap duration
68+ _REG_LATENT = const (0x22 ) # Tap latency
69+ _REG_WINDOW = const (0x23 ) # Tap window
70+ _REG_THRESH_ACT = const (0x24 ) # Activity threshold
71+ _REG_THRESH_INACT = const (0x25 ) # Inactivity threshold
72+ _REG_TIME_INACT = const (0x26 ) # Inactivity time
73+ _REG_ACT_INACT_CTL = const (0x27 ) # Axis enable control for [in]activity detection
74+ _REG_THRESH_FF = const (0x28 ) # Free-fall threshold
75+ _REG_TIME_FF = const (0x29 ) # Free-fall time
76+ _REG_TAP_AXES = const (0x2A ) # Axis control for single/double tap
77+ _REG_ACT_TAP_STATUS = const (0x2B ) # Source for single/double tap
78+ _REG_BW_RATE = const (0x2C ) # Data rate and power mode control
79+ _REG_POWER_CTL = const (0x2D ) # Power-saving features control
80+ _REG_INT_ENABLE = const (0x2E ) # Interrupt enable control
81+ _REG_INT_MAP = const (0x2F ) # Interrupt mapping control
82+ _REG_INT_SOURCE = const (0x30 ) # Source of interrupts
83+ _REG_DATA_FORMAT = const (0x31 ) # Data format control
84+ _REG_DATAX0 = const (0x32 ) # X-axis data 0
85+ _REG_DATAX1 = const (0x33 ) # X-axis data 1
86+ _REG_DATAY0 = const (0x34 ) # Y-axis data 0
87+ _REG_DATAY1 = const (0x35 ) # Y-axis data 1
88+ _REG_DATAZ0 = const (0x36 ) # Z-axis data 0
89+ _REG_DATAZ1 = const (0x37 ) # Z-axis data 1
90+ _REG_FIFO_CTL = const (0x38 ) # FIFO control
91+ _REG_FIFO_STATUS = const (0x39 ) # FIFO status
92+ _INT_SINGLE_TAP = const (0b01000000 ) # SINGLE_TAP bit
93+ _INT_DOUBLE_TAP = const (0b00100000 ) # DOUBLE_TAP bit
94+ _INT_ACT = const (0b00010000 ) # ACT bit
95+ _INT_INACT = const (0b00001000 ) # INACT bit
96+ _INT_FREE_FALL = const (0b00000100 ) # FREE_FALL bit
97+
98+
99+ class DataRate : # pylint: disable=too-few-public-methods
98100 """An enum-like class representing the possible data rates.
99101 Possible values are
100102
@@ -116,25 +118,26 @@ class DataRate: #pylint: disable=too-few-public-methods
116118 - ``DataRate.RATE_0_10_HZ``
117119
118120 """
119- RATE_3200_HZ = const (0b1111 ) # 1600Hz Bandwidth 140mA IDD
120- RATE_1600_HZ = const (0b1110 ) # 800Hz Bandwidth 90mA IDD
121- RATE_800_HZ = const (0b1101 ) # 400Hz Bandwidth 140mA IDD
122- RATE_400_HZ = const (0b1100 ) # 200Hz Bandwidth 140mA IDD
123- RATE_200_HZ = const (0b1011 ) # 100Hz Bandwidth 140mA IDD
124- RATE_100_HZ = const (0b1010 ) # 50Hz Bandwidth 140mA IDD
125- RATE_50_HZ = const (0b1001 ) # 25Hz Bandwidth 90mA IDD
126- RATE_25_HZ = const (0b1000 ) # 12.5Hz Bandwidth 60mA IDD
127- RATE_12_5_HZ = const (0b0111 ) # 6.25Hz Bandwidth 50mA IDD
128- RATE_6_25HZ = const (0b0110 ) # 3.13Hz Bandwidth 45mA IDD
129- RATE_3_13_HZ = const (0b0101 ) # 1.56Hz Bandwidth 40mA IDD
130- RATE_1_56_HZ = const (0b0100 ) # 0.78Hz Bandwidth 34mA IDD
131- RATE_0_78_HZ = const (0b0011 ) # 0.39Hz Bandwidth 23mA IDD
132- RATE_0_39_HZ = const (0b0010 ) # 0.20Hz Bandwidth 23mA IDD
133- RATE_0_20_HZ = const (0b0001 ) # 0.10Hz Bandwidth 23mA IDD
134- RATE_0_10_HZ = const (0b0000 ) # 0.05Hz Bandwidth 23mA IDD (default value)
135-
136-
137- class Range : #pylint: disable=too-few-public-methods
121+
122+ RATE_3200_HZ = const (0b1111 ) # 1600Hz Bandwidth 140mA IDD
123+ RATE_1600_HZ = const (0b1110 ) # 800Hz Bandwidth 90mA IDD
124+ RATE_800_HZ = const (0b1101 ) # 400Hz Bandwidth 140mA IDD
125+ RATE_400_HZ = const (0b1100 ) # 200Hz Bandwidth 140mA IDD
126+ RATE_200_HZ = const (0b1011 ) # 100Hz Bandwidth 140mA IDD
127+ RATE_100_HZ = const (0b1010 ) # 50Hz Bandwidth 140mA IDD
128+ RATE_50_HZ = const (0b1001 ) # 25Hz Bandwidth 90mA IDD
129+ RATE_25_HZ = const (0b1000 ) # 12.5Hz Bandwidth 60mA IDD
130+ RATE_12_5_HZ = const (0b0111 ) # 6.25Hz Bandwidth 50mA IDD
131+ RATE_6_25HZ = const (0b0110 ) # 3.13Hz Bandwidth 45mA IDD
132+ RATE_3_13_HZ = const (0b0101 ) # 1.56Hz Bandwidth 40mA IDD
133+ RATE_1_56_HZ = const (0b0100 ) # 0.78Hz Bandwidth 34mA IDD
134+ RATE_0_78_HZ = const (0b0011 ) # 0.39Hz Bandwidth 23mA IDD
135+ RATE_0_39_HZ = const (0b0010 ) # 0.20Hz Bandwidth 23mA IDD
136+ RATE_0_20_HZ = const (0b0001 ) # 0.10Hz Bandwidth 23mA IDD
137+ RATE_0_10_HZ = const (0b0000 ) # 0.05Hz Bandwidth 23mA IDD (default value)
138+
139+
140+ class Range : # pylint: disable=too-few-public-methods
138141 """An enum-like class representing the possible measurement ranges in +/- G.
139142
140143 Possible values are
@@ -145,10 +148,12 @@ class Range: #pylint: disable=too-few-public-methods
145148 - ``Range.RANGE_2_G``
146149
147150 """
148- RANGE_16_G = const (0b11 ) # +/- 16g
149- RANGE_8_G = const (0b10 ) # +/- 8g
150- RANGE_4_G = const (0b01 ) # +/- 4g
151- RANGE_2_G = const (0b00 ) # +/- 2g (default value)
151+
152+ RANGE_16_G = const (0b11 ) # +/- 16g
153+ RANGE_8_G = const (0b10 ) # +/- 8g
154+ RANGE_4_G = const (0b01 ) # +/- 4g
155+ RANGE_2_G = const (0b00 ) # +/- 2g (default value)
156+
152157
153158# pylint: enable=bad-whitespace
154159class ADXL345 :
@@ -158,8 +163,8 @@ class ADXL345:
158163 :param address: The I2C device address for the sensor. Default is ``0x53``.
159164
160165 """
161- def __init__ (self , i2c , address = _ADXL345_DEFAULT_ADDRESS ):
162166
167+ def __init__ (self , i2c , address = _ADXL345_DEFAULT_ADDRESS ):
163168
164169 self ._i2c = i2c_device .I2CDevice (i2c , address )
165170 self ._buffer = bytearray (6 )
@@ -173,7 +178,7 @@ def __init__(self, i2c, address=_ADXL345_DEFAULT_ADDRESS):
173178 @property
174179 def acceleration (self ):
175180 """The x, y, z acceleration values returned in a 3-tuple in m / s ^ 2."""
176- x , y , z = unpack (' <hhh' , self ._read_register (_REG_DATAX0 , 6 ))
181+ x , y , z = unpack (" <hhh" , self ._read_register (_REG_DATAX0 , 6 ))
177182 x = x * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
178183 y = y * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
179184 z = z * _ADXL345_MG2G_MULTIPLIER * _STANDARD_GRAVITY
@@ -207,14 +212,22 @@ def events(self):
207212
208213 for event_type , value in self ._enabled_interrupts .items ():
209214 if event_type == "motion" :
210- self ._event_status [event_type ] = interrupt_source_register & _INT_ACT > 0
215+ self ._event_status [event_type ] = (
216+ interrupt_source_register & _INT_ACT > 0
217+ )
211218 if event_type == "tap" :
212219 if value == 1 :
213- self ._event_status [event_type ] = interrupt_source_register & _INT_SINGLE_TAP > 0
220+ self ._event_status [event_type ] = (
221+ interrupt_source_register & _INT_SINGLE_TAP > 0
222+ )
214223 else :
215- self ._event_status [event_type ] = interrupt_source_register & _INT_DOUBLE_TAP > 0
224+ self ._event_status [event_type ] = (
225+ interrupt_source_register & _INT_DOUBLE_TAP > 0
226+ )
216227 if event_type == "freefall" :
217- self ._event_status [event_type ] = interrupt_source_register & _INT_FREE_FALL > 0
228+ self ._event_status [event_type ] = (
229+ interrupt_source_register & _INT_FREE_FALL > 0
230+ )
218231
219232 return self ._event_status
220233
@@ -233,11 +246,12 @@ def enable_motion_detection(self, *, threshold=18):
233246 """
234247 active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
235248
236-
237- self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
238- self ._write_register_byte (_REG_ACT_INACT_CTL , 0b01110000 ) # enable activity on X,Y,Z
249+ self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
250+ self ._write_register_byte (
251+ _REG_ACT_INACT_CTL , 0b01110000
252+ ) # enable activity on X,Y,Z
239253 self ._write_register_byte (_REG_THRESH_ACT , threshold )
240- self ._write_register_byte (_REG_INT_ENABLE , _INT_ACT ) # Inactive interrupt only
254+ self ._write_register_byte (_REG_INT_ENABLE , _INT_ACT ) # Inactive interrupt only
241255
242256 active_interrupts |= _INT_ACT
243257 self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
@@ -252,7 +266,6 @@ def disable_motion_detection(self):
252266 self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
253267 self ._enabled_interrupts .pop ("motion" )
254268
255-
256269 def enable_freefall_detection (self , * , threshold = 10 , time = 25 ):
257270 """
258271 Freefall detection parameters:
@@ -273,7 +286,7 @@ def enable_freefall_detection(self, *, threshold=10, time=25):
273286
274287 active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
275288
276- self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
289+ self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
277290 self ._write_register_byte (_REG_THRESH_FF , threshold )
278291 self ._write_register_byte (_REG_TIME_FF , time )
279292
@@ -289,7 +302,9 @@ def disable_freefall_detection(self):
289302 self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
290303 self ._enabled_interrupts .pop ("freefall" )
291304
292- def enable_tap_detection (self , * , tap_count = 1 , threshold = 20 , duration = 50 , latency = 20 , window = 255 ):#pylint: disable=line-too-long
305+ def enable_tap_detection (
306+ self , * , tap_count = 1 , threshold = 20 , duration = 50 , latency = 20 , window = 255
307+ ): # pylint: disable=line-too-long
293308 """
294309 The tap detection parameters.
295310
@@ -317,8 +332,10 @@ def enable_tap_detection(self, *, tap_count=1, threshold=20, duration=50, latenc
317332 """
318333 active_interrupts = self ._read_register_unpacked (_REG_INT_ENABLE )
319334
320- self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
321- self ._write_register_byte (_REG_TAP_AXES , 0b00000111 ) # enable X, Y, Z axes for tap
335+ self ._write_register_byte (_REG_INT_ENABLE , 0x0 ) # disable interrupts for setup
336+ self ._write_register_byte (
337+ _REG_TAP_AXES , 0b00000111
338+ ) # enable X, Y, Z axes for tap
322339 self ._write_register_byte (_REG_THRESH_TAP , threshold )
323340 self ._write_register_byte (_REG_DUR , duration )
324341
@@ -334,7 +351,9 @@ def enable_tap_detection(self, *, tap_count=1, threshold=20, duration=50, latenc
334351 self ._write_register_byte (_REG_INT_ENABLE , active_interrupts )
335352 self ._enabled_interrupts ["tap" ] = 2
336353 else :
337- raise ValueError ("tap must be 0 to disable, 1 for single tap, or 2 for double tap" )
354+ raise ValueError (
355+ "tap must be 0 to disable, 1 for single tap, or 2 for double tap"
356+ )
338357
339358 def disable_tap_detection (self ):
340359 "Disable tap detection"
@@ -394,6 +413,7 @@ def _write_register_byte(self, register, value):
394413 with self ._i2c as i2c :
395414 i2c .write (self ._buffer , start = 0 , end = 2 )
396415
416+
397417class ADXL343 (ADXL345 ):
398418 """
399419 Stub class for the ADXL343 3-axis accelerometer
0 commit comments