fix tick overflow and implement periodic timer class.
This commit is contained in:
		@@ -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):
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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('<H', bytearray(self._i2c_rdwr([REG_CHIP_ID_L], 2)))[0]
 | 
			
		||||
        if chip_id != CHIP_ID:
 | 
			
		||||
@@ -81,16 +80,14 @@ class Trackball(Module):
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    def during_bootup(self, keyboard):
 | 
			
		||||
        return
 | 
			
		||||
        self._timer = PeriodicTimer(self.polling_interval)
 | 
			
		||||
 | 
			
		||||
    def before_matrix_scan(self, keyboard):
 | 
			
		||||
        '''
 | 
			
		||||
        Return value will be injected as an extra matrix update
 | 
			
		||||
        '''
 | 
			
		||||
        now = ticks_ms()
 | 
			
		||||
        if now - self.last_tick < self.polling_interval:
 | 
			
		||||
        if not self._timer.tick():
 | 
			
		||||
            return
 | 
			
		||||
        self.last_tick = now
 | 
			
		||||
 | 
			
		||||
        up, down, left, right, switch, state = self._read_raw_state()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user