Skip to content

Commit

Permalink
Change Pause class to use seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
process1183 committed Nov 2, 2018
1 parent 7f00c4c commit f978a65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions adafruit_drv2605.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,9 @@ def __repr__(self):


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

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

@property
def duration(self):
"""Return the pause duration."""
return self._duration & 0x7f
"""Pause duration in seconds."""
# Remove wait time flag bit and convert duration to seconds
return (self._duration & 0x7f) / 100.0

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

def __repr__(self):
return "{}({})".format(type(self).__qualname__, self.duration)
Expand Down Expand Up @@ -318,7 +318,7 @@ def __getitem__(self, slot):
# pylint: disable=protected-access
slot_contents = self._drv2605._read_u8(_DRV2605_REG_WAVESEQ1 + slot)
if slot_contents & 0x80:
return Pause(slot_contents & 0x7f)
return Pause((slot_contents & 0x7f) / 100.0)
return Effect(slot_contents)

def __iter__(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/drv2605_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# them in interesting ways. Index the sequence property with a
# slot number 0 to 6.
# Optionally, you can assign a pause to a slot. E.g.
# drv.sequence[1] = adafruit_drv2605.Pause(5) # Pause for 50 milliseconds
# drv.sequence[1] = adafruit_drv2605.Pause(0.5) # Pause for half a second
drv.play() # Play the effect.
time.sleep(0.5)
# Increment effect ID and wrap back around to 1.
Expand Down

0 comments on commit f978a65

Please sign in to comment.