bcat_oled.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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