Skip to content

handle ZeroDivisionError #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 10, 2022

Conversation

The-Debarghya
Copy link
Contributor

As mentioned in #75 if animation_time is set to 0, no animation should occur.

@tekktrik tekktrik self-requested a review October 7, 2022 18:49
@tekktrik
Copy link
Member

tekktrik commented Oct 7, 2022

Hi, I'll take a look at this but I think it may need a small tweak to get it to work.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @The-Debarghya!

The current way it's coded only handles when the switch is turning off (value going to False). It won't ever turn the switch back on this way because it's only calling draw_position(0) and never draw_position(1).

I tested this out on a Feather ESP32-S2 TFT and confirmed that is the current behavior.

I think it needs an if to check if current value is on or off then it can determine which position to draw. It also needs to allow the last part of the function execute. The part that is setting self._value to it's new state. So we need this not to break but instead to use else for the logic when animation_time > 0 so that it will run in that case, and we'll still get down to the _value updates if animation_time is zero.

I made those tweaks and it does seem to work as intended after that. This is the code that I ended up with:

# if it's instant instead of animating
if self._animation_time == 0:
    print(f"0 anim time: {self._value}")
    if not self._value:
        position = 1
        self._draw_position(1)
    else:
        position = 0
        self._draw_position(0)

else:  # we need to animate over time
    # constrain the elapsed time
    elapsed_time = time.monotonic() - start_time
    if elapsed_time > self._animation_time:
        elapsed_time = self._animation_time

    # Determines the direction of movement, depending upon if the
    # switch is going from on->off or off->on
    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

    print(position)
    # 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:
    self._value = True
    break
if (
    position <= 0
) and self._value:  # ensures that the final position is drawn
    self._value = False
    break

@The-Debarghya
Copy link
Contributor Author

Now I get it, thanks a lot!

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I tested successfully on Feather ESP32-S2 TFT with this code:

import time
import board
import displayio
from adafruit_displayio_layout.widgets.switch_round import SwitchRound as Switch

display = board.DISPLAY

# Create the switch
my_switch = Switch(20, 30, animation_time=0)

my_group = displayio.Group()
my_group.append(my_switch)

# Add my_group to the display
display.show(my_group)

# Start the main loop
while True:
    time.sleep(1)
    my_switch.value = True
    time.sleep(1)
    my_switch.value = False

@FoamyGuy FoamyGuy merged commit 323bf5d into adafruit:main Oct 10, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants