Massively improved, still not complete. Much easier to use now

This commit is contained in:
Kyle Brown 2019-02-21 18:27:35 -08:00
parent 31983a0873
commit 2ad1fe8a9c
5 changed files with 339 additions and 142 deletions

View File

@ -92,8 +92,8 @@ class Firmware:
uart_pin = None uart_pin = None
pixel_pin = None pixel_pin = None
num_pixels = None num_pixels = None
rgb_order = (1, 0, 2) # GRB WS2812
pixels = None pixels = None
pixel_state = rgb.pixelinit()
def __init__(self): def __init__(self):
# Attempt to sanely guess a coord_mapping if one is not provided # Attempt to sanely guess a coord_mapping if one is not provided
@ -188,13 +188,6 @@ class Firmware:
else: else:
return busio.UART(tx=pin, rx=None, timeout=timeout) return busio.UART(tx=pin, rx=None, timeout=timeout)
def init_pixels(self, pixel_pin, num_pixels=0):
try:
import neopixel
return neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False)
except ImportError as e:
print(e)
return None
def go(self): def go(self):
assert self.keymap, 'must define a keymap with at least one row' assert self.keymap, 'must define a keymap with at least one row'
@ -217,7 +210,8 @@ class Firmware:
self.uart = self.init_uart(self.uart_pin) self.uart = self.init_uart(self.uart_pin)
if self.pixel_pin is not None: if self.pixel_pin is not None:
self.pixels = self.init_pixels(self.pixel_pin, self.num_pixels) self.pixels = rgb.RGB(self.pixel_pin, self.rgb_order, self.num_pixels)
self.matrix = MatrixScanner( self.matrix = MatrixScanner(
cols=self.col_pins, cols=self.col_pins,
@ -274,8 +268,8 @@ class Firmware:
if self.debug_enabled and state_changed: if self.debug_enabled and state_changed:
print('New State: {}'.format(self._state._to_dict())) print('New State: {}'.format(self._state._to_dict()))
if self.pixel_state['animation_mode'] is not None: if self.pixels.animation_mode is not None:
self.pixel_state = rgb.animate(self.pixel_state, self.pixels) self.pixels = self.pixels.animate()
gc.collect() gc.collect()

View File

@ -121,11 +121,61 @@ def td_released(key, state, *args, **kwargs):
def rgb_tog(key, state, *args, **kwargs): def rgb_tog(key, state, *args, **kwargs):
state.config.pixel_state['enable'] = not state.config.pixel_state['enable'] state.config.pixels.enabled = not state.config.pixels.enabled
return state
def rgb_forward(key, state, *args, **kwargs):
# TODO
return state
def rgb_reverse(key, state, *args, **kwargs):
# TODO
return state
def rgb_hui(key, state, *args, **kwargs):
state.config.pixels.increase_hue(state.config.pixels.hue_step)
return state
def rgb_hud(key, state, *args, **kwargs):
state.config.pixels.decrease_hue(state.config.pixels.hue_step)
return state
def rgb_sai(key, state, *args, **kwargs):
state.config.pixels.increase_sat(state.config.pixels.sat_step)
return state
def rgb_sad(key, state, *args, **kwargs):
state.config.pixels.decrease_sat(state.config.pixels.sat_step)
return state
def rgb_vai(key, state, *args, **kwargs):
state.config.pixels.increase_val(state.config.pixels.val_step)
return state
def rgb_vad(key, state, *args, **kwargs):
state.config.pixels.decrease_val(state.config.pixels.val_step)
return state
def rgb_mode_static(key, state, *args, **kwargs):
state.config.pixels.animation_mode = 'static'
return state return state
def rgb_mode_breathe(key, state, *args, **kwargs): def rgb_mode_breathe(key, state, *args, **kwargs):
state.config.pixel_state['animation_mode'] = 'breathing' state.config.pixels.animation_mode = 'breathing'
return state
def rgb_mode_rainbow(key, state, *args, **kwargs):
state.config.pixels.animation_mode = 'rainbow'
return state return state

View File

@ -627,7 +627,17 @@ make_key(names=('GESC',), on_press=handlers.gesc_pressed, on_release=handlers.ge
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=('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_TOG',), on_press=handlers.rgb_tog)
make_key(names=('RGB_MODE_FORWARD', 'RGB_MOD'), on_press=handlers.rgb_forward)
make_key(names=('RGB_MODE_REVERSE', 'RGB_RMOD'), on_press=handlers.rgb_reverse)
make_key(names=('RGB_HUI',), on_press=handlers.rgb_hui)
make_key(names=('RGB_HUD',), on_press=handlers.rgb_hud)
make_key(names=('RGB_SAI',), on_press=handlers.rgb_sai)
make_key(names=('RGB_SAD',), on_press=handlers.rgb_sad)
make_key(names=('RGB_VAI',), on_press=handlers.rgb_vai)
make_key(names=('RGB_VAD',), on_press=handlers.rgb_vad)
make_key(names=('RGB_MODE_PLAIN', 'RGB_M_P'), on_press=handlers.rgb_mode_static)
make_key(names=('RGB_MODE_BREATHE', 'RGB_M_B'), on_press=handlers.rgb_mode_breathe) make_key(names=('RGB_MODE_BREATHE', 'RGB_M_B'), on_press=handlers.rgb_mode_breathe)
make_key(names=('RGB_MODE_RAINBOW', 'RGB_M_R'), on_press=handlers.rgb_mode_rainbow)
make_key( make_key(
names=('LEADER', 'LEAD'), names=('LEADER', 'LEAD'),
on_press=handlers.leader_pressed, on_press=handlers.leader_pressed,

View File

@ -2,172 +2,307 @@ from math import sin, exp, pi, floor
from math import e as M_E from math import e as M_E
import time import time
COLORS = {
'OFF': (0, 0, 0),
'RED': (255, 0, 0),
'GREEN': (0, 255, 0),
'BLUE': (0, 0, 255, 0),
'YELLOW': (255, 150, 0),
'CYAN': (0, 255, 255),
'PURPLE': (180, 0, 255),
'WHITE': (255, 255, 255),
}
class RGB:
hue = 240
sat = 100
val = 80
animation_mode = 'breathing'
pos = 0
time = floor(time.monotonic() * 10)
intervals = (30, 20, 10, 5)
speed = 120 # Bigger is slower
enabled = True
neopixel = None
rgbw = False
num_pixels = 0
disable_auto_write = False
hue_step = 5
sat_step = 5
val_step = 5
limit_val = 255
def pixelinit(): def __init__(self, pixel_pin, rgb_order, num_pixels=0):
return { try:
'h': 180, import neopixel
's': 100, self.neopixel = neopixel.NeoPixel(pixel_pin,
'v': 80, num_pixels,
'animation_mode': 'Breathing', pixel_order=rgb_order,
'pos': 0, auto_write=False)
'time': time_ms(), if len(rgb_order) == 4:
'intervals': (30, 20, 10, 5), self.rgbw = True
'speed': 120, # Bigger is slower self.num_pixels = num_pixels
'enable': True
}
except ImportError as e:
print(e)
def time_ms(): def __repr__(self):
return floor(time.monotonic() * 10) return 'RGB({})'.format(self._to_dict())
def _to_dict(self):
ret = {
'hue': self.hue,
'sat': self.sat,
'val': self.val,
'animation_mode': self.animation_mode,
'time': self.time,
'intervals': self.intervals,
'speed': self.speed,
'enabled': self.enabled,
'neopixel': self.neopixel,
'disable_auto_write': self.disable_auto_write,
}
def hsv_to_rgb(hue, sat, val): return ret
r = 0
g = 0
b = 0
RGBLIGHT_LIMIT_VAL = 255
if val > 255: def time_ms(self):
val = 255 return floor(time.monotonic() * 10)
if sat == 0: def hsv_to_rgb(self, hue, sat, val):
r = val '''
g = val Converts HSV values, and returns a tuple of RGB values
b = val :param hue:
:param sat:
:param val:
:return: (r, g, b)
'''
r = 0
g = 0
b = 0
self.limit_val = 255
else: if val > 255:
base = ((255 - sat) * val) >> 8 val = 255
color = (val - base) * (hue % 60) / 60
x = floor(hue / 60) if sat == 0:
if x == 0:
r = val r = val
g = base + color
b = base
elif x == 1:
r = val - color
g = val g = val
b = base
elif x == 2:
r = base
g = val
b = base + color
elif x == 3:
r = base
g = val - color
b = val b = val
elif x == 4:
r = base + color
g = base
b = val
elif x == 5:
r = val
g = base
b = val - color
return floor(r), floor(g), floor(b) else:
base = ((255 - sat) * val) >> 8
color = (val - base) * (hue % 60) / 60
x = floor(hue / 60)
if x == 0:
r = val
g = base + color
b = base
elif x == 1:
r = val - color
g = val
b = base
elif x == 2:
r = base
g = val
b = base + color
elif x == 3:
r = base
g = val - color
b = val
elif x == 4:
r = base + color
g = base
b = val
elif x == 5:
r = val
g = base
b = val - color
def set_hsv(hue, sat, val, pixels, index): return floor(r), floor(g), floor(b)
set_rgb(hsv_to_rgb(hue, sat, val), pixels, index)
def hsv_to_rgbw(self, hue, sat, val):
'''
Converts HSV values, and returns a tuple of RGBW values
:param hue:
:param sat:
:param val:
:return: (r, g, b, w)
'''
rgb = self.hsv_to_rgb(hue, sat, val)
return rgb[0], rgb[1], rgb[2], min(rgb)
def set_hsv_fill(hue, sat, val, pixels): def set_hsv(self, hue, sat, val, index):
pixels.fill(hsv_to_rgb(hue, sat, val)) '''
pixels.show() Takes HSV values and displays it on a single LED/Neopixel
:param hue:
:param sat:
:param val:
:param index: Index of LED/Pixel
'''
if self.neopixel:
if self.rgbw:
self.set_rgb(self.hsv_to_rgbw(hue, sat, val), index)
else:
self.set_rgb(self.hsv_to_rgb(hue, sat, val), index)
def set_hsv_fill(self, hue, sat, val):
'''
Takes HSV values and displays it on all LEDs/Neopixels
:param hue:
:param sat:
:param val:
:param index: Index of LED/Pixel
'''
if self.neopixel:
if self.rgbw:
self.neopixel.fill(self.hsv_to_rgbw(hue, sat, val))
else:
self.neopixel.fill(self.hsv_to_rgb(hue, sat, val))
self.neopixel.show()
def set_rgb(rgb, pixels, index): def set_rgb(self, rgb, index):
pixels[index] = (rgb[0], rgb[1], rgb[2]) '''
pixels.show() Takes an RGB or RGBW and displays it on a single LED/Neopixel
:param rgb: RGB or RGBW
:param index: Index of LED/Pixel
'''
if self.neopixel:
self.neopixel[index] = rgb
if not self.disable_auto_write:
self.neopixel.show()
def set_rgb_fill(self, rgb):
'''
Takes an RGB or RGBW and displays it on all LEDs/Neopixels
:param rgb: RGB or RGBW
'''
if self.neopixel:
self.neopixel.fill(rgb)
self.neopixel.show()
def set_rgb_fill(rgb, pixels): def increase_hue(self, step):
pixels.fill(rgb[0], rgb[1], rgb[2]) '''
pixels.show() Increases hue by step amount rolling at 360 and returning to 0
:param step:
'''
self.hue = (self.hue + step) % 360
def decrease_hue(self, step):
'''
Decreases hue by step amount rolling at 0 and returning to 360
:param step:
'''
if self.hue - step < 0:
self.hue = (self.hue + 360 - step) % 360
else:
self.hue = (self.hue - step) % 360
def increase_hue(hue, step): def increase_sat(self, step):
return (hue + step) % 360 '''
Increases saturation by step amount stopping at 100
:param step:
'''
if self.sat + step >= 100:
self.sat = 100
else:
self.sat += step
def decrease_sat(self, step):
'''
Decreases saturation by step amount stopping at 0
:param step:
'''
if self.sat + step <= 0:
self.sat = 0
else:
self.sat -= step
def decrease_hue(hue, step): def increase_val(self, step):
if hue - step < 0: '''
return (hue + 360 - step) % 360 Increases value by step amount stopping at 100
else: :param step:
return (hue - step) % 360 '''
if self.val + step >= 100:
self.val = 100
else:
self.val += step
def decrease_val(self, step):
'''
Decreases value by step amount stopping at 0
:param step:
'''
if self.val + step <= 0:
self.val = 0
else:
self.val -= step
def off(pixels): def off(self):
set_hsv_fill(0, 0, 0, pixels) '''
Turns off all LEDs/Neopixels without changing stored values
'''
if self.neopixel:
if not self.disable_auto_write:
self.set_hsv_fill(0, 0, 0)
def show(self):
'''
Turns on all LEDs/Neopixels without changing stored values
'''
if self.neopixel:
self.neopixel.show()
def animate(state, pixels): def animate(self):
if state['enable']: '''
if state['animation_mode'] == 'breathing': Activates a "step" in the animation based on the active mode
return effect_breathing(state, pixels) :return: Returns the new state in animation
elif state['animation_mode'] == 'rainbow': '''
return effect_rainbow(state, pixels) if self.enabled:
else: if self.animation_mode == 'breathing':
off(pixels) return self.effect_breathing()
elif self.animation_mode == 'rainbow':
return self.effect_rainbow()
if self.animation_mode == 'static':
return self.effect_static()
else:
self.off()
return state return self
def animation_step(self):
interval = self.time_ms() - self.time
if interval >= max(self.intervals):
self.time = self.time_ms()
return max(self.intervals)
if interval in self.intervals:
return interval
else:
return False
def animation_step(state): def effect_static(self):
interval = time_ms() - state['time'] self.set_hsv_fill(self.hue, self.sat, self.val)
if interval >= max(state['intervals']): return self
state['time'] = time_ms()
return max(state['intervals'])
if interval in state['intervals']:
return interval
else:
return False
def effect_breathing(self):
RGBLIGHT_EFFECT_BREATHE_CENTER = 1.5 # 1.0-2.7
RGBLIGHT_EFFECT_BREATHE_MAX = 100 # 0-255
# http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
# https://github.com/qmk/qmk_firmware/blob/9f1d781fcb7129a07e671a46461e501e3f1ae59d/quantum/rgblight.c#L787
self.val = floor((exp(sin((self.pos/255.0)*pi)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*
(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E)))
self.pos = (self.pos + 1) % 256;
self.set_hsv_fill(self.hue, self.sat, self.val)
def effect_breathing(state, pixels): return self
RGBLIGHT_EFFECT_BREATHE_CENTER = 1.5 # 1.0-2.7
RGBLIGHT_EFFECT_BREATHE_MAX = 150 # 0-255
interval = time_ms() - state['time']
# 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 def effect_rainbow(self):
if self.animation_step(self):
self.increase_hue(self.hue, 1)
self.set_hsv_fill(self.hue, self.sat, self.val)
return self
def effect_rainbow(state, pixels): def effect_rainbow_swirl(self):
if animation_step(state): interval = self.animation_step(self)
state['h'] = increase_hue(state['h'], 1) if interval:
set_hsv_fill(state['h'], state['s'], state['v'], pixels) for i in range(0, self.num_pixels):
self.hue = (360 / self.num_pixels * i + self.hue) % 360
self.set_hsv_fill(self.hue, self.sat, self.val)
return state if interval % 2:
self.increase_hue(self.hue, 1)
else:
self.decrease_hue(self.hue, 1)
return self
def effect_rainbow_swirl(state, pixels):
interval = animation_step(state)
if interval:
MAX_RGB_NUM = 12 # TODO Actually pass this
for i in range(0, MAX_RGB_NUM):
state['h'] = (360 / MAX_RGB_NUM * i + state['h']) % 360
set_hsv_fill(state['h'], state['s'], state['v'], pixels)
if interval % 2:
state['h'] = increase_hue(state['h'], 1)
else:
state['h'] = decrease_hue(state['h'], 1)
return state

View File

@ -69,48 +69,56 @@ r3 = 4
def base(*args, **kwargs): def base(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'breathing'
keyboard.pixels.fill(OFF) keyboard.pixels.fill(OFF)
keyboard.pixels.show() keyboard.pixels.show()
return df_pressed(*args, **kwargs) return df_pressed(*args, **kwargs)
def layer1p(*args, **kwargs): def layer1p(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'User'
keyboard.pixels.fill(WHITE) keyboard.pixels.fill(WHITE)
keyboard.pixels.show() keyboard.pixels.show()
return mo_pressed(*args, **kwargs) return mo_pressed(*args, **kwargs)
def layer1r(*args, **kwargs): def layer1r(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'breathing'
keyboard.pixels.fill(OFF) keyboard.pixels.fill(OFF)
keyboard.pixels.show() keyboard.pixels.show()
return mo_released(*args, **kwargs) return mo_released(*args, **kwargs)
def layer2p(*args, **kwargs): def layer2p(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'User'
keyboard.pixels.fill(BLUE) keyboard.pixels.fill(BLUE)
keyboard.pixels.show() keyboard.pixels.show()
return lt_pressed(*args, **kwargs) return lt_pressed(*args, **kwargs)
def layer2r(*args, **kwargs): def layer2r(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'breathing'
keyboard.pixels.fill(OFF) keyboard.pixels.fill(OFF)
keyboard.pixels.show() keyboard.pixels.show()
return lt_released(*args, **kwargs) return lt_released(*args, **kwargs)
def layer3p(*args, **kwargs): def layer3p(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'User'
keyboard.pixels.fill(PURPLE) keyboard.pixels.fill(PURPLE)
keyboard.pixels.show() keyboard.pixels.show()
return mo_pressed(*args, **kwargs) return mo_pressed(*args, **kwargs)
def layer3r(*args, **kwargs): def layer3r(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'breathing'
keyboard.pixels.fill(OFF) keyboard.pixels.fill(OFF)
keyboard.pixels.show() keyboard.pixels.show()
return mo_released(*args, **kwargs) return mo_released(*args, **kwargs)
def gaming(*args, **kwargs): def gaming(*args, **kwargs):
keyboard.pixel_state['animation_mode'] = 'User'
keyboard.pixels.fill(CYAN) keyboard.pixels.fill(CYAN)
keyboard.pixels.show() keyboard.pixels.show()
return df_pressed(*args, **kwargs) return df_pressed(*args, **kwargs)