]> git.donarmstrong.com Git - qmk_firmware.git/blob - users/drashna/drashna.c
[Keyboard] Add QMK configurator JSON for Alice PCB (#6397)
[qmk_firmware.git] / users / drashna / drashna.c
1 /*
2 Copyright 2017 Christopher Courtney <drashna@live.com> @drashna
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "drashna.h"
19
20 userspace_config_t userspace_config;
21 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
22 #    define DRASHNA_UNICODE_MODE UC_WIN
23 #else
24 // set to 2 for UC_WIN, set to 4 for UC_WINC
25 #    define DRASHNA_UNICODE_MODE 2
26 #endif
27
28 // This block is for all of the gaming macros, as they were all doing
29 // the same thing, but with differring text sent.
30 bool send_game_macro(const char *str, keyrecord_t *record, bool override) {
31     if (!record->event.pressed || override) {
32         uint16_t keycode;
33         if (userspace_config.is_overwatch) {
34             keycode = KC_BSPC;
35         } else {
36             keycode = KC_ENTER;
37         }
38         clear_keyboard();
39         tap_code(keycode);
40         wait_ms(TAP_CODE_DELAY);
41         send_string_with_delay(str, TAP_CODE_DELAY);
42         wait_ms(TAP_CODE_DELAY);
43         tap_code(KC_ENTER);
44     }
45     if (override) wait_ms(3000);
46     return false;
47 }
48
49 bool mod_key_press_timer(uint16_t code, uint16_t mod_code, bool pressed) {
50     static uint16_t this_timer;
51     if (pressed) {
52         this_timer = timer_read();
53     } else {
54         if (timer_elapsed(this_timer) < TAPPING_TERM) {
55             tap_code(code);
56         } else {
57             register_code(mod_code);
58             tap_code(code);
59             unregister_code(mod_code);
60         }
61     }
62     return false;
63 }
64
65 bool mod_key_press(uint16_t code, uint16_t mod_code, bool pressed, uint16_t this_timer) {
66     if (pressed) {
67         this_timer = timer_read();
68     } else {
69         if (timer_elapsed(this_timer) < TAPPING_TERM) {
70             tap_code(code);
71         } else {
72             register_code(mod_code);
73             tap_code(code);
74             unregister_code(mod_code);
75         }
76     }
77     return false;
78 }
79
80 void bootmagic_lite(void) {
81     matrix_scan();
82 #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
83     wait_ms(DEBOUNCING_DELAY * 2);
84 #elif defined(DEBOUNCE) && DEBOUNCE > 0
85     wait_ms(DEBOUNCE * 2);
86 #else
87     wait_ms(30);
88 #endif
89     matrix_scan();
90     if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
91         bootloader_jump();
92     }
93 }
94
95 // Add reconfigurable functions here, for keymap customization
96 // This allows for a global, userspace functions, and continued
97 // customization of the keymap.  Use _keymap instead of _user
98 // functions in the keymaps
99 __attribute__((weak))
100 void matrix_init_keymap(void) {}
101
102 // Call user matrix init, set default RGB colors and then
103 // call the keymap's init function
104 void matrix_init_user(void) {
105     userspace_config.raw = eeconfig_read_user();
106
107 #ifdef BOOTLOADER_CATERINA
108     DDRD &= ~(1 << 5);
109     PORTD &= ~(1 << 5);
110
111     DDRB &= ~(1 << 0);
112     PORTB &= ~(1 << 0);
113 #endif
114
115 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
116     set_unicode_input_mode(DRASHNA_UNICODE_MODE);
117     get_unicode_input_mode();
118 #endif  // UNICODE_ENABLE
119     matrix_init_keymap();
120 }
121
122 __attribute__((weak))
123 void keyboard_post_init_keymap(void) {}
124
125 void keyboard_post_init_user(void) {
126 #ifdef RGBLIGHT_ENABLE
127     keyboard_post_init_rgb();
128 #endif
129     keyboard_post_init_keymap();
130 }
131
132 __attribute__((weak))
133 void shutdown_keymap(void) {}
134
135 void shutdown_user(void) {
136 #ifdef RGBLIGHT_ENABLE
137     rgblight_enable_noeeprom();
138     rgblight_mode_noeeprom(1);
139     rgblight_setrgb_red();
140 #endif  // RGBLIGHT_ENABLE
141 #ifdef RGB_MATRIX_ENABLE
142     // uint16_t timer_start = timer_read();
143     // rgb_matrix_set_color_all( 0xFF, 0x00, 0x00 );
144     // while(timer_elapsed(timer_start) < 250) { wait_ms(1); }
145 #endif  // RGB_MATRIX_ENABLE
146     shutdown_keymap();
147 }
148
149 __attribute__((weak))
150 void suspend_power_down_keymap(void) {}
151
152 void suspend_power_down_user(void) {
153     suspend_power_down_keymap();
154 }
155
156 __attribute__((weak))
157 void suspend_wakeup_init_keymap(void) {}
158
159 void suspend_wakeup_init_user(void) {
160     suspend_wakeup_init_keymap();
161 }
162
163 __attribute__((weak))
164 void matrix_scan_keymap(void) {}
165
166 // No global matrix scan code, so just run keymap's matrix
167 // scan function
168 void matrix_scan_user(void) {
169     static bool has_ran_yet;
170     if (!has_ran_yet) {
171         has_ran_yet = true;
172         startup_user();
173     }
174
175 #ifdef TAP_DANCE_ENABLE  // Run Diablo 3 macro checking code.
176     run_diablo_macro_check();
177 #endif  // TAP_DANCE_ENABLE
178
179 #ifdef RGBLIGHT_ENABLE
180     matrix_scan_rgb();
181 #endif  // RGBLIGHT_ENABLE
182
183     matrix_scan_keymap();
184 }
185
186 __attribute__((weak))
187 layer_state_t layer_state_set_keymap(layer_state_t state) { return state; }
188
189 // on layer change, no matter where the change was initiated
190 // Then runs keymap's layer change check
191 layer_state_t layer_state_set_user(layer_state_t state) {
192     state = update_tri_layer_state(state, _RAISE, _LOWER, _ADJUST);
193 #ifdef RGBLIGHT_ENABLE
194     state = layer_state_set_rgb(state);
195 #endif  // RGBLIGHT_ENABLE
196     return layer_state_set_keymap(state);
197 }
198
199 __attribute__((weak))
200 layer_state_t default_layer_state_set_keymap(layer_state_t state) { return state; }
201
202 // Runs state check and changes underglow color and animation
203 layer_state_t default_layer_state_set_user(layer_state_t state) {
204     state = default_layer_state_set_keymap(state);
205 #if 0
206 #    ifdef RGBLIGHT_ENABLE
207   state = default_layer_state_set_rgb(state);
208 #    endif  // RGBLIGHT_ENABLE
209 #endif
210     return state;
211 }
212
213 __attribute__((weak))
214 void led_set_keymap(uint8_t usb_led) {}
215
216 // Any custom LED code goes here.
217 // So far, I only have keyboard specific code,
218 // So nothing goes here.
219 void led_set_user(uint8_t usb_led) {
220     led_set_keymap(usb_led);
221 }
222
223 __attribute__((weak))
224 void eeconfig_init_keymap(void) {}
225
226 void eeconfig_init_user(void) {
227     userspace_config.raw              = 0;
228     userspace_config.rgb_layer_change = true;
229     eeconfig_update_user(userspace_config.raw);
230 #if (defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE))
231     set_unicode_input_mode(DRASHNA_UNICODE_MODE);
232     get_unicode_input_mode();
233 #else
234     eeprom_update_byte(EECONFIG_UNICODEMODE, DRASHNA_UNICODE_MODE);
235 #endif
236     eeconfig_init_keymap();
237     keyboard_init();
238 }