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

@ -37,11 +37,41 @@ If you want to create your own animations, or for example, change the lighting i
## Direct variable access
|Define |Default |Description |
|-----------------------------------|-----------|--------------------------------------------------------------------------------------------------------|
|`keyboard.led.brightness` |`0` |Sets the brightness by percent 0-100 |
|`keyboard.led.brightness_limit` |`100` |Sets the limit of brightness |
|`keyboard.led.brightness_step` |`5` |Sets the step value to change brightness by |
|`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.brightness` |`0` |Sets the brightness by percent 0-100 |
|`keyboard.led.brightness_limit` |`100` |Sets the limit of brightness |
|`keyboard.led.brightness_step` |`5` |Sets the step value to change brightness by |
|`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. |
## 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
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
```
## 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
### Incorrect colors
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
from math import e, exp, pi, sin
from micropython import const
import pulseio
from micropython import const
led_config = {
'brightness_step': 5,
@ -12,6 +12,7 @@ led_config = {
'animation_speed': 1,
}
class led:
brightness = 0
time = int(time.monotonic() * 1000)
@ -25,6 +26,7 @@ class led:
animation_mode = 'static'
animation_speed = 1
enabled = True
user_animation = None
def __init__(self, led_pin, config):
self.led = pulseio.PWMOut(led_pin)
@ -33,6 +35,8 @@ class led:
self.animation_mode = const(config['animation_mode'])
self.animation_speed = const(config['animation_speed'])
self.breathe_center = const(config['breathe_center'])
if config['user_animation']:
self.user_animation = config['user_animation']
def __repr__(self):
return 'LED({})'.format(self._to_dict())
@ -131,6 +135,8 @@ class led:
return self.effect_breathing()
elif self.animation_mode == 'static':
return self.effect_static()
elif self.animation_mode == 'user':
return self.user_animation(self)
else:
self.off()

View File

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

View File

@ -23,14 +23,14 @@ keyboard.rgb_config['val_default'] = 20
keyboard.rgb_config['knight_effect_length'] = 6
keyboard.rgb_config['animation_mode'] = 'static'
keyboard.rgb_config['animation_speed'] = 2
keyboard.debug_enabled = False
keyboard.debug_enabled = True
# ---------------------- Custom Functions --------------------------------------------
def portal_lights(*args, **kwargs):
keyboard.pixels.animation_mode = 'static_standby'
keyboard.pixels.disable_auto_write = True
keyboard.pixels.rgb_animation_mode = 'User'
for i in range(0, 9):
keyboard.pixels.set_hsv(10, 100, 100, i)
for i in range(10, 16):
@ -41,8 +41,21 @@ def portal_lights(*args, **kwargs):
def portal_off(*args, **kwargs):
keyboard.pixels.disable_auto_write = False
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 --------------------------------------------
@ -80,8 +93,8 @@ keyboard.leader_dictionary = {
'fu': emoticons.F,
'meh': emoticons.MEH,
'yay': emoticons.YAY,
'p': LON,
'po': LOFF,
'p': LON,
'po': LOFF,
}
# ---------------------- Keymap ---------------------------------------------------------