先来说下原理:用c程序调用ioctl驱动来调用键盘上的显示灯。使其按照我们的程序来实现亮灭。再来说下我要做这个功能的初衷:现在有一种lifi技术,也就是各国都在研空的利用可见光上网的技术。这种技术的优点和缺点在网上一搜一大把,我就不在这里缀述了。我要做在是:实现这种可见光并将其接收,在实现通讯的基础上,解决数据上行的难题。好了,不多说,看代码。
#include
#include
#include
#include
#include <sys/stat.h>
#include <linux/kd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#define ERROR -1
int fd; /* File descriptor for console (/dev/tty/) */
void sighandler(int signum);
void main()
{
int i;
/* To be used as the fd in ioctl(). */
if ((fd = open("/dev/console", O_NOCTTY)) == ERROR)
{
perror("open");
exit(ERROR);
}
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
signal(SIGQUIT, sighandler);
signal(SIGTSTP, sighandler);
printf("w00w00!\n\n");
printf("To exit hit Control-C.\n");
int j=1;
int k=0;
int time=0.02*1000000;//0.02为时间秒
while (1)
{
i = 0x07;//0x02 为num锁定键,0x01 为scroll锁定键,0x03 为num和scroll锁定键,0x04 为caps锁定键,0x05 为caps和scroll锁定键,0x06 为num和caps锁定键,0x07 为num和caps和scroll锁定键
j++;
k++;
if ((ioctl(fd, KDSETLED, i)) == ERROR) {
perror("ioctl");
close(fd);
//exit(ERROR);
}
usleep(time);
if ((ioctl(fd, KDSETLED, 0x0)) == ERROR)
{
perror("ioctl");
close(fd);
//exit(ERROR);
}
usleep(time);
if(k>=10000){
printf("%d\n",j);
k=0;
}
/*for (i = 0x01; i <= 0x04; i++)
{
// We do this because there is no LED for 0x03.
if (i == 0x03) continue;
if (i == 0x04) continue;
usleep(500000);
//usleep(1);
if ((ioctl(fd, KDSETLED, i)) == ERROR) {
perror("ioctl");
close(fd);
exit(ERROR);
}
}*/
}
close(fd);
}
void sighandler(int signum)
{
/* Turn off all leds. No LED == 0x0. */
if ((ioctl(fd, KDSETLED, 0x0)) == ERROR)
{
perror("ioctl");
close(fd);
exit(ERROR);
}
printf("\nw00w00!\n");
close(fd);
exit(0);
}