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.

216 lines
7.8 KiB

[Keymap] Update bcat's keymaps/userspace to share logic, add OLED functionality, and set up one of my macropads for WFH (#14702) * Add script to build all bcat keymaps at once * Move userspace RGB to separate source file * Move layer handling logic into userspace * Move keycap aliases into userspace * Add OLED userspace library and Lily58 OLED setup * Add Luna keyboard pet, generic OLED pet framework Luna artwork and original implementation by HellSingCoder, licensed under GPL v2.0. See also: https://github.com/qmk/qmk_firmware/blob/6dfe915e26d7147e6c2bed495d3b01cf5b21e6ec/keyboards/sofle/keymaps/helltm/keymap.c * Use OLED on bcat's Crkbd I had to turn off a few unused features to address firmware size limits. * Remove vestigial NK_TOGG keybindings * Add post-render hook to OLED pet API This enables OLED pets to draw custom widgets (e.g., LED indicator status) on top of their animation frames. * Add Isda keyboard pet For future use on my Unicorne keyboard. Unicorn artwork by sparrow666, licensed under GPL v2.0. See also: https://opengameart.org/content/unicorn-2 * Replace OLED timeout implementation with custom The default implementation never lets the OLED turn off if a continuous animation is in progress. The custom one does. * Move keyboard state for OLED functions into struct No change in firmware size, but makes keymaps read a little nicer and enables more functionality in OLED pets. * Enable continuously running OLED pet (for Luna) * Sync OLED state; enable Bootmagic only when needed The new extensible split transport for Split Common finally allows OLED on/off status to be synced between halves of the keyboard. :) Unfortunately, this required disabling Bootmagic Lite to keep my Crkbd under the firmware size limit. (I now after 28 bytes free on avr-gcc version 8.5.0.) So now I'll enable Bootmagic only on keyboards that actually require it, i.e., ones lacking an accessible reset button. * Update 9-Key macropad keymap for working from home * Remove includes redundant with quantum.h Co-authored-by: Drashna Jaelre <drashna@live.com> * Simplify BCAT_OLED_PET makefile logic * Swap some keys on my 9-Key macropad around * Inline spurious variable in OLED code * Remove max brightness that's now set by default The default max brightness is only 120 rather than 150, but that might actually fix some weirdness I've seen with bright white LED settings. * Enable specific RGBLIGHT modes instead of default The general trend these days seems to be enabling only the modes you want, so I'm manually expanding the ones currently enabled by RGBLIGHT_ANIMATIONS. I'd like to try out the TWINKLE mode too, but it seems not to work at all on ARM right now, and all my usable RGBLIGHT keebs are ARM boards. * Reenable RGB_MATRIX animations after #15018 My Crkbd still has a reasonable amount of free space with these: 27974/28672 (97%, 698 bytes free). The RGB_MATRIX_KEYPRESSES effects would put it over the firmware size limit, but I really don't ever use those anyway. * Use new get_u8_str function for WPM display Co-authored-by: Drashna Jaelre <drashna@live.com>
2 years ago
  1. /* Copyright 2021 Jonathan Rascher
  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 "bcat_oled.h"
  17. #include "quantum.h"
  18. #include "bcat.h"
  19. #if defined(BCAT_OLED_PET)
  20. # include "bcat_oled_pet.h"
  21. #endif
  22. #define TRIANGLE_UP 0x1e
  23. #define TRIANGLE_DOWN 0x1f
  24. #if defined(BCAT_OLED_PET)
  25. static bool oled_pet_should_jump = false;
  26. #endif
  27. /* Should be overridden by the keymap to render the OLED contents. For split
  28. * keyboards, this function is only called on the master side.
  29. */
  30. __attribute__((weak)) void oled_task_keymap(const oled_keyboard_state_t *keyboard_state) {}
  31. bool oled_task_user(void) {
  32. #if defined(SPLIT_KEYBOARD)
  33. if (is_keyboard_master()) {
  34. #endif
  35. /* Custom OLED timeout implementation that only considers user activity.
  36. * Allows the OLED to turn off in the middle of a continuous animation.
  37. */
  38. static const uint16_t TIMEOUT_MILLIS = 60000 /* 1 min */;
  39. if (last_input_activity_elapsed() < TIMEOUT_MILLIS) {
  40. if (!is_oled_on()) {
  41. oled_on();
  42. }
  43. oled_keyboard_state_t keyboard_state = {
  44. .mods = get_mods(),
  45. .leds = host_keyboard_led_state(),
  46. .wpm = get_current_wpm(),
  47. };
  48. oled_task_keymap(&keyboard_state);
  49. } else if (is_oled_on()) {
  50. oled_off();
  51. }
  52. #if defined(SPLIT_KEYBOARD)
  53. } else {
  54. /* Display logo embedded at standard location in the OLED font on the
  55. * slave side. By default, this is a "QMK firmware" logo, but many
  56. * keyboards substitute their own logo. Occupies 21x3 character cells.
  57. *
  58. * Since the slave display buffer never changes, we don't need to worry
  59. * about oled_render incorrectly turning the OLED on. Instead, we rely
  60. * on SPLIT_OLED_ENABLE to propagate OLED on/off status from master.
  61. */
  62. static const char PROGMEM logo[] = {
  63. // clang-format off
  64. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94,
  65. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4,
  66. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4,
  67. 0x00,
  68. // clang-format on
  69. };
  70. oled_write_P(logo, /*invert=*/false);
  71. }
  72. #endif
  73. return false;
  74. }
  75. void render_oled_layers(void) {
  76. oled_advance_char();
  77. oled_advance_char();
  78. #if defined(BCAT_ORTHO_LAYERS)
  79. oled_write_char(IS_LAYER_ON(LAYER_LOWER) ? TRIANGLE_DOWN : ' ', /*invert=*/false);
  80. oled_advance_char();
  81. oled_write_char(IS_LAYER_ON(LAYER_RAISE) ? TRIANGLE_UP : ' ', /*invert=*/false);
  82. #else
  83. switch (get_highest_layer(layer_state)) {
  84. case LAYER_FUNCTION_1:
  85. oled_write_P(PSTR("FN1"), /*invert=*/false);
  86. break;
  87. case LAYER_FUNCTION_2:
  88. oled_write_P(PSTR("FN2"), /*invert=*/false);
  89. break;
  90. default:
  91. oled_write_P(PSTR(" "), /*invert=*/false);
  92. break;
  93. }
  94. #endif
  95. }
  96. void render_oled_indicators(led_t leds) {
  97. oled_advance_char();
  98. oled_advance_char();
  99. oled_write_P(leds.num_lock ? PSTR("NUM") : PSTR(" "), /*invert=*/false);
  100. oled_advance_char();
  101. oled_advance_char();
  102. oled_write_P(leds.caps_lock ? PSTR("CAP") : PSTR(" "), /*invert=*/false);
  103. oled_advance_char();
  104. oled_advance_char();
  105. oled_write_P(leds.scroll_lock ? PSTR("SCR") : PSTR(" "), /*invert=*/false);
  106. }
  107. void render_oled_wpm(uint8_t wpm) {
  108. static const uint16_t UPDATE_MILLIS = 100;
  109. static uint32_t update_timeout = 0;
  110. if (timer_expired32(timer_read32(), update_timeout)) {
  111. oled_advance_char();
  112. oled_advance_char();
  113. oled_write_P(wpm > 0 ? PSTR("WPM") : PSTR(" "), /*invert=*/false);
  114. if (wpm > 0) {
  115. oled_advance_char();
  116. oled_advance_char();
  117. oled_write(get_u8_str(wpm, ' '), /*invert=*/false);
  118. } else {
  119. oled_advance_page(/*clearPageRemainder=*/true);
  120. }
  121. update_timeout = timer_read32() + UPDATE_MILLIS;
  122. }
  123. }
  124. #if defined(BCAT_OLED_PET)
  125. void process_record_oled(uint16_t keycode, const keyrecord_t *record) {
  126. switch (keycode) {
  127. case KC_SPACE:
  128. if (oled_pet_can_jump()) {
  129. oled_pet_should_jump = record->event.pressed;
  130. }
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. static void redraw_oled_pet(uint8_t col, uint8_t line, bool jumping, oled_pet_state_t state) {
  137. oled_set_cursor(col, line);
  138. if (jumping) {
  139. oled_write_raw_P(oled_pet_frame(state), oled_pet_frame_bytes());
  140. oled_set_cursor(col, line + oled_pet_frame_lines());
  141. oled_advance_page(/*clearPageRemainder=*/true);
  142. } else {
  143. oled_advance_page(/*clearPageRemainder=*/true);
  144. oled_write_raw_P(oled_pet_frame(state), oled_pet_frame_bytes());
  145. }
  146. }
  147. void render_oled_pet(uint8_t col, uint8_t line, const oled_keyboard_state_t *keyboard_state) {
  148. /* Current animation to draw. We track changes to avoid redrawing the same
  149. * frame repeatedly, allowing oled_pet_post_render to draw over the
  150. * animation frame.
  151. */
  152. static oled_pet_state_t state = 0;
  153. static bool state_changed = true;
  154. /* Minimum time until the pet comes down after jumping. */
  155. static const uint16_t JUMP_MILLIS = 200;
  156. static bool jumping = false;
  157. /* Time until the next animation or jump state change. */
  158. static uint32_t update_timeout = 0;
  159. static uint32_t jump_timeout = 0;
  160. /* If the user pressed the jump key, immediately redraw instead of waiting
  161. * for the animation frame to update. That way, the pet appears to respond
  162. * to jump commands quickly rather than lagging. If the user released the
  163. * jump key, wait for the jump timeout to avoid overly brief jumps.
  164. */
  165. bool redraw = state_changed;
  166. if (oled_pet_should_jump && !jumping) {
  167. redraw = true;
  168. jumping = true;
  169. jump_timeout = timer_read32() + JUMP_MILLIS;
  170. } else if (!oled_pet_should_jump && jumping && timer_expired32(timer_read32(), jump_timeout)) {
  171. redraw = true;
  172. jumping = false;
  173. }
  174. /* Draw the actual animation, then move the cursor to the end of the
  175. * rendered area. (Note that we take up an extra line to account for
  176. * jumping, which shifts the animation up or down a line.)
  177. */
  178. if (redraw) {
  179. redraw_oled_pet(col, line, jumping, state);
  180. }
  181. oled_pet_post_render(col, line + !jumping, keyboard_state, redraw);
  182. oled_set_cursor(col, line + oled_pet_frame_lines() + 1);
  183. /* If the update timer expired, recompute the pet's animation state. */
  184. if (timer_expired32(timer_read32(), update_timeout)) {
  185. oled_pet_state_t new_state = oled_pet_next_state(state, keyboard_state);
  186. state_changed = new_state != state;
  187. state = new_state;
  188. update_timeout = timer_read32() + oled_pet_update_millis(keyboard_state);
  189. } else {
  190. state_changed = false;
  191. }
  192. }
  193. #endif