qmk_firmware

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

keymap.c (12758B)


      1 /*
      2 Copyright 2020 Seth Bonner <fl3tching101@gmail.com>
      3 This program is free software: you can redistribute it and/or modify
      4 it under the terms of the GNU General Public License as published by
      5 the Free Software Foundation, either version 2 of the License, or
      6 (at your option) any later version.
      7 This program is distributed in the hope that it will be useful,
      8 but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 GNU General Public License for more details.
     11 You should have received a copy of the GNU General Public License
     12 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     13 */
     14 #include QMK_KEYBOARD_H
     15 #include "keycode_lookup.h"
     16 #include <string.h>
     17 
     18 #ifdef CONSOLE_ENABLE
     19 #include "print.h"
     20 #endif
     21 
     22 // Each layer gets a name for readability, which is then used in the keymap matrix below.
     23 // The underscores don't mean anything - you can have a layer called STUFF or any other name.
     24 // Layer names don't all need to be of the same length, obviously, and you can also skip them
     25 // entirely and just use numbers.
     26 #define _BASE 0
     27 #define _SPEC 1 // Special layer
     28 
     29 // Use the following format to create custom key codes to make macros out of and such
     30 enum custom_keycodes {
     31     KC_EXAM = SAFE_RANGE // "Examine" key code to show the keycode of a key pressed afterwards on the OLED
     32 };
     33 
     34 
     35 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
     36     [_BASE] = LAYOUT(
     37         UG_NEXT,                   KC_NUM,
     38         KC_P7,   KC_P8,   KC_P9,   KC_DEL,
     39         KC_P4,   KC_P5,   KC_P6,   KC_END,
     40         KC_P1,   KC_P2,   KC_P3,   KC_F13,
     41         KC_P0,   MO(1),   KC_PDOT, KC_PENT
     42     ),
     43     [_SPEC] = LAYOUT(
     44         UG_PREV,                    KC_MUTE,
     45         KC_NO,    KC_NO,   KC_NO,   KC_EXAM,
     46         KC_NO,    KC_NO,   KC_NO,   KC_NO,
     47         QK_BOOT,  UG_TOGG, UG_SPDU, UG_SPDD,
     48         KC_NO,    _______, KC_NO,   KC_NO
     49     )
     50 };
     51 
     52 bool encoder_update_user(uint8_t index, bool clockwise){
     53   if(index == 0) { // first encoder
     54     if(clockwise){
     55       tap_code(KC_AUDIO_VOL_UP);
     56     }else{
     57       tap_code(KC_AUDIO_VOL_DOWN);
     58     }
     59   }else if(index == 1){ // second encoder
     60     if(clockwise){
     61       rgblight_increase_val();
     62     }else{
     63       rgblight_decrease_val();
     64     }
     65   }
     66     return true;
     67 }
     68 
     69 #ifdef OLED_ENABLE
     70 
     71 #define ANIM_FRAMES 3
     72 #define ANIM_FRAME_DURATION 110 // Number of milliseconds per frame (no faster than 110ms, last line struggles)
     73 #define BACKGROUND_FRAMES 21
     74 #define ROCKET_CENTER_POS 3
     75 #define SPLASH_DUR 100 // Measured in frames, see above for frame length (note, 231 is used as a key value later on, CTRL+F for uses of this to make sure everything is good)
     76 
     77 uint32_t anim_timer = 0;
     78 uint8_t current_frame = 0;
     79 uint8_t rocket_y_position = 3;
     80 uint8_t rocket_pos_change = 0;
     81 uint8_t background_frame = 0;
     82 uint8_t splash_dur_counter = 0;
     83 bool examine_engaged = false;
     84 uint16_t examined_keycode = KC_NO;
     85 char lastKeycodeString[32] = { 0 };
     86 
     87 const char star_background [8] [21] =
     88 {
     89     {0x88, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x00, 0x00, 0x00, 0x00, 0x93},
     90     {0x00, 0x00, 0x89, 0x00, 0x00, 0x00, 0x8D, 0x00, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8E, 0x00, 0x00},
     91     {0x00, 0x8F, 0x00, 0x00, 0x8A, 0x00, 0x00, 0x00, 0x00, 0x92, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
     92     {0x00, 0x00, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x8D, 0x00, 0x00, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00},
     93     {0x8D, 0x00, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x8F, 0x8B, 0x00, 0x00, 0x00, 0x00},
     94     {0x00, 0x8A, 0x00, 0x00, 0x8E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00, 0x8D, 0x00, 0x00, 0x00, 0x8F, 0x00, 0x00},
     95     {0x00, 0x00, 0x00, 0x8C, 0x00, 0x00, 0x8F, 0x00, 0x89, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x8D, 0x00, 0x00, 0x00, 0x8F},
     96     {0x00, 0x8B, 0x00, 0x00, 0x91, 0x00, 0x00, 0x92, 0x00, 0x00, 0x00, 0x8E, 0x00, 0x00, 0x90, 0x00, 0x00, 0x8C, 0x00, 0x00},
     97 };
     98 
     99 static void oled_write_ln_centered(const char * data, bool inverted)
    100 {
    101     if(strlen(data) >= 21) // If more than 1 line of text is passed in, return without doing anything
    102     {
    103         return;
    104     }
    105 
    106     // Character buffer to build up the string in
    107     char line_buf[21];
    108 
    109     // Amount to offset string from left side
    110     uint8_t offset = (22 - strlen(data)) / 2;
    111 
    112     // Formatted string centering... look, it works, don't ask how...
    113     snprintf(line_buf, 21, "%*s%s%*s", offset, "", data, offset, ""); // Centers data within 21 character buffer
    114 
    115     oled_write_ln(line_buf, inverted);
    116 }
    117 
    118 // Prints the exhaust characters in an order determined by the phase for animation purposes
    119 // startX - The x axis starting point in characters for the exhaust (3 behind the rocket)
    120 // startY - The y axis starting point in characters for the exhaust (middle of the rocket)
    121 // phase - The "phase" of the animation, no real rhyme or reason to the exact number, but each frame move +1 to make the animation work
    122 static void render_exhaust(uint8_t startX, uint8_t startY, uint8_t phase)
    123 {
    124     oled_set_cursor(startX, startY);
    125     oled_write_char(0x85 + (phase % 3), false);
    126     phase++;
    127     oled_write_char(0x85 + (phase % 3), false);
    128     phase++;
    129     oled_write_char(0x85 + (phase % 3), false);
    130 }
    131 
    132 // Renders the "stars" behind the rocket
    133 // startY - The starting Y location (in characters) of the rocket so that stars aren't rendered on top of the rocket
    134 static void render_stars(uint8_t startY, uint8_t phase)
    135 {
    136     // Line 0
    137     oled_set_cursor(0, 0);
    138     for(int i = 0; i < 21; i++)
    139     {
    140         oled_write_char(star_background[0][(i + phase) % 21], false);
    141     }
    142     // Line 1
    143     oled_set_cursor(0, 1);
    144     for(int i = 0; i < 21; i++)
    145     {
    146         oled_write_char(star_background[1][(i + phase) % 21], false);
    147     }
    148     // Line 2
    149     oled_set_cursor(0, 2);
    150     for(int i = 0; i < 21; i++)
    151     {
    152         oled_write_char(star_background[2][(i + phase) % 21], false);
    153     }
    154     // Line 3
    155     oled_set_cursor(0, 3);
    156     for(int i = 0; i < 21; i++)
    157     {
    158         oled_write_char(star_background[3][(i + phase) % 21], false);
    159     }
    160     // Line 4
    161     oled_set_cursor(0, 4);
    162     for(int i = 0; i < 21; i++)
    163     {
    164         oled_write_char(star_background[4][(i + phase) % 21], false);
    165     }
    166     // Line 5
    167     oled_set_cursor(0, 5);
    168     for(int i = 0; i < 21; i++)
    169     {
    170         oled_write_char(star_background[5][(i + phase) % 21], false);
    171     }
    172     // Line 6
    173     oled_set_cursor(0, 6);
    174     for(int i = 0; i < 21; i++)
    175     {
    176         oled_write_char(star_background[6][(i + phase) % 21], false);
    177     }
    178     // Line 7
    179     oled_set_cursor(0, 7);
    180     for(int i = 0; i < 21; i++)
    181     {
    182         oled_write_char(star_background[7][(i + phase) % 21], false);
    183     }
    184 
    185 }
    186 
    187 static void render_logo(uint8_t startX, uint8_t startY)
    188 {
    189     oled_set_cursor(startX, startY);
    190     oled_write_char(0x80, false);
    191     oled_write_char(0x81, false);
    192     oled_write_char(0x82, false);
    193     oled_write_char(0x83, false);
    194     oled_write_char(0x84, false);
    195     oled_set_cursor(startX, startY + 1);
    196     oled_write_char(0xA0, false);
    197     oled_write_char(0xA1, false);
    198     oled_write_char(0xA2, false);
    199     oled_write_char(0xA3, false);
    200     oled_write_char(0xA4, false);
    201     oled_write_char(0xA5, false);
    202     oled_set_cursor(startX, startY + 2);
    203     oled_write_char(0xC0, false);
    204     oled_write_char(0xC1, false);
    205     oled_write_char(0xC2, false);
    206     oled_write_char(0xC3, false);
    207     oled_write_char(0xC4, false);
    208 }
    209 
    210 oled_rotation_t oled_init_user(oled_rotation_t rotation) { return OLED_ROTATION_180; }
    211 
    212 bool oled_task_user(void)
    213 {
    214     // Playing the animation
    215     if((timer_elapsed32(anim_timer) > ANIM_FRAME_DURATION) && (splash_dur_counter < SPLASH_DUR))
    216     {
    217         anim_timer = timer_read32(); // read the current timer value
    218         current_frame = (current_frame + 1) % ANIM_FRAMES; // Frame in the exhaust animation
    219         background_frame = (background_frame + 1) % BACKGROUND_FRAMES; // Frame in the star animation
    220 
    221         // Move the rocket up and down
    222         if((rocket_pos_change / 9) == 0)
    223         {
    224             rocket_y_position = ROCKET_CENTER_POS;
    225         }
    226         else if((rocket_pos_change / 9) == 1)
    227         {
    228             rocket_y_position = ROCKET_CENTER_POS + 1;
    229         }
    230         else if((rocket_pos_change / 9) == 2)
    231         {
    232             rocket_y_position = ROCKET_CENTER_POS;
    233         }
    234         if((rocket_pos_change / 9) == 3)
    235         {
    236             rocket_y_position = ROCKET_CENTER_POS - 1;
    237         }
    238 
    239         // Renders the scene piece by piece
    240         render_stars(8, background_frame); // Render star background
    241         render_exhaust(6, rocket_y_position + 1, current_frame); // Render exhaust
    242         render_logo(9, rocket_y_position); // Render the rocket
    243 
    244         // Timing for rocket position change
    245         if(rocket_pos_change < 36)
    246         {
    247             rocket_pos_change++;
    248         }
    249         else
    250         {
    251             rocket_pos_change = 0;
    252         }
    253 
    254         splash_dur_counter++;
    255     }
    256     else if((splash_dur_counter >= SPLASH_DUR) && (splash_dur_counter != 231)) // Should only run once at end of splash screen duration
    257     {
    258         splash_dur_counter = 231; // Nice known value
    259         oled_clear(); // Clear the screen
    260     }
    261 
    262 
    263     // After the splash screen
    264     if(splash_dur_counter == 231)
    265     {
    266         uint8_t light_level = rgblight_get_val();
    267         light_level = (uint8_t)(100.0 * ((float)light_level/(float)RGBLIGHT_LIMIT_VAL)); // Convert to %
    268         char c_light_level[3];
    269         itoa(light_level, c_light_level, 10);
    270 
    271         // Display lock LED statuses
    272         led_t led_state = host_keyboard_led_state();
    273         if(led_state.num_lock)
    274         {
    275             oled_write(PSTR("   |"), false);
    276             oled_write(PSTR("NUM"), true);
    277             oled_write(PSTR("|"), false);
    278         }
    279         else
    280         {
    281             oled_write(PSTR("   |NUM|"), false);
    282         }
    283 
    284         if(led_state.caps_lock)
    285         {
    286             oled_write(PSTR("|"), false);
    287             oled_write(PSTR("CAP"), true);
    288             oled_write(PSTR("|"), false);
    289         }
    290         else
    291         {
    292             oled_write(PSTR("|CAP|"), false);
    293         }
    294 
    295         if(led_state.scroll_lock)
    296         {
    297             oled_write(PSTR("|"), false);
    298             oled_write(PSTR("SCR"), true);
    299             oled_write(PSTR("|   "), false);
    300         }
    301         else
    302         {
    303             oled_write(PSTR("|SCR|   "), false);
    304         }
    305 
    306         // Print the examine info
    307         if(examine_engaged == true)
    308         {
    309             oled_set_cursor(0, 2);
    310             oled_write_ln(PSTR("      Keycode:      "), false);
    311             oled_write_ln_centered(lastKeycodeString, false);
    312         }
    313         else
    314         {
    315             oled_set_cursor(0, 2);
    316             oled_write_ln(PSTR("                    "), false);
    317             oled_write_ln(PSTR("                    "), false);
    318         }
    319 
    320         // Print the backlight % bottom right
    321         oled_set_cursor(11, 7);
    322         oled_write(PSTR("BKLT: "), false);
    323         oled_write(c_light_level, false);
    324         oled_write(PSTR("%"), false);
    325 
    326         // Print the layer number in bottom left
    327         oled_set_cursor(0, 7);
    328         oled_write(PSTR("L: "), false);
    329         switch (get_highest_layer(layer_state))
    330         {
    331         case 0:
    332             oled_write(PSTR("0"), false);
    333             break;
    334         case 1:
    335             oled_write(PSTR("1"), false);
    336             break;
    337         case 2:
    338             oled_write(PSTR("2"), false);
    339             break;
    340         case 3:
    341             oled_write(PSTR("3"), false);
    342             break;
    343         default:
    344             oled_write(PSTR("Und"), false);
    345             break;
    346         }
    347 
    348 
    349     }
    350 
    351     return false;
    352 }
    353 
    354 // Process the extra/extended keycode functionality
    355 bool process_record_user(uint16_t keycode, keyrecord_t *record)
    356 {
    357     bool ret = true; // True will allow QMK to process the key as usual after the function runs, false skips QMK processing after this function runs
    358 
    359     switch (keycode)
    360     {
    361     case KC_EXAM:
    362         if(record->event.pressed) // On pressed, flip bool examine_engaged
    363         {
    364             if(examine_engaged == false)
    365             {
    366                 examine_engaged = true;
    367             }
    368             else
    369             {
    370                 examine_engaged = false;
    371             }
    372             ret = false;
    373         }
    374         else // On release do nothing
    375         {
    376             ret = false;
    377         }
    378         break;
    379 
    380     default: // For any key other than EX, simply let QMK process after saving away what it was
    381         memset(lastKeycodeString, 0, sizeof(lastKeycodeString));
    382         memcpy(lastKeycodeString, translate_keycode_to_string(keycode), sizeof(((lookup_table_t *)0)->key_string));
    383         ret = true;
    384         break;
    385     }
    386 
    387     return ret;
    388 }
    389 #endif