38
38
39
39
# pylint: disable=bad-whitespace
40
40
# Internal constants:
41
- _DRV2605_ADDR = const (0x5A )
42
- _DRV2605_REG_STATUS = const (0x00 )
43
- _DRV2605_REG_MODE = const (0x01 )
44
- _DRV2605_REG_RTPIN = const (0x02 )
45
- _DRV2605_REG_LIBRARY = const (0x03 )
46
- _DRV2605_REG_WAVESEQ1 = const (0x04 )
47
- _DRV2605_REG_WAVESEQ2 = const (0x05 )
48
- _DRV2605_REG_WAVESEQ3 = const (0x06 )
49
- _DRV2605_REG_WAVESEQ4 = const (0x07 )
50
- _DRV2605_REG_WAVESEQ5 = const (0x08 )
51
- _DRV2605_REG_WAVESEQ6 = const (0x09 )
52
- _DRV2605_REG_WAVESEQ7 = const (0x0A )
53
- _DRV2605_REG_WAVESEQ8 = const (0x0B )
54
- _DRV2605_REG_GO = const (0x0C )
55
- _DRV2605_REG_OVERDRIVE = const (0x0D )
56
- _DRV2605_REG_SUSTAINPOS = const (0x0E )
57
- _DRV2605_REG_SUSTAINNEG = const (0x0F )
58
- _DRV2605_REG_BREAK = const (0x10 )
59
- _DRV2605_REG_AUDIOCTRL = const (0x11 )
60
- _DRV2605_REG_AUDIOLVL = const (0x12 )
61
- _DRV2605_REG_AUDIOMAX = const (0x13 )
62
- _DRV2605_REG_RATEDV = const (0x16 )
63
- _DRV2605_REG_CLAMPV = const (0x17 )
64
- _DRV2605_REG_AUTOCALCOMP = const (0x18 )
65
- _DRV2605_REG_AUTOCALEMP = const (0x19 )
66
- _DRV2605_REG_FEEDBACK = const (0x1A )
67
- _DRV2605_REG_CONTROL1 = const (0x1B )
68
- _DRV2605_REG_CONTROL2 = const (0x1C )
69
- _DRV2605_REG_CONTROL3 = const (0x1D )
70
- _DRV2605_REG_CONTROL4 = const (0x1E )
71
- _DRV2605_REG_VBAT = const (0x21 )
72
- _DRV2605_REG_LRARESON = const (0x22 )
41
+ _DRV2605_ADDR = const (0x5A )
42
+ _DRV2605_REG_STATUS = const (0x00 )
43
+ _DRV2605_REG_MODE = const (0x01 )
44
+ _DRV2605_REG_RTPIN = const (0x02 )
45
+ _DRV2605_REG_LIBRARY = const (0x03 )
46
+ _DRV2605_REG_WAVESEQ1 = const (0x04 )
47
+ _DRV2605_REG_WAVESEQ2 = const (0x05 )
48
+ _DRV2605_REG_WAVESEQ3 = const (0x06 )
49
+ _DRV2605_REG_WAVESEQ4 = const (0x07 )
50
+ _DRV2605_REG_WAVESEQ5 = const (0x08 )
51
+ _DRV2605_REG_WAVESEQ6 = const (0x09 )
52
+ _DRV2605_REG_WAVESEQ7 = const (0x0A )
53
+ _DRV2605_REG_WAVESEQ8 = const (0x0B )
54
+ _DRV2605_REG_GO = const (0x0C )
55
+ _DRV2605_REG_OVERDRIVE = const (0x0D )
56
+ _DRV2605_REG_SUSTAINPOS = const (0x0E )
57
+ _DRV2605_REG_SUSTAINNEG = const (0x0F )
58
+ _DRV2605_REG_BREAK = const (0x10 )
59
+ _DRV2605_REG_AUDIOCTRL = const (0x11 )
60
+ _DRV2605_REG_AUDIOLVL = const (0x12 )
61
+ _DRV2605_REG_AUDIOMAX = const (0x13 )
62
+ _DRV2605_REG_RATEDV = const (0x16 )
63
+ _DRV2605_REG_CLAMPV = const (0x17 )
64
+ _DRV2605_REG_AUTOCALCOMP = const (0x18 )
65
+ _DRV2605_REG_AUTOCALEMP = const (0x19 )
66
+ _DRV2605_REG_FEEDBACK = const (0x1A )
67
+ _DRV2605_REG_CONTROL1 = const (0x1B )
68
+ _DRV2605_REG_CONTROL2 = const (0x1C )
69
+ _DRV2605_REG_CONTROL3 = const (0x1D )
70
+ _DRV2605_REG_CONTROL4 = const (0x1E )
71
+ _DRV2605_REG_VBAT = const (0x21 )
72
+ _DRV2605_REG_LRARESON = const (0x22 )
73
73
74
74
# User-facing mode value constants:
75
- MODE_INTTRIG = 0x00
76
- MODE_EXTTRIGEDGE = 0x01
77
- MODE_EXTTRIGLVL = 0x02
78
- MODE_PWMANALOG = 0x03
79
- MODE_AUDIOVIBE = 0x04
80
- MODE_REALTIME = 0x05
81
- MODE_DIAGNOS = 0x06
82
- MODE_AUTOCAL = 0x07
83
- LIBRARY_EMPTY = 0x00
84
- LIBRARY_TS2200A = 0x01
85
- LIBRARY_TS2200B = 0x02
86
- LIBRARY_TS2200C = 0x03
87
- LIBRARY_TS2200D = 0x04
88
- LIBRARY_TS2200E = 0x05
89
- LIBRARY_LRA = 0x06
75
+ MODE_INTTRIG = 0x00
76
+ MODE_EXTTRIGEDGE = 0x01
77
+ MODE_EXTTRIGLVL = 0x02
78
+ MODE_PWMANALOG = 0x03
79
+ MODE_AUDIOVIBE = 0x04
80
+ MODE_REALTIME = 0x05
81
+ MODE_DIAGNOS = 0x06
82
+ MODE_AUTOCAL = 0x07
83
+ LIBRARY_EMPTY = 0x00
84
+ LIBRARY_TS2200A = 0x01
85
+ LIBRARY_TS2200B = 0x02
86
+ LIBRARY_TS2200C = 0x03
87
+ LIBRARY_TS2200D = 0x04
88
+ LIBRARY_TS2200E = 0x05
89
+ LIBRARY_LRA = 0x06
90
90
# pylint: enable=bad-whitespace
91
91
92
+
92
93
class DRV2605 :
93
94
"""TI DRV2605 haptic feedback motor driver module."""
94
95
@@ -103,13 +104,13 @@ def __init__(self, i2c, address=_DRV2605_ADDR):
103
104
status = self ._read_u8 (_DRV2605_REG_STATUS )
104
105
device_id = (status >> 5 ) & 0x07
105
106
if device_id not in (3 , 7 ):
106
- raise RuntimeError (' Failed to find DRV2605, check wiring!' )
107
+ raise RuntimeError (" Failed to find DRV2605, check wiring!" )
107
108
# Configure registers to initialize chip.
108
- self ._write_u8 (_DRV2605_REG_MODE , 0x00 ) # out of standby
109
- self ._write_u8 (_DRV2605_REG_RTPIN , 0x00 ) # no real-time-playback
110
- self ._write_u8 (_DRV2605_REG_WAVESEQ1 , 1 ) # strong click
109
+ self ._write_u8 (_DRV2605_REG_MODE , 0x00 ) # out of standby
110
+ self ._write_u8 (_DRV2605_REG_RTPIN , 0x00 ) # no real-time-playback
111
+ self ._write_u8 (_DRV2605_REG_WAVESEQ1 , 1 ) # strong click
111
112
self ._write_u8 (_DRV2605_REG_WAVESEQ2 , 0 )
112
- self ._write_u8 (_DRV2605_REG_OVERDRIVE , 0 ) # no overdrive
113
+ self ._write_u8 (_DRV2605_REG_OVERDRIVE , 0 ) # no overdrive
113
114
self ._write_u8 (_DRV2605_REG_SUSTAINPOS , 0 )
114
115
self ._write_u8 (_DRV2605_REG_SUSTAINNEG , 0 )
115
116
self ._write_u8 (_DRV2605_REG_BREAK , 0 )
@@ -168,7 +169,7 @@ def mode(self):
168
169
@mode .setter
169
170
def mode (self , val ):
170
171
if not 0 <= val <= 7 :
171
- raise ValueError (' Mode must be a value within 0-7!' )
172
+ raise ValueError (" Mode must be a value within 0-7!" )
172
173
self ._write_u8 (_DRV2605_REG_MODE , val )
173
174
174
175
@property
@@ -193,7 +194,7 @@ def library(self):
193
194
@library .setter
194
195
def library (self , val ):
195
196
if not 0 <= val <= 6 :
196
- raise ValueError (' Library must be a value within 0-6!' )
197
+ raise ValueError (" Library must be a value within 0-6!" )
197
198
self ._write_u8 (_DRV2605_REG_LIBRARY , val )
198
199
199
200
@property
@@ -215,9 +216,9 @@ def set_waveform(self, effect_id, slot=0):
215
216
waveform / effect.
216
217
"""
217
218
if not 0 <= effect_id <= 123 :
218
- raise ValueError (' Effect ID must be a value within 0-123!' )
219
+ raise ValueError (" Effect ID must be a value within 0-123!" )
219
220
if not 0 <= slot <= 6 :
220
- raise ValueError (' Slot must be a value within 0-6!' )
221
+ raise ValueError (" Slot must be a value within 0-6!" )
221
222
self ._write_u8 (_DRV2605_REG_WAVESEQ1 + slot , effect_id )
222
223
223
224
# pylint: disable=invalid-name
@@ -233,9 +234,9 @@ def use_LRM(self):
233
234
self ._write_u8 (_DRV2605_REG_FEEDBACK , feedback | 0x80 )
234
235
235
236
236
-
237
237
class Effect :
238
238
"""DRV2605 waveform sequence effect."""
239
+
239
240
def __init__ (self , effect_id ):
240
241
self ._effect_id = 0
241
242
# pylint: disable=invalid-name
@@ -257,16 +258,16 @@ def id(self):
257
258
def id (self , effect_id ):
258
259
"""Set the effect ID."""
259
260
if not 0 <= effect_id <= 123 :
260
- raise ValueError (' Effect ID must be a value within 0-123!' )
261
+ raise ValueError (" Effect ID must be a value within 0-123!" )
261
262
self ._effect_id = effect_id
262
263
263
264
def __repr__ (self ):
264
265
return "{}({})" .format (type (self ).__qualname__ , self .id )
265
266
266
267
267
-
268
268
class Pause :
269
269
"""DRV2605 waveform sequence timed delay."""
270
+
270
271
def __init__ (self , duration ):
271
272
# Bit 7 must be set for a slot to be interpreted as a delay
272
273
self ._duration = 0x80
@@ -281,43 +282,43 @@ def raw_value(self):
281
282
def duration (self ):
282
283
"""Pause duration in seconds."""
283
284
# Remove wait time flag bit and convert duration to seconds
284
- return (self ._duration & 0x7f ) / 100.0
285
+ return (self ._duration & 0x7F ) / 100.0
285
286
286
287
@duration .setter
287
288
def duration (self , duration ):
288
289
"""Set the pause duration in seconds."""
289
290
if not 0.0 <= duration <= 1.27 :
290
- raise ValueError (' Pause duration must be a value within 0.0-1.27!' )
291
+ raise ValueError (" Pause duration must be a value within 0.0-1.27!" )
291
292
# Add wait time flag bit and convert duration to centiseconds
292
293
self ._duration = 0x80 | round (duration * 100.0 )
293
294
294
295
def __repr__ (self ):
295
296
return "{}({})" .format (type (self ).__qualname__ , self .duration )
296
297
297
298
298
-
299
299
class _DRV2605_Sequence :
300
300
"""Class to enable List-like indexing of the waveform sequence slots."""
301
+
301
302
def __init__ (self , DRV2605_instance ):
302
303
self ._drv2605 = DRV2605_instance
303
304
304
305
def __setitem__ (self , slot , effect ):
305
306
"""Write an Effect or Pause to a slot."""
306
307
if not 0 <= slot <= 6 :
307
- raise IndexError (' Slot must be a value within 0-6!' )
308
+ raise IndexError (" Slot must be a value within 0-6!" )
308
309
if not isinstance (effect , (Effect , Pause )):
309
- raise TypeError (' Effect must be either an Effect() or Pause()!' )
310
+ raise TypeError (" Effect must be either an Effect() or Pause()!" )
310
311
# pylint: disable=protected-access
311
312
self ._drv2605 ._write_u8 (_DRV2605_REG_WAVESEQ1 + slot , effect .raw_value )
312
313
313
314
def __getitem__ (self , slot ):
314
315
"""Read an effect ID from a slot. Returns either a Pause or Effect class."""
315
316
if not 0 <= slot <= 6 :
316
- raise IndexError (' Slot must be a value within 0-6!' )
317
+ raise IndexError (" Slot must be a value within 0-6!" )
317
318
# pylint: disable=protected-access
318
319
slot_contents = self ._drv2605 ._read_u8 (_DRV2605_REG_WAVESEQ1 + slot )
319
320
if slot_contents & 0x80 :
320
- return Pause ((slot_contents & 0x7f ) / 100.0 )
321
+ return Pause ((slot_contents & 0x7F ) / 100.0 )
321
322
return Effect (slot_contents )
322
323
323
324
def __iter__ (self ):
@@ -327,4 +328,4 @@ def __iter__(self):
327
328
328
329
def __repr__ (self ):
329
330
"""Return a string representation of all slot's effects."""
330
- return repr ([ effect for effect in self ] )
331
+ return repr (list ( self ) )
0 commit comments