qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

snes_macropad.c (3470B)


      1 // Copyright 2023 John Barbero Unenge (@jbarberu)
      2 // SPDX-License-Identifier: GPL-2.0-or-later
      3 
      4 #include "quantum.h"
      5 
      6 // oled keylog rendering has been kindly borrowed from crkbd <3
      7 
      8 char     key_name = ' ';
      9 uint16_t last_keycode;
     10 uint8_t  last_row;
     11 uint8_t  last_col;
     12 
     13 static const char PROGMEM code_to_name[60] = {' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'R', 'E', 'B', 'T', '_', '-', '=', '[', ']', '\\', '#', ';', '\'', '`', ',', '.', '/', ' ', ' ', ' '};
     14 
     15 static void set_keylog(uint16_t keycode, keyrecord_t *record) {
     16     last_row = record->event.key.row;
     17     last_col = record->event.key.col;
     18 
     19     key_name     = ' ';
     20     last_keycode = keycode;
     21     if (IS_QK_MOD_TAP(keycode)) {
     22         if (record->tap.count) {
     23           keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
     24         } else {
     25           keycode = 0xE0 + biton(QK_MOD_TAP_GET_MODS(keycode) & 0xF) + biton(QK_MOD_TAP_GET_MODS(keycode) & 0x10);
     26         }
     27     } else if (IS_QK_LAYER_TAP(keycode) && record->tap.count) {
     28         keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
     29     } else if (IS_QK_MODS(keycode)) {
     30         keycode = QK_MODS_GET_BASIC_KEYCODE(keycode);
     31     } else if (IS_QK_ONE_SHOT_MOD(keycode)) {
     32         keycode = 0xE0 + biton(QK_ONE_SHOT_MOD_GET_MODS(keycode) & 0xF) + biton(QK_ONE_SHOT_MOD_GET_MODS(keycode) & 0x10);
     33     }
     34     if (keycode > ARRAY_SIZE(code_to_name)) {
     35         return;
     36     }
     37 
     38     // update keylog
     39     key_name = pgm_read_byte(&code_to_name[keycode]);
     40 }
     41 
     42 static const char *depad_str(const char *depad_str, char depad_char) {
     43     while (*depad_str == depad_char) {
     44         ++depad_str;
     45     }
     46     return depad_str;
     47 }
     48 
     49 static void oled_render_keylog(void) {
     50     oled_write_char('0' + last_row, false);
     51     oled_write("x", false);
     52     oled_write_char('0' + last_col, false);
     53     oled_write(", k", false);
     54     const char *last_keycode_str = get_u16_str(last_keycode, ' ');
     55     oled_write(depad_str(last_keycode_str, ' '), false);
     56     oled_write(":", false);
     57     oled_write_char(key_name, false);
     58 }
     59 
     60 __attribute__((weak)) const char * get_layer_name_user(uint8_t layer) {
     61     return get_u8_str(layer, ' ');
     62 }
     63 
     64 static void oled_render_layer(void) {
     65     oled_write("Layer: ", false);
     66     oled_write_ln(get_layer_name_user(get_highest_layer(layer_state)), false);
     67 }
     68 
     69 bool oled_task_kb(void) {
     70     if (!oled_task_user()) {
     71         return false;
     72     }
     73 
     74     oled_render_layer();
     75     oled_render_keylog();
     76     oled_advance_page(true);
     77     return false;
     78 }
     79 
     80 static void setupForFlashing(void) {
     81     oled_clear();
     82     oled_write("                     ", false);
     83     oled_write("  In flash mode...   ", false);
     84     oled_write("                     ", false);
     85     oled_write("                     ", false);
     86 
     87     // Force data to be rendered
     88     oled_render_dirty(true);
     89 }
     90 
     91 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
     92     if (record->event.pressed) {
     93         set_keylog(keycode, record);
     94     }
     95     return process_record_user(keycode, record);
     96 }
     97 
     98 void keyboard_post_init_kb(void) {
     99     rgblight_enable_noeeprom();
    100     rgblight_sethsv_noeeprom(HSV_MAGENTA);
    101     rgblight_mode_noeeprom(RGBLIGHT_MODE_RAINBOW_SWIRL);
    102     keyboard_post_init_user();
    103 }
    104 
    105 bool shutdown_kb(bool jump_to_bootloader) {
    106     if (!shutdown_user(jump_to_bootloader)) {
    107         return false;
    108     }
    109     setupForFlashing();
    110     return true;
    111 }