S3C6410 按键控制LED应用程序 ---源代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define ON 0
#define OFF 1

int k_fd;
int l_fd;

int led_status[4] = {1,1,1,1};

void change_led_status(int num,int status)
{
	led_status[num] = status;
}

void send_cmd(int num)
{
	int ret;
	if(led_status[num] == ON)
	{
		ret = ioctl(l_fd,(num+1)*10 + OFF);
		if(ret < 0)
		{
			printf("send cmd error!\n");
			return;
		}
		led_status[num] = OFF;		
	}
	else{

		ret = ioctl(l_fd,(num+1)*10 + ON);
		if(ret < 0)
                {
                        printf("send cmd error!\n");
                        return;
                }
                led_status[num] = ON;
	}

	return;
}

int choose_key(char *tmp)
{
	int i;
	int num;

	num = -1;

	for(i = 0; i < 6; i++)
	{
		if(tmp[i] == '1')
		  num = i;
	}	

	return num;
}

int ctrl_led(int led_key_num)
{
	switch(led_key_num){

	case 0:
		send_cmd(led_key_num);			
		return 0;
	case 1: 
		send_cmd(led_key_num);
		return 1;
	case 2:
		send_cmd(led_key_num);
		return 2;
	case 3:
		send_cmd(led_key_num);
		return 3;
	case 4:
		return 4;

	default:
		return -1;
	}

}

int main(int argc,char ** argv)
{

	int count;
	int num;
	int ret;
//	char key[6] = {'0','0','0','0','0','0'};

	k_fd = open("/dev/keys",0);
	if(k_fd < 0)
	{
		printf("open keys dev error!\n");
		return -1;
	}

	l_fd = open("/dev/leds",0);
	if(l_fd < 0)
	{
		printf("open leds dev error!\n");
		return -1;
	}

	while(1)
	{
		char current_keys[6];
		count = read(k_fd,current_keys,sizeof(current_keys[0])*6);
		if(count < 0)
		{
			perror("read keys:");
		}
//		printf("read bytes is :%d\n",count);
/*
		if(read(fd,current_keys,sizeof current_keys) != sizeof current_keys)
		{
			perror("read buttons:");
			//exit(1);
		}
*/
//		printf("%s\n",current_keys);	
		num = choose_key(current_keys);
		if(num < 0)
		{
			continue;
		}
		
		ret = ctrl_led(num);
		if(ret == 4)
		{
			sleep(1);
			break;	
		}
			
	}

	close(k_fd);
	close(l_fd);

	return 0;
}

你可能感兴趣的:(S3C6410 按键控制LED应用程序 ---源代码)