按键驱动

 
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>
#include <pthread.h>

#define CMD_CHANGLE "\xEE\xB1\x00\x00\x01\xFF\xFC\xFF\xFF"
int salesman_num=6;
int fd;
int key_cond = 0;
int set_opt(int,int,int,char,int);
static pthread_mutex_t key_lock;
void key_run();

void main(int argc, char* argv[])
{
	int ret,flag = 0;
	int i=0;
	pthread_t key_id;
	char uart[] = {"/dev/ttySAC3"};
	
	
	unsigned char cmd[9] = { 0xEE, 0xB1, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF}; 
	unsigned char buf[16]={0};
	//sprintf(cmd,"%H %H %H %H %H %H %H %H %H","EE", "B1", "00", "00", flag++, "FF", "FC", "FF", "FF");
	//printf("%s\n",cmd);
	printf("%s\n",uart);
	pthread_mutex_init(&key_lock, NULL);
	if((fd = open(uart, O_RDWR))<0)
	{	
		printf("open %s is failed\n", uart);	
		return;
	}
	else{
		set_opt(fd, 9600, 8, 'N', 1);
		//set_opt(fd, 115200, 8, 'N', 1); 
		ret = pthread_create(&key_id,NULL,(void *)key_run,NULL);
		if(ret!=0){
		printf("Create pthread error!\n");
		exit(1);
		}
		sleep(5);
		while(1){
			if(key_cond){
				pthread_mutex_lock(&key_lock);
				key_cond=0;
				pthread_mutex_unlock(&key_lock);
				//sleep(5);
				continue;
			}
			if(flag > 5 ){
				flag = 1;
				//cmd[4] = salesman_num;			
			}
			cmd[4] = flag;
			write(fd,cmd,9);
			printf("%x\n",cmd[4]);
			sleep(3);
			memset(buf,0,16);
			read(fd,buf,9);
			for(i=0;i<9;i++ )
				printf("%x ,",buf[i]);
			printf("\n");
			flag++;
		}
	}
}



int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		return -1;
	}
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;
	newtio.c_cflag &= ~CSIZE;

	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;
		break;
	}

	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}

	switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	case 921600:
		printf("B921600\n");
		cfsetispeed(&newtio, B921600);
                cfsetospeed(&newtio, B921600);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
	newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;
	newtio.c_cc[VMIN] = 0;
	tcflush(fd,TCIFLUSH);
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
	return 0;
}

void key_run(){
	int fd_key;
	int version;
	int ret;
	unsigned char cmd[9] = { 0xEE, 0xB1, 0x00, 0x00, 0x00, 0xFF, 0xFC, 0xFF, 0xFF};
	struct input_event ev;
	fd_key = open("/dev/input/event0", O_RDONLY);
	if (fd < 0) {
		printf("open file failed\n");
		exit(1);
	}
	ioctl(fd_key, EVIOCGVERSION, &version);
	printf("evdev driver version is 0x%x: %d.%d.%d\n",version, version>>16, (version>>8) & 0xff, version & 0xff);

	while (1) {
		ret = read(fd_key, &ev, sizeof(struct input_event));
		if (ret < 0) {
			printf("read event error!\n");
			exit(1);
		}
		if (ev.type == EV_KEY){
			printf("type %d,code %d, value %d\n", ev.type, ev.code, ev.value);
			if(ev.value){
				pthread_mutex_lock(&key_lock);
				key_cond=1;
				pthread_mutex_unlock(&key_lock);
				if(salesman_num >=15){
					salesman_num = 6;
				}else{
					salesman_num++;
				}
				cmd[4] = salesman_num;
				write(fd,cmd,9);
			}
			//cmd [4] = salesman_num;
			//write(fd,cmd,9);
		}
	}
}

 

#CC = /usr/local/arm/4.3.2/bin/arm-linux-gcc
#CFLAGS = -I/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include

CC = /usr/local/arm/arm-2009q3/bin/arm-none-linux-gnueabi-gcc
CFLAGS = -I/usr/local/arm/arm-2009q3/arm-none-linux-gnueabi/libc/usr/include

uarttest: uarttest.c
	$(CC) $(CFLAGS) -o uarttest uarttest.c -static -lpthread
clean:
	rm -rf uarttest

  

 

上一篇:stm32 不使用MircoLib情况下使用printf方法


下一篇:HI3518环境搭建