1、尝试一下应用发方式使用USER按键
2、监听用户按键USER设备节点状态,检测按键事件
在虚拟机的Ubuntut系统里,先建一个文件夹src,下面两个文件makefile和tl_key_test.c
tl_key_test.c脚本
- /* Copyright 2018 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved. */
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #define INADEQUATE_CONDItiONS 3
- typedef enum { KEY_CODE_NONE = 0, KEY_CODE_USER0, KEY_CODE_USER1 } KeyCode;
- /* Exit flag */
- volatile bool g_quit = false;
- /* Short option names */
- static const char g_shortopts [] = ":d:vh";
- /* Option names */
- static const struct option g_longopts [] = {
- { "device", required_argument, NULL, 'd' },
- { "version", no_argument, NULL, 'v' },
- { "help", no_argument, NULL, 'h' },
- { 0, 0, 0, 0 }
- };
- static void usage(FILE *fp, int argc, char **argv) {
- fprintf(fp,
- "Usage: %s [options]nn"
- "Options:n"
- " -d | --device Device n"
- " -v | --version Display version informationn"
- " -h | --help Show help contentnn"
- "", basename(argv[0]));
- }
- static void opt_parsing_err_handle(int argc, char **argv, int flag) {
- /* Exit if no input parameters are entered */
- int state = 0;
- if (argc < 2) {
- printf("No input parameters are entered, please check the input.n");
- state = -1;
- } else {
- /* Feedback Error parameter information then exit */
- if (optind < argc || flag) {
- printf("Error: Parameter parsing failedn");
- if (flag)
- printf("tunrecognized option '%s'n", argv[optind-1]);
- while (optind < argc) {
- printf("tunrecognized option '%s'n", argv[optind++]);
- }
- state = -1;
- }
- }
- if (state == -1) {
- printf("Tips: '-h' or '--help' to get helpnn");
- exit(2);
- }
- }
- void sig_handle(int arg) {
- g_quit = true;
- }
- static int check_button_pressed(int fd) {
- assert(fd >= 0);
- /* wait button being pressed or released. */
- fd_set input;
- FD_ZERO(&input);
- FD_SET(fd, &input);
- int ret = select(fd + 1, &input, NULL, NULL, NULL);
- if (ret < 0) {
- printf("%s", strerror(errno));
- return -1;
- }
- /* read event */
- struct input_event buf;
- if (read(fd, &buf, sizeof(struct input_event)) < 0) {
- printf("%s", strerror(errno));
- return -1;
- }
- /* Check the input_event value */
- switch (buf.code) {
- case KEY_PROG1:
- /* 1: pressed; 0: released */
- if (buf.value == 1)
- return KEY_CODE_USER0;
- break;
- case KEY_PROG2:
- if (buf.value == 1)
- return KEY_CODE_USER1;
- break;
- default:
- return KEY_CODE_NONE;
- break;
- }
- return KEY_CODE_NONE;
- }
- int main(int argc, char **argv) {
- int c = 0;
- int flag = 0;
- char *dev;
- /* Parsing input parameters */
- while ((c = getopt_long(argc, argv, g_shortopts, g_longopts, NULL)) != -1) {
- switch (c) {
- case 'd':
- dev = optarg;
- break;
- case 'v':
- /* Display the version */
- printf("version : 1.0n");
- exit(0);
- case 'h':
- usage(stdout, argc, argv);
- exit(0);
-
- default :
- flag = 1;
- break;
- }
- }
- opt_parsing_err_handle(argc, argv, flag);
- /* Ctrl+c handler */
- signal(SIGINT, sig_handle);
- int fd = open(dev, O_RDONLY);
- if (fd < 0) {
- printf("Error: Failed to open devicen");
- return INADEQUATE_CONDITIONS;
- }
- printf("Please press the key to test.n");
- while (!g_quit) {
- int key_code = check_button_pressed(fd);
- if (key_code < 0)
- continue;
- switch (key_code) {
- case KEY_CODE_USER0:
- printf("Key user0 pressed!n");
- break;
- case KEY_CODE_USER1:
- printf("Key user1 pressed!n");
- break;
- default:
- break;
- }
- }
- close(fd);
- return 0;
- }
复制代码
makefile脚本
- tl_key_test:tl_key_test.c
- $(CC) -Wall [ DISCUZ_CODE_513 ]lt; -o $@
- clean:
- rm -f tl_key_test *.o *~
- install:
- cp tl_key_test $(PREFIX)
复制代码
3、生成执行文件tl_key_test
- source /home/tronlong/ti-processor-sdk-linux-rt-am335x-evm-04.03.00.05/linux-devkit/environment-setup
- make
复制代码
4、 开发板eth0接好网线,获得的IP是 192.168.31.40 通过OpenSSH进行文件传输 命令行命令
- scp tl_key_test root@192.168.31.40:/
复制代码
5、通过开发板串口进入开发板的命行,可以看见传递过来的文件,并能执行
USER按键信息已完美监听到了。
0
|
|
|
|