From 5a0eb1e02b777062674cc4be3d5f2df66f1904ec Mon Sep 17 00:00:00 2001 From: Kris Wilk Date: Tue, 7 Mar 2023 10:40:05 -0500 Subject: [PATCH 1/5] Add functionality to set the RTP value --- adafruit_drv2605.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/adafruit_drv2605.py b/adafruit_drv2605.py index aa2d14b..10d047a 100644 --- a/adafruit_drv2605.py +++ b/adafruit_drv2605.py @@ -208,6 +208,12 @@ def sequence(self) -> "_DRV2605_Sequence": """ return self._sequence + def set_realtime_value(self, val: int) -> None: + """Set the output value (amplitude) used for Real-Time Playback""" + if not 0 <= val <= 255: + raise ValueError("Real-Time Playback value must be a value within 0-255!") + self._write_u8(_DRV2605_REG_RTPIN, val) + def set_waveform(self, effect_id: int, slot: int = 0) -> None: """Select an effect waveform for the specified slot (default is slot 0, but up to 8 effects can be combined with slot values 0 to 7). See the From 684ef878a7705dcef2c08a2a3bab94a9628a3fbc Mon Sep 17 00:00:00 2001 From: Kris Wilk Date: Tue, 7 Mar 2023 11:01:48 -0500 Subject: [PATCH 2/5] Clarify documentation of set_realtime_value() --- adafruit_drv2605.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/adafruit_drv2605.py b/adafruit_drv2605.py index 10d047a..9524dfb 100644 --- a/adafruit_drv2605.py +++ b/adafruit_drv2605.py @@ -209,7 +209,10 @@ def sequence(self) -> "_DRV2605_Sequence": return self._sequence def set_realtime_value(self, val: int) -> None: - """Set the output value (amplitude) used for Real-Time Playback""" + """Set the output value used for Real-Time Playback. By default, the device + interprets the value as SIGNED (2s complement), and its effect depends on + other operating parameters. Consult the datasheet for more information! + """ if not 0 <= val <= 255: raise ValueError("Real-Time Playback value must be a value within 0-255!") self._write_u8(_DRV2605_REG_RTPIN, val) From ab70daa770828a0d3437dcea1de9e419894f6924 Mon Sep 17 00:00:00 2001 From: Kris Wilk Date: Thu, 9 Mar 2023 09:42:38 -0500 Subject: [PATCH 3/5] Make "realtime_value" a property, improve docstring --- adafruit_drv2605.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/adafruit_drv2605.py b/adafruit_drv2605.py index 9524dfb..3be3d11 100644 --- a/adafruit_drv2605.py +++ b/adafruit_drv2605.py @@ -208,11 +208,33 @@ def sequence(self) -> "_DRV2605_Sequence": """ return self._sequence - def set_realtime_value(self, val: int) -> None: - """Set the output value used for Real-Time Playback. By default, the device - interprets the value as SIGNED (2s complement), and its effect depends on - other operating parameters. Consult the datasheet for more information! + @property + def realtime_value(self) -> int: + """The output value used in Real-Time Playback mode. When the device is + switched to ``MODE_REALTIME``, the motor is driven continuously with an + amplitude/direction determined by this value. + + By default, the device interprets it as SIGNED (2s complement), and its exact + effect depends on other operating parameters. + + See the datasheet for more information! + + E.g.: + + .. code-block:: python + + # Configure the output amplitude to 50% + drv.realtime_value = 64 + + # Buzz the motor briefly + drv.mode = adafruit_drv2605.MODE_REALTIME + time.sleep(0.25) + drv.mode = adafruit_drv2605.MODE_INTTRIG """ + return self._read_u8(_DRV2605_REG_RTPIN) + + @realtime_value.setter + def realtime_value(self, val: int) -> None: if not 0 <= val <= 255: raise ValueError("Real-Time Playback value must be a value within 0-255!") self._write_u8(_DRV2605_REG_RTPIN, val) From 4e51c8fa9d9137f630f69e2354c4c0605ccf19f9 Mon Sep 17 00:00:00 2001 From: Kris Wilk Date: Thu, 9 Mar 2023 10:43:53 -0500 Subject: [PATCH 4/5] Adjust permitted input for realtime_value, improve docstring and example --- adafruit_drv2605.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/adafruit_drv2605.py b/adafruit_drv2605.py index 3be3d11..a9b1de3 100644 --- a/adafruit_drv2605.py +++ b/adafruit_drv2605.py @@ -214,8 +214,9 @@ def realtime_value(self) -> int: switched to ``MODE_REALTIME``, the motor is driven continuously with an amplitude/direction determined by this value. - By default, the device interprets it as SIGNED (2s complement), and its exact - effect depends on other operating parameters. + By default, the device expects a SIGNED 8-bit integer, and its exact + effect depends on both the type of motor (ERM/LRA) and whether the device + is operating in open- or closed-loop (unidirectional/bidirectional) mode. See the datasheet for more information! @@ -223,20 +224,26 @@ def realtime_value(self) -> int: .. code-block:: python - # Configure the output amplitude to 50% + # Start real-time playback + drv.realtime_value = 0 + drv.mode = adafruit_drv2605.MODE_REALTIME + + # Buzz the motor briefly at 50% and 100% amplitude drv.realtime_value = 64 + time.sleep(0.5) + drv.realtime_value = 127 + time.sleep(0.5) - # Buzz the motor briefly - drv.mode = adafruit_drv2605.MODE_REALTIME - time.sleep(0.25) + # Stop real-time playback + drv.realtime_value = 0 drv.mode = adafruit_drv2605.MODE_INTTRIG """ return self._read_u8(_DRV2605_REG_RTPIN) @realtime_value.setter def realtime_value(self, val: int) -> None: - if not 0 <= val <= 255: - raise ValueError("Real-Time Playback value must be a value within 0-255!") + if not -127 <= val <= 255: + raise ValueError("Real-Time Playback value must be a value between -127 and 255!") self._write_u8(_DRV2605_REG_RTPIN, val) def set_waveform(self, effect_id: int, slot: int = 0) -> None: From f4c324dfd8804295b8217267a75de496af2b6dfe Mon Sep 17 00:00:00 2001 From: Kris Wilk Date: Thu, 9 Mar 2023 11:00:36 -0500 Subject: [PATCH 5/5] Reformat to satisfy black --- adafruit_drv2605.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_drv2605.py b/adafruit_drv2605.py index a9b1de3..a8efb34 100644 --- a/adafruit_drv2605.py +++ b/adafruit_drv2605.py @@ -243,7 +243,7 @@ def realtime_value(self) -> int: @realtime_value.setter def realtime_value(self, val: int) -> None: if not -127 <= val <= 255: - raise ValueError("Real-Time Playback value must be a value between -127 and 255!") + raise ValueError("Real-Time Playback value must be between -127 and 255!") self._write_u8(_DRV2605_REG_RTPIN, val) def set_waveform(self, effect_id: int, slot: int = 0) -> None: