Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include "keyboard.h"
  2.  
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5.  
  6. enum KEYCODE {
  7.     NULL_KEY = 0,
  8.     Q_PRESSED = 0x10,
  9.     Q_RELEASED = 0x90,
  10.     W_PRESSED = 0x11,
  11.     W_RELEASED = 0x91,
  12.     E_PRESSED = 0x12,
  13.     E_RELEASED = 0x92,
  14.     R_PRESSED = 0x13,
  15.     R_RELEASED = 0x93,
  16.     T_PRESSED = 0x14,
  17.     T_RELEASED = 0x94,
  18.     Y_PRESSED = 0x15,
  19.     Y_RELEASED = 0x95,
  20.     U_PRESSED = 0x16,
  21.     U_RELEASED = 0x96,
  22.     I_PRESSED = 0x17,
  23.     I_RELEASED = 0x97,
  24.     O_PRESSED = 0x18,
  25.     O_RELEASED = 0x98,
  26.     P_PRESSED = 0x19,
  27.     P_RELEASED = 0x99,
  28.     A_PRESSED = 0x1E,
  29.     A_RELEASED = 0x9E,
  30.     S_PRESSED = 0x1F,
  31.     S_RELEASED = 0x9F,
  32.     D_PRESSED = 0x20,
  33.     D_RELEASED = 0xA0,
  34.     F_PRESSED = 0x21,
  35.     F_RELEASED = 0xA1,
  36.     G_PRESSED = 0x22,
  37.     G_RELEASED = 0xA2,
  38.     H_PRESSED = 0x23,
  39.     H_RELEASED = 0xA3,
  40.     J_PRESSED = 0x24,
  41.     J_RELEASED = 0xA4,
  42.     K_PRESSED = 0x25,
  43.     K_RELEASED = 0xA5,
  44.     L_PRESSED = 0x26,
  45.     L_RELEASED = 0xA6,
  46.     Z_PRESSED = 0x2C,
  47.     Z_RELEASED = 0xAC,
  48.     X_PRESSED = 0x2D,
  49.     X_RELEASED = 0xAD,
  50.     C_PRESSED = 0x2E,
  51.     C_RELEASED = 0xAE,
  52.     V_PRESSED = 0x2F,
  53.     V_RELEASED = 0xAF,
  54.     B_PRESSED = 0x30,
  55.     B_RELEASED = 0xB0,
  56.     N_PRESSED = 0x31,
  57.     N_RELEASED = 0xB1,
  58.     M_PRESSED = 0x32,
  59.     M_RELEASED = 0xB2,
  60.    
  61.     ZERO_PRESSED = 0x0B,
  62.     ZERO_RELEASED = 0x8B,
  63.     ONE_PRESSED = 0x2,
  64.     ONE_RELEASED = 0x82,
  65.     NINE_PRESSED = 0xA,
  66.     NINE_RELEASED = 0x8A,
  67.    
  68.     POINT_PRESSED = 0x34,
  69.     POINT_RELEASED = 0xB4,
  70.    
  71.     SLASH_RELEASED = 0xB5,
  72.    
  73.     BACKSPACE_PRESSED = 0xE,
  74.     BACKSPACE_RELEASED = 0x8E,
  75.  
  76.     SPACE_PRESSED = 0x39,
  77.     SPACE_RELEASED = 0xB9,
  78.  
  79.     ENTER_PRESSED = 0x1C,
  80.     ENTER_RELEASED = 0x9C,
  81.  
  82.     LSHIFT_PRESSED = 0x2A,
  83.     LSHIFT_RELEASED = 0xAA,
  84.  
  85.     RSHIFT_PRESSED = 0x36,
  86.     RSHIFT_RELEASED = 0xB6,
  87. };
  88.  
  89. bool shiftPressed = false;
  90.  
  91. static char* _qwertzuiop = "qwertyuiop";
  92. static char* _asdfghjkl = "asdfghjkl";
  93. static char* _yxcvbnm = "zxcvbnm";
  94. static char* _num = "123456789";
  95.  
  96. static char* _caps_qwertzuiop = "QWERTYUIOP";
  97. static char* _caps_asdfghjkl = "ASDFGHJKL";
  98. static char* _caps_yxcvbnm = "ZXCVBNM";
  99.  
  100. uint8_t keyboard_to_ascii(uint8_t key) {
  101.     if(key == ENTER_PRESSED) return '\n';
  102.     if(key == SPACE_PRESSED) return ' ';
  103.     if(key == ENTER_PRESSED) return '\r';
  104.     if(key == POINT_RELEASED) return '.';
  105.     if(key == SLASH_RELEASED) return '/';
  106.     if(key == ZERO_PRESSED) return '0';
  107.     if(key >= ONE_PRESSED && key <= NINE_PRESSED)
  108.         return _num[key - ONE_PRESSED];
  109.     if(key >= Q_PRESSED && key <= ENTER_PRESSED)
  110.     {
  111.         if (shiftPressed) {
  112.             return _caps_qwertzuiop[key - Q_PRESSED];
  113.         } else {
  114.             return _qwertzuiop[key - Q_PRESSED];
  115.         }
  116.     } else if(key >= A_PRESSED && key <= L_PRESSED)
  117.     {
  118.         if (shiftPressed) {
  119.             return _caps_asdfghjkl[key - A_PRESSED];
  120.         } else {
  121.             return _asdfghjkl[key - A_PRESSED];
  122.         }
  123.     } else if(key >= Z_PRESSED && key <= M_PRESSED)
  124.     {
  125.         if (shiftPressed) {
  126.             return _caps_yxcvbnm[key - Z_PRESSED];
  127.         } else {
  128.             return _yxcvbnm[key - Z_PRESSED];
  129.         }
  130.     }
  131.    
  132.     if (key == LSHIFT_PRESSED || key == RSHIFT_PRESSED) {
  133.         shiftPressed = true;
  134.     } else {
  135.         shiftPressed = false;
  136.     }
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement