The virtualtimers module lets you regularly fire a function without holding up other Python logic. It is useful to update the screen for example, whilst keeping button presses low-latency. If you replace
time.sleep() with functions called by virtualtimers, your app will feel much more snappy.
|begin||period||Calling this function starts the virtualtimers task. It needs to be called only once in your app. The
|stop||-||Stops the virtualtimers task. After calling this, you need to call
|new||delay, callback||Adds a new function callback to be executed after
|update||period, callback||Updates the function callback to be executed every
Updating the screen every X ms
import virtualtimers, rgb pixel_x = 0 # This is the function we want to call regularly def update_screen(): # Show a black bar that cycles through the screen on every run global pixel_x rgb.clear() for y in range(8): rgb.pixel(pos=(pixel_x, y), color=(0,0,0)) pixel_x = (pixel_x + 1) % 32 # Let virtualtimers know that we want to run this function # again after 500 ms next_execution = 500 # in ms return next_execution # We set the default background colour to green rgb.background((0,255,0)) # The timer is started with a minimum interval of 100ms virtualtimers.begin(100) # and we tell it to execute update_screen immediately (no delay). # Note that update_screen returns the number of ms until the next time # it should run again. virtualtimers.new(0, update_screen)