附件二 led测试源码/*FILENAME:led_test.c
*AUTHOR:Michael Li
*DATE:2013-06-03
*DESCRIPTION:the test of the led driver
* */
#include<stdio.h>
#include<fcntl.h>
#define DEVICENAME "/dev/led"
#define IOCTL_LED_ON 1
#define IOCTL_LED_OFF 0
int main(int argc, char **argv)
{
FILE *fd;
int led_no = 0;
fd = (FILE *)open(DEVICENAME,0);
if(argc < 3)
{
printf("The arguments you input are wrong\n");
printf("USE:\n");
printf("\t\t./program\tled-number\ton/off\n");
return -1;
}
//led_no = strtoul(argv[1],0,0) -1;//its wrong to set like this
led_no = strtoul(argv[1],0,0);//operate the led_1 by input 1 and led_2 by input 2
printf("111111\n");
if(!strcmp(argv[2],"on"))//turn on the led
{
ioctl(fd,IOCTL_LED_ON,led_no);
printf("you want the %d on\n",led_no);
}
else if(!strcmp(argv[2],"off"))//turn off the led
{
ioctl(fd,IOCTL_LED_OFF,led_no);
}
else printf("error!\n");
return 0;
}
附件三:流水灯/*FILENAME:led_test.c
*AUTHOR:Michael Li
*DATE:2013-06-03
*DESCRIPTION:the test of the led driver,please refer to the label "usage:" below.
* */
#include<stdio.h>
#include<fcntl.h>
#include <signal.h>
//#include <time.h>
#include <sys/time.h>
#define DEVICENAME "/dev/led"
#define IOCTL_LED_ON 1
#define IOCTL_LED_OFF 0
void sigoutime();
void timer_init();
void delay(int time);
int time_1= 0;
int main(int argc, char **argv)
{
FILE *fd;
int led_no = 0;
int i = 0;
fd = (FILE *)open(DEVICENAME,0);
int delay_grade = 0;
timer_init();
usage:
if(argc < 3)
{
printf("The arguments you input are wrong\n");
printf("USE:\n");
printf("\t./program\t<right/left/both>\t<1/2/3>\n");
printf("\t3:slow;2:faster;1:fastest\n");
printf("\teg: ./led_flow\tleft\t1\n\tlet the led light start from left and the speed is the fastest\n");
return -1;
}
if(!strcmp(argv[2],"1"))
delay_grade = 1;//20ms
else if (!(argv[2],"2"))
delay_grade = 15;//300ms
else if(!(argv[2],"3"))
delay_grade = 50;//1s
else
delay_grade = 30;//600ms
if(!strcmp(argv[1],"left"))//turn on the led
{
for(i=0;i<40;i++)
{
if((i%2)!=0)
{
led_no = 1;
}
else
led_no = 2;
ioctl(fd,IOCTL_LED_ON,led_no);
printf("you want the %d on\n",led_no);
delay(delay_grade);
ioctl(fd,IOCTL_LED_OFF,led_no);
}
}
else if(!strcmp(argv[1],"right"))//turn off the led
{
for(i=0;i<20;i++)
{
if((i%2)==0)
{
led_no = 1;
}
else
led_no = 2;
ioctl(fd,IOCTL_LED_ON,led_no);
delay(delay_grade);
ioctl(fd,IOCTL_LED_OFF,led_no);
}
}
else if(!strcmp(argv[1]),"both")
{
for(i=0;i<50;i++)
{
ioctl(fd,IOCTL_LED_ON,1);
ioctl(fd,IOCTL_LED_ON,2);
delay(delay_grade);//delay 0.8s
ioctl(fd,IOCTL_LED_OFF,1);
ioctl(fd,IOCTL_LED_OFF,2);
delay(delay_grade);//delay 0.8s
}
}
else printf("error!\n");
close(fd);
return 0;
}
void delay(int time)
{
while(time_1<time);
time_1 = 0; //you must clean the time_1 every time you sign a time
return;
}
void timer_init() //内核定时器初始化函数
{
struct itimerval value; //(1)定义类型为itimerval的结构体变量
signal(SIGALRM, sigoutime);//注册信号的处理函数
value.it_value.tv_sec = 0;//秒——设置第一次运行sigroutine函数时间
value.it_value.tv_usec = 100000;//微妙——设置为0
value.it_interval.tv_sec = 0;//秒——设置间隔运行sigroutine函数的时间(实际定时)
value.it_interval.tv_usec = 20000;//微妙———设置为20ms
setitimer(ITIMER_REAL, &value, NULL); //(2)设置 “真实计时器“参数
}
void sigoutime()
{
time_1++;
return;
}