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