Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Last active April 3, 2022 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswilburn/48a9433efea4c1c6d7c6526a374bdfda to your computer and use it in GitHub Desktop.
Save thomaswilburn/48a9433efea4c1c6d7c6526a374bdfda to your computer and use it in GitHub Desktop.
QMK retro toggle lighting for CTRL
#include "print.h"
#include QMK_KEYBOARD_H
enum ctrl_keycodes {
L_BRI = SAFE_RANGE, //LED Brightness Increase //Working
L_BRD, //LED Brightness Decrease //Working
L_EDG_I, //LED Edge Brightness Increase
L_EDG_D, //LED Edge Brightness Decrease
L_EDG_M, //LED Edge lighting mode
L_PTN, //LED Pattern Select Next //Working
L_PTP, //LED Pattern Select Previous //Working
L_PSI, //LED Pattern Speed Increase //Working
L_PSD, //LED Pattern Speed Decrease //Working
L_RATIOD,
L_RATIOI,
L_T_MD, //LED Toggle Mode //Working
L_T_ONF, //LED Toggle On / Off //Broken
L_ON, //LED On //Broken
L_OFF, //LED Off //Broken
L_T_BR, //LED Toggle Breath Effect //Working
L_T_PTD, //LED Toggle Scrolling Pattern Direction //Working
U_T_AGCR, //USB Toggle Automatic GCR control //Working
DBG_TOG, //DEBUG Toggle On / Off //
DBG_MTRX, //DEBUG Toggle Matrix Prints //
DBG_KBD, //DEBUG Toggle Keyboard Prints //
DBG_MOU, //DEBUG Toggle Mouse Prints //
DBG_FAC, //DEBUG Factory light testing (All on white)
MD_BOOT //Restart into bootloader after hold timeout //Working
};
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT(
KC_ESC, 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_WBAK, TG(3), KC_WFWD,
KC_GRV, 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_INS, KC_HOME, KC_PGUP,
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_DEL, KC_END, KC_PGDN,
LT(4, 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_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_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_APP, KC_RCTL, MO(4), KC_LEFT, KC_DOWN, KC_RGHT
),
[1] = LAYOUT(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
),
[2] = LAYOUT(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
),
[3] = LAYOUT(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, KC_7, KC_8, KC_9, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, KC_4, KC_5, KC_6, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, KC_1, KC_2, KC_3, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, KC_0, KC_0, KC_DOT, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
),
[4] = LAYOUT(
KC_SLEP, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, KC_PSCR, KC_SCRL, KC_PAUS,
XXXXXXX, TO(0), TO(1), TO(2), XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, KC_MPLY, KC_MUTE, KC_VOLU,
L_T_BR, L_PSD, L_BRI, L_PSI, L_EDG_I, XXXXXXX, XXXXXXX, XXXXXXX, U_T_AGCR,XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, KC_MPRV, KC_MNXT, KC_VOLD,
L_T_PTD, L_PTP, L_BRD, L_PTN, L_EDG_D, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
XXXXXXX, L_T_MD, L_T_ONF, XXXXXXX, L_EDG_M, MD_BOOT, NK_TOGG, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
XXXXXXX, XXXXXXX, XXXXXXX, DBG_FAC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX
),
/*
[X] = LAYOUT(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, NK_TOGG, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
),
*/
};
#define MODS_SHIFT (get_mods() & MOD_BIT(KC_LSHIFT) || get_mods() & MOD_BIT(KC_RSHIFT))
#define MODS_CTRL (get_mods() & MOD_BIT(KC_LCTL) || get_mods() & MOD_BIT(KC_RCTRL))
#define MODS_ALT (get_mods() & MOD_BIT(KC_LALT) || get_mods() & MOD_BIT(KC_RALT))
#define LAMP_LENGTH 24
uint16_t lamp_history[LAMP_LENGTH] = {};
int history_index = 0;
// taken from the g_led_config in config_led.c
uint16_t led_lookup[11][8] = {
{ 0, 1, 2, 3, 4, 5, 6, 7 },
{ 16, 17, 18, 19, 20, 21, 22, 23 },
{ 33, 34, 35, 36, 37, 38, 39, 40 },
{ 50, 51, 52, 53, 54, 55, 56, 57 },
{ 63, 64, 65, 66, 67, 68, 69, 70 },
{ 76, 77, 78, 79, 80, 81, 82, 83 },
{ 8, 9, 10, 11, 12, 13, 14, 15 },
{ 24, 25, 26, 27, 28, 29, 30, 31 },
{ 41, 42, 43, 44, 45, 46, 47, 48 },
{ 58, 59, 60, 61, 62, 75, 49, 32 },
{ 71, 72, 73, 74, 84, 85, 86, 0 }
};
void keyboard_post_init_user(void) {
rgb_matrix_enable();
for (int i = 0; i < LAMP_LENGTH; i++) {
lamp_history[i] = 255;
}
}
void update_lamp(uint16_t led, bool force_off) {
uint32_t value = 1 << (led % 32);
led_instruction_t *lighting = &led_instructions[0];
uint32_t *target = &lighting->id0;
// assuming that id0-3 are contiguous, offset to the correct bitmask
target += (led / 32);
if (force_off) {
*target &= 0xFFFFFFFF ^ value;
} else {
*target ^= value;
}
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
static uint32_t key_timer;
static uint8_t scroll_effect = 0;
// update the toggle lighting bitmask on keypress
if (record->event.pressed) {
uint16_t led = led_lookup[record->event.key.row][record->event.key.col];
uint16_t from_history = lamp_history[history_index];
if (from_history != 255 && from_history != led) {
update_lamp(lamp_history[history_index], true);
}
update_lamp(led, false);
lamp_history[history_index] = led;
history_index = (history_index + 1) % LAMP_LENGTH;
}
switch (keycode) {
case L_BRI ... U_T_AGCR:
if (record->event.pressed) {
md_led_changed();
}
break;
}
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_EDG_M:
if (record->event.pressed) {
led_edge_mode++;
if (led_edge_mode > LED_EDGE_MODE_MAX) {
led_edge_mode = LED_EDGE_MODE_ALL;
}
}
return false;
case L_EDG_I:
if (record->event.pressed) {
led_edge_brightness += 0.1;
if (led_edge_brightness > 1) { led_edge_brightness = 1; }
}
return false;
case L_EDG_D:
if (record->event.pressed) {
led_edge_brightness -= 0.1;
if (led_edge_brightness < 0) { led_edge_brightness = 0; }
}
return false;
case L_RATIOI:
if (record->event.pressed) {
led_ratio_brightness += 0.2;
if (led_ratio_brightness > 2.0) { led_ratio_brightness = 2.0; }
}
return false;
case L_RATIOD:
if (record->event.pressed) {
led_ratio_brightness -= 0.2;
if (led_ratio_brightness < 0.0) { led_ratio_brightness = 0.0; }
}
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) {
scroll_effect++;
if (scroll_effect == 1) { //Patterns with scroll move horizontal (Right to left)
led_animation_direction = 1;
led_animation_orientation = 0;
led_animation_circular = 0;
} else if (scroll_effect == 2) { //Patterns with scroll move vertical (Top to bottom)
led_animation_direction = 1;
led_animation_orientation = 1;
led_animation_circular = 0;
} else if (scroll_effect == 3) { //Patterns with scroll move vertical (Bottom to top)
led_animation_direction = 0;
led_animation_orientation = 1;
led_animation_circular = 0;
} else if (scroll_effect == 4) { //Patterns with scroll explode from center
led_animation_direction = 0;
led_animation_orientation = 0;
led_animation_circular = 1;
} else if (scroll_effect == 5) { //Patterns with scroll implode on center
led_animation_direction = 1;
led_animation_orientation = 0;
led_animation_circular = 1;
} else { //Patterns with scroll move horizontal (Left to right)
scroll_effect = 0;
led_animation_direction = 0;
led_animation_orientation = 0;
led_animation_circular = 0;
}
}
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_FAC:
if (record->event.pressed && MODS_SHIFT && MODS_CTRL) {
led_lighting_mode = LED_MODE_NORMAL;
led_edge_brightness = 1;
led_edge_mode = LED_EDGE_MODE_ALL;
led_animation_breathing = 0;
led_animation_id = 7; //led_programs.c led_setups leds_white index
gcr_desired = LED_GCR_MAX;
led_enabled = 1;
I2C3733_Control_Set(led_enabled);
}
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
}
}
led_instruction_t led_instructions[] = {
//LEDs are normally inactive, no processing is performed on them
//Flags are used in matching criteria for an LED to be active and indicate how to color it
//Flags can be found in tmk_core/protocol/arm_atsam/md_rgb_matrix.h (prefixed with LED_FLAG_)
//LED IDs can be found in config_led.h in the keyboard's directory
// toggle switch lighting layer
{ .flags = LED_FLAG_MATCH_ID | LED_FLAG_USE_RGB | LED_FLAG_MATCH_LAYER, .layer = 1, .r = 0xFD, .g = 0x7B, .b = 0x07 },
// pattern lighting layer
{ .flags = LED_FLAG_USE_ROTATE_PATTERN | LED_FLAG_MATCH_LAYER, .layer = 2 },
// excel layer
{ .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .g = 255, .id0 = 0x3800000, .id1 = 0xE000700, .id2 = 0xFF8001C0, .id3 = 0x00FFFFFF, .layer = 3 },
// fn layer
{ .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0x0A, .g = 0x47, .b = 0xC2, .id0 = 0x001F001F, .id1 = 0x801C001E, .id2 = 0x00003001 },
{ .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0x0A, .g = 0x8B, .b = 0xCC, .id0 = 0x00600020, .id1 = 0x00600060, .id2 = 0x00004006 },
{ .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0x00, .g = 0xB5, .b = 0xB5, .id0 = 0x018000C0, .id1 = 0x01800180, .id2 = 0x00000018 },
{ .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0x00, .g = 0xC2, .b = 0x37, .id0 = 0xFE00FF00, .id1 = 0x7E03FE01, .id2 = 0x007F8FE0 },
{ .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0x00, .g = 0x80, .b = 0x80, .id0 = 0, .id1 = 0, .id2 = 0xFF800000, .id3 = 0x007FFFFF },
// { .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0, .g = 0x80, .b = 0x80, .id0 = 0xAAAAAAAA, .id1 = 0xAAAAAAAA, .id2 = 0xAAAAAAAA, .id3 = 0xAAAAAAAA },
// { .flags = LED_FLAG_MATCH_LAYER | LED_FLAG_USE_RGB | LED_FLAG_MATCH_ID, .layer = 4, .r = 0, .g = 0xFF, .b = 0x50, .id0 = 0x55555555, .id1 = 0x55555555, .id2 = 0x55555555, .id3 = 0x55555555 },
//end must be set to 1 to indicate end of instruction set
{ .end = 1 }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment