Breathing now finally working, a key to enable it, not sane defaults, and a toggle button

This commit is contained in:
Kyle Brown 2019-02-20 20:40:46 -08:00
parent 0ba5911f8f
commit 31983a0873
5 changed files with 78 additions and 49 deletions

View File

@ -277,4 +277,5 @@ class Firmware:
if self.pixel_state['animation_mode'] is not None: if self.pixel_state['animation_mode'] is not None:
self.pixel_state = rgb.animate(self.pixel_state, self.pixels) self.pixel_state = rgb.animate(self.pixel_state, self.pixels)
gc.collect() gc.collect()

View File

@ -118,3 +118,14 @@ def td_pressed(key, state, *args, **kwargs):
def td_released(key, state, *args, **kwargs): def td_released(key, state, *args, **kwargs):
return state._process_tap_dance(key, False) return state._process_tap_dance(key, False)
def rgb_tog(key, state, *args, **kwargs):
state.config.pixel_state['enable'] = not state.config.pixel_state['enable']
return state
def rgb_mode_breathe(key, state, *args, **kwargs):
state.config.pixel_state['animation_mode'] = 'breathing'
return state

View File

@ -625,6 +625,9 @@ make_key(names=('DEBUG', 'DBG'), on_press=handlers.debug_pressed, on_release=han
make_key(names=('GESC',), on_press=handlers.gesc_pressed, on_release=handlers.gesc_released) make_key(names=('GESC',), on_press=handlers.gesc_pressed, on_release=handlers.gesc_released)
make_key(names=('BKDL',), on_press=handlers.bkdl_pressed, on_release=handlers.bkdl_released) make_key(names=('BKDL',), on_press=handlers.bkdl_pressed, on_release=handlers.bkdl_released)
make_key(names=('GESC', 'GRAVE_ESC'), on_press=handlers.gesc_pressed, on_release=handlers.gesc_released)
make_key(names=('RGB_TOG',), on_press=handlers.rgb_tog)
make_key(names=('RGB_MODE_BREATHE', 'RGB_M_B'), on_press=handlers.rgb_mode_breathe)
make_key( make_key(
names=('LEADER', 'LEAD'), names=('LEADER', 'LEAD'),
on_press=handlers.leader_pressed, on_press=handlers.leader_pressed,

View File

@ -1,4 +1,5 @@
from math import sin, exp, pi from math import sin, exp, pi, floor
from math import e as M_E
import time import time
COLORS = { COLORS = {
@ -15,31 +16,30 @@ COLORS = {
def pixelinit(): def pixelinit():
return { return {
'h': 0, 'h': 180,
's': 255, 's': 100,
'v': 255, 'v': 80,
'animation_mode': None, 'animation_mode': 'Breathing',
'pos': 0, 'pos': 0,
'timer': None, 'time': time_ms(),
'intervals': (0, 0, 0, 0), 'intervals': (30, 20, 10, 5),
'speed': 120, # Bigger is slower 'speed': 120, # Bigger is slower
'enable': True 'enable': True
} }
def time_ms(): def time_ms():
return time.monotonic_ns() / 10 return floor(time.monotonic() * 10)
def hsv_to_rgb(hue, sat, val): def hsv_to_rgb(hue, sat, val):
r = 0 r = 0
g = 0 g = 0
b = 0 b = 0
# TODO Actually pass this limit to allow overrides
RGBLIGHT_LIMIT_VAL = 255 RGBLIGHT_LIMIT_VAL = 255
if val > RGBLIGHT_LIMIT_VAL: if val > 255:
val=RGBLIGHT_LIMIT_VAL val = 255
if sat == 0: if sat == 0:
r = val r = val
@ -50,33 +50,33 @@ def hsv_to_rgb(hue, sat, val):
base = ((255 - sat) * val) >> 8 base = ((255 - sat) * val) >> 8
color = (val - base) * (hue % 60) / 60 color = (val - base) * (hue % 60) / 60
x = hue / 60 x = floor(hue / 60)
if x == 0: if x == 0:
r = val r = val
g = base + color g = base + color
b = base b = base
elif x == 1: elif x == 1:
r = val - color r = val - color
g = val g = val
b = base b = base
elif x == 2: elif x == 2:
r = base r = base
g = val g = val
b = base + color b = base + color
elif x == 3: elif x == 3:
r = base r = base
g = val - color g = val - color
b = val b = val
elif x == 4: elif x == 4:
r = base + color r = base + color
g = base g = base
b = val b = val
elif x == 5: elif x == 5:
r = val r = val
g = base g = base
b = val - color b = val - color
return r, g, b return floor(r), floor(g), floor(b)
def set_hsv(hue, sat, val, pixels, index): def set_hsv(hue, sat, val, pixels, index):
@ -99,27 +99,37 @@ def set_rgb_fill(rgb, pixels):
def increase_hue(hue, step): def increase_hue(hue, step):
return hue + step % 360 return (hue + step) % 360
def decrease_hue(hue, step): def decrease_hue(hue, step):
if hue - step < 0: if hue - step < 0:
return (hue + 360 - step) % 360 return (hue + 360 - step) % 360
else: else:
return hue - step % 360 return (hue - step) % 360
def off(pixels):
set_hsv_fill(0, 0, 0, pixels)
def animate(state, pixels): def animate(state, pixels):
if state['animation_mode'] == 'breathing': if state['enable']:
return effect_breathing(state, pixels) if state['animation_mode'] == 'breathing':
elif state['animation_mode'] == 'rainbow': return effect_breathing(state, pixels)
return effect_rainbow(state, pixels) elif state['animation_mode'] == 'rainbow':
return effect_rainbow(state, pixels)
else:
off(pixels)
return state return state
def animation_step(state): def animation_step(state):
interval = state['time'] - time_ms() interval = time_ms() - state['time']
if interval >= max(state['intervals']):
state['time'] = time_ms()
return max(state['intervals'])
if interval in state['intervals']: if interval in state['intervals']:
return interval return interval
else: else:
@ -127,10 +137,14 @@ def animation_step(state):
def effect_breathing(state, pixels): def effect_breathing(state, pixels):
if animation_step(state): RGBLIGHT_EFFECT_BREATHE_CENTER = 1.5 # 1.0-2.7
# http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ RGBLIGHT_EFFECT_BREATHE_MAX = 150 # 0-255
state['v'] = (exp(sin(state['step'] / 2000.0 * pi)) - 0.36787944) * 108.0 interval = time_ms() - state['time']
set_hsv_fill(state['h'], state['s'], state['v'], pixels) # http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
# https://github.com/qmk/qmk_firmware/blob/9f1d781fcb7129a07e671a46461e501e3f1ae59d/quantum/rgblight.c#L787
state['v'] = floor((exp(sin((state['pos']/255.0)*pi)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E)))
state['pos'] = (state['pos'] + 1) % 256;
set_hsv_fill(state['h'], state['s'], state['v'], pixels)
return state return state

View File

@ -169,7 +169,7 @@ keyboard.keymap = [
[KC.GRV, KC.QUOTE, KC.COMMA, KC.DOT, KC.P, KC.Y, KC.F, KC.G, KC.C, KC.R, KC.L, KC.BKSP], [KC.GRV, KC.QUOTE, KC.COMMA, KC.DOT, KC.P, KC.Y, KC.F, KC.G, KC.C, KC.R, KC.L, KC.BKSP],
[KC.TAB, KC.A, KC.O, KC.E, KC.U, KC.I, KC.D, KC.H, KC.T, KC.N, KC.S, KC.ENT], [KC.TAB, KC.A, KC.O, KC.E, KC.U, KC.I, KC.D, KC.H, KC.T, KC.N, KC.S, 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.SLSH], [KC.LSFT, KC.SCLN, KC.Q, KC.J, KC.K, KC.X, KC.B, KC.M, KC.W, KC.V, KC.Z, KC.SLSH],
[KC.LCTRL, KC.LGUI, KC.LALT, KC.LEAD, LAYER_1, KC.LT(r2, KC.SPC), KC.LT(r2, KC.SPC), LAYER_3, KC.LEFT, KC.DOWN, KC.UP, KC.RIGHT], [KC.LCTRL, KC.LGUI, KC.LALT, KC.RGB_TOG, LAYER_1, KC.LT(r2, KC.SPC), KC.LT(r2, KC.SPC), LAYER_3, KC.LEFT, KC.DOWN, KC.UP, KC.RIGHT],
], ],
[ [
# gw # gw