diff options
Diffstat (limited to 'keyboards/clueboard/keymaps/tetris/keymap.c')
| -rw-r--r-- | keyboards/clueboard/keymaps/tetris/keymap.c | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/keyboards/clueboard/keymaps/tetris/keymap.c b/keyboards/clueboard/keymaps/tetris/keymap.c new file mode 100644 index 000000000..c5a16b158 --- /dev/null +++ b/keyboards/clueboard/keymaps/tetris/keymap.c | |||
| @@ -0,0 +1,209 @@ | |||
| 1 | #include "clueboard.h" | ||
| 2 | #include "tetris_text.h" | ||
| 3 | |||
| 4 | // Helpful defines | ||
| 5 | #define GRAVE_MODS (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)|MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)|MOD_BIT(KC_LALT)|MOD_BIT(KC_RALT)) | ||
| 6 | #define _______ KC_TRNS | ||
| 7 | |||
| 8 | // Each layer gets a name for readability, which is then used in the keymap matrix below. | ||
| 9 | // The underscores don't mean anything - you can have a layer called STUFF or any other name. | ||
| 10 | // Layer names don't all need to be of the same length, obviously, and you can also skip them | ||
| 11 | // entirely and just use numbers. | ||
| 12 | #define _BL 0 | ||
| 13 | #define _FL 1 | ||
| 14 | #define _CL 2 | ||
| 15 | |||
| 16 | const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | ||
| 17 | /* Keymap _BL: Base Layer (Default Layer) | ||
| 18 | */ | ||
| 19 | [_BL] = KEYMAP( | ||
| 20 | F(0), 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_GRV, KC_BSPC, KC_PGUP, \ | ||
| 21 | 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_PGDN, \ | ||
| 22 | 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_NUHS, KC_ENT, \ | ||
| 23 | KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RO, KC_RSFT, KC_UP, \ | ||
| 24 | KC_LCTL, KC_LGUI, KC_LALT, KC_MHEN, KC_SPC,KC_SPC, KC_HENK, KC_RALT, KC_RCTL, MO(_FL), KC_LEFT, KC_DOWN, KC_RGHT), | ||
| 25 | |||
| 26 | /* Keymap _FL: Function Layer | ||
| 27 | */ | ||
| 28 | [_FL] = KEYMAP( | ||
| 29 | 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_DEL, BL_STEP, \ | ||
| 30 | _______, _______, _______,_______,_______,F(1) ,_______,_______,KC_PSCR,KC_SLCK, KC_PAUS, _______, _______, _______, _______, \ | ||
| 31 | _______, _______, MO(_CL),_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______, \ | ||
| 32 | _______, _______, _______,_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______, KC_PGUP, \ | ||
| 33 | _______, _______, _______, _______, _______,_______, _______, _______, _______, MO(_FL), KC_HOME, KC_PGDN, KC_END), | ||
| 34 | |||
| 35 | /* Keymap _CL: Control layer | ||
| 36 | */ | ||
| 37 | [_CL] = KEYMAP( | ||
| 38 | _______, _______, _______,_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______, RGB_TOG, RGB_VAI, \ | ||
| 39 | _______, _______, _______,_______,RESET, _______,_______,_______,_______,_______, _______, _______, _______, _______, RGB_VAD, \ | ||
| 40 | _______, _______, MO(_CL),_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, _______, \ | ||
| 41 | MO(_FL), _______, _______,_______,_______,_______,_______,_______,_______,_______, _______, _______, _______, MO(_FL), RGB_SAI, \ | ||
| 42 | _______, _______, _______,_______, RGB_MOD, RGB_MOD, _______, _______, _______, _______, RGB_HUD, RGB_SAD, RGB_HUI), | ||
| 43 | }; | ||
| 44 | |||
| 45 | /* This is a list of user defined functions. F(N) corresponds to item N | ||
| 46 | of this list. | ||
| 47 | */ | ||
| 48 | const uint16_t PROGMEM fn_actions[] = { | ||
| 49 | [0] = ACTION_FUNCTION(0), // Calls action_function() | ||
| 50 | [1] = ACTION_FUNCTION(1) | ||
| 51 | }; | ||
| 52 | |||
| 53 | static uint8_t tetris_key_presses = 0; | ||
| 54 | static uint16_t tetris_timer = 0; | ||
| 55 | static uint8_t tetris_running = 0; | ||
| 56 | static int tetris_keypress = 0; | ||
| 57 | |||
| 58 | void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) { | ||
| 59 | static uint8_t mods_pressed; | ||
| 60 | static bool mod_flag; | ||
| 61 | |||
| 62 | switch (id) { | ||
| 63 | case 0: | ||
| 64 | // clueboard specific hook to make escape quite tetris | ||
| 65 | if (tetris_running) { | ||
| 66 | tetris_running = 0; | ||
| 67 | return; | ||
| 68 | } | ||
| 69 | |||
| 70 | /* Handle the combined Grave/Esc key | ||
| 71 | */ | ||
| 72 | mods_pressed = get_mods()&GRAVE_MODS; // Check to see what mods are pressed | ||
| 73 | |||
| 74 | if (record->event.pressed) { | ||
| 75 | /* The key is being pressed. | ||
| 76 | */ | ||
| 77 | if (mods_pressed) { | ||
| 78 | mod_flag = true; | ||
| 79 | add_key(KC_GRV); | ||
| 80 | send_keyboard_report(); | ||
| 81 | } else { | ||
| 82 | add_key(KC_ESC); | ||
| 83 | send_keyboard_report(); | ||
| 84 | } | ||
| 85 | } else { | ||
| 86 | /* The key is being released. | ||
| 87 | */ | ||
| 88 | if (mod_flag) { | ||
| 89 | mod_flag = false; | ||
| 90 | del_key(KC_GRV); | ||
| 91 | send_keyboard_report(); | ||
| 92 | } else { | ||
| 93 | del_key(KC_ESC); | ||
| 94 | send_keyboard_report(); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | break; | ||
| 98 | case 1: | ||
| 99 | if (record->event.pressed) { | ||
| 100 | tetris_running = 1; | ||
| 101 | tetris_timer = 0; | ||
| 102 | tetris_keypress = 0; | ||
| 103 | // set randomness using total number of key presses | ||
| 104 | tetris_start(tetris_key_presses); | ||
| 105 | } | ||
| 106 | break; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | /* | ||
| 111 | * Set up tetris | ||
| 112 | */ | ||
| 113 | |||
| 114 | bool process_record_user(uint16_t keycode, keyrecord_t *record) { | ||
| 115 | if (record->event.pressed) { | ||
| 116 | tetris_key_presses++; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (tetris_running && record->event.pressed) { | ||
| 120 | tetris_keypress = 0; | ||
| 121 | switch (keycode) { | ||
| 122 | case KC_UP: tetris_keypress = 1; break; | ||
| 123 | case KC_LEFT: tetris_keypress = 2; break; | ||
| 124 | case KC_DOWN: tetris_keypress = 3; break; | ||
| 125 | case KC_RIGHT: tetris_keypress = 4; break; | ||
| 126 | // Make ESC stop tetris (on keyboards other than clueboard) | ||
| 127 | // case KC_ESC: tetris_running = 0; return false; | ||
| 128 | } | ||
| 129 | if (tetris_keypress != 0) { | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | return true; | ||
| 135 | } | ||
| 136 | |||
| 137 | // Runs constantly in the background, in a loop. | ||
| 138 | void matrix_scan_user(void) { | ||
| 139 | if (tetris_running) { | ||
| 140 | tetris_timer++; | ||
| 141 | if (tetris_timer > 1000) { | ||
| 142 | // every 1000 times this is run is about 100 ms. | ||
| 143 | if (!tetris_tick(100)) { | ||
| 144 | // game over | ||
| 145 | tetris_running = 0; | ||
| 146 | } | ||
| 147 | tetris_timer = 0; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | void send_keycode(uint16_t keycode) { | ||
| 153 | register_code(keycode); | ||
| 154 | unregister_code(keycode); | ||
| 155 | } | ||
| 156 | |||
| 157 | void send_keycode_shift(uint16_t keycode) { | ||
| 158 | register_code(KC_LSFT); | ||
| 159 | register_code(keycode); | ||
| 160 | unregister_code(keycode); | ||
| 161 | unregister_code(KC_LSFT); | ||
| 162 | } | ||
| 163 | |||
| 164 | void tetris_send_up(void) { | ||
| 165 | send_keycode(KC_UP); | ||
| 166 | } | ||
| 167 | void tetris_send_left(void) { | ||
| 168 | send_keycode(KC_LEFT); | ||
| 169 | } | ||
| 170 | void tetris_send_down(void) { | ||
| 171 | send_keycode(KC_DOWN); | ||
| 172 | } | ||
| 173 | void tetris_send_right(void) { | ||
| 174 | send_keycode(KC_RGHT); | ||
| 175 | } | ||
| 176 | void tetris_send_backspace(void) { | ||
| 177 | send_keycode(KC_BSPC); | ||
| 178 | } | ||
| 179 | void tetris_send_delete(void) { | ||
| 180 | send_keycode(KC_DEL); | ||
| 181 | } | ||
| 182 | |||
| 183 | void tetris_send_string(const char *s) { | ||
| 184 | for (int i = 0; s[i] != 0; i++) { | ||
| 185 | if (s[i] >= 'a' && s[i] <= 'z') { | ||
| 186 | send_keycode(KC_A + (s[i] - 'a')); | ||
| 187 | } else if (s[i] >= 'A' && s[i] <= 'Z') { | ||
| 188 | send_keycode_shift(KC_A + (s[i] - 'A')); | ||
| 189 | } else if (s[i] >= '1' && s[i] <= '9') { | ||
| 190 | send_keycode(KC_1 + (s[i] - '1')); | ||
| 191 | } else { | ||
| 192 | switch (s[i]) { | ||
| 193 | case ' ': send_keycode(KC_SPACE); break; | ||
| 194 | case '.': send_keycode(KC_DOT); break; | ||
| 195 | case '0': send_keycode(KC_0); break; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | void tetris_send_newline(void) { | ||
| 202 | send_keycode(KC_ENT); | ||
| 203 | } | ||
| 204 | |||
| 205 | int tetris_get_keypress(void) { | ||
| 206 | int out = tetris_keypress; | ||
| 207 | tetris_keypress = 0; | ||
| 208 | return out; | ||
| 209 | } | ||
