Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is a keypad driver for a Raspberry Pi gpio keyboard.
- *
- */
- // #include <linux/fb.h>
- // #include <linux/ioctl.h>
- // #include <stdlib.h>
- // #include <sys/select.h>
- // #include <sys/time.h>
- // #include <termios.h>
- // #include <sys/types.h>
- // #include <poll.h>
- // #include <stdbool.h>
- // #include <sys/mman.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <wiringPi.h>
- #include <fcntl.h>
- #include <string.h>
- #include <errno.h>
- #include <linux/input.h>
- #include <linux/uinput.h>
- #include <sys/stat.h>
- #include <iostream>
- // #include <signal.h>
- #define die(str, args...) do { \
- perror(str); \
- exit(EXIT_FAILURE); \
- } while(0)
- #define col0 2
- #define col1 3
- #define col2 4
- #define col3 5
- #define row0 6
- #define row1 25
- #define row2 28
- #define row3 29
- #define GND -1
- int col;
- int row;
- unsigned char columnGPIO[] = {2, 3, 4, 5};
- unsigned char rowGPIO[] = {6, 25, 28, 29};
- unsigned char keypadMap[] = {
- KEY_A, KEY_B, KEY_C, KEY_D,
- KEY_E, KEY_F, KEY_G, KEY_H,
- KEY_I, KEY_J, KEY_K, KEY_L,
- KEY_LEFTSHIFT, KEY_N, KEY_O, KEY_P
- };
- int keyPadNumber[4][4] = {
- {0, 1, 2, 3},
- {4, 5, 6, 7},
- {8, 9, 10, 11},
- {12, 13, 14, 15}
- };
- int keyPadState[4][4] = { //Copies over the state of the of the matrix
- {1, 1, 1, 1},
- {1, 1, 1, 1},
- {1, 1, 1, 1},
- {1, 1, 1, 1}
- };
- unsigned short debounce[4][4]; //Stores the debounce value
- void pin_init(){
- wiringPiSetup();
- for (col = 0; col < 4; col++){
- pinMode (columnGPIO[col], OUTPUT);
- digitalWrite (columnGPIO[col], LOW);
- }
- for (row = 0; row < 4; row++){
- pinMode (rowGPIO[row], INPUT);
- pullUpDnControl (rowGPIO[row], PUD_UP);
- }
- }
- int send_event(int fd, __u16 type, __u16 code, __s32 value)
- {
- struct input_event event;
- memset(&event, 0, sizeof(event));
- event.type = type;
- event.code = code;
- event.value = value;
- if (write(fd, &event, sizeof(event)) != sizeof(event)) {
- fprintf(stderr, "Error on send_event");
- return -1;
- }
- return 0;
- }
- int main(void) {
- //Process ID
- pid_t pid;
- pid = getpid();
- if (pid > 0){
- //printf("\nPID Parent= %d\n", pid);
- //return 0;
- }
- if (pid == 0){
- //printf("\nPID child = %d\n", pid);
- return 0;
- }
- /* uinput init */
- int i,fd;
- struct uinput_user_dev device;
- memset(&device, 0, sizeof device);
- fd=open("/dev/uinput",O_WRONLY);
- strcpy(device.name,"test keyboard");
- device.id.bustype=BUS_USB;
- device.id.vendor=1;
- device.id.product=1;
- device.id.version=1;
- if (write(fd,&device,sizeof(device)) != sizeof(device)) {
- fprintf(stderr, "error setup\n");
- }
- if (ioctl(fd,UI_SET_EVBIT,EV_KEY) < 0)
- fprintf(stderr, "error evbit key\n");
- for (i=0;i<16;i++)
- if (ioctl(fd,UI_SET_KEYBIT, keypadMap[i]) < 0)
- fprintf(stderr, "error evbit key\n");
- if (ioctl(fd,UI_SET_EVBIT,EV_REL) < 0)
- fprintf(stderr, "error evbit rel\n");
- for(i=REL_X;i<REL_MAX;i++)
- if (ioctl(fd,UI_SET_RELBIT,i) < 0)
- fprintf(stderr, "error setrelbit %d\n", i);
- if (ioctl(fd,UI_DEV_CREATE) < 0){
- fprintf(stderr, "error create\n");
- }
- sleep(2);
- /* end uinput init */
- /* init event0 */
- int fdkey;
- fdkey = open("/dev/input/event0", O_RDONLY);
- // struct input_event evkey;
- int flags = fcntl(fd, F_GETFL, 0);
- //int err;
- fcntl(fdkey, F_SETFL, flags | O_NONBLOCK);
- /* end init event0 */
- pin_init();
- //int j, m=0,k;
- //int returnKeyPress = 0;
- digitalWrite(columnGPIO[0], 0); //We're done, set back as high
- digitalWrite(columnGPIO[1], 0); //We're done, set back as high
- digitalWrite(columnGPIO[2], 0); //We're done, set back as high
- digitalWrite(columnGPIO[3], 0); //We're done, set back as high
- delay (500);
- while(1) {
- int syncNow = 0; //If we see a change we set this flag to update keyboard
- for (col = 0 ; col < 4 ; col++) {
- digitalWrite(columnGPIO[col], 0); //Set this column as active low
- delayMicroseconds(1);
- for (row = 0 ; row < 4 ; row++) {
- int state = !digitalRead(rowGPIO[row]); //1 = pressed 0 = open (inversed)
- if (state != keyPadState[row][col]) { //Has state changed since last time?
- send_event(fd, EV_KEY, keypadMap[keyPadNumber[row][col]], state);
- keyPadState[row][col] = state;
- syncNow = 1;
- }
- }
- digitalWrite(columnGPIO[col], 1); //We're done, set back as high
- }
- if (syncNow) {
- send_event(fd, EV_SYN, SYN_REPORT, 0);
- }
- }
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement