theme_djinn_default.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2018-2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <hal.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <printf.h>
  7. #include "qp.h"
  8. #include "backlight.h"
  9. #include "transactions.h"
  10. #include "split_util.h"
  11. #include "djinn.h"
  12. #include "theme_djinn_default.h"
  13. #include "djinn.qgf.h"
  14. #include "lock-caps-ON.qgf.h"
  15. #include "lock-scrl-ON.qgf.h"
  16. #include "lock-num-ON.qgf.h"
  17. #include "lock-caps-OFF.qgf.h"
  18. #include "lock-scrl-OFF.qgf.h"
  19. #include "lock-num-OFF.qgf.h"
  20. #include "thintel15.qff.h"
  21. static painter_image_handle_t djinn_logo;
  22. static painter_image_handle_t lock_caps_on;
  23. static painter_image_handle_t lock_caps_off;
  24. static painter_image_handle_t lock_num_on;
  25. static painter_image_handle_t lock_num_off;
  26. static painter_image_handle_t lock_scrl_on;
  27. static painter_image_handle_t lock_scrl_off;
  28. static painter_font_handle_t thintel;
  29. //----------------------------------------------------------
  30. // RGB Matrix naming
  31. #if defined(RGB_MATRIX_ENABLE)
  32. # include <rgb_matrix.h>
  33. # if defined(RGB_MATRIX_EFFECT)
  34. # undef RGB_MATRIX_EFFECT
  35. # endif // defined(RGB_MATRIX_EFFECT)
  36. # define RGB_MATRIX_EFFECT(x) RGB_MATRIX_EFFECT_##x,
  37. enum {
  38. RGB_MATRIX_EFFECT_NONE,
  39. # include "rgb_matrix_effects.inc"
  40. # undef RGB_MATRIX_EFFECT
  41. # ifdef RGB_MATRIX_CUSTOM_KB
  42. # include "rgb_matrix_kb.inc"
  43. # endif
  44. # ifdef RGB_MATRIX_CUSTOM_USER
  45. # include "rgb_matrix_user.inc"
  46. # endif
  47. };
  48. # define RGB_MATRIX_EFFECT(x) \
  49. case RGB_MATRIX_EFFECT_##x: \
  50. return #x;
  51. const char *rgb_matrix_name(uint8_t effect) {
  52. switch (effect) {
  53. case RGB_MATRIX_EFFECT_NONE:
  54. return "NONE";
  55. # include "rgb_matrix_effects.inc"
  56. # undef RGB_MATRIX_EFFECT
  57. # ifdef RGB_MATRIX_CUSTOM_KB
  58. # include "rgb_matrix_kb.inc"
  59. # endif
  60. # ifdef RGB_MATRIX_CUSTOM_USER
  61. # include "rgb_matrix_user.inc"
  62. # endif
  63. default:
  64. return "UNKNOWN";
  65. }
  66. }
  67. #endif // defined(RGB_MATRIX_ENABLE)
  68. //----------------------------------------------------------
  69. // UI Initialisation
  70. void keyboard_post_init_display(void) {
  71. djinn_logo = qp_load_image_mem(gfx_djinn);
  72. lock_caps_on = qp_load_image_mem(gfx_lock_caps_ON);
  73. lock_caps_off = qp_load_image_mem(gfx_lock_caps_OFF);
  74. lock_num_on = qp_load_image_mem(gfx_lock_num_ON);
  75. lock_num_off = qp_load_image_mem(gfx_lock_num_OFF);
  76. lock_scrl_on = qp_load_image_mem(gfx_lock_scrl_ON);
  77. lock_scrl_off = qp_load_image_mem(gfx_lock_scrl_OFF);
  78. thintel = qp_load_font_mem(font_thintel15);
  79. }
  80. //----------------------------------------------------------
  81. // UI Drawing
  82. void draw_ui_user(bool force_redraw) {
  83. bool hue_redraw = force_redraw;
  84. static uint16_t last_hue = 0xFFFF;
  85. #if defined(RGB_MATRIX_ENABLE)
  86. uint16_t curr_hue = rgb_matrix_get_hue();
  87. #else // defined(RGB_MATRIX_ENABLE)
  88. uint16_t curr_hue = 0;
  89. #endif // defined(RGB_MATRIX_ENABLE)
  90. if (last_hue != curr_hue) {
  91. last_hue = curr_hue;
  92. hue_redraw = true;
  93. }
  94. bool layer_state_redraw = false;
  95. static uint32_t last_layer_state = 0;
  96. if (last_layer_state != layer_state) {
  97. last_layer_state = layer_state;
  98. layer_state_redraw = true;
  99. }
  100. bool power_state_redraw = false;
  101. static usbpd_allowance_t last_current_state = (usbpd_allowance_t)(~0);
  102. if (last_current_state != kb_state.current_setting) {
  103. last_current_state = kb_state.current_setting;
  104. power_state_redraw = true;
  105. }
  106. bool scan_redraw = false;
  107. static uint32_t last_scan_update = 0;
  108. if (timer_elapsed32(last_scan_update) > 125) {
  109. last_scan_update = timer_read32();
  110. scan_redraw = true;
  111. }
  112. bool wpm_redraw = false;
  113. static uint32_t last_wpm_update = 0;
  114. if (timer_elapsed32(last_wpm_update) > 125) {
  115. last_wpm_update = timer_read32();
  116. wpm_redraw = true;
  117. }
  118. #if defined(RGB_MATRIX_ENABLE)
  119. bool rgb_effect_redraw = false;
  120. static uint16_t last_effect = 0xFFFF;
  121. uint8_t curr_effect = rgb_matrix_config.mode;
  122. if (last_effect != curr_effect) {
  123. last_effect = curr_effect;
  124. rgb_effect_redraw = true;
  125. }
  126. #endif // defined(RGB_MATRIX_ENABLE)
  127. // Show the Djinn logo and two vertical bars on both sides
  128. if (hue_redraw) {
  129. qp_drawimage_recolor(lcd, 120 - djinn_logo->width / 2, 32, djinn_logo, curr_hue, 255, 255, curr_hue, 255, 0);
  130. qp_rect(lcd, 0, 0, 8, 319, curr_hue, 255, 255, true);
  131. qp_rect(lcd, 231, 0, 239, 319, curr_hue, 255, 255, true);
  132. }
  133. int ypos = 4;
  134. // Show layer info on the left side
  135. if (is_keyboard_left()) {
  136. char buf[64] = {0};
  137. int xpos = 16;
  138. #if defined(RGB_MATRIX_ENABLE)
  139. if (hue_redraw || rgb_effect_redraw) {
  140. static int max_rgb_xpos = 0;
  141. xpos = 16;
  142. snprintf(buf, sizeof(buf), "rgb: %s", rgb_matrix_name(curr_effect));
  143. for (int i = 5; i < sizeof(buf); ++i) {
  144. if (buf[i] == 0)
  145. break;
  146. else if (buf[i] == '_')
  147. buf[i] = ' ';
  148. else if (buf[i - 1] == ' ')
  149. buf[i] = toupper(buf[i]);
  150. else if (buf[i - 1] != ' ')
  151. buf[i] = tolower(buf[i]);
  152. }
  153. xpos += qp_drawtext_recolor(lcd, xpos, ypos, thintel, buf, curr_hue, 255, 255, curr_hue, 255, 0);
  154. if (max_rgb_xpos < xpos) {
  155. max_rgb_xpos = xpos;
  156. }
  157. qp_rect(lcd, xpos, ypos, max_rgb_xpos, ypos + thintel->line_height, 0, 0, 0, true);
  158. }
  159. ypos += thintel->line_height + 4;
  160. #endif // defined(RGB_MATRIX_ENABLE)
  161. if (hue_redraw || layer_state_redraw) {
  162. extern const char *current_layer_name(void);
  163. const char *layer_name = current_layer_name();
  164. static int max_layer_xpos = 0;
  165. xpos = 16;
  166. snprintf(buf, sizeof(buf), "layer: %s", layer_name);
  167. xpos += qp_drawtext_recolor(lcd, xpos, ypos, thintel, buf, curr_hue, 255, 255, curr_hue, 255, 0);
  168. if (max_layer_xpos < xpos) {
  169. max_layer_xpos = xpos;
  170. }
  171. qp_rect(lcd, xpos, ypos, max_layer_xpos, ypos + thintel->line_height, 0, 0, 0, true);
  172. }
  173. ypos += thintel->line_height + 4;
  174. if (hue_redraw || power_state_redraw) {
  175. static int max_power_xpos = 0;
  176. xpos = 16;
  177. snprintf(buf, sizeof(buf), "power: %s", usbpd_str(kb_state.current_setting));
  178. xpos += qp_drawtext_recolor(lcd, xpos, ypos, thintel, buf, curr_hue, 255, 255, curr_hue, 255, 0);
  179. if (max_power_xpos < xpos) {
  180. max_power_xpos = xpos;
  181. }
  182. qp_rect(lcd, xpos, ypos, max_power_xpos, ypos + thintel->line_height, 0, 0, 0, true);
  183. }
  184. ypos += thintel->line_height + 4;
  185. if (hue_redraw || scan_redraw) {
  186. static int max_scans_xpos = 0;
  187. xpos = 16;
  188. snprintf(buf, sizeof(buf), "scans: %d", (int)theme_state.scan_rate);
  189. xpos += qp_drawtext_recolor(lcd, xpos, ypos, thintel, buf, curr_hue, 255, 255, curr_hue, 255, 0);
  190. if (max_scans_xpos < xpos) {
  191. max_scans_xpos = xpos;
  192. }
  193. qp_rect(lcd, xpos, ypos, max_scans_xpos, ypos + thintel->line_height, 0, 0, 0, true);
  194. }
  195. ypos += thintel->line_height + 4;
  196. if (hue_redraw || wpm_redraw) {
  197. static int max_wpm_xpos = 0;
  198. xpos = 16;
  199. snprintf(buf, sizeof(buf), "wpm: %d", (int)get_current_wpm());
  200. xpos += qp_drawtext_recolor(lcd, xpos, ypos, thintel, buf, curr_hue, 255, 255, curr_hue, 255, 0);
  201. if (max_wpm_xpos < xpos) {
  202. max_wpm_xpos = xpos;
  203. }
  204. qp_rect(lcd, xpos, ypos, max_wpm_xpos, ypos + thintel->line_height, 0, 0, 0, true);
  205. }
  206. ypos += thintel->line_height + 4;
  207. }
  208. // Show LED lock indicators on the right side
  209. if (!is_keyboard_left()) {
  210. static led_t last_led_state = {0};
  211. if (hue_redraw || last_led_state.raw != host_keyboard_led_state().raw) {
  212. last_led_state.raw = host_keyboard_led_state().raw;
  213. qp_drawimage_recolor(lcd, 239 - 12 - (32 * 3), 0, last_led_state.caps_lock ? lock_caps_on : lock_caps_off, curr_hue, 255, last_led_state.caps_lock ? 255 : 32, curr_hue, 255, 0);
  214. qp_drawimage_recolor(lcd, 239 - 12 - (32 * 2), 0, last_led_state.num_lock ? lock_num_on : lock_num_off, curr_hue, 255, last_led_state.num_lock ? 255 : 32, curr_hue, 255, 0);
  215. qp_drawimage_recolor(lcd, 239 - 12 - (32 * 1), 0, last_led_state.scroll_lock ? lock_scrl_on : lock_scrl_off, curr_hue, 255, last_led_state.scroll_lock ? 255 : 32, curr_hue, 255, 0);
  216. }
  217. }
  218. }
  219. //----------------------------------------------------------
  220. // Sync
  221. theme_runtime_config theme_state;
  222. void rpc_theme_sync_callback(uint8_t m2s_size, const void *m2s_buffer, uint8_t s2m_size, void *s2m_buffer) {
  223. if (m2s_size == sizeof(theme_state)) {
  224. memcpy(&theme_state, m2s_buffer, m2s_size);
  225. }
  226. }
  227. void theme_init(void) {
  228. // Register keyboard state sync split transaction
  229. transaction_register_rpc(THEME_DATA_SYNC, rpc_theme_sync_callback);
  230. // Reset the initial shared data value between master and slave
  231. memset(&theme_state, 0, sizeof(theme_state));
  232. }
  233. void theme_state_update(void) {
  234. if (is_keyboard_master()) {
  235. // Keep the scan rate in sync
  236. theme_state.scan_rate = get_matrix_scan_rate();
  237. }
  238. }
  239. void theme_state_sync(void) {
  240. if (!is_transport_connected()) return;
  241. if (is_keyboard_master()) {
  242. // Keep track of the last state, so that we can tell if we need to propagate to slave
  243. static theme_runtime_config last_theme_state;
  244. static uint32_t last_sync;
  245. bool needs_sync = false;
  246. // Check if the state values are different
  247. if (memcmp(&theme_state, &last_theme_state, sizeof(theme_runtime_config))) {
  248. needs_sync = true;
  249. memcpy(&last_theme_state, &theme_state, sizeof(theme_runtime_config));
  250. }
  251. // Send to slave every 125ms regardless of state change
  252. if (timer_elapsed32(last_sync) > 125) {
  253. needs_sync = true;
  254. }
  255. // Perform the sync if requested
  256. if (needs_sync) {
  257. if (transaction_rpc_send(THEME_DATA_SYNC, sizeof(theme_runtime_config), &theme_state)) {
  258. last_sync = timer_read32();
  259. } else {
  260. dprint("Failed to perform rpc call\n");
  261. }
  262. }
  263. }
  264. }