pi50.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "quantum.h"
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #ifdef OLED_ENABLE
  20. static uint32_t oled_logo_timer = 0;
  21. static bool clear_logo = true;
  22. static const char PROGMEM my_logo[] = {
  23. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  26. 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f,
  27. 0x0f, 0x0f, 0x0f, 0x1f, 0xff, 0xff, 0xff, 0x1f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
  28. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x00,
  29. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  35. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
  36. 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0f, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  44. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0x00,
  45. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  50. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xfb,
  51. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff,
  52. 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  55. };
  56. #endif
  57. #ifdef OLED_ENABLE
  58. void init_timer(void){
  59. oled_logo_timer = timer_read32();
  60. };
  61. void user_oled_magic(void) {
  62. // Host Keyboard Layer Status
  63. oled_write_P(PSTR("Layer: "), false);
  64. switch (get_highest_layer(layer_state)) {
  65. case 0:
  66. oled_write_P(PSTR("One\n"), false);
  67. break;
  68. case 1:
  69. oled_write_P(PSTR("Two\n"), false);
  70. break;
  71. case 2:
  72. oled_write_P(PSTR("Three\n"), false);
  73. break;
  74. case 3:
  75. oled_write_P(PSTR("Four\n"), false);
  76. break;
  77. case 4:
  78. oled_write_P(PSTR("Five\n"), false);
  79. break;
  80. case 5:
  81. oled_write_P(PSTR("Six\n"), false);
  82. break;
  83. case 6:
  84. oled_write_P(PSTR("Seven\n"), false);
  85. break;
  86. case 7:
  87. oled_write_P(PSTR("Eight\n"), false);
  88. break;
  89. case 8:
  90. oled_write_P(PSTR("Nine\n"), false);
  91. break;
  92. case 9:
  93. oled_write_P(PSTR("Ten\n"), false);
  94. break;
  95. default:
  96. // Or use the write_ln shortcut over adding '\n' to the end of your string
  97. oled_write_ln_P(PSTR("Undefined"), false);
  98. }
  99. // Host Keyboard LED Status
  100. led_t led_state = host_keyboard_led_state();
  101. oled_write_P(led_state.caps_lock ? PSTR("Cap(x) ") : PSTR("Cap( ) "), false);
  102. oled_write_P(led_state.num_lock ? PSTR("Num(x) ") : PSTR("Num( ) "), false);
  103. oled_write_P(led_state.scroll_lock ? PSTR("Scrl(x)") : PSTR("Scrl( )"), false);
  104. char *mode_name = strdup(rgb_matrix_get_mode_name(rgb_matrix_get_mode()));
  105. if (mode_name != NULL) {
  106. int len = strlen(mode_name);
  107. bool capitalize_next = true;
  108. for (int i = 0; i < len; i++) {
  109. if (i == 21 && mode_name[i] == '_') {
  110. continue; // Skip the underscore if it's the 22nd character
  111. }
  112. if (mode_name[i] == '_') {
  113. mode_name[i] = ' ';
  114. capitalize_next = true;
  115. } else if (capitalize_next) {
  116. mode_name[i] = mode_name[i] >= 'a' && mode_name[i] <= 'z' ? mode_name[i] - 'a' + 'A' : mode_name[i];
  117. capitalize_next = false;
  118. } else {
  119. mode_name[i] = mode_name[i] >= 'A' && mode_name[i] <= 'Z' ? mode_name[i] - 'A' + 'a' : mode_name[i];
  120. }
  121. }
  122. // Add line break and spaces if necessary
  123. if (len < 19) {
  124. strcat(mode_name, "\n");
  125. for (int i = 0; i < 21; i++) {
  126. strcat(mode_name, " ");
  127. }
  128. } else {
  129. // Find the most recent ' ' before the 21st character and replace it with a line break
  130. int break_pos = -1;
  131. for (int i = 18; i >= 0; i--) {
  132. if (mode_name[i] == ' ') {
  133. break_pos = i;
  134. break;
  135. }
  136. }
  137. if (break_pos >= 0) {
  138. mode_name[break_pos] = '\n';
  139. for (int i = 0; i < (21 - (len - break_pos - 1)); i++) {
  140. strcat(mode_name, " ");
  141. }
  142. } else {
  143. // No '_' found, just add spaces
  144. for (int i = 0; i < (21 - len); i++) {
  145. strcat(mode_name, " ");
  146. }
  147. }
  148. }
  149. oled_write_P(PSTR(mode_name), false);
  150. free(mode_name);
  151. }
  152. }
  153. void render_logo(void) {
  154. oled_write_raw_P(my_logo, sizeof(my_logo));
  155. }
  156. void clear_screen(void) {
  157. if (clear_logo){
  158. for (uint8_t i = 0; i < OLED_DISPLAY_HEIGHT; ++i) {
  159. for (uint8_t j = 0; j < OLED_DISPLAY_WIDTH; ++j) {
  160. oled_write_raw_byte(0x0, i*OLED_DISPLAY_WIDTH + j);
  161. }
  162. }
  163. clear_logo = false;
  164. }
  165. }
  166. oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
  167. return OLED_ROTATION_180;
  168. }
  169. void keyboard_post_init_kb(void) {
  170. init_timer();
  171. keyboard_post_init_user();
  172. }
  173. # define SHOW_LOGO 5000
  174. bool oled_task_kb(void) {
  175. if (!oled_task_user()) { return false; }
  176. if ((timer_elapsed32(oled_logo_timer) < SHOW_LOGO)){
  177. render_logo();
  178. }else{
  179. clear_screen();
  180. user_oled_magic();
  181. }
  182. return false;
  183. }
  184. #endif