oled.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /** @file oled.h
  2. * @brief mcrown oled service implementation.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * @author Mario Corona (mariocc@comunidad.unam.mx) 2021
  18. *
  19. */
  20. #include QMK_KEYBOARD_H
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "mcrown.h"
  24. #include "oled.h"
  25. #define ASCII_TABLE_LENGTH (0x80)
  26. #define KEYLOG_STRING_STARTUP (KEYLOG_EOL_LEN+1)
  27. #define ALT_CODE (0x7E)
  28. #define SPECIAL_KEYS_SHIFT(kc) (0x18+(kc))
  29. static void render_keylogger_status(void);
  30. static char keylog_str[KEYLOG_EOL_LEN] = {' '};
  31. static uint16_t log_timer = 0;
  32. static uint8_t current_cursor_pos=0;
  33. static uint32_t cursor_oled_timer = 0;
  34. static uint32_t standby_oled_timer = 0;
  35. static char last_c=' ';
  36. /* Provides the ASCII value or the address of the character selected of the OLED font specified in glcfont.c */
  37. static const char ascii_t[ASCII_TABLE_LENGTH] = {
  38. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  39. /* | | | | | | | | | | | | | | | | */
  40. 0x0F, 0x1A, 0x1B, 0x19, 0x18, 0x0E, ' ', ' ', 0x11, 0x1C, 0x97, ' ', ' ', ' ', ' ', ' ', /* 0 */
  41. /* | | | | | | | | | | | | | | | | */
  42. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 0x1D, ' ', ' ', ' ', ' ', /* 1 */
  43. /* | | | | | | | | | | | | | | | | */
  44. 0x16, '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', /* 2 */
  45. /* | | | | | | | | | | | | | | | | */
  46. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', /* 3 */
  47. /* | | | | | | | | | | | | | | | | */
  48. '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', /* 4 */
  49. /* | | | | | | | | | | | | | | | | */
  50. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', /* 5 */
  51. /* | | | | | | | | | | | | | | | | */
  52. '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 6 */
  53. /* | | | | | | | | | | | | | | | | */
  54. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 0x7F, /* 7 */
  55. };
  56. /* This table is to remap and get the corresponding ASCII value based on the KEYCODE (taken as the index of the array) of quatum_keycodes.h module */
  57. static const unsigned char code_to_ascii[ASCII_TABLE_LENGTH] = {
  58. /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
  59. /* | | | | | | | | | | | | | | | | */
  60. 0x00, 0x00, 0x00, 0x00, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', /* 0 */
  61. /* | | | | | | | | | | | | | | | | */
  62. 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', /* 1 */
  63. /* | | | | | | | | | | | | | | | | */
  64. '3', '4', '5', '6', '7', '8', '9', '0', 0x0A, 0x1B, 0x08, 0x09, ' ', '-', '=', '[', /* 2 */
  65. /* | | | | | | | | | | | | | | | | */
  66. ']', '\\', 0x00, ';', '\'', '`', ',', '.', '/', 0x00, 0x00, 0x00, 0x00, 0x00, '!', '@', /* 3 */
  67. /* | | | | | | | | | | | | | | | | */
  68. '#', '$', '%', '^', '&', '*', '(', ')', 0x00, 0x00, 0x00, 0x00, 0x00, '_', '+', '{', /* 4 */
  69. /* | | | | | | | | | | | | | | | | */
  70. '}', '|', 0x00, 0x00, 0x00, '~', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 5 */
  71. /* | | | | | | | | | | | | | | | | */
  72. 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 */
  73. /* | | | | | | | | | | | | | | | | */
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, /* 7 */
  75. };
  76. /** @brief maps the keycode to get the ascii value.
  77. *
  78. * If any argument is invalid, the function has no effect.
  79. *
  80. * @param keycode value of the pressed key.
  81. * @return ascii value of the pressed key or a special value for non-ascii keys.
  82. */
  83. inline static char get_ascii(int16_t keycode){
  84. uint8_t ascii_idx=0x00;
  85. if(keycode<KC_F1){
  86. ascii_idx=code_to_ascii[(uint8_t)keycode];
  87. }else if(keycode<KC_KP_ENTER){
  88. ascii_idx=code_to_ascii[SPECIAL_KEYS_SHIFT(keycode)];
  89. }else if(KC_LANG1==keycode){
  90. ascii_idx=code_to_ascii[ALT_CODE];
  91. }else if( QK_LSFT==(QK_LSFT&keycode) ){
  92. ascii_idx=code_to_ascii[RM_LSFT(keycode)];
  93. }
  94. return ascii_t[ascii_idx];
  95. }
  96. /** @brief detect retuns the rotation of the display based on the keyboard side.
  97. *
  98. * If any argument is invalid, the function has no effect.
  99. *
  100. * @param oled_rotation_t rotation
  101. * @return rotation of the display.
  102. */
  103. oled_rotation_t oled_init_user(oled_rotation_t rotation){
  104. oled_rotation_t oled_rot=OLED_ROTATION_180;
  105. if(true==is_keyboard_master()){
  106. #ifdef OLED_VERTICAL
  107. oled_rot=OLED_ROTATION_270;
  108. #else
  109. oled_rot=OLED_ROTATION_0;
  110. #endif
  111. }
  112. return oled_rot;
  113. }
  114. /** @brief renders the keylog string and display it. This function also toggles the cursor.
  115. *
  116. * If any argument is invalid, the function has no effect.
  117. *
  118. * @param void.
  119. * @return void.
  120. */
  121. static void render_keylogger_status(void){
  122. static bool cursor_f=true;
  123. if(timer_elapsed32(cursor_oled_timer) > 300){
  124. cursor_oled_timer = timer_read32();
  125. cursor_f=!cursor_f;
  126. }
  127. oled_write_P(PSTR("\n>:"), false);
  128. if(current_cursor_pos>(KEYLOG_LEN-1)){
  129. current_cursor_pos=0;
  130. memset(keylog_str, ' ', sizeof(char)*KEYLOG_EOL_LEN);
  131. /* Here the EOL is to clear with white spaces all the keylog area */
  132. keylog_str[KEYLOG_EOL_LEN-1] = '\0';
  133. oled_write(keylog_str, false);
  134. /* Reset EOL to the begining of the keylog string */
  135. keylog_str[0] = '\0';
  136. }
  137. oled_write(keylog_str, false);
  138. oled_write_char(last_c, cursor_f);
  139. }
  140. /** @brief displays the current active layout.
  141. *
  142. * If any argument is invalid, the function has no effect.
  143. *
  144. * @param void.
  145. * @return void.
  146. */
  147. void render_layout_state(void){
  148. CUSTOM_LAYERS_T current_layer;
  149. current_layer=(CUSTOM_LAYERS_T)get_highest_layer(layer_state);
  150. #ifdef OLED_VERTICAL
  151. oled_write_P(PSTR("Lyt:\n"), false);
  152. #else
  153. oled_write_P(PSTR("Layout: "), false);
  154. #endif
  155. switch (current_layer){
  156. #ifdef OLED_VERTICAL
  157. case _COLEMAK:
  158. oled_write_P(PSTR("Clmak\n"), false);
  159. break;
  160. case _DVORAK:
  161. oled_write_P(PSTR("Dvak\n"), false);
  162. break;
  163. case _QWERTY:
  164. oled_write_P(PSTR("Qwty\n"), false);
  165. break;
  166. default:
  167. oled_write_P(PSTR("Undf\n"), false);
  168. break;
  169. #else
  170. case _COLEMAK:
  171. oled_write_P(PSTR("Colemak\n"), false);
  172. break;
  173. case _DVORAK:
  174. oled_write_P(PSTR("Dvorak\n"), false);
  175. break;
  176. case _QWERTY:
  177. oled_write_P(PSTR("Qwerty\n"), false);
  178. break;
  179. default:
  180. oled_write_P(PSTR("Undefined\n"), false);
  181. break;
  182. #endif
  183. }
  184. }
  185. /** @brief displays the current active layer.
  186. *
  187. * If any argument is invalid, the function has no effect.
  188. *
  189. * @param void.
  190. * @return void.
  191. */
  192. void render_layer_state(void){
  193. CUSTOM_LAYERS_T current_layer;
  194. current_layer=(CUSTOM_LAYERS_T)get_highest_layer(layer_state);
  195. #ifdef OLED_VERTICAL
  196. oled_write_P(PSTR("Lyr:\n"), false);
  197. #else
  198. oled_write_P(PSTR("Layer:"), false);
  199. #endif
  200. switch(current_layer){
  201. #ifdef OLED_VERTICAL
  202. case _LOWER:
  203. oled_write_P(PSTR("Lwr\n"), true);
  204. break;
  205. case _RAISE:
  206. oled_write_P(PSTR("Ris\n"), true);
  207. break;
  208. case _ADJUST:
  209. oled_write_P(PSTR("Adj\n"), true);
  210. break;
  211. case _NUMPAD:
  212. oled_write_P(PSTR("Num\n"), true);
  213. break;
  214. default:
  215. oled_write_P(PSTR("Def\n"), false);
  216. break;
  217. #else
  218. case _LOWER:
  219. oled_write_P(PSTR(" Lower "), true);
  220. break;
  221. case _RAISE:
  222. oled_write_P(PSTR(" Raise "), true);
  223. break;
  224. case _ADJUST:
  225. oled_write_P(PSTR(" Adjust "), true);
  226. break;
  227. case _NUMPAD:
  228. oled_write_P(PSTR(" Numpad "), true);
  229. break;
  230. default:
  231. oled_write_P(PSTR(" Default "), false);
  232. break;
  233. #endif
  234. }
  235. }
  236. /** @brief displays the current status of the main display/
  237. *
  238. * If any argument is invalid, the function has no effect.
  239. *
  240. * @param void.
  241. * @return void.
  242. */
  243. void render_status(void){
  244. render_layout_state();
  245. oled_write_P(PSTR("\n"), false);
  246. render_layer_state();
  247. render_keylogger_status();
  248. }
  249. /** @brief renders the logo to be displayed.
  250. *
  251. * If any argument is invalid, the function has no effect.
  252. *
  253. * @param void.
  254. * @return void.
  255. */
  256. static void render_logo(void){
  257. static const char PROGMEM qmk_logo[] = {
  258. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94,
  259. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4,
  260. 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4,
  261. 0x00};
  262. oled_write_P(qmk_logo, false);
  263. }
  264. /** @brief executes the actions for both displays.
  265. *
  266. * If any argument is invalid, the function has no effect.
  267. *
  268. * @param void.
  269. * @return void.
  270. */
  271. void oled_task_user(void){
  272. if (timer_elapsed32(standby_oled_timer) > 15000){
  273. oled_off();
  274. }else{
  275. oled_on();
  276. if(true==is_keyboard_master()){
  277. render_status();
  278. }else{
  279. render_logo();
  280. oled_write_P(PSTR("\n"), false);
  281. oled_scroll_left();
  282. }
  283. }
  284. }
  285. /** @brief process the current key and add it to the keylog string.
  286. *
  287. * If any argument is invalid, the function has no effect.
  288. *
  289. * @param keycode pressed key.
  290. * @return void.
  291. */
  292. extern void add_keylog(uint16_t keycode){
  293. if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)){
  294. keycode&=0x00FF;
  295. }
  296. if(current_cursor_pos>(KEYLOG_LEN-1)||(current_cursor_pos>KEYLOG_STRING_STARTUP)){
  297. current_cursor_pos=0;
  298. last_c=get_ascii(keycode);
  299. current_cursor_pos++;
  300. }else{
  301. if(keycode <= KC_TILD){
  302. keylog_str[current_cursor_pos]=last_c;
  303. last_c=get_ascii(keycode);
  304. current_cursor_pos++;
  305. }
  306. keylog_str[current_cursor_pos] = '\0';
  307. }
  308. log_timer = timer_read();
  309. standby_oled_timer = timer_read32();
  310. }