📄 ptest.c
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Copyright (C) 2002, 2003 Motorola Semiconductors HK Ltd * */#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <semaphore.h>#include <time.h>int gfinishread;sem_t sem;void* readkey(void* arg){ char* buf = (char*) arg; int fd; int i; int size; gfinishread=0; i=0; if ((fd = open("/dev/i2c-keypad",O_RDONLY))==-1){ printf("error: can't open /dev/i2c-keypad\n"); return NULL; } /*if((fd = open("/dev/keypad",O_RDONLY))==-1){ printf("error: can't open /dev/keypad. now try to open /tmp/keypad\n"); close(fd); } else if ((fd = open("/tmp/keypad",O_RDONLY))==-1){ printf("error: can't open /tmp/keypad\n"); return NULL; }*/ while(i<102){ if(read(fd,buf,2)==-1){ printf("error: no of read bytes are not equal to no. of return bytes."); gfinishread=1; close(fd); return NULL; } sem_post(&sem); i=i+1; } gfinishread=1; close(fd); return NULL; }void* printkey(void* arg){ char* buf = (char*) arg; while(!gfinishread){ sem_wait(&sem); putchar('\r'); putchar((((char)~buf[0]&0x01) && ((char)buf[1]&0xff))?'0':' '); putchar((((char)~buf[0]&0x02) && ((char)buf[1]&0xff))?'1':' '); putchar((((char)~buf[0]&0x04) && ((char)buf[1]&0xff))?'2':' '); putchar((((char)~buf[0]&0x08) && ((char)buf[1]&0xff))?'3':' '); putchar((((char)~buf[0]&0x10) && ((char)buf[1]&0xff))?'4':' '); putchar((((char)~buf[0]&0x20) && ((char)buf[1]&0xff))?'5':' '); putchar((((char)~buf[0]&0x40) && ((char)buf[1]&0xff))?'6':' '); putchar((((char)~buf[0]&0x80) && ((char)buf[1]&0xff))?'7':' '); putchar((((char)~buf[1]&0x01) && ((char)buf[0]&0xff))?'A':' '); putchar((((char)~buf[1]&0x02) && ((char)buf[0]&0xff))?'B':' '); putchar((((char)~buf[1]&0x04) && ((char)buf[0]&0xff))?'C':' '); putchar((((char)~buf[1]&0x08) && ((char)buf[0]&0xff))?'D':' '); putchar((((char)~buf[1]&0x10) && ((char)buf[0]&0xff))?'E':' '); putchar((((char)~buf[1]&0x20) && ((char)buf[0]&0xff))?'F':' '); putchar((((char)~buf[1]&0x40) && ((char)buf[0]&0xff))?'G':' '); putchar((((char)~buf[1]&0x80) && ((char)buf[0]&0xff))?'H':' '); fflush(stdout); } return NULL;}int timedelay(void){ sleep(1);/* time_t t1, t2; time(&t1); time(&t2); while(!difftime(t2,t1)){ time(&t2); } return 0;*/}void* presskeytime(void* arg){ char* buf = (char*) arg; int sec=0; short int b0=0, b1=0, b2=0, b3=0, b4=0, b5=0 ,b6=0, b7=0;// ba=0, bb=0, bc=0, bd=0 ,be=0, bf=0, bg=0, bh=0; short int bf0=1; short int bf1=1; short int bf2=1; short int bf3=1; short int bf4=1; short int bf5=1; short int bf6=1; short int bf7=1;// bfa=1, bfb=1, bfc=1, bfd=1 ,bfe=1, bff=1, bfg=1, bfh=1; while(!gfinishread){ timedelay(); //printf("\n%x %x\n",(char)~buf[0],(char)buf[1]); if((((char)~buf[0]&0x01) && ((char)buf[1]&0xff))&& bf0){ //printf("yes\n"); b0+=1; bf0=1; } else if(((char)~buf[0]&0x01) && ((char)buf[1]&0xff)){ //printf("half\n"); bf0=1; } else{ bf0=0; } if((((char)~buf[0]&0x02) && ((char)buf[1]&0xff))&& bf1){ b1+=1; bf1=1; } else if(((char)~buf[0]&0x02) && ((char)buf[1]&0xff)){ bf1=1; } else bf1=0; if((((char)~buf[0]&0x04) && ((char)buf[1]&0xff))&& bf2){ b2+=1; bf2=1; } else if(((char)~buf[0]&0x04) && ((char)buf[1]&0xff)){ bf2=1; } else{ bf2=0; } if((((char)~buf[0]&0x08) && ((char)buf[1]&0xff))&& bf3){ b3+=1; bf3=1; } else if(((char)~buf[0]&0x08) && ((char)buf[1]&0xff)){ bf3=1; } else{ bf3=0; } if((((char)~buf[0]&0x10) && ((char)buf[1]&0xff))&& bf4){ b4+=1; bf4=1; } else if(((char)~buf[0]&0x10) && ((char)buf[1]&0xff)){ bf4=1; } else{ bf4=0; } if((((char)~buf[0]&0x20) && ((char)buf[1]&0xff))&& bf5){ b5+=1; bf5=1; } else if(((char)~buf[0]&0x20) && ((char)buf[1]&0xff)){ bf5=1; } else{ bf5=0; } if((((char)~buf[0]&0x40) && ((char)buf[1]&0xff))&& bf6){ b6+=1; bf6=1; } else if(((char)~buf[0]&0x40) && ((char)buf[1]&0xff)){ bf6=1; } else{ bf6=0; } if((((char)~buf[0]&0x80) && ((char)buf[1]&0xff))&& bf7){ b7+=1; bf7=1; } else if(((char)~buf[0]&0x80) && ((char)buf[1]&0xff)){ bf7=1; } else{ bf7=0; } sec+=1; } printf("\nButton X is PRESSed HOW MANY SEC during %d seconds?\n",sec); printf(" 0 1 2 3 4 5 6 7\n"); printf("%3d %3d %3d %3d %3d %3d %3d %3d\n",b0,b1,b2,b3,b4,b5,b6,b7); return NULL;} int main(){ pthread_t thread_readkey_id; pthread_t thread_printkey_id; pthread_t thread_presskeytime_id; char buf[2]; sem_init(&sem, 0, 0); pthread_create (&thread_readkey_id, NULL, &readkey, buf); pthread_create (&thread_printkey_id, NULL, &printkey, buf); pthread_create (&thread_presskeytime_id, NULL, &presskeytime, buf); /* Make sure the first thread has finished. */ pthread_join (thread_readkey_id, NULL); /* Make sure the second thread has finished. */ pthread_join (thread_printkey_id, NULL); /* Make sure the third thread has finished. */ pthread_join (thread_presskeytime_id, NULL); /* Now we can safely return. */ printf("Keypad Test program has completed"); /*switch(buf[0]){ case 0xEF: switch(buf[1]){ case 0xEF: break; case 0xF7: break; case 0xFB: break; case 0xFD: break; case 0xFE: break; default: } break; case 0xF7: switch(buf[1]){ case 0xEF: break; case 0xF7: break; case 0xFB: break; case 0xFD: break; case 0xFE: break; default: } break; case 0xFB: switch(buf[1]){ case 0xEF: break; case 0xF7: break; case 0xFB: break; case 0xFD: break; case 0xFE: break; default: } break; case 0xFD: switch(buf[1]){ case 0xEF: break; case 0xF7: break; case 0xFB: break; case 0xFD: break; case 0xFE: break; default: } break; case 0xFE: switch(buf[1]){ case 0xEF: break; case 0xF7: break; case 0xFB: break; case 0xFD: break; case 0xFE: break; default: } break; default: switch(buf[1]){ case 0xEF: break; case 0xF7: break; case 0xFB: break; case 0xFD: break; case 0xFE: break; default: } } */ /*if ((~buf[0]&0x10) && (~buf[1]&0x10)) printf("L "); if ((~buf[0]&0x10) && (~buf[1]&0x08)) printf("R "); if ((~buf[0]&0x10) && (~buf[1]&0x04)) printf("D "); if ((~buf[0]&0x08) && (~buf[1]&0x02)) printf("U "); if ((~buf[0]&0x04) && (~buf[1]&0x01)) printf("A "); if ((~buf[0]&0x02) && (~buf[1]&0x10)) printf("B "); if ((buf[0]==0xFF) && (buf[1]==0xFF)) printf("No keys are pressed "); printf("\n"); */ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -