summaryrefslogtreecommitdiffstats
path: root/keyboards/mechwild/sugarglider/sugarglider.c
blob: 76c6d2983202e58091f0283a63d0552758e2153b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2023 Kyle McCreery
// SPDX-License-Identifier: GPL-2.0-or-later

#include "sugarglider.h"

#ifndef GLIDEPOINT_DPI_OPTIONS
#    define GLIDEPOINT_DPI_OPTIONS \
        { 400, 800, 1200, 1600, 2000, 2400, 2800, 3200, 3600, 4000 }
#endif

#ifndef GLIDEPOINT_DPI_DEFAULT
#    define GLIDEPOINT_DPI_DEFAULT 1
#endif

keyboard_config_t keyboard_config;
uint16_t          dpi_array[] = GLIDEPOINT_DPI_OPTIONS;
#define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t))

void board_init(void) {
    // B9 is configured as I2C1_SDA in the board file; that function must be
    // disabled before using B7 as I2C1_SDA.
    setPinInputHigh(B9);
    setPinOutput(B12);
    setPinOutput(B13);
    setPinOutput(B14);
    writePinLow(B12);
    writePinLow(B13);
    writePinLow(B14);
    setPinOutput(C13);
}

#ifdef DYNAMIC_TAPPING_TERM_ENABLE
void tap_modify(int change_value, bool tap_status) {
    if (keyboard_config.dt_term_config < 0) {
        keyboard_config.dt_term_config *= -1;
    }

    keyboard_config.dt_term_config += change_value;

    if (tap_status == false ) {
        keyboard_config.dt_term_config *= -1;
        g_tapping_term = 0;
    } else {
        g_tapping_term = keyboard_config.dt_term_config;
    }
    eeconfig_update_kb(keyboard_config.raw);
}

void tap_toggle(void) {
    keyboard_config.dt_term_config *= -1;
    if (keyboard_config.dt_term_config > 0) {
        g_tapping_term = keyboard_config.dt_term_config;
    } else {
        g_tapping_term = 0;
    }
    eeconfig_update_kb(keyboard_config.raw);
}
#endif  // ifdef DYNAMIC_TAPPING_TERM_ENABLE

#ifdef DIP_SWITCH_ENABLE
bool dip_switch_update_kb(uint8_t index, bool active) {
    if (!dip_switch_update_user(index, active)) { return false; }
    switch (index) {
        case 0:
            if(active != host_keyboard_led_state().caps_lock) { tap_code(KC_CAPS); }
            break;
        break;
    }
    return true;
}
#endif

#if defined(ENCODER_ENABLE) && ! defined(ENCODER_MAP_ENABLE)
bool encoder_update_kb(uint8_t index, bool clockwise) {
    if (!encoder_update_user(index, clockwise)) { return false; }
    switch (index) {
        case 0:
            if (clockwise) {
                tap_code(KC_VOLU);
            } else {
                tap_code(KC_VOLD);
            }
        break;
        case 1:
            if (clockwise) {
                tap_code(KC_VOLU);
            } else {
                tap_code(KC_VOLD);
            }
        break;
        case 2:
            if (clockwise) {
                tap_code(KC_PGUP);
            } else {
                tap_code(KC_PGDN);
            }
        break;
        case 3:
            if (clockwise) {
                tap_code(KC_WH_U);
            } else {
                tap_code(KC_WH_D);
            }
        break;
    }
    return true;
}
#endif

bool led_update_kb(led_t led_state) {
    bool res = led_update_user(led_state);
    if(res) {
        writePin(B12, led_state.num_lock);     // Updates status LEDs
        writePin(B13, led_state.caps_lock);    // Updates status LEDs
        writePin(B14, led_state.scroll_lock);  // Updates status LEDs
        writePin(C13, !led_state.caps_lock);   // Updates status LEDs, this is the LED on the blackpill itself
    }
    return res;
}

#ifdef OLED_ENABLE
static void render_logo(void) {     // Render MechWild "MW" Logo
    static const char PROGMEM logo_1[] = {0xCC, 0xCD, 0xCE, 0xCF, 0x00};
    static const char PROGMEM logo_2[] = {0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00};
    static const char PROGMEM logo_3[] = {0xD5, 0xD6, 0xD7, 0xD8, 0x00};
    static const char PROGMEM logo_4[] = {0xDE, 0xD9, 0xDA, 0xDB, 0x00};
    
    oled_write_P(logo_1, false);
    oled_set_cursor(0,1);
    oled_write_P(logo_2, false);
    oled_set_cursor(0,2);
    oled_write_P(logo_3, false);
    oled_set_cursor(0,3);
    oled_write_P(logo_4, false);
}

#ifdef OLED_DISPLAY_128X64 // Wide OLED Functionality
oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
    return OLED_ROTATION_180;       // flips the display 180 degrees
}

bool clear_screen = true;           // used to manage singular screen clears to prevent display glitch
bool clear_screen_art = true;       // used to manage singular screen clears to prevent display glitch
static void render_sugarglider_wide(void) {     // Render sugarglider logo
    static const char PROGMEM sgl_1[] = {0xDE, 0xDE, 0xDE, 0x98, 0x99, 0xDE, 0x9A, 0x9B, 0x9C, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xCC, 0xCD, 0xCE, 0xCF, 0x00};
    static const char PROGMEM sgl_2[] = {0x9D, 0x9E, 0xDE, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xDE, 0xDE, 0xDE, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0x00};
    static const char PROGMEM sgl_3[] = {0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xDE, 0xDE, 0xDE, 0xD5, 0xD6, 0xD7, 0xD8, 0x00};
    static const char PROGMEM sgl_4[] = {0xDE, 0xB2, 0xDE, 0xB3, 0xB4, 0xB5, 0xB6, 0xDE, 0xDE, 0xB7, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xD9, 0xDA, 0xDB, 0x00};
    static const char PROGMEM sgl_5[] = {0xDE, 0xB8, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xB9, 0x00};
    static const char PROGMEM sgl_6[] = {0xDE, 0xBA, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE, 0xBB, 0xBC, 0x00};
    static const char PROGMEM sgl_7[] = {0xDE, 0xBD, 0xBE, 0xDE, 0xDE, 0xBF, 0xDE, 0xDE, 0xC0, 0xC1, 0x00};
    static const char PROGMEM sgl_8[] = {0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0x00};

    oled_write_P(sgl_1, false);
    oled_set_cursor(0,1);
    oled_write_P(sgl_2, false);
    oled_set_cursor(0,2);
    oled_write_P(sgl_3, false);
    oled_set_cursor(0,3);
    oled_write_P(sgl_4, false);
    oled_set_cursor(0,4);
    oled_write_P(sgl_5, false);
    oled_set_cursor(0,5);
    oled_write_P(sgl_6, false);
    oled_set_cursor(0,6);
    oled_write_P(sgl_7, false);
    oled_set_cursor(0,7);
    oled_write_P(sgl_8, false);
}

bool oled_task_kb(void) {
    if(!oled_task_user()) {
        return false;
    }
    if ( !(host_keyboard_led_state().num_lock || host_keyboard_led_state().caps_lock || host_keyboard_led_state().scroll_lock ) && get_highest_layer(layer_state) == 0 ) {      // If none of the status LEDs are active and also the active layer is the base layer
        if (clear_screen_art == true) {
            oled_clear();
            oled_render();
            clear_screen_art = false;
        }
        render_sugarglider_wide();
#ifdef POINTING_DEVICE_ENABLE                           // only display dpi and tap info if pointing devices are active
        oled_set_cursor(10,5);
        oled_write_P(PSTR(" DPI:"), false);
        oled_write(get_u16_str(dpi_array[keyboard_config.dpi_config], ' '), false);
#ifdef DYNAMIC_TAPPING_TERM_ENABLE                      // only display tap info if it is being configured dynamically
        oled_set_cursor(10,6);
        oled_write_P(PSTR(" TAP:"), false);
        if (keyboard_config.dt_term_config < 0) {
            oled_write_P(PSTR("Off  "), false);
        } else {
            oled_write(get_u16_str(g_tapping_term, ' '), false);
        }
#endif
#endif
        clear_screen = true;
    } else {        // If any of the status LEDs are active or if the active layer is not the base layer
        if (clear_screen == true) {
            oled_clear();
            oled_render();
            clear_screen = false;
        }
        render_logo();
        oled_set_cursor(8,1);
        switch (get_highest_layer(layer_state)) {
            case 0:
                oled_write_P(PSTR("Base  "), false);
                break;
            case 1:
                oled_write_P(PSTR("Lower "), false);
                break;
            case 2:
                oled_write_P(PSTR("Raise "), false);
                break;
            case 3:
                oled_write_P(PSTR("Adjust"), false);
                break;
            case 4:
                oled_write_P(PSTR("Mouse "), false);
                break;
#ifdef STENO_ENABLE
            case 5:
                oled_write_P(PSTR("Steno Layer"), false);
                break;
#endif
            default:
                oled_write_P(PSTR("Layer ?"), false);    // Should never display, here as a catchall
        }
        led_t led_state = host_keyboard_led_state();
        oled_set_cursor(8,0);
        oled_write_P(led_state.num_lock ? PSTR("NUM ") : PSTR("    "), false);
        oled_write_P(led_state.caps_lock ? PSTR("CAP ") : PSTR("    "), false);
        oled_write_P(led_state.scroll_lock ? PSTR("SCR") : PSTR("    "), false);
#ifdef POINTING_DEVICE_ENABLE                   // only display dpi and tap info if pointing devices are active
        oled_set_cursor(8,2);
        oled_write_P(PSTR("DPI:"), false);
        oled_write(get_u16_str(dpi_array[keyboard_config.dpi_config], ' '), false);
#ifdef DYNAMIC_TAPPING_TERM_ENABLE              // only display tap info if it is being configured dynamically
        oled_set_cursor(8,3);
        oled_write_P(PSTR("TAP:"), false);
        if (keyboard_config.dt_term_config < 0) {
            oled_write_P(PSTR("Off  "), false);
        } else {
            oled_write(get_u16_str(g_tapping_term, ' '), false);
        }
#endif
#endif
        clear_screen_art = true;
    }
    return true;
}
#else  // Using an OLED but not the wide OLED
oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
    return OLED_ROTATION_270;       // flips the display 270 degrees
}


bool oled_task_kb(void) {
    if(!oled_task_user()) {
        return false;
    }
    render_logo();

    oled_set_cursor(0,5);
    switch (get_highest_layer(layer_state)) {
        case 0:
            oled_write_P(PSTR("Base "), false);
            break;
        case 1:
            oled_write_P(PSTR("Lower"), false);
            break;
        case 2:
            oled_write_P(PSTR("Raise"), false);
            break;
        case 3:
            oled_write_P(PSTR("Adjst"), false);
            break;
        case 4:
            oled_write_P(PSTR("Mouse"), false);
            break;
#ifdef STENO_ENABLE
        case 5:
            oled_write_P(PSTR("Steno"), false);
            break;
#endif
        default:
            oled_write_P(PSTR("?????"), false);    // Should never display, here as a catchall
    }
        led_t led_state = host_keyboard_led_state();
        oled_set_cursor(0,6);
        oled_write_P(led_state.num_lock ? PSTR("NUM  ") : PSTR("     "), false);
        oled_write_P(led_state.caps_lock ? PSTR("CAP  ") : PSTR("     "), false);
        oled_write_P(led_state.scroll_lock ? PSTR("SCR  ") : PSTR("     "), false);
#ifdef POINTING_DEVICE_ENABLE
        oled_set_cursor(0,11);
        oled_write_P(PSTR("DPI: "), false);
        oled_write(get_u16_str(dpi_array[keyboard_config.dpi_config], ' '), false);
#endif
#ifdef DYNAMIC_TAPPING_TERM_ENABLE              // only display tap info if it is being configured dynamically
        oled_set_cursor(0,13);
        oled_write_P(PSTR("TAP: "), false);
        if (keyboard_config.dt_term_config < 0) {
            oled_write_P(PSTR("Off  "), false);
        } else {
            oled_write(get_u16_str(g_tapping_term, ' '), false);
        }
#endif
    return true;
}
#endif // Not using wide OLED
#endif // ifdef OLED_ENABLE

bool process_record_kb(uint16_t keycode, keyrecord_t* record) {
    switch(keycode) {
#ifdef POINTING_DEVICE_ENABLE
        case DPI_UP:
            if (record->event.pressed) {
                keyboard_config.dpi_config = (keyboard_config.dpi_config + 1) % DPI_OPTION_SIZE;
                eeconfig_update_kb(keyboard_config.raw);
                pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
            }
            return false;
        case DPI_DN:
            if (record->event.pressed) {
                if (keyboard_config.dpi_config > 0) {
                    keyboard_config.dpi_config = (keyboard_config.dpi_config - 1) % DPI_OPTION_SIZE;
                } else {
                    keyboard_config.dpi_config = DPI_OPTION_SIZE - 1;
                }
                eeconfig_update_kb(keyboard_config.raw);
                pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
            }
            return false;
        case DPI_FINE:
            if (record->event.pressed) {
                pointing_device_set_cpi(dpi_array[0]);
            } else {
                pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
            }
            return false;
#endif
#ifdef DYNAMIC_TAPPING_TERM_ENABLE // only include tap info keycodes if it is being configured dynamically
        case TAP_UP:
            if (record->event.pressed) {
                if (keyboard_config.dt_term_config < 5000) {
                    tap_modify(DYNAMIC_TAPPING_TERM_INCREMENT, true);
                }
            }
            return false;
        case TAP_DN:
            if (record->event.pressed) {
                if (keyboard_config.dt_term_config > 0) {
                    tap_modify(-1 * DYNAMIC_TAPPING_TERM_INCREMENT, true);
                }
            }
            return false;
        case TAP_ON:
            if (record->event.pressed) {
                tap_modify(0, true);
            }
            return false;
        case TAP_OFF:
            if (record->event.pressed) {
                tap_modify(0, false);
            }
            return false;
        case TAP_TOG:
            if (record->event.pressed) {
                tap_toggle();
            }
            return false;
#endif // ifdef 
    }
    return process_record_user(keycode, record);
}

void pointing_device_init_kb(void) {
#ifdef POINTING_DEVICE_ENABLE
    pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
#endif
}

void eeconfig_init_kb(void) {
#ifdef POINTING_DEVICE_ENABLE
    keyboard_config.dpi_config = GLIDEPOINT_DPI_DEFAULT;
#endif
#ifdef DYNAMIC_TAPPING_TERM_ENABLE // only set tap term from eeprom if it is being configured dynamically
    keyboard_config.dt_term_config = TAPPING_TERM;
#endif
    eeconfig_update_kb(keyboard_config.raw);
#ifdef STENO_ENABLE
    steno_set_mode(STENO_MODE_GEMINI); // or STENO_MODE_BOLT
#endif
    eeconfig_init_user();
}

void matrix_init_kb(void) {
    // is safe to just read DPI setting since matrix init
    // comes before pointing device init.
    keyboard_config.raw = eeconfig_read_kb();
#ifdef POINTING_DEVICE_ENABLE
    if (keyboard_config.dpi_config > DPI_OPTION_SIZE) {
        eeconfig_init_kb();
    }
#endif
    matrix_init_user();
}

void keyboard_post_init_kb(void) {
#ifdef POINTING_DEVICE_ENABLE
    pointing_device_set_cpi(dpi_array[keyboard_config.dpi_config]);
#endif
#ifdef RGBLIGHT_ENABLE
    rgblight_toggle_noeeprom();     //double toggle post init removes the weirdness with rgb strips having a yellow first LED
    rgblight_toggle_noeeprom();
#endif
#ifdef DYNAMIC_TAPPING_TERM_ENABLE
    tap_toggle();   // Need it to reevaluate this setting after initiating so that it is current after init
    tap_toggle();
#endif
    keyboard_post_init_user();
#ifdef OLED_ENABLE  // purposefully after user post init to allow the RGB to startup first
    wait_ms(200);   // Avoids a startup issue where the oled renders and then turns off with blackpill
    oled_on();
#endif
}