- Notifications
You must be signed in to change notification settings - Fork 16
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
handle ZeroDivisionError #76
Conversation
| Hi, I'll take a look at this but I think it may need a small tweak to get it to work. |
There was a problem hiding this 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| Now I get it, thanks a lot! |
There was a problem hiding this 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 Updating https://github.com/adafruit/Adafruit_CircuitPython_BNO055 to 5.4.7 from 5.4.6: > Merge pull request adafruit/Adafruit_CircuitPython_BNO055#108 from tcfranks/main Updating https://github.com/adafruit/Adafruit_CircuitPython_RFM69 to 2.1.13 from 2.1.12: > Merge pull request adafruit/Adafruit_CircuitPython_RFM69#42 from tcfranks/main Updating https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_Layout to 1.19.9 from 1.19.8: > Merge pull request adafruit/Adafruit_CircuitPython_DisplayIO_Layout#76 from The-Debarghya/bug-75-zerodivisionerror Updating https://github.com/adafruit/Adafruit_CircuitPython_Logging to 4.2.0 from 4.1.6: > Merge pull request adafruit/Adafruit_CircuitPython_Logging#40 from tekktrik/dev/allow-root-logger Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 5.5.2 from 5.5.1: > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#122 from calcut/fetch_all_messages_in_loop Updating https://github.com/adafruit/Adafruit_CircuitPython_PYOA to 2.5.12 from 2.5.11: > Merge pull request adafruit/Adafruit_CircuitPython_PYOA#37 from tekktrik/dev/fix-install
As mentioned in #75 if animation_time is set to 0, no animation should occur.