diff --git a/kmk/extensions/rgb.py b/kmk/extensions/rgb.py index 3aa31d1..063846a 100644 --- a/kmk/extensions/rgb.py +++ b/kmk/extensions/rgb.py @@ -1,11 +1,11 @@ import neopixel -from supervisor import ticks_ms from math import e, exp, pi, sin from kmk.extensions import Extension from kmk.handlers.stock import passthrough as handler_passthrough from kmk.keys import make_key +from kmk.kmktime import PeriodicTimer rgb_config = {} @@ -82,7 +82,6 @@ class RGB(Extension): self.refresh_rate = refresh_rate self._substep = 0 - self._time = ticks_ms() make_key( names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough @@ -154,7 +153,7 @@ class RGB(Extension): return def during_bootup(self, sandbox): - return + self._timer = PeriodicTimer(1000 // self.refresh_rate) def before_matrix_scan(self, sandbox): return @@ -425,9 +424,7 @@ class RGB(Extension): if self.animation_mode is AnimationModes.STATIC_STANDBY: return - now = ticks_ms() - if self.enable and now - self._time > (1000 // self.refresh_rate): - self._time = now + if self.enable and self._timer.tick(): self._animation_step() if self.animation_mode == AnimationModes.BREATHING: self.effect_breathing() @@ -489,7 +486,6 @@ class RGB(Extension): self.set_hsv_fill(self.hue, self.sat, self.val) def effect_swirl(self): - print('.', ticks_ms() % 1000, self.animation_speed, self._step) self.increase_hue(self._step) self.disable_auto_write = True # Turn off instantly showing for i in range(0, self.num_pixels): diff --git a/kmk/kmktime.py b/kmk/kmktime.py index 82ed7cd..c4ef99b 100644 --- a/kmk/kmktime.py +++ b/kmk/kmktime.py @@ -1,4 +1,5 @@ from micropython import const +from supervisor import ticks_ms _TICKS_PERIOD = const(1 << 29) _TICKS_MAX = const(_TICKS_PERIOD - 1) @@ -13,3 +14,17 @@ def ticks_diff(new, start): def check_deadline(new, start, ms): return ticks_diff(new, start) < ms + + +class PeriodicTimer: + def __init__(self, period): + self.period = period + self.last_tick = ticks_ms() + + def tick(self): + now = ticks_ms() + if ticks_diff(now, self.last_tick) >= self.period: + self.last_tick = now + return True + else: + return False diff --git a/kmk/modules/pimoroni_trackball.py b/kmk/modules/pimoroni_trackball.py index addba0d..eec22ca 100644 --- a/kmk/modules/pimoroni_trackball.py +++ b/kmk/modules/pimoroni_trackball.py @@ -3,12 +3,12 @@ Extension handles usage of Trackball Breakout by Pimoroni Product page: https://shop.pimoroni.com/products/trackball-breakout ''' from micropython import const -from supervisor import ticks_ms import math import struct from kmk.keys import make_key +from kmk.kmktime import PeriodicTimer from kmk.modules import Module from kmk.modules.mouse_keys import PointingDevice @@ -66,7 +66,6 @@ class Trackball(Module): self.mode = mode self.previous_state = False # click state self.polling_interval = 20 - self.last_tick = ticks_ms() chip_id = struct.unpack('