logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

theme_djinn_default.c (11261B)


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