Config changes and uses consts to save ram

This commit is contained in:
Kyle Brown 2019-03-16 12:29:16 -07:00
parent 86b8c4ffb0
commit 9d8682c866
6 changed files with 121 additions and 114 deletions

View File

@ -14,15 +14,15 @@ Want your keyboard to shine? Add some lights!
|`KC.LED_MODE_BREATHE` |`LED_M_B` |Breathing animation |
## Configuration
|Define |Default |Description |
|-------------------------------|-------------|------------------------------------------------|
|`keyboard.led_brightness_step` |`5` |The number of steps to change the brightness by |
|`keyboard.led_brightness_limit`|`100` |The maximum brightness level in percent |
|Define |Default |Description |
|-----------------------------------------|-------------|------------------------------------------------|
|`keyboard.led_config['brightness_step']` |`5` |The number of steps to change the brightness by |
|`keyboard.led_config['brightness_limit']`|`100` |The maximum brightness level in percent |
## Built-in Animation Configuration
|Define |Default |Description |
|-------------------------------|-------------|-------------------------------------------------------------------------------------|
|`keyboard.led_breath_center` |`1.5` |Used to calculate the curve for the breathing animation. Anywhere from 1.0 - 2.7 is valid|
|Define |Default |Description |
|-----------------------------------------|-------------|-------------------------------------------------------------------------------------|
|`keyboard.led_config['breath_center']` |`1.5` |Used to calculate the curve for the breathing animation. Anywhere from 1.0 - 2.7 is valid|
## Functions

View File

@ -15,8 +15,8 @@ Currently we support the following addressable LEDs:
KMK uses [Hue, Saturation, and Value](https://en.wikipedia.org/wiki/HSL_and_HSV) to select colors rather than RGB. The color wheel below demonstrates how this works.
Changing the **Hue** cycles around the circle.
Changing the **Saturation** moves between the inner and outer sections of the wheel, affecting the intensity of the color.
Changing the **Hue** cycles around the circle.
Changing the **Saturation** moves between the inner and outer sections of the wheel, affecting the intensity of the color.
Changing the **Value** sets the overall brightness.
## [Keycodes]
@ -39,19 +39,22 @@ Changing the **Value** sets the overall brightness.
|`KC.RGB_MODE_KNIGHT` |`RGB_M_K` |Knightrider animation |
## Configuration
|Define |Default |Description |
|-------------------------|-------------|-----------------------------------------------------------------------------|
|`keyboard.rgb_order` |`(1, 0, 2)` |The order of the pixels R G B, and optionally white. Example(1, 0, 2, 3) |
|`keyboard.rgb_hue_step` |`10` |The number of steps to cycle through the hue by |
|`keyboard.rgb_sat_step` |`17` |The number of steps to change the saturation by |
|`keyboard.rgb_val_step` |`17` |The number of steps to change the brightness by |
|`keyboard.rgb_val_limit` |`255` |The maximum brightness level |
|Define |Default |Description |
|-------------------------------------|-------------|-----------------------------------------------------------------------------|
|`keyboard.rgb_config['rgb_order']` |`(1, 0, 2)` |The order of the pixels R G B, and optionally white. Example(1, 0, 2, 3) |
|`keyboard.rgb_config['hue_step']` |`10` |The number of steps to cycle through the hue by |
|`keyboard.rgb_config['sat_step']` |`17` |The number of steps to change the saturation by |
|`keyboard.rgb_config['val_step']` |`17` |The number of steps to change the brightness by |
|`keyboard.rgb_config['hue_default']` |`0` |The default hue when the keyboard boots |
|`keyboard.rgb_config['sat_default']` |`100` |The default saturation when the keyboard boots |
|`keyboard.rgb_config['val_default']` |`100` |The default value (brightness) when the keyboard boots |
|`keyboard.rgb_config['val_limit']` |`255` |The maximum brightness level |
## Built-in Animation Configuration
|Define |Default |Description |
|-----------------------------------|-------------|-------------------------------------------------------------------------------------|
|`keyboard.rgb_breath_center` |`1.5` |Used to calculate the curve for the breathing animation. Anywhere from 1.0 - 2.7 is valid|
|`keyboard.rgb_knight_effect_length`|`4` |The number of LEDs to light up for the "Knight" animation |
|Define |Default |Description |
|----------------------------------------------|-------------|-------------------------------------------------------------------------------------|
|`keyboard.rgb_config['breathe_center']` |`1.5` |Used to calculate the curve for the breathing animation. Anywhere from 1.0 - 2.7 is valid|
|`keyboard.rgb_config['knight_effect_length']` |`4` |The number of LEDs to light up for the "Knight" animation |
## Functions

View File

@ -93,29 +93,11 @@ class Firmware:
# RGB config
rgb_pixel_pin = None
rgb_pixels = None
rgb_num_pixels = 0
rgb_order = (1, 0, 2) # GRB WS2812
rgb_val_limit = 255
rgb_hue_default = 0
rgb_sat_default = 100
rgb_val_default = rgb_val_limit
rgb_hue_step = 1
rgb_sat_step = 1
rgb_val_step = 1
rgb_animation_speed = 1
rgb_breathe_center = 1.5 # 1.0-2.7
rgb_knight_effect_length = 3
rgb_animation_mode = 'static'
rgb_config = rgb.rgb_config
# led config (mono color)
led = None
led_pin = None
led_brightness_step = 5
led_brightness_limit = 100
led_breathe_center = 1.5
led_animation_mode = 'static'
led_animation_speed = 1
led_config = led.led_config
def __init__(self):
# Attempt to sanely guess a coord_mapping if one is not provided
@ -240,21 +222,14 @@ class Firmware:
self.uart = self.init_uart(self.uart_pin)
if self.rgb_pixel_pin:
self.pixels = rgb.RGB(self.rgb_pixel_pin, self.rgb_order, self.rgb_num_pixels,
self.rgb_hue_step, self.rgb_sat_step, self.rgb_val_step,
self.rgb_hue_default, self.rgb_sat_default, self.rgb_val_default,
self.rgb_breathe_center, self.rgb_knight_effect_length,
self.rgb_val_limit, self.rgb_animation_mode,
self.rgb_animation_speed,
)
self.pixels = rgb.RGB(self.rgb_config, self.rgb_pixel_pin)
self.rgb_config = None # No longer needed
else:
self.pixels = None
if self.led_pin:
self.led = led.led(self.led_pin, self.led_brightness_step, self.led_brightness_limit,
self.led_animation_mode, self.led_animation_speed,
self.led_breathe_center,
)
self.led = led.led(self.led_pin, self.led_config)
self.led_config = None # No longer needed
else:
self.led = None

View File

@ -1,8 +1,16 @@
import time
from math import e, exp, pi, sin
from micropython import const
import pulseio
led_config = {
'brightness_step': 5,
'brightness_limit': 100,
'breathe_center': 1.5,
'animation_mode': 'static',
'animation_speed': 1,
}
class led:
brightness = 0
@ -13,19 +21,18 @@ class led:
led = None
brightness_step = 5
brightness_limit = 100
breath_center = 1.5
breathe_center = 1.5
animation_mode = 'static'
animation_speed = 1
enabled = True
def __init__(self, led_pin, brightness_step, brightness_limit,
animation_mode, animation_speed, breath_center):
def __init__(self, led_pin, config):
self.led = pulseio.PWMOut(led_pin)
self.brightness_step = brightness_step
self.brightness_limit = brightness_limit
self.animation_mode = animation_mode
self.animation_speed = animation_speed
self.breath_center = breath_center
self.brightness_step = const(config['brightness_step'])
self.brightness_limit = const(config['brightness_limit'])
self.animation_mode = const(config['animation_mode'])
self.animation_speed = const(config['animation_speed'])
self.breathe_center = const(config['breathe_center'])
def __repr__(self):
return 'LED({})'.format(self._to_dict())
@ -37,9 +44,14 @@ class led:
'brightness_limit': self.brightness_limit,
'animation_mode': self.animation_mode,
'animation_speed': self.animation_speed,
'breath_center': self.breath_center,
'breathe_center': self.breathe_center,
}
def _init_effect(self):
self.pos = 0
self.effect_init = False
return self
def time_ms(self):
return int(time.monotonic() * 1000)
@ -94,7 +106,7 @@ class led:
def effect_breathing(self):
# http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
# https://github.com/qmk/qmk_firmware/blob/9f1d781fcb7129a07e671a46461e501e3f1ae59d/quantum/rgblight.c#L806
self.brightness = int((exp(sin((self.pos / 255.0) * pi)) - self.breath_center / e) *
self.brightness = int((exp(sin((self.pos / 255.0) * pi)) - self.breathe_center / e) *
(self.brightness_limit / (e - 1 / e)))
self.pos = (self.pos + self.animation_speed) % 256
self.set_brightness(self.brightness)
@ -113,7 +125,7 @@ class led:
:return: Returns the new state in animation
"""
if self.effect_init:
self.init_effect()
self._init_effect()
if self.enabled:
if self.animation_mode == 'breathing':
return self.effect_breathing()

View File

@ -1,6 +1,24 @@
import time
from math import e, exp, pi, sin
from micropython import const
rgb_config = {
'pixels': None,
'num_pixels': 0,
'pixel_pin': None,
'val_limit': 255,
'hue_default' : 0,
'sat_default': 100,
'rgb_order': (1, 0, 2), # GRB WS2812
'val_default': 100,
'hue_step': 1,
'sat_step': 1,
'val_step': 1,
'animation_speed': 1,
'breathe_center': 1.5, # 1.0-2.7
'knight_effect_length': 3,
'animation_mode': 'static'
}
class RGB:
hue = 0
@ -15,43 +33,39 @@ class RGB:
rgbw = False
reverse_animation = False
disable_auto_write = False
animation_mode = 'static'
# Set by config
num_pixels = 0
hue_step = 10
sat_step = 17
val_step = 17
breath_center = 1.5 # 1.0-2.7
knight_effect_length = 4
val_limit = 255
animation_mode = 'static'
num_pixels = None
hue_step = None
sat_step = None
val_step = None
breathe_center = None # 1.0-2.7
knight_effect_length = None
val_limit = None
effect_init = False
def __init__(self, pixel_pin, rgb_order, num_pixels,
hue_step, sat_step, val_step,
hue_default, sat_default, val_default,
breath_center, knight_effect_length,
val_limit, animation_mode, animation_speed):
def __init__(self, config, pixel_pin):
try:
import neopixel
self.neopixel = neopixel.NeoPixel(pixel_pin,
num_pixels,
pixel_order=rgb_order,
config['num_pixels'],
pixel_order=config['rgb_order'],
auto_write=False)
if len(rgb_order) == 4:
if len(config['rgb_order']) == 4:
self.rgbw = True
self.num_pixels = num_pixels
self.hue_step = hue_step
self.sat_step = sat_step
self.val_step = val_step
self.hue = hue_default
self.sat = sat_default
self.val = val_default
self.breath_center = breath_center
self.knight_effect_length = knight_effect_length
self.val_limit = val_limit
self.rgb_animation_mode = animation_mode
self.animation_speed = animation_speed
self.num_pixels = const(config['num_pixels'])
self.hue_step = const(config['hue_step'])
self.sat_step = const(config['sat_step'])
self.val_step = const(config['val_step'])
self.hue = const(config['hue_default'])
self.sat = const(config['sat_default'])
self.val = const(config['val_default'])
self.breathe_center = const(config['breathe_center'])
self.knight_effect_length = const(config['knight_effect_length'])
self.val_limit = const(config['val_limit'])
self.rgb_animation_mode = const(config['animation_mode'])
self.animation_speed = const(config['animation_speed'])
except ImportError as e:
print(e)
@ -399,7 +413,7 @@ class RGB:
def effect_breathing(self):
# http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
# https://github.com/qmk/qmk_firmware/blob/9f1d781fcb7129a07e671a46461e501e3f1ae59d/quantum/rgblight.c#L806
self.val = int((exp(sin((self.pos / 255.0) * pi)) - self.breath_center / e) *
self.val = int((exp(sin((self.pos / 255.0) * pi)) - self.breathe_center / e) *
(self.val_limit / (e - 1 / e)))
self.pos = (self.pos + self.animation_speed) % 256
self.set_hsv_fill(self.hue, self.sat, self.val)

View File

@ -10,25 +10,29 @@ keyboard = Firmware()
keyboard.leader_mode = LeaderMode.ENTER
keyboard.unicode_mode = UnicodeMode.LINUX
keyboard.tap_time = 150
keyboard.leader_timeout = 999999999999
keyboard.rgb_num_pixels = 16
keyboard.rgb_hue_default = 260
keyboard.rgb_sat_default = 100
keyboard.rgb_val_default = 20
keyboard.rgb_knight_effect_length = 6
keyboard.rgb_animation_mode = 'static'
keyboard.rgb_animation_speed = 3
keyboard.debug_enabled = True
keyboard.leader_timeout = 999999
keyboard.rgb_config['num_pixels'] = 16
keyboard.rgb_config['val_limit'] = 150
keyboard.rgb_config['hue_step'] = 10
keyboard.rgb_config['sat_step'] = 5
keyboard.rgb_config['val_step'] = 5
keyboard.rgb_config['hue_default'] = 260
keyboard.rgb_config['sat_default'] = 100
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
# ---------------------- Custom Functions --------------------------------------------
def portal_lights(*args, **kwargs):
keyboard.pixels.disable_auto_write = True
keyboard.pixels.rgb_animation_mode = 'User'
for i in range(0, 9):
keyboard.pixels.set_hsv(21, 100, 100, i)
keyboard.pixels.set_hsv(10, 100, 100, i)
for i in range(10, 16):
keyboard.pixels.set_hsv(220, 100, 100, i)
keyboard.pixels.show()
@ -44,7 +48,6 @@ def portal_off(*args, **kwargs):
LON = make_key(on_press=portal_lights)
LOFF = make_key(on_press=portal_off)
_______ = KC.TRNS
XXXXXXX = KC.NO
HOME = KC.MT(KC.HOME, KC.LSFT)
@ -73,12 +76,12 @@ keyboard.leader_dictionary = {
'flip': emoticons.ANGRY_TABLE_FLIP,
'cheer': emoticons.CHEER,
'wat': emoticons.WAT,
'ff': emoticons.FF,
'f': emoticons.F,
'f': emoticons.FF,
'fu': emoticons.F,
'meh': emoticons.MEH,
'yay': emoticons.YAY,
'p': LON,
'po': LOFF,
'p': LON,
'po': LOFF,
}
# ---------------------- Keymap ---------------------------------------------------------
@ -91,20 +94,20 @@ keyboard.keymap = [
[KC.COMM, KC.DOT, KC.P, KC.Y, KC.F, KC.G, KC.C, KC.R],
[KC.L, KC.SLSH, KC.EQL, KC.BSLS, KC.TAB, KC.A, KC.O, KC.E],
[KC.U, KC.I, KC.D, KC.H, KC.T, KC.N, KC.S, KC.MINS],
[KC.ENT, KC.LSFT, KC.SCLN, KC.Q, KC.J, KC.K, KC.X, KC.B],
[KC.M, KC.W, KC.V, KC.Z, KC.RSFT, KC.LCTL, KC.LGUI, KC.MO(FN1)],
[KC.ENT, HOME, KC.SCLN, KC.Q, KC.J, KC.K, KC.X, KC.B],
[KC.M, KC.W, KC.V, KC.Z, END, KC.LCTL, KC.LGUI, KC.MO(FN1)],
[KC.SPC, KC.LEFT, KC.DOWN, KC.UP, KC.RIGHT],
],
[
[KC.GESC, KC.F1, KC.F2, KC.F3, KC.F4, KC.F5, KC.F6, KC.F7],
[KC.F8, KC.F9, KC.F10, KC.F11, KC.F12, KC.DEL, KC.RGB_TOG, _______],
[_______, _______, _______, _______, _______, _______, _______, _______],
[_______, _______, KC.VOLU, _______, _______, _______, _______, _______],
[KC.F8, KC.F9, KC.F10, KC.F11, KC.F12, KC.DEL, KC.RGB_TOG, KC.RGB_HUD],
[KC.RGB_HUI, _______, _______, _______, _______, _______, _______, _______],
[_______, _______, KC.VOLU, _______, _______, KC.RGB_SAD, KC.RGB_SAI, _______],
[_______, _______, _______, _______, _______, _______, _______, KC.VOLD],
[_______, _______, _______, _______, _______, _______, _______, _______],
[_______, _______, KC.RGB_VAD, KC.RGB_VAI, _______, _______, _______, _______],
[_______, _______, _______, _______, _______, KC.RGB_M_K, _______, _______],
[_______, KC.LALT, KC.RGB_M_S, LON, LOFF],
[_______, KC.LALT, KC.RGB_M_S, _______, _______],
],
]