- Description
- Circuit Python Setup
- Code Examples - non-CircuitPython
- Code Examples - CircuitPython
- Easing Types
- CLASS: Vspeed
- init
- move
- sequence
- sequence_change_seq_num
- sequence_run
- set_position
- set_bounds
This Python library is decended from the VarspeedServo library (https://github.com/netlabtoolkit/VarSpeedServo), originally written for the Arduino in C++. Unlike the old Arduino library, VarSpeedPython is not tied to servos, and can be used more generally for timed moves from one value to another. It is also not bound to any processor architecture with hardware interrupts etc.
The library is designed for projects that need to control values over time. For example, in setting the angle of a servo, setting the brightness of an LED, or moving a graphic on a screen. You can set the amount of time for a change in value, and apply easing to each move. It also provides a function for running sequences of moves (that can be looped or repeated if desired), where each move in the sequence has a new position and speed. More than one move or sequence can be run at the same time.
VarSpeedPython objects are designed to be called repeatedly from within an event loop and do not block execution.
To set up on CircuitPython:
- Put varspeed.py in the lib directory on CIRCUITPY
- Put easing_functions.py in the lib directory on CIRCUITPY
- Copy the python from any of the examples into code.py at the top of CIRCUITPY
- move_simple.py — a non-CircuitPython dependent example that can be run in any Python environment
- sequence_simple.py — a non-CircuitPython dependent example that can be run in any Python environment
- move_simple_led.py — changes the brightness of an LED
- move_simple_servo.py — changes the angle of a servo
- sequence_simple_servo.py — runs a sequence of moves for a servo
- two_sequences_at_once_led.py — shows how to do two sequences simultaneously, for example dimming two LEDs in opposite directions
- two_sequences_at_once_servo.py — runs different sequences for different servos simultaneously
For any move (even within a sequence), you can set an easing function using the following easing functions:
- LinearInOut
- QuadEaseInOut, QuadEaseIn, QuadEaseOut
- CubicEaseIn, CubicEaseOut, CubicEaseInOut
- QuarticEaseIn, QuarticEaseOut, QuarticEaseInOut
- QuinticEaseIn, QuinticEaseOut, QuinticEaseInOut
- SineEaseIn, SineEaseOut, SineEaseInOut
- CircularEaseIn, CircularEaseOut, CircularEaseInOut
- ExponentialEaseIn, ExponentialEaseOut, ExponentialEaseInOut
- ElasticEaseIn, ElasticEaseOut, ElasticEaseInOut
- BackEaseIn, BackEaseOut, BackEaseInOut
- BounceEaseIn, BounceEaseOut, BounceEaseInOut
class Vspeed():Provides an non-blocking object that can be called repeatedly with the move() and sequence() functions to generate a timed series of values from a current position to a new position(s)
def __init__(self, init_position = 0, result = "int"):Creates and initialzes a Vspeed object.
- init_position (int/float) : sets the initial position of the object.
- result (string = "int" or "float") : sets the type of the returned position.
- object : returns a varspeed object
def move(self, new_position = 0, time_secs = 2.0, steps = 20, easing = "LinearInOut"):Generates a series of values that transition from the current position to a new_position.
- new_position (float or int) : position output will change to over time_secs
- time_secs (int) : time for the transition to the new_position
- steps (int) : number of steps to change from the start position to the new_position
- easing (string) : the easing function to use for the transition
- position (int or float) : each new position, marked by changed being True
- running (Boolean) : if true there are more steps to go in the transition
- changed (Boolean) : indicates if the latest value is different from the previous value
def sequence(self, sequence, loop_max = 1):Creates a series of values in a sequence of moves as specified in the sequence array.
- sequence (array of tuples) : perform a sequence of moves (position,time,steps,easing) in the array.
- loop_max (int) : how many time to loop the sequence, zero means loop forever.
- position (int or float) : each new position, marked by changed being True
- running (Boolean) : if true there are more steps to go in the transition
- changed (Boolean) : indicates if the latest position value is different from the previous value
def sequence_change_seq_num(self, seq_position = 0):Sets the current sequence number.
- seq_position (int) : jump to the element in the sequence[seq_position] array.
nothing
def sequence_run(self, value = True):Pauses or unpauses a running sequence.
- value (Boolean) : pauses (False) or unpauses (True) the run of the sequence.
nothing
def set_position(self, position = 0):Sets the current position from which the next move will proceed
- position (int or float) : sets the current position of the object
nothing
def set_bounds(self, lower_bound = 0, upper_bound = 1000, bounded=True):Sets the lower and upper bounds of values returned by a move or sequence
- lower_bound (int) : sets the lower allowed bound of the output of a move or sequnce
- upper_bound (int) : sets the upper allowed bound of the output of a move or sequnce
- bounded (Boolean) : turns bounds checking on (True - Default) or off (False)
nothing