keymap.c 11 KB

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