Added support for user animations with docs
This commit is contained in:
		
							
								
								
									
										30
									
								
								docs/led.md
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								docs/led.md
									
									
									
									
									
								
							@@ -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_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.
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								docs/rgb.md
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								docs/rgb.md
									
									
									
									
									
								
							@@ -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.
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								kmk/rgb.py
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								kmk/rgb.py
									
									
									
									
									
								
							@@ -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:
 | 
			
		||||
 
 | 
			
		||||
@@ -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 --------------------------------------------
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user