pi50.c (8509B)
1 /* Copyright 2023 ziptyze 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include "quantum.h" 18 #include <ctype.h> 19 #include <stdio.h> 20 21 #ifdef OLED_ENABLE 22 23 static uint32_t oled_logo_timer = 0; 24 static bool clear_logo = true; 25 static const char PROGMEM my_logo[] = { 26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 28 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 29 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 30 0x0f, 0x0f, 0x0f, 0x1f, 0xff, 0xff, 0xff, 0x1f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 31 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x00, 32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 37 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 38 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 39 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x00, 46 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 47 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 52 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 53 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xfb, 54 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 55 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 58 }; 59 60 #endif 61 62 #ifdef OLED_ENABLE 63 64 void init_timer(void){ 65 oled_logo_timer = timer_read32(); 66 }; 67 68 void user_oled_magic(void) { 69 // Host Keyboard Layer Status 70 oled_write_P(PSTR("Layer: "), false); 71 72 switch (get_highest_layer(layer_state)) { 73 case 0: 74 oled_write_P(PSTR("One\n"), false); 75 break; 76 case 1: 77 oled_write_P(PSTR("Two\n"), false); 78 break; 79 case 2: 80 oled_write_P(PSTR("Three\n"), false); 81 break; 82 case 3: 83 oled_write_P(PSTR("Four\n"), false); 84 break; 85 case 4: 86 oled_write_P(PSTR("Five\n"), false); 87 break; 88 case 5: 89 oled_write_P(PSTR("Six\n"), false); 90 break; 91 case 6: 92 oled_write_P(PSTR("Seven\n"), false); 93 break; 94 case 7: 95 oled_write_P(PSTR("Eight\n"), false); 96 break; 97 case 8: 98 oled_write_P(PSTR("Nine\n"), false); 99 break; 100 case 9: 101 oled_write_P(PSTR("Ten\n"), false); 102 break; 103 default: 104 // Or use the write_ln shortcut over adding '\n' to the end of your string 105 oled_write_ln_P(PSTR("Undefined"), false); 106 } 107 108 // Host Keyboard LED Status 109 led_t led_state = host_keyboard_led_state(); 110 oled_write_P(led_state.caps_lock ? PSTR("Cap(x) ") : PSTR("Cap( ) "), false); 111 oled_write_P(led_state.num_lock ? PSTR("Num(x) ") : PSTR("Num( ) "), false); 112 oled_write_P(led_state.scroll_lock ? PSTR("Scrl(x)") : PSTR("Scrl( )"), false); 113 114 char *mode_name = strdup(rgb_matrix_get_mode_name(rgb_matrix_get_mode())); 115 if (mode_name != NULL) { 116 int len = strlen(mode_name); 117 bool capitalize_next = true; 118 for (int i = 0; i < len; i++) { 119 if (i == 21 && mode_name[i] == '_') { 120 continue; // Skip the underscore if it's the 22nd character 121 } 122 if (mode_name[i] == '_') { 123 mode_name[i] = ' '; 124 capitalize_next = true; 125 } else if (capitalize_next) { 126 mode_name[i] = mode_name[i] >= 'a' && mode_name[i] <= 'z' ? mode_name[i] - 'a' + 'A' : mode_name[i]; 127 capitalize_next = false; 128 } else { 129 mode_name[i] = mode_name[i] >= 'A' && mode_name[i] <= 'Z' ? mode_name[i] - 'A' + 'a' : mode_name[i]; 130 } 131 } 132 133 // Add line break and spaces if necessary 134 if (len < 19) { 135 strcat(mode_name, "\n"); 136 for (int i = 0; i < 21; i++) { 137 strcat(mode_name, " "); 138 } 139 } else { 140 // Find the most recent ' ' before the 21st character and replace it with a line break 141 int break_pos = -1; 142 for (int i = 18; i >= 0; i--) { 143 if (mode_name[i] == ' ') { 144 break_pos = i; 145 break; 146 } 147 } 148 if (break_pos >= 0) { 149 mode_name[break_pos] = '\n'; 150 for (int i = 0; i < (21 - (len - break_pos - 1)); i++) { 151 strcat(mode_name, " "); 152 } 153 } else { 154 // No '_' found, just add spaces 155 for (int i = 0; i < (21 - len); i++) { 156 strcat(mode_name, " "); 157 } 158 } 159 } 160 161 oled_write_P(PSTR(mode_name), false); 162 free(mode_name); 163 } 164 165 166 } 167 168 void render_logo(void) { 169 oled_write_raw_P(my_logo, sizeof(my_logo)); 170 } 171 172 void clear_screen(void) { 173 if (clear_logo){ 174 for (uint8_t i = 0; i < OLED_DISPLAY_HEIGHT; ++i) { 175 for (uint8_t j = 0; j < OLED_DISPLAY_WIDTH; ++j) { 176 oled_write_raw_byte(0x0, i*OLED_DISPLAY_WIDTH + j); 177 } 178 } 179 clear_logo = false; 180 } 181 } 182 183 oled_rotation_t oled_init_kb(oled_rotation_t rotation) { 184 return OLED_ROTATION_180; 185 } 186 187 void keyboard_post_init_kb(void) { 188 init_timer(); 189 190 keyboard_post_init_user(); 191 } 192 193 # define SHOW_LOGO 5000 194 bool oled_task_kb(void) { 195 if (!oled_task_user()) { return false; } 196 if ((timer_elapsed32(oled_logo_timer) < SHOW_LOGO)){ 197 render_logo(); 198 }else{ 199 clear_screen(); 200 user_oled_magic(); 201 } 202 return false; 203 } 204 205 #endif