求助高手帮我注释下这个LED测试代码,有doc附件可下载方便注释,先谢谢啦
led测试代码.rar
(4.74 KB)
在pxa270目录下新建文件夹,命名为led_test,在该文件夹下利用gedit编辑器,编写一个Led驱动测试代码led_test.c;
/************************************************
*************************************************/
#include <stdio.h>
#include <fcntl.h>
//#include <linux/kernel.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <time.h>
#include <stdbool.h>
#define LED_DEV "/dev/xsb_Led"
bool bstop=false;
int fd;
void display_menu()
{
printf(" *****Choice Menu*********\n");
printf(" [0] Open Device\n");
printf(" [1] Left Shift\n");
printf(" [2] Right Shift\n");
printf(" [T] Stop Shift\n");
printf(" [C] Close Device\n");
printf(" [x] Exit Test\n");
printf(" ***********************\n");
printf(" Please input your choise: ");
}
void *Led_Right_Shift(void *data)
{
unsigned char ledvalue=0x01;
unsigned char tmp;
int i=0;
while(1) {
for(i=0;i<8;i++){
if(bstop) {
bstop=false;
return;
}
tmp=~ledvalue;
write(fd,&tmp,1);
sleep(1);
if(i<7)
ledvalue=ledvalue<<1;
else
ledvalue=0x01;
}
}
}
void *Led_Left_Shift(void *data)
{
unsigned char ledvalue=0x80;
unsigned char tmp;
int i=0;
while(1) {
for(i=0;i<8;i++) {
if(bstop) {
bstop=false;
return;
}
tmp=~ledvalue;
write(fd,&tmp,1);
sleep(1);
if(i<7)
ledvalue=ledvalue>>1;
else
ledvalue=0x80;
}
}
}
int main(int argc, char **argv)
{
fd=-1;
pthread_t th_shift=0;
char ch=0x00;
display_menu();
void *retval;
while(1) {
ch=getchar();
switch(ch) {
case '0':
if(fd>0)
printf("##Led Device has been open##%d \n",fd);
else {
fd = open(LED_DEV, O_RDWR);
if(fd < 0)
printf("####LED Device open Fail####\n");
else
printf("####LED Device open Success####%d \n",fd);
}
display_menu();
break;
case '1':
if(fd) {
if(th_shift) {
bstop=true;
pthread_join(th_shift,&retval);
}
bstop=false;
pthread_create(&th_shift,NULL,Led_Right_Shift,NULL);
}
else
printf("The device is not open!!");
display_menu();
break;
case '2':
if(fd) {
if(th_shift) {
bstop=true;
pthread_join(th_shift,&retval);
}
bstop=false;
pthread_create(&th_shift,NULL,Led_Left_Shift,NULL);
}
else
printf("The device is not open!!");
display_menu();
break;
case 't':
case 'T':
bstop=true;
display_menu();
break;
case 'c':
case 'C':
if(fd)
close(fd);
display_menu();
bstop=false;
break;
case 'x':
case 'X':
exit(1);
default:
break;
}
}
return(0);
}
[ 本帖最后由 jizhumizhu 于 2010-12-6 10:34 编辑 ]