keymap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Copyright 2021 dogspace <https://github.com/dogspace>
  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 QMK_KEYBOARD_H
  17. enum layer_names {
  18. _LAY0,
  19. _LAY1,
  20. _LAY2,
  21. _LAY3
  22. };
  23. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  24. [_LAY0] = LAYOUT(
  25. KC_PSLS, KC_PAST, KC_PMNS,
  26. KC_VOLD, KC_VOLU, KC_P7, KC_P8, KC_P9, KC_PPLS,
  27. KC_TRNS, KC_TRNS, KC_P4, KC_P5, KC_P6, KC_PPLS,
  28. KC_TRNS, KC_TRNS, KC_P1, KC_P2, KC_P3, KC_PENT,
  29. KC_TRNS, KC_TRNS, KC_P0, KC_P0, KC_PDOT, KC_PENT
  30. ),
  31. [_LAY1] = LAYOUT(
  32. ___, ___, ___,
  33. ___, ___, ___, ___, ___, ___,
  34. ___, ___, ___, ___, ___, ___,
  35. ___, ___, ___, ___, ___, ___,
  36. ___, ___, ___, ___, ___, ___
  37. ),
  38. [_LAY2] = LAYOUT(
  39. ___, ___, ___,
  40. ___, ___, ___, ___, ___, ___,
  41. ___, ___, ___, ___, ___, ___,
  42. ___, ___, ___, ___, ___, ___,
  43. ___, ___, ___, ___, ___, ___
  44. ),
  45. [_LAY3] = LAYOUT(
  46. ___, ___, ___,
  47. ___, ___, ___, ___, ___, ___,
  48. ___, ___, ___, ___, ___, ___,
  49. ___, ___, ___, ___, ___, ___,
  50. ___, ___, ___, ___, ___, ___
  51. ),
  52. };
  53. #ifdef OLED_ENABLE
  54. /*=========================================== OLED CONFIGURATION ===========================================*/
  55. #define OLED_ROTATE true // OLED rotation (flip 180* from default orientation)
  56. #define GRAPH_DIRECTION true // Graph movement (true = right to left, false = left to right)
  57. #define GRAPH_TOP_WPM 100.0 // Minimum WPM required to reach the top of the graph
  58. #define GRAPH_REFRESH 1000 // In milliseconds, determines the graph-line frequency
  59. #define ICON_MED_WPM 10 // WPM required to display the medium snail
  60. #define ICON_FAST_WPM 25 // WPM required to display the fast snail
  61. // Layer names: Should be exactly 5 characters in length if vertical display, or 6 characters if horizontal
  62. #define MA_LAYER_NAME "LAY 0" // Layer _MA name
  63. #define L1_LAYER_NAME "LAY 1" // Layer _L1 name
  64. #define L2_LAYER_NAME "LAY 2" // Layer _L2 name
  65. #define L3_LAYER_NAME "LAY 3" // Layer _L3 name
  66. #define CAPLCK_STR "CAPLK" // Caps Lock string
  67. #define NUMLCK_STR "NUMLK" // Num Lock string
  68. #define SCRLK_STR "SCRLK" // Scroll Lock string
  69. #define EMPTY_STR " " // Empty string
  70. /*================================================================================================================*/
  71. typedef struct oled_params {
  72. bool first_loop : 1;
  73. uint8_t wpm_icon : 7;
  74. uint16_t timer;
  75. uint8_t wpm_limit;
  76. uint8_t max_wpm;
  77. uint8_t graph_lines[32];
  78. } oled_params;
  79. oled_params oled_data;
  80. void oled_init_data(void) {
  81. // Initialize oled params
  82. oled_data.first_loop = true;
  83. oled_data.wpm_icon = 5;
  84. oled_data.timer = 0;
  85. oled_data.wpm_limit = 20;
  86. oled_data.max_wpm = 0;
  87. for (int i=0; i<32; i++) {
  88. oled_data.graph_lines[i] = 0;
  89. }
  90. }
  91. // Set OLED rotation
  92. oled_rotation_t oled_init_user(oled_rotation_t rotation) {
  93. oled_init_data();
  94. return OLED_ROTATE ? OLED_ROTATION_270 : OLED_ROTATION_90;
  95. }
  96. // Draw static background image to OLED (keyboard with no bottom row)
  97. static void render_background(void) {
  98. static const char PROGMEM nullbits_n_oled[] = {
  99. 0x00, 0xe0, 0xf0, 0xf0, 0xf8, 0xf8, 0xf0, 0xf0, 0xe0, 0x80, 0x20, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  100. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xc0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
  101. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
  102. 0x1f, 0x1f, 0x1f, 0x1f, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfc, 0xf0, 0x00, 0x00,
  103. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
  105. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  107. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  109. 0x00, 0x07, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x07, 0x03, 0x00,
  111. };
  112. oled_write_raw_P(nullbits_n_oled, sizeof(nullbits_n_oled));
  113. }
  114. // Toggles pixel on/off, converts horizontal coordinates to vertical equivalent if necessary
  115. static void write_pixel(uint8_t x, uint8_t y, bool onoff) {
  116. oled_write_pixel(y, 127 - x, onoff);
  117. }
  118. // Write active layer name
  119. static void render_layer_state(void) {
  120. oled_set_cursor(0, 15);
  121. switch (get_highest_layer(layer_state)) {
  122. case _LAY0:
  123. oled_write_P(PSTR(MA_LAYER_NAME), false);
  124. break;
  125. case _LAY1:
  126. oled_write_P(PSTR(L1_LAYER_NAME), false);
  127. break;
  128. case _LAY2:
  129. oled_write_P(PSTR(L2_LAYER_NAME), false);
  130. break;
  131. case _LAY3:
  132. oled_write_P(PSTR(L3_LAYER_NAME), false);
  133. break;
  134. default:
  135. oled_write("ERROR", false);
  136. break;
  137. }
  138. }
  139. // Update WPM counters
  140. static void render_wpm_counters(uint8_t current_wpm) {
  141. uint8_t cursorposition_cur = 13;
  142. uint8_t cursorposition_max = 14;
  143. oled_set_cursor(0, cursorposition_cur);
  144. oled_write(get_u8_str(current_wpm, '0'), false);
  145. if (current_wpm > oled_data.max_wpm) {
  146. oled_data.max_wpm = current_wpm;
  147. oled_data.wpm_limit = oled_data.max_wpm + 20;
  148. oled_set_cursor(0, cursorposition_max);
  149. oled_write(get_u8_str(current_wpm, '0'), false);
  150. }
  151. }
  152. static void render_led_status(void) {
  153. // Host Keyboard LED Status
  154. uint8_t led_usb_state = host_keyboard_leds();
  155. oled_set_cursor(0, 8);
  156. oled_write_P(IS_LED_ON(led_usb_state, USB_LED_CAPS_LOCK) ? PSTR(CAPLCK_STR) : PSTR(EMPTY_STR), false);
  157. oled_set_cursor(0, 9);
  158. oled_write_P(IS_LED_ON(led_usb_state, USB_LED_NUM_LOCK) ? PSTR(NUMLCK_STR) : PSTR(EMPTY_STR), false);
  159. oled_set_cursor(0, 10);
  160. oled_write_P(IS_LED_ON(led_usb_state, USB_LED_SCROLL_LOCK) ? PSTR(SCRLK_STR) : PSTR(EMPTY_STR), false);
  161. }
  162. // Update WPM snail icon
  163. static void render_wpm_icon(uint8_t current_wpm) {
  164. // wpm_icon is used to prevent unnecessary redraw
  165. if ((current_wpm < ICON_MED_WPM) && (oled_data.wpm_icon != 0)) {
  166. oled_data.wpm_icon = 0;
  167. } else if ((current_wpm >= ICON_MED_WPM) && (current_wpm < ICON_FAST_WPM) && (oled_data.wpm_icon != 1)) {
  168. oled_data.wpm_icon = 1;
  169. } else if ((current_wpm >= ICON_FAST_WPM) && (oled_data.wpm_icon != 2)) {
  170. oled_data.wpm_icon = 2;
  171. } else {
  172. return;
  173. }
  174. static const char PROGMEM snails[][2][24] = {
  175. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x20, 0xA0, 0x20, 0x40, 0x40, 0x80, 0x00, 0x00, 0x00, 0x80, 0x40, 0x20, 0x50, 0x88, 0x04, 0x00, 0x00},
  176. {0x40, 0x60, 0x50, 0x4E, 0x51, 0x64, 0x4A, 0x51, 0x54, 0x49, 0x41, 0x62, 0x54, 0x49, 0x46, 0x41, 0x40, 0x30, 0x09, 0x04, 0x02, 0x01, 0x00, 0x00}},
  177. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x40, 0x40, 0x40, 0x40, 0x80, 0x80, 0x00, 0x00, 0x00, 0x04, 0x98, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00},
  178. {0x60, 0x50, 0x54, 0x4A, 0x51, 0x64, 0x4A, 0x51, 0x55, 0x49, 0x41, 0x62, 0x54, 0x49, 0x46, 0x41, 0x21, 0x10, 0x0A, 0x08, 0x05, 0x02, 0x00, 0x00}},
  179. {{0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x40, 0x40, 0x40, 0x80, 0x80, 0x10, 0x10, 0x10, 0x20, 0x40, 0x40, 0xC0, 0x80, 0x80, 0x00, 0x00, 0x00},
  180. {0x60, 0x58, 0x54, 0x62, 0x49, 0x54, 0x52, 0x51, 0x55, 0x49, 0x62, 0x52, 0x4D, 0x45, 0x46, 0x22, 0x21, 0x11, 0x10, 0x0A, 0x08, 0x05, 0x02, 0x00}}
  181. };
  182. oled_set_cursor(0, 11);
  183. oled_write_raw_P(snails[oled_data.wpm_icon][0], sizeof(snails[oled_data.wpm_icon][0]));
  184. oled_set_cursor(0, 12);
  185. oled_write_raw_P(snails[oled_data.wpm_icon][1], sizeof(snails[oled_data.wpm_icon][1]));
  186. }
  187. // Update WPM graph
  188. static void render_wpm_graph(uint8_t current_wpm) {
  189. uint8_t line_height = ((current_wpm / GRAPH_TOP_WPM) * 7);
  190. if (line_height > 7) {
  191. line_height = 7;
  192. }
  193. // Count graph line pixels, return if nothing to draw
  194. uint8_t pixel_count = line_height;
  195. for (int i = 0; i < 31; i++) {
  196. pixel_count += oled_data.graph_lines[i];
  197. }
  198. if (pixel_count == 0) {
  199. return;
  200. }
  201. // Shift array elements left or right depending on GRAPH_DIRECTION pend new graph line
  202. if (GRAPH_DIRECTION) {
  203. for (int i = 0; i < 31; i++) {
  204. oled_data.graph_lines[i] = oled_data.graph_lines[i + 1];
  205. }
  206. oled_data.graph_lines[31] = line_height;
  207. } else {
  208. for (int i = 31; i > 0; i--) {
  209. oled_data.graph_lines[i] = oled_data.graph_lines[i - 1];
  210. }
  211. oled_data.graph_lines[0] = line_height;
  212. }
  213. // Draw all graph lines (left to right, bottom to top)
  214. uint16_t draw_count, arrpos;
  215. for (int x = 1; x <= 63; x += 2) {
  216. arrpos = x / 2;
  217. draw_count = oled_data.graph_lines[arrpos];
  218. for (int y = 31; y >= 25; y--) {
  219. if (draw_count > 0) {
  220. write_pixel(x, y, true);
  221. draw_count--;
  222. } else {
  223. write_pixel(x, y, false);
  224. }
  225. }
  226. }
  227. }
  228. // Call OLED functions
  229. bool oled_task_user(void) {
  230. // Draw OLED keyboard, prevent redraw
  231. if (oled_data.first_loop) {
  232. render_background();
  233. oled_data.first_loop = false;
  234. }
  235. // Get current WPM, subtract 25% for accuracy and prevent large jumps caused by simultaneous keypresses
  236. uint8_t current_wpm = get_current_wpm();
  237. // Write active layer name to display
  238. render_layer_state();
  239. // Update WPM counters
  240. render_wpm_counters(current_wpm);
  241. // Update WPM snail icon
  242. render_wpm_icon(current_wpm);
  243. // Update LED status
  244. render_led_status();
  245. // Update WPM graph every graph_refresh milliseconds
  246. if (timer_elapsed(oled_data.timer) > GRAPH_REFRESH) {
  247. render_wpm_graph(current_wpm);
  248. oled_data.timer = timer_read();
  249. }
  250. return false;
  251. }
  252. #endif
  253. bool wpm_keycode_user(uint16_t keycode) {
  254. // Count all keycodes on the macropad
  255. return true;
  256. }