You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
9.1 KiB

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