From 4b033857b17251f05ce4c418a3d142c8eef2a870 Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Sat, 9 Feb 2019 13:12:04 -0800 Subject: [PATCH] Initial RGB with test animation running automatically --- kmk/firmware.py | 39 ++++++++++++++- kmk/rgb.py | 58 ++++++++++++++++++++++ user_keymaps/kdb424/nyquist_converter.py | 63 ++++++++++++++++++++---- 3 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 kmk/rgb.py diff --git a/kmk/firmware.py b/kmk/firmware.py index 907005b..af182ed 100644 --- a/kmk/firmware.py +++ b/kmk/firmware.py @@ -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 diff --git a/kmk/rgb.py b/kmk/rgb.py new file mode 100644 index 0000000..c7bf5b1 --- /dev/null +++ b/kmk/rgb.py @@ -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 + + + diff --git a/user_keymaps/kdb424/nyquist_converter.py b/user_keymaps/kdb424/nyquist_converter.py index ab77ffb..fcb9ffe 100644 --- a/user_keymaps/kdb424/nyquist_converter.py +++ b/user_keymaps/kdb424/nyquist_converter.py @@ -1,10 +1,11 @@ import board import busio +import neopixel from kmk.consts import DiodeOrientation, LeaderMode, UnicodeMode -from kmk.handlers.sequences import (compile_unicode_string_sequences, - simple_key_sequence) -from kmk.keys import KC +from kmk.handlers.layers import df_pressed +from kmk.handlers.sequences import (compile_unicode_string_sequences) +from kmk.keys import KC, layer_key_validator, make_argumented_key from kmk.mcus.circuitpython_samd51 import Firmware from kmk.pins import Pin as P @@ -27,6 +28,24 @@ keyboard.tap_time = 150 keyboard.leader_timeout = 2000 keyboard.debug_enabled = True +keyboard.pixel_pin = board.TX +keyboard.num_pixels = 12 +OFF = (0, 0, 0) +CYAN = (0, 255, 255) + +''' +pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False) + +RED = (255, 0, 0) +YELLOW = (255, 150, 0) +GREEN = (0, 255, 0) +BLUE = (0, 0, 255) +PURPLE = (180, 0, 255) + +pixels.fill(OFF) +pixels.show() +''' + emoticons = compile_unicode_string_sequences({ # Emoticons, but fancier 'ANGRY_TABLE_FLIP': r'(ノಠ痊ಠ)ノ彡┻━┻', @@ -49,7 +68,6 @@ keyboard.leader_dictionary = { 'f': emoticons.F, 'meh': emoticons.MEH, 'yay': emoticons.YAY, - 'gw': KC.DF(1), } _______ = KC.TRNS @@ -62,6 +80,33 @@ gw = 1 r1 = 2 r2 = 3 r3 = 4 + + +def base(*args, **kwargs): + keyboard.pixels.fill(OFF) + keyboard.pixels.show() + return df_pressed(*args, **kwargs) + + +def gaming(*args, **kwargs): + keyboard.pixels.fill(CYAN) + keyboard.pixels.show() + return df_pressed(*args, **kwargs) + +make_argumented_key( + validator=layer_key_validator, + names=('LAYER_GAMING',), + on_press=gaming, +) + +make_argumented_key( + validator=layer_key_validator, + names=('LAYER_BASE',), + on_press=base, +) +BASE = KC.LAYER_BASE(df) +GAMING = KC.LAYER_GAMING(gw) + # ---------------------- Keymap --------------------------------------------------------- keyboard.keymap = [ @@ -99,11 +144,11 @@ keyboard.keymap = [ ], [ # r3 - [KC.GESC, KC.N1, KC.N2, KC.N3, KC.N4, KC.N5, KC.N6, KC.N7, KC.F10, KC.F11, KC.F12, KC.DEL], - [_______, _______, _______, _______, _______, _______, _______, _______, KC.F7, KC.F8, KC.F9, SHFT_INS], - [_______, _______, _______, _______, _______, _______, _______, _______, KC.F4, KC.F5, KC.F6, KC.VOLU], - [_______, _______, _______, _______, _______, _______, _______, _______, KC.F1, KC.F2, KC.F4, KC.VOLD], - [KC.DF(df), KC.DF(gw), _______, _______, _______, _______, _______, _______, _______, _______, _______, XXXXXXX], + [KC.GESC, KC.N1, KC.N2, KC.N3, KC.N4, KC.N5, KC.N6, KC.N7, KC.F10, KC.F11, KC.F12, KC.DEL], + [_______, _______, _______, _______, _______, _______, _______, _______, KC.F7, KC.F8, KC.F9, SHFT_INS], + [_______, _______, _______, _______, _______, _______, _______, _______, KC.F4, KC.F5, KC.F6, KC.VOLU], + [_______, _______, _______, _______, _______, _______, _______, _______, KC.F1, KC.F2, KC.F4, KC.VOLD], + [BASE, GAMING, _______, _______, _______, _______, _______, _______, _______, _______, _______, XXXXXXX], ], ]