base animation speed on system timer

This commit is contained in:
xs5871 2022-02-25 20:36:54 +00:00 committed by Kyle Brown
parent b7e3e4ea8c
commit 8cec85e3ec
2 changed files with 48 additions and 60 deletions

View File

@ -122,6 +122,7 @@ rgb_ext = RGB(pixel_pin=rgb_pixel_pin,
knight_effect_length=3, knight_effect_length=3,
animation_mode=AnimationModes.STATIC, animation_mode=AnimationModes.STATIC,
reverse_animation=False, reverse_animation=False,
refresh_rate=60,
) )
``` ```

View File

@ -1,6 +1,6 @@
import neopixel import neopixel
from supervisor import ticks_ms
import time
from math import e, exp, pi, sin from math import e, exp, pi, sin
from kmk.extensions import Extension from kmk.extensions import Extension
@ -24,8 +24,6 @@ class AnimationModes:
class RGB(Extension): class RGB(Extension):
pos = 0 pos = 0
time = int(time.monotonic() * 10)
intervals = (30, 20, 10, 5)
def __init__( def __init__(
self, self,
@ -47,8 +45,8 @@ class RGB(Extension):
reverse_animation=False, reverse_animation=False,
user_animation=None, user_animation=None,
disable_auto_write=False, disable_auto_write=False,
loopcounter=0,
pixels=None, pixels=None,
refresh_rate=60,
): ):
if pixels is None: if pixels is None:
self.pixels = neopixel.NeoPixel( self.pixels = neopixel.NeoPixel(
@ -81,7 +79,10 @@ class RGB(Extension):
self.reverse_animation = reverse_animation self.reverse_animation = reverse_animation
self.user_animation = user_animation self.user_animation = user_animation
self.disable_auto_write = disable_auto_write self.disable_auto_write = disable_auto_write
self.loopcounter = loopcounter self.refresh_rate = refresh_rate
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
@ -173,10 +174,6 @@ class RGB(Extension):
def on_powersave_disable(self, sandbox): def on_powersave_disable(self, sandbox):
self._do_update() self._do_update()
@staticmethod
def time_ms():
return int(time.monotonic() * 1000)
def hsv_to_rgb(self, hue, sat, val): def hsv_to_rgb(self, hue, sat, val):
''' '''
Converts HSV values, and returns a tuple of RGB values Converts HSV values, and returns a tuple of RGB values
@ -293,7 +290,7 @@ class RGB(Extension):
Increases hue by step amount rolling at 360 and returning to 0 Increases hue by step amount rolling at 360 and returning to 0
:param step: :param step:
''' '''
if not step: if step is None:
step = self.hue_step step = self.hue_step
self.hue = (self.hue + step) % 360 self.hue = (self.hue + step) % 360
@ -306,7 +303,7 @@ class RGB(Extension):
Decreases hue by step amount rolling at 0 and returning to 360 Decreases hue by step amount rolling at 0 and returning to 360
:param step: :param step:
''' '''
if not step: if step is None:
step = self.hue_step step = self.hue_step
if (self.hue - step) <= 0: if (self.hue - step) <= 0:
@ -322,7 +319,7 @@ class RGB(Extension):
Increases saturation by step amount stopping at 100 Increases saturation by step amount stopping at 100
:param step: :param step:
''' '''
if not step: if step is None:
step = self.sat_step step = self.sat_step
if self.sat + step >= 100: if self.sat + step >= 100:
@ -338,7 +335,7 @@ class RGB(Extension):
Decreases saturation by step amount stopping at 0 Decreases saturation by step amount stopping at 0
:param step: :param step:
''' '''
if not step: if step is None:
step = self.sat_step step = self.sat_step
if (self.sat - step) <= 0: if (self.sat - step) <= 0:
@ -354,7 +351,7 @@ class RGB(Extension):
Increases value by step amount stopping at 100 Increases value by step amount stopping at 100
:param step: :param step:
''' '''
if not step: if step is None:
step = self.val_step step = self.val_step
if (self.val + step) >= 100: if (self.val + step) >= 100:
self.val = 100 self.val = 100
@ -369,7 +366,7 @@ class RGB(Extension):
Decreases value by step amount stopping at 0 Decreases value by step amount stopping at 0
:param step: :param step:
''' '''
if not step: if step is None:
step = self.val_step step = self.val_step
if (self.val - step) <= 0: if (self.val - step) <= 0:
self.val = 0 self.val = 0
@ -425,10 +422,13 @@ class RGB(Extension):
if self.effect_init: if self.effect_init:
self._init_effect() self._init_effect()
if self.animation_mode is not AnimationModes.STATIC_STANDBY: if self.animation_mode is AnimationModes.STATIC_STANDBY:
self.loopcounter += 1 return
if self.loopcounter >= 7 and self.enable:
self.loopcounter = 0 now = ticks_ms()
if self.enable and now - self._time > (1000 // self.refresh_rate):
self._time = now
self._animation_step()
if self.animation_mode == AnimationModes.BREATHING: if self.animation_mode == AnimationModes.BREATHING:
self.effect_breathing() self.effect_breathing()
elif self.animation_mode == AnimationModes.RAINBOW: elif self.animation_mode == AnimationModes.RAINBOW:
@ -447,27 +447,13 @@ class RGB(Extension):
pass pass
else: else:
self.off() self.off()
if self.loopcounter >= 7:
self.loopcounter = 0
def _animation_step(self): def _animation_step(self):
interval = self.time_ms() - self.time self._substep += self.animation_speed / 4
if interval >= max(self.intervals): self._step = int(self._substep)
self.time = self.time_ms() self._substep -= self._step
return max(self.intervals)
if interval in self.intervals:
return interval
return None
def _init_effect(self): def _init_effect(self):
if (
self.animation_mode == AnimationModes.BREATHING
or self.animation_mode == AnimationModes.BREATHING_RAINBOW
):
self.intervals = (30, 20, 10, 5)
elif self.animation_mode == AnimationModes.SWIRL:
self.intervals = (50, 50)
self.pos = 0 self.pos = 0
self.reverse_animation = False self.reverse_animation = False
self.effect_init = False self.effect_init = False
@ -491,19 +477,20 @@ class RGB(Extension):
multip_2 = self.val_limit / (e - 1 / e) multip_2 = self.val_limit / (e - 1 / e)
self.val = int(multip_1 * multip_2) self.val = int(multip_1 * multip_2)
self.pos = (self.pos + self.animation_speed) % 256 self.pos = (self.pos + self._step) % 256
self.set_hsv_fill(self.hue, self.sat, self.val) self.set_hsv_fill(self.hue, self.sat, self.val)
def effect_breathing_rainbow(self): def effect_breathing_rainbow(self):
self.increase_hue(self.animation_speed) self.increase_hue(self._step)
self.effect_breathing() self.effect_breathing()
def effect_rainbow(self): def effect_rainbow(self):
self.increase_hue(self.animation_speed) self.increase_hue(self._step)
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):
self.increase_hue(self.animation_speed) print('.', ticks_ms() % 1000, self.animation_speed, 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):
self.set_hsv( self.set_hsv(
@ -529,9 +516,9 @@ class RGB(Extension):
self.reverse_animation = not self.reverse_animation self.reverse_animation = not self.reverse_animation
if self.reverse_animation: if self.reverse_animation:
self.pos -= self.animation_speed / 2 self.pos -= self._step / 2
else: else:
self.pos += self.animation_speed / 2 self.pos += self._step / 2
# Show final results # Show final results
self.disable_auto_write = False # Resume showing changes self.disable_auto_write = False # Resume showing changes