Skip to content

Commit f978a65

Browse files
committed
Change Pause class to use seconds
#9 (comment)
1 parent 7f00c4c commit f978a65

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

adafruit_drv2605.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,9 @@ def __repr__(self):
267267

268268

269269
class Pause:
270-
"""Class for adding a pause to the DRV2605 waveform sequence.
271-
Duration is specified in tens of milliseconds, as per page 35
272-
of the datasheet. I.e. Pause time = 10ms * duration
273-
"""
270+
"""DRV2605 waveform sequence timed delay."""
274271
def __init__(self, duration):
272+
# Bit 7 must be set for a slot to be interpreted as a delay
275273
self._duration = 0x80
276274
self.duration = duration
277275

@@ -282,15 +280,17 @@ def raw_value(self):
282280

283281
@property
284282
def duration(self):
285-
"""Return the pause duration."""
286-
return self._duration & 0x7f
283+
"""Pause duration in seconds."""
284+
# Remove wait time flag bit and convert duration to seconds
285+
return (self._duration & 0x7f) / 100.0
287286

288287
@duration.setter
289288
def duration(self, duration):
290-
"""Set the pause duration."""
291-
if not 0 <= duration <= 127:
292-
raise ValueError('Pause duration must be a value within 0-127!')
293-
self._duration = 0x80 | duration
289+
"""Set the pause duration in seconds."""
290+
if not 0.0 <= duration <= 1.27:
291+
raise ValueError('Pause duration must be a value within 0.0-1.27!')
292+
# Add wait time flag bit and convert duration to centiseconds
293+
self._duration = 0x80 | round(duration * 100.0)
294294

295295
def __repr__(self):
296296
return "{}({})".format(type(self).__qualname__, self.duration)
@@ -318,7 +318,7 @@ def __getitem__(self, slot):
318318
# pylint: disable=protected-access
319319
slot_contents = self._drv2605._read_u8(_DRV2605_REG_WAVESEQ1 + slot)
320320
if slot_contents & 0x80:
321-
return Pause(slot_contents & 0x7f)
321+
return Pause((slot_contents & 0x7f) / 100.0)
322322
return Effect(slot_contents)
323323

324324
def __iter__(self):

examples/drv2605_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# them in interesting ways. Index the sequence property with a
2525
# slot number 0 to 6.
2626
# Optionally, you can assign a pause to a slot. E.g.
27-
# drv.sequence[1] = adafruit_drv2605.Pause(5) # Pause for 50 milliseconds
27+
# drv.sequence[1] = adafruit_drv2605.Pause(0.5) # Pause for half a second
2828
drv.play() # Play the effect.
2929
time.sleep(0.5)
3030
# Increment effect ID and wrap back around to 1.

0 commit comments

Comments
 (0)