fix tick overflow and implement periodic timer class.

This commit is contained in:
xs5871 2022-03-05 11:12:28 +00:00 committed by Kyle Brown
parent e2f2c228d1
commit de1d602b25
3 changed files with 21 additions and 13 deletions

View File

@ -1,11 +1,11 @@
import neopixel import neopixel
from supervisor import ticks_ms
from math import e, exp, pi, sin from math import e, exp, pi, sin
from kmk.extensions import Extension from kmk.extensions import Extension
from kmk.handlers.stock import passthrough as handler_passthrough from kmk.handlers.stock import passthrough as handler_passthrough
from kmk.keys import make_key from kmk.keys import make_key
from kmk.kmktime import PeriodicTimer
rgb_config = {} rgb_config = {}
@ -82,7 +82,6 @@ class RGB(Extension):
self.refresh_rate = refresh_rate self.refresh_rate = refresh_rate
self._substep = 0 self._substep = 0
self._time = ticks_ms()
make_key( make_key(
names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough names=('RGB_TOG',), on_press=self._rgb_tog, on_release=handler_passthrough
@ -154,7 +153,7 @@ class RGB(Extension):
return return
def during_bootup(self, sandbox): def during_bootup(self, sandbox):
return self._timer = PeriodicTimer(1000 // self.refresh_rate)
def before_matrix_scan(self, sandbox): def before_matrix_scan(self, sandbox):
return return
@ -425,9 +424,7 @@ class RGB(Extension):
if self.animation_mode is AnimationModes.STATIC_STANDBY: if self.animation_mode is AnimationModes.STATIC_STANDBY:
return return
now = ticks_ms() if self.enable and self._timer.tick():
if self.enable and now - self._time > (1000 // self.refresh_rate):
self._time = now
self._animation_step() self._animation_step()
if self.animation_mode == AnimationModes.BREATHING: if self.animation_mode == AnimationModes.BREATHING:
self.effect_breathing() self.effect_breathing()
@ -489,7 +486,6 @@ class RGB(Extension):
self.set_hsv_fill(self.hue, self.sat, self.val) self.set_hsv_fill(self.hue, self.sat, self.val)
def effect_swirl(self): def effect_swirl(self):
print('.', ticks_ms() % 1000, self.animation_speed, self._step)
self.increase_hue(self._step) self.increase_hue(self._step)
self.disable_auto_write = True # Turn off instantly showing self.disable_auto_write = True # Turn off instantly showing
for i in range(0, self.num_pixels): for i in range(0, self.num_pixels):

View File

@ -1,4 +1,5 @@
from micropython import const from micropython import const
from supervisor import ticks_ms
_TICKS_PERIOD = const(1 << 29) _TICKS_PERIOD = const(1 << 29)
_TICKS_MAX = const(_TICKS_PERIOD - 1) _TICKS_MAX = const(_TICKS_PERIOD - 1)
@ -13,3 +14,17 @@ def ticks_diff(new, start):
def check_deadline(new, start, ms): def check_deadline(new, start, ms):
return ticks_diff(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

View File

@ -3,12 +3,12 @@ Extension handles usage of Trackball Breakout by Pimoroni
Product page: https://shop.pimoroni.com/products/trackball-breakout Product page: https://shop.pimoroni.com/products/trackball-breakout
''' '''
from micropython import const from micropython import const
from supervisor import ticks_ms
import math import math
import struct import struct
from kmk.keys import make_key from kmk.keys import make_key
from kmk.kmktime import PeriodicTimer
from kmk.modules import Module from kmk.modules import Module
from kmk.modules.mouse_keys import PointingDevice from kmk.modules.mouse_keys import PointingDevice
@ -66,7 +66,6 @@ class Trackball(Module):
self.mode = mode self.mode = mode
self.previous_state = False # click state self.previous_state = False # click state
self.polling_interval = 20 self.polling_interval = 20
self.last_tick = ticks_ms()
chip_id = struct.unpack('<H', bytearray(self._i2c_rdwr([REG_CHIP_ID_L], 2)))[0] chip_id = struct.unpack('<H', bytearray(self._i2c_rdwr([REG_CHIP_ID_L], 2)))[0]
if chip_id != CHIP_ID: if chip_id != CHIP_ID:
@ -81,16 +80,14 @@ class Trackball(Module):
) )
def during_bootup(self, keyboard): def during_bootup(self, keyboard):
return self._timer = PeriodicTimer(self.polling_interval)
def before_matrix_scan(self, keyboard): def before_matrix_scan(self, keyboard):
''' '''
Return value will be injected as an extra matrix update Return value will be injected as an extra matrix update
''' '''
now = ticks_ms() if not self._timer.tick():
if now - self.last_tick < self.polling_interval:
return return
self.last_tick = now
up, down, left, right, switch, state = self._read_raw_state() up, down, left, right, switch, state = self._read_raw_state()