Advertisement
Guest User

Gamepad_GPIO_Matrix_Driver_03.c

a guest
Nov 29th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.95 KB | None | 0 0
  1.  
  2. /*
  3.  * This is a keypad driver for a Raspberry Pi gpio keyboard.
  4.  *
  5.  */
  6.  
  7. // #include <linux/fb.h>
  8. // #include <linux/ioctl.h>
  9. // #include <stdlib.h>
  10. // #include <sys/select.h>
  11. // #include <sys/time.h>
  12. // #include <termios.h>
  13. // #include <sys/types.h>
  14. // #include <poll.h>
  15. // #include <stdbool.h>
  16. // #include <sys/mman.h>
  17.  
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <wiringPi.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <linux/input.h>
  25. #include <linux/uinput.h>
  26. #include <sys/stat.h>
  27. #include <iostream>
  28. // #include <signal.h>
  29.  
  30. #define die(str, args...) do { \
  31. perror(str); \
  32. exit(EXIT_FAILURE); \
  33. } while(0)
  34.  
  35. #define col0 2
  36. #define col1 3
  37. #define col2 4
  38. #define col3 5
  39.  
  40. #define row0 6
  41. #define row1 25
  42. #define row2 28
  43. #define row3 29
  44.  
  45. #define GND -1
  46.  
  47. int col;
  48. int row;
  49.  
  50. unsigned char columnGPIO[] = {2, 3, 4, 5};
  51. unsigned char rowGPIO[] = {6, 25, 28, 29};
  52.  
  53. unsigned char keypadMap[] = {
  54.    KEY_A,          KEY_B,  KEY_C,  KEY_D,
  55.    KEY_E,          KEY_F,  KEY_G,  KEY_H,
  56.    KEY_I,          KEY_J,  KEY_K,  KEY_L,
  57.    KEY_LEFTSHIFT,  KEY_N,  KEY_O,  KEY_P
  58. };
  59.  
  60. int keyPadNumber[4][4] = {
  61.    {0,  1,  2,  3},
  62.    {4,  5,  6,  7},
  63.    {8,  9,  10, 11},
  64.    {12, 13, 14, 15}
  65. };
  66.  
  67. int keyPadState[4][4] = {          //Copies over the state of the of the matrix
  68.    {1, 1, 1, 1},
  69.    {1, 1, 1, 1},
  70.    {1, 1, 1, 1},
  71.    {1, 1, 1, 1}
  72. };
  73.  
  74. unsigned short debounce[4][4];               //Stores the debounce value
  75.  
  76. void pin_init(){
  77.    wiringPiSetup();
  78.    for (col = 0; col < 4; col++){
  79.       pinMode (columnGPIO[col],  OUTPUT);
  80.       digitalWrite (columnGPIO[col], LOW);
  81.    }
  82.    for (row = 0; row < 4; row++){
  83.       pinMode (rowGPIO[row], INPUT);
  84.       pullUpDnControl (rowGPIO[row], PUD_UP);
  85.    }
  86. }
  87.  
  88. int send_event(int fd, __u16 type, __u16 code, __s32 value)
  89. {
  90.    struct input_event event;
  91.  
  92.    memset(&event, 0, sizeof(event));
  93.    event.type = type;
  94.    event.code = code;
  95.    event.value = value;
  96.  
  97.    if (write(fd, &event, sizeof(event)) != sizeof(event)) {
  98.       fprintf(stderr, "Error on send_event");
  99.       return -1;
  100.    }
  101.  
  102.    return 0;
  103. }
  104.  
  105. int main(void) {
  106.    //Process ID
  107.    pid_t pid;
  108.    pid = getpid();
  109.    if (pid > 0){
  110.       //printf("\nPID Parent= %d\n", pid);
  111.       //return 0;
  112.    }
  113.    if (pid == 0){
  114.       //printf("\nPID child = %d\n", pid);
  115.       return 0;
  116.    }
  117.  
  118.    /* uinput init */
  119.    int i,fd;
  120.    struct uinput_user_dev device;
  121.    memset(&device, 0, sizeof device);
  122.  
  123.    fd=open("/dev/uinput",O_WRONLY);
  124.    strcpy(device.name,"test keyboard");
  125.  
  126.    device.id.bustype=BUS_USB;
  127.    device.id.vendor=1;
  128.    device.id.product=1;
  129.    device.id.version=1;
  130.  
  131.    if (write(fd,&device,sizeof(device)) != sizeof(device)) {
  132.       fprintf(stderr, "error setup\n");
  133.    }
  134.  
  135.    if (ioctl(fd,UI_SET_EVBIT,EV_KEY) < 0)
  136.       fprintf(stderr, "error evbit key\n");
  137.  
  138.    for (i=0;i<16;i++)
  139.       if (ioctl(fd,UI_SET_KEYBIT, keypadMap[i]) < 0)
  140.          fprintf(stderr, "error evbit key\n");
  141.  
  142.       if (ioctl(fd,UI_SET_EVBIT,EV_REL) < 0)
  143.          fprintf(stderr, "error evbit rel\n");
  144.  
  145.       for(i=REL_X;i<REL_MAX;i++)
  146.          if (ioctl(fd,UI_SET_RELBIT,i) < 0)
  147.             fprintf(stderr, "error setrelbit %d\n", i);
  148.  
  149.          if (ioctl(fd,UI_DEV_CREATE) < 0){
  150.             fprintf(stderr, "error create\n");
  151.          }
  152.  
  153.          sleep(2);
  154.       /* end uinput init */
  155.  
  156.       /* init event0 */
  157.       int fdkey;
  158.       fdkey = open("/dev/input/event0", O_RDONLY);
  159.       // struct input_event evkey;
  160.  
  161.       int flags = fcntl(fd, F_GETFL, 0);
  162.       //int err;
  163.       fcntl(fdkey, F_SETFL, flags | O_NONBLOCK);
  164.       /* end init event0 */
  165.  
  166.       pin_init();
  167.  
  168.       //int j, m=0,k;
  169.       //int returnKeyPress = 0;
  170.  
  171.       digitalWrite(columnGPIO[0], 0);         //We're done, set back as high
  172.       digitalWrite(columnGPIO[1], 0);         //We're done, set back as high
  173.       digitalWrite(columnGPIO[2], 0);         //We're done, set back as high
  174.       digitalWrite(columnGPIO[3], 0);         //We're done, set back as high
  175.  
  176.       delay (500);
  177.  
  178.       while(1) {
  179.          int syncNow = 0;              //If we see a change we set this flag to update keyboard
  180.          for (col = 0 ; col < 4 ; col++) {
  181.             digitalWrite(columnGPIO[col], 0);         //Set this column as active low
  182.             delayMicroseconds(1);
  183.             for (row = 0 ; row < 4 ; row++) {
  184.  
  185.                int state = !digitalRead(rowGPIO[row]);   //1 = pressed 0 = open (inversed)
  186.  
  187.                if (state != keyPadState[row][col]) {     //Has state changed since last time?
  188.                   send_event(fd, EV_KEY, keypadMap[keyPadNumber[row][col]], state);
  189.                   keyPadState[row][col] = state;
  190.                   syncNow = 1;
  191.                }
  192.  
  193.             }
  194.             digitalWrite(columnGPIO[col], 1);         //We're done, set back as high
  195.          }
  196.          if (syncNow) {
  197.             send_event(fd, EV_SYN, SYN_REPORT, 0);
  198.          }
  199.       }
  200.       close(fd);
  201.       return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement