@@ -81,11 +81,10 @@ def __init__(self):
81
81
82
82
@property
83
83
def data_rate (self ):
84
- """Get/set the data rate of the accelerometer. Can be DATA_RATE_400_HZ,
85
- DATA_RATE_200_HZ, DATA_RATE_100_HZ, DATA_RATE_50_HZ, DATA_RATE_25_HZ,
86
- DATA_RATE_10_HZ, DATA_RATE_1_HZ, DATA_RATE_POWERDOWN, DATA_RATE_LOWPOWER_1K6HZ,
87
- or DATA_RATE_LOWPOWER_5KHZ.
88
- """
84
+ """The data rate of the accelerometer. Can be DATA_RATE_400_HZ, DATA_RATE_200_HZ,
85
+ DATA_RATE_100_HZ, DATA_RATE_50_HZ, DATA_RATE_25_HZ, DATA_RATE_10_HZ,
86
+ DATA_RATE_1_HZ, DATA_RATE_POWERDOWN, DATA_RATE_LOWPOWER_1K6HZ, or
87
+ DATA_RATE_LOWPOWER_5KHZ."""
89
88
ctl1 = self ._read_register_byte (REG_CTRL1 )
90
89
return (ctl1 >> 4 ) & 0x0F
91
90
@@ -98,9 +97,8 @@ def data_rate(self, rate):
98
97
99
98
@property
100
99
def range (self ):
101
- """Get/set the range of the accelerometer. Can be RANGE_2_G, RANGE_4_G,
102
- RANGE_8_G, or RANGE_16_G.
103
- """
100
+ """The range of the accelerometer. Can be RANGE_2_G, RANGE_4_G, RANGE_8_G, or
101
+ RANGE_16_G."""
104
102
ctl4 = self ._read_register_byte (REG_CTRL4 )
105
103
return (ctl4 >> 4 ) & 0x03
106
104
@@ -183,35 +181,35 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
183
181
# y1 = 900
184
182
return 1800 + (raw + 32512 )* (- 900 / 65024 )
185
183
186
- def read_click_raw (self ):
187
- """Read the raw click register byte value."""
188
- return self ._read_register_byte (REG_CLICKSRC )
189
-
190
- def read_click (self ):
191
- """Read a 2-tuple of bools where the first value is True if a single
192
- click was detected and the second value is True if a double click was
193
- detected.
194
- """
195
- raw = self .read_click_raw ()
196
- return (raw & 0x10 > 0 , raw & 0x20 > 0 )
197
-
198
- def set_click (self , click , threshold , * ,
184
+ @property
185
+ def tapped (self ):
186
+ """True if a tap was detected recently. Whether its a single tap or double tap is
187
+ determined by the tap param on `set_tap`. This may be True over multiple reads
188
+ even if only a single tap or single double tap occurred."""
189
+ raw = self ._read_register_byte (REG_CLICKSRC )
190
+ return raw & 0x40 > 0
191
+
192
+ def set_tap (self , tap , threshold , * ,
199
193
time_limit = 10 , time_latency = 20 , time_window = 255 , click_cfg = None ):
200
- """Set the click detection parameters. Must specify at least:
201
- click - Set to 0 to disable click detection, 1 to detect only single
202
- clicks, and 2 to detect single & double clicks.
203
- threshold - A threshold for the click detection. The higher the value
204
- the less sensitive the detection. This changes based on
205
- the accelerometer range. Good values are 5-10 for 16G,
206
- 10-20 for 8G, 20-40 for 4G, and 40-80 for 2G.
207
- Optionally specify (see datasheet for meaning of these):
208
- time_limit - Time limit register value (default 10).
209
- time_latency - Time latency register value (default 20).
210
- time_window - Time window register value (default 255).
211
- """
212
- if (click < 0 or click > 2 ) and click_cfg is None :
213
- raise ValueError ('Click must be 0 (disabled), 1 (single click), or 2 (double click)!' )
214
- if click == 0 and click_cfg is None :
194
+ """Set the tap detection parameters.
195
+
196
+ .. note:: Tap related registers are called CLICK_ in the datasheet.
197
+
198
+ :param int tap: 0 to disable tap detection, 1 to detect only single
199
+ taps, and 2 to detect only double taps.
200
+ :param int threshold: A threshold for the tap detection. The higher the value
201
+ the less sensitive the detection. This changes based on the accelerometer
202
+ range. Good values are 5-10 for 16G, 10-20 for 8G, 20-40 for 4G, and 40-80 for
203
+ 2G.
204
+ :param int time_limit: TIME_LIMIT register value (default 10).
205
+ :param int time_latency: TIME_LATENCY register value (default 20).
206
+ :param int time_window: TIME_WINDOW register value (default 255).
207
+ :param int click_cfg: CLICK_CFG register value."""
208
+ if (tap < 0 or tap > 2 ) and click_cfg is None :
209
+ raise ValueError ('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!' )
210
+ if threshold > 127 or threshold < 0 :
211
+ raise ValueError ('Threshold out of range (0-127)' )
212
+ if tap == 0 and click_cfg is None :
215
213
# Disable click interrupt.
216
214
r = self ._read_register_byte (REG_CTRL3 )
217
215
r &= ~ (0x80 ) # Turn off I1_CLICK.
@@ -224,9 +222,9 @@ def set_click(self, click, threshold, *,
224
222
if click_cfg is not None :
225
223
# Custom click configuration register value specified, use it.
226
224
self ._write_register_byte (REG_CLICKCFG , click_cfg )
227
- elif click == 1 :
225
+ elif tap == 1 :
228
226
self ._write_register_byte (REG_CLICKCFG , 0x15 ) # Turn on all axes & singletap.
229
- elif click == 2 :
227
+ elif tap == 2 :
230
228
self ._write_register_byte (REG_CLICKCFG , 0x2A ) # Turn on all axes & doubletap.
231
229
self ._write_register_byte (REG_CLICKTHS , threshold )
232
230
self ._write_register_byte (REG_TIMELIMIT , time_limit )
0 commit comments