exist"Shell scriptsIn the article "Implementation of the flashing of the keyboard LED light", we have felt the joy of control and step by stephardwareApproaching, this time we areLinuxThe system calls are used in C language to implement this function. This involves the application layer timer and ioctl system call to control the state of the keyboard LED light.
Regarding the application layer timer, it needs to involve a signal mechanism, which includes two types: alarm alarm clock and timer timer. They and signal mechanism are explained as follows (both are automatic loops, that is, no processing is required.functionSet the timer timeout setting again):
After running the man alarm command, there are instructions for using the function, and its function prototype is as follows:
#include <>
unsigned int alarm(unsigned int seconds);
When the set time is up, a SIGALRM signal will be sent, and the corresponding signal processing function needs to be processed together.
After running the man setitimer command, there are relevant instructions. The function prototypes to be used this time are as follows:
#include <sys/>
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
Which is the set timer type, which corresponds to the following:
ITIMER_REAL: Real-time timer, sending SIGALRM signal;
ITIMER_VIRTUAL: Apply the process execution time timer and send SIGVTALRM signal;
ITIMER_PROF: The application process execution and kernel interaction time timer sends a SIGPROF signal.
As can be seen from the above, there can only be 3 timers at the same time, corresponding to 3 types. So how to deal with the above signals? The relevant function prototypes for executing the man signal command are as follows:
#include <>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
In addition to matching the timer, this signal function can also capture other signals. You can view it by executing the man 7 signal command.
OK, that's all about the timer section. Regarding keyboard LED light control, we need to use the ioctl system call function of the TTY device. This function is a standard character device interface (can not be implemented), and its interface prototype (execute man ioctl) is as follows:
#include <sys/>
int ioctl(int d, int request, ...);
where d is the corresponding device description symbol, request is the command code, ... is the parameter value corresponding to the command code. For Keyboard LED, we use the command code of KDSETLED, and the corresponding device is /dev/console, here d corresponds to the return value of the device open, and... corresponding to the passed value, the undefined state is 0xff, and the three lights (Number lock, Caps Lock, Scroll Lock) in the upper right corner of the keyboard are all lit up, corresponding to 0x07.
The following is the source code for implementing C programming to control the flashing of the PC keyboard LED light:
#include <>
#include <sys/>
#include <>
#include <linux/>
#include <sys/>
void slam_alarm_handler(int a)
{
int tty = open("/dev/console", 0), led;
if(tty<3)
{
perror("open:");
exit(0);
}
led = 0x07;
if(ioctl(tty,KDSETLED,led)>0)
perror("ioctl led on:");
sleep(1);
led = 0xff;
if(ioctl(tty,KDSETLED,led)>0)
perror("ioctl led off:");
close(tty);
}
int main(void)
{
/* The struct itimerval
* it_interval:means interval everytime after first time
* it_value:means the first time interval
* The ITIMER_REAL timer come with SIGALRM signal
*/
struct itimerval t;
t.it_interval.tv_usec = 0;
t.it_interval.tv_sec = 2;
t.it_value.tv_usec = 0;
t.it_value.tv_sec = 2;
if(setitimer(ITIMER_REAL, &t, NULL) < 0)
{
printf("Setitimer failed.\n");
exit(-1);
}
signal(SIGALRM, slam_alarm_handler);
while(1)
{
sleep(2);
}
exit(0);
}
The corresponding Makefile content is as follows:
all:
gcc -o timer_keyboard_led_flash timer_keyboard_led_flash.c
clean:
rm -rf timer_keyboard_led_flash
The corresponding source code file directory tree is as follows:
/home/xinu/xinu/c_cpp/timer_keyboard_led_flash/
├── Makefile
└── timer_keyboard_led_flash.c
After compiling the generated file, root user permission is required during execution.ubuntuNeed nextsudo ./timer_keyboard_led_flashCommands to execute.