From 63e212c0b78be664785433c11cd728f15f50cd6a Mon Sep 17 00:00:00 2001 From: Sean Dwyer Date: Tue, 6 Nov 2018 11:54:53 -0800 Subject: [PATCH] Add localized LED flash effect on keypress to Massdrop ALT (#4340) --- .../massdrop/alt/keymaps/reywood/keymap.c | 195 ++++++++++++++++++ .../alt/keymaps/reywood/rgb_matrix_user.c | 193 +++++++++++++++++ .../alt/keymaps/reywood/rgb_matrix_user.h | 3 + .../massdrop/alt/keymaps/reywood/rules.mk | 34 +++ tmk_core/protocol/arm_atsam/led_matrix.c | 14 +- tmk_core/protocol/arm_atsam/led_matrix.h | 3 +- 6 files changed, 438 insertions(+), 4 deletions(-) create mode 100644 keyboards/massdrop/alt/keymaps/reywood/keymap.c create mode 100644 keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.c create mode 100644 keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.h create mode 100644 keyboards/massdrop/alt/keymaps/reywood/rules.mk diff --git a/keyboards/massdrop/alt/keymaps/reywood/keymap.c b/keyboards/massdrop/alt/keymaps/reywood/keymap.c new file mode 100644 index 000000000..8b69f0e8e --- /dev/null +++ b/keyboards/massdrop/alt/keymaps/reywood/keymap.c @@ -0,0 +1,195 @@ +#include QMK_KEYBOARD_H +#include "rgb_matrix_user.h" + +enum alt_keycodes { + L_BRI = SAFE_RANGE, //LED Brightness Increase + L_BRD, //LED Brightness Decrease + L_PTN, //LED Pattern Select Next + L_PTP, //LED Pattern Select Previous + L_PSI, //LED Pattern Speed Increase + L_PSD, //LED Pattern Speed Decrease + L_T_MD, //LED Toggle Mode + L_T_ONF, //LED Toggle On / Off + L_ON, //LED On + L_OFF, //LED Off + L_T_BR, //LED Toggle Breath Effect + L_T_PTD, //LED Toggle Scrolling Pattern Direction + U_T_AUTO, //USB Extra Port Toggle Auto Detect / Always Active + U_T_AGCR, //USB Toggle Automatic GCR control + DBG_TOG, //DEBUG Toggle On / Off + DBG_MTRX, //DEBUG Toggle Matrix Prints + DBG_KBD, //DEBUG Toggle Keyboard Prints + DBG_MOU, //DEBUG Toggle Mouse Prints + MD_BOOT, //Restart into bootloader after hold timeout +}; + +#define TG_NKRO MAGIC_TOGGLE_NKRO //Toggle 6KRO / NKRO mode +#define ______ KC_TRNS + +keymap_config_t keymap_config; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + [0] = LAYOUT( + KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_DEL, \ + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_HOME, \ + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_PGUP, \ + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_PGDN, \ + KC_LCTL, KC_LALT, KC_LGUI, KC_SPC, KC_RGUI, MO(1), KC_LEFT, KC_DOWN, KC_RGHT \ + ), + [1] = LAYOUT( + KC_GRV, 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_MUTE, \ + _______, _______, _______, KC_UP, _______, _______, _______, U_T_AUTO,U_T_AGCR,_______, KC_PSCR, KC_SLCK, KC_PAUS, _______, KC_END, \ + _______, _______, KC_LEFT, KC_DOWN, KC_RGHT, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ + _______, _______, _______, _______, _______, MD_BOOT, TG_NKRO, _______, _______, _______, _______, _______, KC_VOLU, _______, \ + _______, _______, _______, KC_MPLY, MO(2), _______, KC_MRWD, KC_VOLD, KC_MFFD \ + ), + [2] = LAYOUT( + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ + L_T_BR, L_PSD, L_BRI, L_PSI, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ + L_T_PTD, L_PTP, L_BRD, L_PTN, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ + _______, L_T_MD, L_T_ONF, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, \ + _______, _______, _______, _______, _______, _______, _______, _______, _______ \ + ), +}; + +const uint16_t PROGMEM fn_actions[] = { + +}; + +// Runs just one time when the keyboard initializes. +void matrix_init_user(void) { +}; + +// Runs constantly in the background, in a loop. +void matrix_scan_user(void) { +}; + +#define MODS_SHIFT (keyboard_report->mods & MOD_BIT(KC_LSHIFT) || keyboard_report->mods & MOD_BIT(KC_RSHIFT)) +#define MODS_CTRL (keyboard_report->mods & MOD_BIT(KC_LCTL) || keyboard_report->mods & MOD_BIT(KC_RCTRL)) +#define MODS_ALT (keyboard_report->mods & MOD_BIT(KC_LALT) || keyboard_report->mods & MOD_BIT(KC_RALT)) + +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + static uint32_t key_timer; + + rgb_matrix_record_key_press(record); + + switch (keycode) { + case L_BRI: + if (record->event.pressed) { + if (LED_GCR_STEP > LED_GCR_MAX - gcr_desired) gcr_desired = LED_GCR_MAX; + else gcr_desired += LED_GCR_STEP; + if (led_animation_breathing) gcr_breathe = gcr_desired; + } + return false; + case L_BRD: + if (record->event.pressed) { + if (LED_GCR_STEP > gcr_desired) gcr_desired = 0; + else gcr_desired -= LED_GCR_STEP; + if (led_animation_breathing) gcr_breathe = gcr_desired; + } + return false; + case L_PTN: + if (record->event.pressed) { + if (led_animation_id == led_setups_count - 1) led_animation_id = 0; + else led_animation_id++; + } + return false; + case L_PTP: + if (record->event.pressed) { + if (led_animation_id == 0) led_animation_id = led_setups_count - 1; + else led_animation_id--; + } + return false; + case L_PSI: + if (record->event.pressed) { + led_animation_speed += ANIMATION_SPEED_STEP; + } + return false; + case L_PSD: + if (record->event.pressed) { + led_animation_speed -= ANIMATION_SPEED_STEP; + if (led_animation_speed < 0) led_animation_speed = 0; + } + return false; + case L_T_MD: + if (record->event.pressed) { + led_lighting_mode++; + if (led_lighting_mode > LED_MODE_MAX_INDEX) led_lighting_mode = LED_MODE_NORMAL; + } + return false; + case L_T_ONF: + if (record->event.pressed) { + led_enabled = !led_enabled; + I2C3733_Control_Set(led_enabled); + } + return false; + case L_ON: + if (record->event.pressed) { + led_enabled = 1; + I2C3733_Control_Set(led_enabled); + } + return false; + case L_OFF: + if (record->event.pressed) { + led_enabled = 0; + I2C3733_Control_Set(led_enabled); + } + return false; + case L_T_BR: + if (record->event.pressed) { + led_animation_breathing = !led_animation_breathing; + if (led_animation_breathing) { + gcr_breathe = gcr_desired; + led_animation_breathe_cur = BREATHE_MIN_STEP; + breathe_dir = 1; + } + } + return false; + case L_T_PTD: + if (record->event.pressed) { + led_animation_direction = !led_animation_direction; + } + return false; + case U_T_AUTO: + if (record->event.pressed && MODS_SHIFT && MODS_CTRL) { + TOGGLE_FLAG_AND_PRINT(usb_extra_manual, "USB extra port manual mode"); + } + return false; + case U_T_AGCR: + if (record->event.pressed && MODS_SHIFT && MODS_CTRL) { + TOGGLE_FLAG_AND_PRINT(usb_gcr_auto, "USB GCR auto mode"); + } + return false; + case DBG_TOG: + if (record->event.pressed) { + TOGGLE_FLAG_AND_PRINT(debug_enable, "Debug mode"); + } + return false; + case DBG_MTRX: + if (record->event.pressed) { + TOGGLE_FLAG_AND_PRINT(debug_matrix, "Debug matrix"); + } + return false; + case DBG_KBD: + if (record->event.pressed) { + TOGGLE_FLAG_AND_PRINT(debug_keyboard, "Debug keyboard"); + } + return false; + case DBG_MOU: + if (record->event.pressed) { + TOGGLE_FLAG_AND_PRINT(debug_mouse, "Debug mouse"); + } + return false; + case MD_BOOT: + if (record->event.pressed) { + key_timer = timer_read32(); + } else { + if (timer_elapsed32(key_timer) >= 500) { + reset_keyboard(); + } + } + return false; + default: + return true; //Process all other keycodes normally + } +} diff --git a/keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.c b/keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.c new file mode 100644 index 000000000..36131e202 --- /dev/null +++ b/keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.c @@ -0,0 +1,193 @@ +#include "quantum.h" +#include "led_matrix.h" + +extern issi3733_led_t *led_cur; +extern uint8_t led_per_run; +extern issi3733_led_t *lede; +extern issi3733_led_t led_map[]; + +static uint16_t last_boost_update; +static uint8_t led_boosts[ISSI3733_LED_COUNT]; +static uint8_t led_boost_index; +static uint8_t led_cur_index; + +#define LED_BOOST_REFRESH_INTERVAL_IN_MS 40 +#define LED_BOOST_DECAY 0.7 +#define LED_BOOST_PROPAGATE 0.5 +#define LED_BOOST_PEAK 100 + +#define MIN_RGB 0x050008 +#define MIN_R (MIN_RGB >> 16 & 0xff) +#define MIN_G (MIN_RGB >> 8 & 0xff) +#define MIN_B (MIN_RGB & 0xff) + +#define MAX_RGB 0xc26eff +#define MAX_R (MAX_RGB >> 16 & 0xff) +#define MAX_G (MAX_RGB >> 8 & 0xff) +#define MAX_B (MAX_RGB & 0xff) + +#define UNDERGLOW_RGB 0x4f002e +#define UNDERGLOW_R (UNDERGLOW_RGB >> 16 & 0xff) +#define UNDERGLOW_G (UNDERGLOW_RGB >> 8 & 0xff) +#define UNDERGLOW_B (UNDERGLOW_RGB & 0xff) + +#define UNDERGLOW_SCAN_CODE 255 + +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +#define __ -1 +static const uint8_t KEY_TO_LED_MAP[MATRIX_ROWS][MATRIX_COLS] = { + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, + {15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, + {30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, __, 42, 43}, + {44, __, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57}, + {58, 59, 60, __, __, __, 61, __, __, __, 62, 63, 64, 65, 66}, +}; + +#define KEY_LED_COUNT 67 +#define KP(c, r) { .col = c, .row = r } // shorthand for keypos_t +static const keypos_t LED_TO_KEY_MAP[KEY_LED_COUNT] = { + KP(0, 0), KP(1, 0), KP(2, 0), KP(3, 0), KP(4, 0), KP(5, 0), KP(6, 0), KP(7, 0), KP(8, 0), KP(9, 0), KP(10, 0), KP(11, 0), KP(12, 0), KP(13, 0), KP(14, 0), + KP(0, 1), KP(1, 1), KP(2, 1), KP(3, 1), KP(4, 1), KP(5, 1), KP(6, 1), KP(7, 1), KP(8, 1), KP(9, 1), KP(10, 1), KP(11, 1), KP(12, 1), KP(13, 1), KP(14, 1), + KP(0, 2), KP(1, 2), KP(2, 2), KP(3, 2), KP(4, 2), KP(5, 2), KP(6, 2), KP(7, 2), KP(8, 2), KP(9, 2), KP(10, 2), KP(11, 2), KP(13, 2), KP(14, 2), + KP(0, 3), KP(2, 3), KP(3, 3), KP(4, 3), KP(5, 3), KP(6, 3), KP(7, 3), KP(8, 3), KP(9, 3), KP(10, 3), KP(11, 3), KP(12, 3), KP(13, 3), KP(14, 3), + KP(0, 4), KP(1, 4), KP(2, 4), KP(6, 4), KP(10, 4), KP(11, 4), KP(12, 4), KP(13, 4), KP(14, 4), +}; + + +static void update_led_boosts(void); +static void update_led_cur_rgb_values(void); +static void set_nearest_led_to_max(uint8_t col, uint8_t row); +static uint8_t calculate_new_color_component_value(uint8_t max, uint8_t min); +static void calculate_new_led_boosts(uint8_t new_led_boosts[]); +static uint8_t calculate_new_led_boost_at(int index); +static uint8_t get_propagated_boost_from_neighbors(int led_position); +static uint8_t get_led_boost_at_keypos(uint8_t row, uint8_t col); +static void set_new_led_boosts(uint8_t* new_led_boosts); +static uint8_t map_key_position_to_led_index(uint8_t col, uint8_t row); + + +void rgb_matrix_init_user(void) { + for (int i = 0; i < ISSI3733_LED_COUNT; i++) { + led_boosts[i] = 0; + } + last_boost_update = timer_read(); + led_boost_index = 0; + led_cur_index = 0; +} + +void led_matrix_run(void) { + uint8_t led_this_run = 0; + + if (led_cur == 0) { //Denotes start of new processing cycle in the case of chunked processing + led_cur = led_map; + led_cur_index = 0; + } + update_led_boosts(); + + while (led_cur < lede && led_this_run < led_per_run) { + update_led_cur_rgb_values(); + + led_cur++; + led_cur_index++; + led_this_run++; + } +} + +void rgb_matrix_record_key_press(keyrecord_t *record) { + if (record->event.pressed) { + keypos_t key = record->event.key; + set_nearest_led_to_max(key.col, key.row); + } +} + + +static void update_led_boosts(void) { + if (timer_elapsed(last_boost_update) > LED_BOOST_REFRESH_INTERVAL_IN_MS) { + last_boost_update = timer_read(); + + uint8_t new_led_boosts[ISSI3733_LED_COUNT]; + calculate_new_led_boosts(new_led_boosts); + set_new_led_boosts(new_led_boosts); + } +} + +static void update_led_cur_rgb_values(void) { + if (led_cur->scan == UNDERGLOW_SCAN_CODE) { + *led_cur->rgb.r = UNDERGLOW_R; + *led_cur->rgb.g = UNDERGLOW_G; + *led_cur->rgb.b = UNDERGLOW_B; + } else { + *led_cur->rgb.r = calculate_new_color_component_value(MAX_R, MIN_R); + *led_cur->rgb.g = calculate_new_color_component_value(MAX_G, MIN_G); + *led_cur->rgb.b = calculate_new_color_component_value(MAX_B, MIN_B); + } +} + +static void set_nearest_led_to_max(uint8_t col, uint8_t row) { + uint8_t led_index = map_key_position_to_led_index(col, row); + if (led_index >= 0 && led_index < ISSI3733_LED_COUNT) { + led_boosts[led_index] = LED_BOOST_PEAK; + } +} + +static uint8_t calculate_new_color_component_value(uint8_t max, uint8_t min) { + uint8_t current_boost = led_boosts[led_cur_index]; + return (float)(max - min) * current_boost / LED_BOOST_PEAK + min; +} + +static void calculate_new_led_boosts(uint8_t new_led_boosts[]) { + for (int i = 0; i < ISSI3733_LED_COUNT; i++) { + new_led_boosts[i] = calculate_new_led_boost_at(i); + } +} + +static uint8_t calculate_new_led_boost_at(int index) { + uint8_t decayed_boost = led_boosts[index] * LED_BOOST_DECAY; + uint8_t propagated_boost = get_propagated_boost_from_neighbors(index); + uint8_t new_boost = (propagated_boost > decayed_boost) ? propagated_boost : decayed_boost; + if (new_boost > LED_BOOST_PEAK) { + new_boost = LED_BOOST_PEAK; + } + return new_boost; +} + +static uint8_t get_propagated_boost_from_neighbors(int led_position) { + if (led_position < 0 || led_position >= KEY_LED_COUNT) { + return 0; + } + keypos_t led_keypos = LED_TO_KEY_MAP[led_position]; + uint8_t top_boost = get_led_boost_at_keypos(led_keypos.row - 1, led_keypos.col); + uint8_t bottom_boost = get_led_boost_at_keypos(led_keypos.row + 1, led_keypos.col); + uint8_t left_boost = get_led_boost_at_keypos(led_keypos.row, led_keypos.col - 1); + uint8_t right_boost = get_led_boost_at_keypos(led_keypos.row, led_keypos.col + 1); + uint8_t max_boost = max(max(top_boost, bottom_boost), max(left_boost, right_boost)); + if (max_boost > LED_BOOST_PEAK) { + max_boost = LED_BOOST_PEAK; + } + return max_boost * LED_BOOST_PROPAGATE; +} + +static uint8_t get_led_boost_at_keypos(uint8_t row, uint8_t col) { + if (row < 0 || row >= MATRIX_ROWS || col < 0 || col >= MATRIX_COLS) { + return 0; + } + uint8_t led_index = KEY_TO_LED_MAP[row][col]; + if (led_index < 0) { + return 0; + } + return led_boosts[led_index]; +} + +static void set_new_led_boosts(uint8_t* new_led_boosts) { + for (int i = 0; i < ISSI3733_LED_COUNT; i++) { + led_boosts[i] = new_led_boosts[i]; + } +} + +static uint8_t map_key_position_to_led_index(uint8_t col, uint8_t row) { + if (row >= 0 && row < MATRIX_ROWS && col >= 0 && col < MATRIX_COLS) { + return KEY_TO_LED_MAP[row][col]; + } + return -1; +} diff --git a/keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.h b/keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.h new file mode 100644 index 000000000..4ffd202e9 --- /dev/null +++ b/keyboards/massdrop/alt/keymaps/reywood/rgb_matrix_user.h @@ -0,0 +1,3 @@ +#pragma once + +void rgb_matrix_record_key_press(keyrecord_t *record); diff --git a/keyboards/massdrop/alt/keymaps/reywood/rules.mk b/keyboards/massdrop/alt/keymaps/reywood/rules.mk new file mode 100644 index 000000000..b5b961a0f --- /dev/null +++ b/keyboards/massdrop/alt/keymaps/reywood/rules.mk @@ -0,0 +1,34 @@ +# project specific files +SRC = led_programs.c +SRC += matrix.c +SRC += rgb_matrix_user.c + +#For platform and packs +ARM_ATSAM = SAMD51J18A +MCU = cortex-m4 + +CUSTOM_MATRIX = yes + +# Build Options +# comment out to disable the options. +# +BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000) +MOUSEKEY_ENABLE = no # Mouse keys(+4700) +EXTRAKEY_ENABLE = yes # Audio control and System control(+450) +CONSOLE_ENABLE = no # Console for debug(+400) +COMMAND_ENABLE = no # Commands for debug and configuration +# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE +SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend +# if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work +NKRO_ENABLE = yes # USB Nkey Rollover +BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality on B7 by default +RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow +MIDI_ENABLE = no # MIDI support (+2400 to 4200, depending on config) +UNICODE_ENABLE = no # Unicode +BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID +AUDIO_ENABLE = no # Audio output on port C6 +FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches +HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) +VIRTSER_ENABLE = no # USB Serial Driver +RAW_ENABLE = no # Raw device +AUTO_SHIFT_ENABLE = no # Auto Shift diff --git a/tmk_core/protocol/arm_atsam/led_matrix.c b/tmk_core/protocol/arm_atsam/led_matrix.c index 7ee1dad22..c328fdc4c 100644 --- a/tmk_core/protocol/arm_atsam/led_matrix.c +++ b/tmk_core/protocol/arm_atsam/led_matrix.c @@ -257,13 +257,15 @@ issi3733_led_t *led_cur; uint8_t led_per_run = 15; float breathe_mult; -void led_matrix_run(led_setup_t *f) +__attribute__ ((weak)) +void led_matrix_run(void) { float ro; float go; float bo; float px; uint8_t led_this_run = 0; + led_setup_t *f = (led_setup_t*)led_setups[led_animation_id]; if (led_cur == 0) //Denotes start of new processing cycle in the case of chunked processing { @@ -459,13 +461,19 @@ uint8_t led_matrix_init(void) //Run led matrix code once for initial LED coloring led_cur = 0; - led_matrix_run((led_setup_t*)led_setups[led_animation_id]); + rgb_matrix_init_user(); + led_matrix_run(); DBGC(DC_LED_MATRIX_INIT_COMPLETE); return 0; } +__attribute__ ((weak)) +void rgb_matrix_init_user(void) { + +} + #define LED_UPDATE_RATE 10 //ms //led data processing can take time, so process data in chunks to free up the processor @@ -502,7 +510,7 @@ void led_matrix_task(void) if (led_cur != lede) { //m15_off; //debug profiling - led_matrix_run((led_setup_t*)led_setups[led_animation_id]); + led_matrix_run(); //m15_on; //debug profiling } } diff --git a/tmk_core/protocol/arm_atsam/led_matrix.h b/tmk_core/protocol/arm_atsam/led_matrix.h index 01b078b71..3f2b9cdb8 100644 --- a/tmk_core/protocol/arm_atsam/led_matrix.h +++ b/tmk_core/protocol/arm_atsam/led_matrix.h @@ -86,6 +86,7 @@ typedef struct led_disp_s { } led_disp_t; uint8_t led_matrix_init(void); +void rgb_matrix_init_user(void); #define LED_MODE_NORMAL 0 //Must be 0 #define LED_MODE_KEYS_ONLY 1 @@ -134,7 +135,7 @@ extern void *led_setups[]; extern issi3733_led_t *led_cur; extern issi3733_led_t *lede; -void led_matrix_run(led_setup_t *f); +void led_matrix_run(void); void led_matrix_task(void); void gcr_compute(void);