Added support for user animations with docs

This commit is contained in:
Kyle Brown 2019-03-16 14:26:19 -07:00
parent 9d8682c866
commit 3f8c6e7648
5 changed files with 96 additions and 13 deletions

View File

@ -43,6 +43,36 @@ If you want to create your own animations, or for example, change the lighting i
|`keyboard.led.animation_mode` |`static` |This can be changed to any modes included, or to something custom for user animations. Any string is valid | |`keyboard.led.animation_mode` |`static` |This can be changed to any modes included, or to something custom for user animations. Any string is valid |
|`keyboard.led.animation_speed` |`1` |Increases animation speed of most animations. Recommended 1-5, Maximum 10. | |`keyboard.led.animation_speed` |`1` |Increases animation speed of most animations. Recommended 1-5, Maximum 10. |
## User animations
User animations can be created as well. An example of a light show would look like this
```python
from kmk.keys import make_key
def start_flicker(*args, **kwargs):
# Setting mode to user will use the user animation
keyboard.led.animation_mode = 'user'
def flicker(self):
# This is the code that is run every cycle that can serve as an animation
# Refer to the kmk/rgb.py for actual examples of what has been done
if self.brightness == 0:
self.brightness = 100
else:
self.brightness = 0
keyboard.led.set_brightness(self.brightness)
return self
# This is what "gives" your function to KMK so it knows what your animation code is
keyboard.led_config['user_animation'] = flicker
# Makes a key that would start your animation
LS = make_key(on_press=start_flicker())
keymap = [...LS,...]
```
# Troubleshooting # Troubleshooting
Make sure that your board supports LED backlight by checking for a line with "LED_PIN". If it does not, you can add it to your keymap. Make sure that your board supports LED backlight by checking for a line with "LED_PIN". If it does not, you can add it to your keymap.

View File

@ -104,6 +104,33 @@ keyboard.pixels.disable_auto_write = True
keyboard.pixels.neopixel() # <-- This is the neopixel object keyboard.pixels.neopixel() # <-- This is the neopixel object
``` ```
## User animations
User animations can be created as well. An example of a light show would look like this
```python
from kmk.keys import make_key
def start_light_show(*args, **kwargs):
# Setting mode to user will use the user animation
keyboard.pixels.animation_mode = 'user'
def light_show(self):
# This is the code that is run every cycle that can serve as an animation
# Refer to the kmk/rgb.py for actual examples of what has been done
self.hue = (self.hue + 35) % 360
keyboard.pixels.set_hsv_fill(self.hue, self.sat, self.val)
return self
# This is what "gives" your function to KMK so it knows what your animation code is
keyboard.rgb_config['user_animation'] = light_show
# Makes a key that would start your animation
LS = make_key(on_press=start_light_show())
keymap = [...LS,...]
```
## Troubleshooting ## Troubleshooting
### Incorrect colors ### Incorrect colors
If your colors are incorrect, check the pixel order of your specific LED's. Here are some common ones. If your colors are incorrect, check the pixel order of your specific LED's. Here are some common ones.

View File

@ -1,8 +1,8 @@
import time import time
from math import e, exp, pi, sin from math import e, exp, pi, sin
from micropython import const
import pulseio import pulseio
from micropython import const
led_config = { led_config = {
'brightness_step': 5, 'brightness_step': 5,
@ -12,6 +12,7 @@ led_config = {
'animation_speed': 1, 'animation_speed': 1,
} }
class led: class led:
brightness = 0 brightness = 0
time = int(time.monotonic() * 1000) time = int(time.monotonic() * 1000)
@ -25,6 +26,7 @@ class led:
animation_mode = 'static' animation_mode = 'static'
animation_speed = 1 animation_speed = 1
enabled = True enabled = True
user_animation = None
def __init__(self, led_pin, config): def __init__(self, led_pin, config):
self.led = pulseio.PWMOut(led_pin) self.led = pulseio.PWMOut(led_pin)
@ -33,6 +35,8 @@ class led:
self.animation_mode = const(config['animation_mode']) self.animation_mode = const(config['animation_mode'])
self.animation_speed = const(config['animation_speed']) self.animation_speed = const(config['animation_speed'])
self.breathe_center = const(config['breathe_center']) self.breathe_center = const(config['breathe_center'])
if config['user_animation']:
self.user_animation = config['user_animation']
def __repr__(self): def __repr__(self):
return 'LED({})'.format(self._to_dict()) return 'LED({})'.format(self._to_dict())
@ -131,6 +135,8 @@ class led:
return self.effect_breathing() return self.effect_breathing()
elif self.animation_mode == 'static': elif self.animation_mode == 'static':
return self.effect_static() return self.effect_static()
elif self.animation_mode == 'user':
return self.user_animation(self)
else: else:
self.off() self.off()

View File

@ -1,5 +1,6 @@
import time import time
from math import e, exp, pi, sin from math import e, exp, pi, sin
from micropython import const from micropython import const
rgb_config = { rgb_config = {
@ -17,9 +18,10 @@ rgb_config = {
'animation_speed': 1, 'animation_speed': 1,
'breathe_center': 1.5, # 1.0-2.7 'breathe_center': 1.5, # 1.0-2.7
'knight_effect_length': 3, 'knight_effect_length': 3,
'animation_mode': 'static' 'animation_mode': 'static',
} }
class RGB: class RGB:
hue = 0 hue = 0
sat = 100 sat = 100
@ -44,6 +46,7 @@ class RGB:
knight_effect_length = None knight_effect_length = None
val_limit = None val_limit = None
effect_init = False effect_init = False
user_animation = None
def __init__(self, config, pixel_pin): def __init__(self, config, pixel_pin):
try: try:
@ -66,6 +69,8 @@ class RGB:
self.val_limit = const(config['val_limit']) self.val_limit = const(config['val_limit'])
self.rgb_animation_mode = const(config['animation_mode']) self.rgb_animation_mode = const(config['animation_mode'])
self.animation_speed = const(config['animation_speed']) self.animation_speed = const(config['animation_speed'])
if config['user_animation']:
self.user_animation = config['user_animation']
except ImportError as e: except ImportError as e:
print(e) print(e)
@ -374,6 +379,8 @@ class RGB:
return self.effect_knight() return self.effect_knight()
elif self.animation_mode == 'swirl': elif self.animation_mode == 'swirl':
return self.effect_swirl() return self.effect_swirl()
elif self.animation_mode == 'user':
return self.user_animation(self)
elif self.animation_mode == 'static_standby': elif self.animation_mode == 'static_standby':
pass pass
else: else:

View File

@ -23,14 +23,14 @@ keyboard.rgb_config['val_default'] = 20
keyboard.rgb_config['knight_effect_length'] = 6 keyboard.rgb_config['knight_effect_length'] = 6
keyboard.rgb_config['animation_mode'] = 'static' keyboard.rgb_config['animation_mode'] = 'static'
keyboard.rgb_config['animation_speed'] = 2 keyboard.rgb_config['animation_speed'] = 2
keyboard.debug_enabled = False keyboard.debug_enabled = True
# ---------------------- Custom Functions -------------------------------------------- # ---------------------- Custom Functions --------------------------------------------
def portal_lights(*args, **kwargs): def portal_lights(*args, **kwargs):
keyboard.pixels.animation_mode = 'static_standby'
keyboard.pixels.disable_auto_write = True keyboard.pixels.disable_auto_write = True
keyboard.pixels.rgb_animation_mode = 'User'
for i in range(0, 9): for i in range(0, 9):
keyboard.pixels.set_hsv(10, 100, 100, i) keyboard.pixels.set_hsv(10, 100, 100, i)
for i in range(10, 16): for i in range(10, 16):
@ -41,8 +41,21 @@ def portal_lights(*args, **kwargs):
def portal_off(*args, **kwargs): def portal_off(*args, **kwargs):
keyboard.pixels.disable_auto_write = False keyboard.pixels.disable_auto_write = False
keyboard.pixels.off() keyboard.pixels.off()
keyboard.pixels.rgb_animation_mode = 'static' keyboard.pixels.animation_mode = 'static'
def start_light_show(*args, **kwargs):
keyboard.pixels.animation_mode = 'user'
def light_show(self):
self.hue = (self.hue + 35) % 360
keyboard.pixels.set_hsv_fill(self.hue, self.sat, self.val)
return self
keyboard.rgb_config['user_animation'] = light_show
LS = make_key(on_press=start_light_show())
# ---------------------- Custom Keys -------------------------------------------- # ---------------------- Custom Keys --------------------------------------------