Skip to content

Instantly share code, notes, and snippets.

@fredrikhr
Created February 27, 2017 15:24
Show Gist options
  • Save fredrikhr/6d9e16224f9db10e9de58aa5893642c0 to your computer and use it in GitHub Desktop.
Save fredrikhr/6d9e16224f9db10e9de58aa5893642c0 to your computer and use it in GitHub Desktop.
Asynchronous Lightflashing on LoPy
class FlashAsyncToken(AbortToken):
def __init__(self, color, times=1, time_on=0.1, time_off=0.1, abort_token=None):
super().__init__()
self.color = color
self.counter = 0
self.times = times
self.time_on = time_on
self.time_off = time_off
self.parent = abort_token
self.__alarm = Timer.Alarm(self.__light_on, us=5, periodic=False)
def is_abort_requested(self):
if self.parent is not None:
return self.parent.is_abort_requested()
return super().is_abort_requested()
def __light_on(self, alarm=None):
pycom.heartbeat(False)
pycom.rgbled(self.color)
self.__alarm = Timer.Alarm(self.__light_off, s=self.time_on, periodic=False)
def __light_off(self, alarm=None):
pycom.heartbeat(False)
pycom.rgbled(0)
self.__alarm = Timer.Alarm(self.__counter_inc, s=self.time_off, periodic=False)
def __counter_inc(self, alarm=None):
self.counter += 1
if self.counter < self.times and not self.is_abort_requested():
self.__light_on()
def flash_rgbled_async(color, times=1, time_on=0.1, time_off=0.1, abort_token=None):
return FlashAsyncToken(color, times=times, time_on=time_on, time_off=time_off, abort_token=abort_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment