From 0634dfd015c60fb63180d5e9eb02e24c76ca0c1e Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Fri, 7 Oct 2022 12:31:33 +0530 Subject: [PATCH 1/4] handle ZeroDivisionError --- adafruit_displayio_layout/widgets/switch_round.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adafruit_displayio_layout/widgets/switch_round.py b/adafruit_displayio_layout/widgets/switch_round.py index 67b0d51..fef3547 100644 --- a/adafruit_displayio_layout/widgets/switch_round.py +++ b/adafruit_displayio_layout/widgets/switch_round.py @@ -754,6 +754,9 @@ def _animate_switch(self): # Determines the direction of movement, depending upon if the # switch is going from on->off or off->on + if self._animation_time == 0: + self._draw_position(0) + break # constrain the elapsed time elapsed_time = time.monotonic() - start_time if elapsed_time > self._animation_time: From bbae1376e54e658ec7ca33aec92580cc318280dc Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Sun, 9 Oct 2022 10:35:00 +0530 Subject: [PATCH 2/4] minor change --- .../widgets/switch_round.py | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/adafruit_displayio_layout/widgets/switch_round.py b/adafruit_displayio_layout/widgets/switch_round.py index fef3547..b27bac1 100644 --- a/adafruit_displayio_layout/widgets/switch_round.py +++ b/adafruit_displayio_layout/widgets/switch_round.py @@ -755,23 +755,28 @@ def _animate_switch(self): # switch is going from on->off or off->on if self._animation_time == 0: - self._draw_position(0) - break - # constrain the elapsed time - elapsed_time = time.monotonic() - start_time - if elapsed_time > self._animation_time: - elapsed_time = self._animation_time - - if self._value: - position = ( - 1 - (elapsed_time) / self._animation_time - ) # fraction from 0 to 1 - else: - position = (elapsed_time) / self._animation_time # fraction from 0 to 1 - - # Update the moving elements based on the current position - # apply the "easing" function to the requested position to adjust motion - self._draw_position(easing(position)) # update the switch position + if not self._value: + position = 1 + self._draw_position(1) + else: + position = 0 + self._draw_position(0) + else: #animate over time + # constrain the elapsed time + elapsed_time = time.monotonic() - start_time + if elapsed_time > self._animation_time: + elapsed_time = self._animation_time + + if self._value: + position = ( + 1 - (elapsed_time) / self._animation_time + ) # fraction from 0 to 1 + else: + position = (elapsed_time) / self._animation_time # fraction from 0 to 1 + + # Update the moving elements based on the current position + # apply the "easing" function to the requested position to adjust motion + self._draw_position(easing(position)) # update the switch position # update the switch value once the motion is complete if (position >= 1) and not self._value: From a634e503c1d74e3b914c50b97cadf9f0db6039c9 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Sun, 9 Oct 2022 10:40:17 +0530 Subject: [PATCH 3/4] Update switch_round.py --- adafruit_displayio_layout/widgets/switch_round.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_displayio_layout/widgets/switch_round.py b/adafruit_displayio_layout/widgets/switch_round.py index b27bac1..d7310b1 100644 --- a/adafruit_displayio_layout/widgets/switch_round.py +++ b/adafruit_displayio_layout/widgets/switch_round.py @@ -772,10 +772,11 @@ def _animate_switch(self): 1 - (elapsed_time) / self._animation_time ) # fraction from 0 to 1 else: - position = (elapsed_time) / self._animation_time # fraction from 0 to 1 + # fraction from 0 to 1 + position = (elapsed_time) / self._animation_time - # Update the moving elements based on the current position - # apply the "easing" function to the requested position to adjust motion + # Update the moving elements based on the current position + # apply the "easing" function to the requested position to adjust motion self._draw_position(easing(position)) # update the switch position # update the switch value once the motion is complete From f2b1656d6d0fd3d0476fc05eb96d6e6564675a74 Mon Sep 17 00:00:00 2001 From: The-Debarghya Date: Sun, 9 Oct 2022 10:50:04 +0530 Subject: [PATCH 4/4] Update switch_round.py --- adafruit_displayio_layout/widgets/switch_round.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_displayio_layout/widgets/switch_round.py b/adafruit_displayio_layout/widgets/switch_round.py index d7310b1..76efe76 100644 --- a/adafruit_displayio_layout/widgets/switch_round.py +++ b/adafruit_displayio_layout/widgets/switch_round.py @@ -761,7 +761,7 @@ def _animate_switch(self): else: position = 0 self._draw_position(0) - else: #animate over time + else: # animate over time # constrain the elapsed time elapsed_time = time.monotonic() - start_time if elapsed_time > self._animation_time: @@ -773,10 +773,10 @@ def _animate_switch(self): ) # fraction from 0 to 1 else: # fraction from 0 to 1 - position = (elapsed_time) / self._animation_time + position = (elapsed_time) / self._animation_time - # Update the moving elements based on the current position - # apply the "easing" function to the requested position to adjust motion + # Update the moving elements based on the current position + # apply the "easing" function to the requested position to adjust motion self._draw_position(easing(position)) # update the switch position # update the switch value once the motion is complete