Initial RGB with test animation running automatically

This commit is contained in:
Kyle Brown
2019-02-09 13:12:04 -08:00
parent e332804dc9
commit 4b033857b1
3 changed files with 150 additions and 10 deletions

View File

@@ -49,6 +49,16 @@ import kmk.internal_state # isort:skip
# Thanks for sticking around. Now let's do real work, starting below
from kmk.util import intify_coordinate as ic
import busio
import gc
import supervisor
from kmk.consts import LeaderMode, UnicodeMode
from kmk.hid import USB_HID
from kmk.internal_state import InternalState
from kmk.keys import KC
from kmk.matrix import MatrixScanner
from kmk import rgb
class Firmware:
@@ -80,6 +90,10 @@ class Firmware:
uart = None
uart_flip = True
uart_pin = None
pixel_pin = None
num_pixels = None
pixels = None
pixel_state = 0, 0
def __init__(self):
# Attempt to sanely guess a coord_mapping if one is not provided
@@ -174,6 +188,14 @@ class Firmware:
else:
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):
assert self.keymap, 'must define a keymap with at least one row'
assert self.row_pins, 'no GPIO pins defined for matrix rows'
@@ -194,7 +216,10 @@ class Firmware:
if self.uart_pin is not None:
self.uart = self.init_uart(self.uart_pin)
self.matrix = self.matrix_scanner(
if self.pixel_pin is not None:
self.pixels = self.init_pixels(self.pixel_pin, self.num_pixels)
self.matrix = MatrixScanner(
cols=self.col_pins,
rows=self.row_pins,
diode_orientation=self.diode_orientation,
@@ -214,6 +239,7 @@ class Firmware:
if self.debug_enabled:
print("Firin' lazers. Keyboard is booted.")
while True:
state_changed = False
@@ -248,3 +274,14 @@ class Firmware:
if self.debug_enabled and state_changed:
print('New State: {}'.format(self._state._to_dict()))
gc.collect()
test = rgb.color_chase(self.pixels,
self.num_pixels,
color=(255,0,0),
color2=(0,255,255),
animation_state=self.pixel_state[0])
if test is not None:
# Debugging some strange errors with NoneType
self.pixel_state = test

58
kmk/rgb.py Normal file
View File

@@ -0,0 +1,58 @@
OFF = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255, 0)
PURPLE = (180, 0, 255)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
def color_chase(pixels, num_pixels, color, color2=OFF, speed=100, animation_state=0):
if animation_state not in range(num_pixels):
color = color2
pixels[int(animation_state - num_pixels)] = color
else:
pixels[animation_state] = color
pixels.show()
animation_state += 1
if animation_state >= num_pixels * 2:
animation_state = 0
return animation_state, 0
def rainbow_cycle(pixels, num_pixels, speed=100, animation_state=0, color_state=0):
color_state += 1
if color_state in range(255):
print(animation_state)
animation_state +=1
if animation_state in range(num_pixels):
rc_index = (animation_state * 256 // num_pixels) + animation_state
print(pixels[animation_state])
print(wheel(rc_index & 255))
pixels[animation_state] = wheel(rc_index & 255)
else:
pixels.show()
return 0, color_state
else:
return animation_state, 0