le_chiffre.c (4406B)
1 // Copyright 2020 tominabox1 (@tominabox1) 2 // Copyright 2019 @foostan 3 // Copyright 2020 Drashna Jaelre <@drashna> 4 // Copyright 2023 QMK Contributors <@qmk> 5 // SPDX-License-Identifier: GPL-2.0-or-later 6 7 #include "quantum.h" 8 #include "le_chiffre.h" 9 10 #if defined(OLED_ENABLE) // Special thanks to Sickbabies for this great OLED widget! 11 12 /* CONSTANTS */ 13 const char PROGMEM code_to_name[53] = { 14 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 15 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 16 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', 17 '5', '6', '7', '8', '9', '0', 'R', 'E', 'B', 'T', 18 '_', '-', '=', '[', ']', '\\', '#', ';', '\'', '`', 19 ',', '.', '/' 20 }; 21 22 const char PROGMEM lechiffre_logo[96] = { 23 // 'lechiffre_logo', 32x20px 24 0x00, 0x3e, 0x20, 0x20, 0x00, 0x18, 0x2c, 0xa8, 0x80, 0x00, 0x1c, 0x22, 0x22, 0x00, 0x3e, 0x08, 25 0x30, 0x00, 0x34, 0x00, 0x3c, 0x0a, 0x00, 0xbc, 0x8a, 0x00, 0x38, 0x08, 0x00, 0x18, 0x2c, 0x28, 26 0x00, 0xb6, 0xb6, 0x00, 0xdb, 0xdb, 0x00, 0x6d, 0x6d, 0x00, 0xdb, 0xdb, 0x00, 0xdb, 0xdb, 0x00, 27 0x00, 0xdb, 0xdb, 0x00, 0xdb, 0xdb, 0x00, 0x6d, 0x6d, 0x00, 0xdb, 0xdb, 0x00, 0xb6, 0xb6, 0x00, 28 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x06, 0x06, 0x06, 0x00, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00, 29 0x00, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00 30 }; 31 32 const char PROGMEM oled_section_break[6] = OLED_SECTION_BREAK; 33 /* END CONSTANTS */ 34 35 /* TRACKERS */ 36 static char keylog_str[KEYLOG_LEN] = {}; 37 static uint16_t log_timer = 0; 38 /* END TRACKERS */ 39 40 /* BEGIN STANDARD QMK FUNCTIONS */ 41 42 oled_rotation_t oled_init_kb(oled_rotation_t rotation) { 43 return OLED_ROTATION_90; 44 } 45 46 bool oled_task_kb(void) { 47 if (!oled_task_user()) { 48 return false; 49 } 50 oled_write_raw_P(lechiffre_logo, sizeof(lechiffre_logo)); 51 oled_set_cursor(0, 3); 52 oled_write_P(oled_section_break, false); 53 render_layer_status(get_u8_str(get_highest_layer(layer_state | default_layer_state), ' ')); 54 oled_write_P(oled_section_break, false); 55 render_mod_status(get_mods() | get_oneshot_mods()); 56 oled_write_P(oled_section_break, false); 57 render_keylock_status(host_keyboard_led_state()); 58 return true; 59 } 60 61 /* END STANDARD QMK FUNCTIONS */ 62 /* BEGIN CUSTOM HELPER FUNCTIONS FOR OLED */ 63 64 /** 65 * Sickbabies deserves credit for the original OLED implementation, 66 * however most of the keylogging code appears to have been lifted from crkbd 67 * -- which is why @foostan and @drashna are now credited here as well. 68 * 69 * Improvements were lifted from crkbd again in 2023, with gratitude. 70 */ 71 void add_keylog(uint16_t keycode, keyrecord_t *record) { 72 if (IS_QK_MOD_TAP(keycode) && record->tap.count) { 73 keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode); 74 } else if (IS_QK_LAYER_TAP(keycode) && record->tap.count) { 75 keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode); 76 } else if (IS_QK_MODS(keycode)) { 77 keycode = QK_MODS_GET_BASIC_KEYCODE(keycode); 78 } 79 80 if (keycode >= KC_A && keycode < KC_CAPS) { 81 keycode -= KC_A; // shift to first letter of alphabet 82 for (uint8_t i = KEYLOG_LEN - 1; i > 0; i--) { 83 keylog_str[i] = keylog_str[i - 1]; 84 } 85 keylog_str[0] = pgm_read_byte(&(code_to_name[keycode])); 86 keylog_str[KEYLOG_LEN - 1] = 0x00; 87 } 88 89 log_timer = timer_read(); 90 } 91 92 void render_keylock_status(led_t led_state) { 93 oled_write_P(PSTR("C"), led_state.caps_lock); 94 oled_advance_char(); 95 oled_write_P(PSTR("N"), led_state.num_lock); 96 oled_advance_char(); 97 oled_write_P(PSTR("S"), led_state.scroll_lock); 98 } 99 100 void render_keylogger_status(void) { 101 // zero out log after 30s idle time 102 if (strlen(keylog_str) > 0 && timer_elapsed(log_timer) > 30000) { 103 keylog_str[0] = 0x00; 104 oled_advance_page(true); 105 oled_advance_page(true); 106 } 107 oled_write(keylog_str, false); 108 } 109 110 void render_layer_status(const char* layer_name) { 111 oled_write(layer_name, false); 112 if (strlen(layer_name) < oled_max_chars()) { 113 oled_advance_page(true); 114 } 115 } 116 117 void render_mod_status(uint8_t modifiers) { 118 oled_write_ln_P(PSTR("SHFT"), (modifiers & MOD_MASK_SHIFT)); 119 oled_write_ln_P(PSTR("ALT"), (modifiers & MOD_MASK_ALT)); 120 oled_write_ln_P(PSTR("CTRL"), (modifiers & MOD_MASK_CTRL)); 121 oled_write_ln_P(PSTR("GUI"), (modifiers & MOD_MASK_GUI)); 122 } 123 #endif // OLED_ENABLE