summaryrefslogtreecommitdiff
path: root/keyboards/crkbd/keymaps/mcrown/oled.c
blob: b728a5806e0940b3c21bf4dd13bcf9f15747f848 (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
/** @file oled.h
 *  @brief mcrown oled service implementation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Mario Corona (mariocc@comunidad.unam.mx) 2021
 *
 */

#include QMK_KEYBOARD_H
#include <stdio.h>
#include <string.h>
#include "mcrown.h"
#include "oled.h"

#define ASCII_TABLE_LENGTH       (0x80)
#define KEYLOG_STRING_STARTUP    (KEYLOG_EOL_LEN+1)
#define ALT_CODE                 (0x7E)
#define SPECIAL_KEYS_SHIFT(kc)   (0x18+(kc))

static void render_keylogger_status(void);

static char keylog_str[KEYLOG_EOL_LEN] = {' '};
static uint16_t log_timer = 0;
static uint8_t current_cursor_pos=0;
static uint32_t cursor_oled_timer = 0;
static uint32_t standby_oled_timer = 0;
static char last_c=' ';

/* Provides the ASCII value or the address of the character selected of the OLED font specified in glcfont.c */
static const char ascii_t[ASCII_TABLE_LENGTH] = {
        /*     0          1         2         3        4         5         6         7         8         9         A         B         C         D         E         F                */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
           0x0F,         0x1A,    0x1B,     0x19,    0x18,     0x0E,      ' ',      ' ',     0x11,      0x1C,    0x97,      ' ',      ' ',      ' ',      ' ',      ' ',         /* 0 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            ' ',         ' ',      ' ',      ' ',     ' ',      ' ',      ' ',      ' ',      ' ',       ' ',     ' ',     0x1D,      ' ',      ' ',      ' ',      ' ',         /* 1 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
           0x16,         '!',      '"',      '#',     '$',      '%',      '&',     '\'',      '(',       ')',     '*',      '+',      ',',      '-',      '.',      '/',         /* 2 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            '0',         '1',      '2',      '3',     '4',      '5',      '6',      '7',      '8',       '9',     ':',      ';',      '<',      '=',      '>',      '?',         /* 3 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            '@',         'A',      'B',      'C',     'D',      'E',      'F',      'G',      'H',       'I',     'J',      'K',      'L',      'M',      'N',      'O',         /* 4 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            'P',         'Q',      'R',      'S',     'T',      'U',      'V',      'W',      'X',       'Y',     'Z',      '[',      '\\',     ']',      '^',      '_',         /* 5 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            '`',         'a',      'b',      'c',     'd',      'e',      'f',      'g',      'h',       'i',     'j',      'k',      'l',      'm',      'n',      'o',         /* 6 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            'p',         'q',      'r',      's',     't',      'u',      'v',      'w',      'x',       'y',     'z',      '{',      '|',      '}',      '~',     0x7F,         /* 7 */
};

/* This table is to remap and get the corresponding ASCII value based on the KEYCODE (taken as the index of the array) of quatum_keycodes.h module */
static const unsigned char code_to_ascii[ASCII_TABLE_LENGTH] = {
        /*     0          1         2         3        4         5         6         7         8         9         A         B         C         D         E         F                */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
           0x00,        0x00,     0x00,     0x00,      'a',      'b',     'c',      'd',      'e',       'f',     'g',      'h',      'i',      'j',      'k',      'l',         /* 0 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            'm',         'n',      'o',      'p',      'q',      'r',     's',      't',      'u',       'v',     'w',      'x',      'y',      'z',      '1',      '2',         /* 1 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            '3',         '4',      '5',      '6',      '7',      '8',     '9',      '0',     0x0A,      0x1B,    0x08,     0x09,      ' ',      '-',      '=',      '[',         /* 2 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            ']',        '\\',     0x00,      ';',     '\'',      '`',     ',',      '.',      '/',      0x00,    0x00,     0x00,     0x00,     0x00,      '!',      '@',         /* 3 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            '#',         '$',      '%',      '^',      '&',      '*',     '(',      ')',     0x00,      0x00,    0x00,     0x00,     0x00,      '_',      '+',      '{',         /* 4 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
            '}',         '|',     0x00,     0x00,     0x00,      '~',    0x00,     0x00,     0x00,      0x00,    0x00,     0x00,     0x00,     0x00,     0x00,     0x00,         /* 5 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
           0x00,        0x00,     0x00,     0x00,     0x7F,     0x00,    0x00,     0x01,     0x02,      0x03,    0x04,     0x00,     0x00,     0x00,     0x00,     0x00,         /* 6 */
        /*          |         |         |         |         |         |         |         |         |         |         |         |         |         |         |         |           */
           0x00,        0x00,     0x00,     0x00,     0x00,     0x00,    0x00,     0x00,     0x00,      0x00,    0x00,     0x00,     0x00,     0x00,     0x05,     0x00,         /* 7 */
};

/** @brief maps the keycode to get the ascii value.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param keycode value of the pressed key.
 *  @return ascii value of the pressed key or a special value for non-ascii keys.
 */
inline static char get_ascii(int16_t keycode){
    uint8_t ascii_idx=0x00;

    if(keycode<KC_F1){
        ascii_idx=code_to_ascii[(uint8_t)keycode];
    }else if(keycode<KC_KP_ENTER){
        ascii_idx=code_to_ascii[SPECIAL_KEYS_SHIFT(keycode)];
    }else if(KC_LANG1==keycode){
        ascii_idx=code_to_ascii[ALT_CODE];
    }else if( QK_LSFT==(QK_LSFT&keycode) ){
        ascii_idx=code_to_ascii[RM_LSFT(keycode)];
    }

    return ascii_t[ascii_idx];
}

/** @brief detect retuns the rotation of the display based on the keyboard side.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param oled_rotation_t rotation
 *  @return rotation of the display.
 */
oled_rotation_t oled_init_user(oled_rotation_t rotation){
    oled_rotation_t oled_rot=OLED_ROTATION_180;

    if(true==is_keyboard_master()){
#ifdef OLED_VERTICAL
        oled_rot=OLED_ROTATION_270;
#else
        oled_rot=OLED_ROTATION_0;
#endif
    }
    return oled_rot;
}

/** @brief renders the keylog string and display it. This function also toggles the cursor.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param void.
 *  @return void.
 */
static void render_keylogger_status(void){
    static bool cursor_f=true;

    if(timer_elapsed32(cursor_oled_timer) > 300){
        cursor_oled_timer = timer_read32();
        cursor_f=!cursor_f;
    }
    oled_write_P(PSTR("\n>:"), false);
    if(current_cursor_pos>(KEYLOG_LEN-1)){
        current_cursor_pos=0;
        memset(keylog_str, ' ', sizeof(char)*KEYLOG_EOL_LEN);
        /* Here the EOL is to clear with white spaces all the keylog area */
        keylog_str[KEYLOG_EOL_LEN-1] = '\0';
        oled_write(keylog_str, false);
        /* Reset EOL to the begining of the keylog string */
        keylog_str[0] = '\0';
    }
    oled_write(keylog_str, false);
    oled_write_char(last_c, cursor_f);

}

/** @brief displays the current active layout.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param void.
 *  @return void.
 */
void render_layout_state(void){
    CUSTOM_LAYERS_T current_layer;
    current_layer=(CUSTOM_LAYERS_T)get_highest_layer(layer_state);

#ifdef OLED_VERTICAL
    oled_write_P(PSTR("Lyt:\n"), false);
#else
    oled_write_P(PSTR("Layout: "), false);
#endif

    switch (current_layer){
#ifdef OLED_VERTICAL
        case _COLEMAK:
            oled_write_P(PSTR("Clmak\n"), false);
            break;

        case _DVORAK:
            oled_write_P(PSTR("Dvak\n"), false);
            break;

        case _QWERTY:
            oled_write_P(PSTR("Qwty\n"), false);
            break;

        default:
            oled_write_P(PSTR("Undf\n"), false);
            break;

#else
        case _COLEMAK:
            oled_write_P(PSTR("Colemak\n"), false);
            break;

        case _DVORAK:
            oled_write_P(PSTR("Dvorak\n"), false);
            break;

        case _QWERTY:
            oled_write_P(PSTR("Qwerty\n"), false);
            break;

        default:
            oled_write_P(PSTR("Undefined\n"), false);
            break;
#endif
    }
}

/** @brief displays the current active layer.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param void.
 *  @return void.
 */
void render_layer_state(void){
    CUSTOM_LAYERS_T current_layer;
    current_layer=(CUSTOM_LAYERS_T)get_highest_layer(layer_state);

#ifdef OLED_VERTICAL
    oled_write_P(PSTR("Lyr:\n"), false);
#else
    oled_write_P(PSTR("Layer:"), false);
#endif

    switch(current_layer){
#ifdef OLED_VERTICAL
        case _LOWER:
            oled_write_P(PSTR("Lwr\n"), true);
            break;

        case _RAISE:
            oled_write_P(PSTR("Ris\n"), true);
            break;

        case _ADJUST:
            oled_write_P(PSTR("Adj\n"), true);
            break;

        case _NUMPAD:
            oled_write_P(PSTR("Num\n"), true);
            break;

        default:
            oled_write_P(PSTR("Def\n"), false);
            break;

#else
        case _LOWER:
            oled_write_P(PSTR(" Lower "), true);
            break;

        case _RAISE:
            oled_write_P(PSTR(" Raise "), true);
            break;

        case _ADJUST:
            oled_write_P(PSTR(" Adjust "), true);
            break;

        case _NUMPAD:
            oled_write_P(PSTR(" Numpad "), true);
            break;

        default:
            oled_write_P(PSTR(" Default "), false);
            break;
#endif
    }
}

/** @brief displays the current status of the main display/
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param void.
 *  @return void.
 */
void render_status(void){
    render_layout_state();
    oled_write_P(PSTR("\n"), false);
    render_layer_state();
    render_keylogger_status();
}

/** @brief renders the logo to be displayed.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param void.
 *  @return void.
 */
static void render_logo(void){
    static const char PROGMEM qmk_logo[] = {
        0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4,
        0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4,
        0x00};

    oled_write_P(qmk_logo, false);
}

/** @brief executes the actions for both displays.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param void.
 *  @return void.
 */
void oled_task_user(void){
    if (timer_elapsed32(standby_oled_timer) > 15000){
        oled_off();
    }else{
        oled_on();
        if(true==is_keyboard_master()){
            render_status();
        }else{
            render_logo();
            oled_write_P(PSTR("\n"), false);
            oled_scroll_left();
        }
    }
}

/** @brief process the current key and add it to the keylog string.
 *
 *  If any argument is invalid, the function has no effect.
 *
 *  @param keycode pressed key.
 *  @return void.
 */
extern void add_keylog(uint16_t keycode){
    if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)){
        keycode&=0x00FF;
    }

    if(current_cursor_pos>(KEYLOG_LEN-1)||(current_cursor_pos>KEYLOG_STRING_STARTUP)){
        current_cursor_pos=0;
        last_c=get_ascii(keycode);
        current_cursor_pos++;
    }else{
        if(keycode <= KC_TILD){
            keylog_str[current_cursor_pos]=last_c;
            last_c=get_ascii(keycode);
            current_cursor_pos++;
        }
        keylog_str[current_cursor_pos] = '\0';
    }

    log_timer = timer_read();

    standby_oled_timer = timer_read32();
}