查看: 3341|回复: 0

【盈鹏飞EVB335x工控板试用】以按键控制LED亮灭

[复制链接]
  • TA的每日心情
    奋斗
    2023-5-10 20:09
  • 签到天数: 1742 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2018-6-12 22:59:09 | 显示全部楼层 |阅读模式
    分享到:
        在EVB335x工控板上,既有按键又有LED灯,我们可以通过编程来用按键控制LED,如使用UP键来点亮LED,用DOWN键来熄灭LED;用LEFT键来打开蜂鸣器,用RIGHT键来关闭蜂鸣器等。
    实现LED灯控制的程序如下,经编译、下载和运行即可见到预期效果。
    snap.jpg
    完成编译


    程序代码如下:
    1. #include <stdio.h>
    2. #include <linux/input.h>
    3. #include <fcntl.h>
    4. #include <stdlib.h>
    5. #include <string.h>
    6. #include <unistd.h>
    7. #include <sys/ioctl.h>
    8. #include <linux/types.h>

    9. typedef struct input_event INPUT_EVENT;
    10. #define DEVICE_NAME "/dev/input/event0"

    11. int main(int argc, char **argv)
    12. {
    13.         int fd,fd1;
    14.         int num;
    15.         INPUT_EVENT event;
    16.         char buf;

    17.         fd1 = open("/sys/class/leds/user-led\:red/brightness",O_WRONLY);
    18.         if(fd1<0){
    19.                 printf("Can not open led device.");
    20.                 return 0;
    21.         }

    22.         fd = open( DEVICE_NAME , O_RDONLY, 0);
    23.         if (fd < 0)
    24.         {
    25.                 perror("Can't open button device...\n");
    26.                 return 0;
    27.         }

    28.         printf("Keybutton test.....\n");

    29.         while(1)
    30.         {
    31.                 num = read(fd, &event, sizeof(INPUT_EVENT));
    32.                 if (sizeof(INPUT_EVENT) != num)
    33.                 {
    34.                         printf("read data error\n");
    35.                         return 0;
    36.                 }

    37.                 if(event.type == EV_KEY)
    38.                 {
    39.                         switch (event.code)
    40.                         {
    41.                                 case KEY_UP:
    42.                                         printf("UP keybutton,the code is: %d",event.code);
    43.                                         buf = '1';                // LED on
    44.                                         write(fd1,&buf,1);
    45.                                         break;

    46.                                 case KEY_DOWN:
    47.                                         printf("DOWN  keybutton,the code is: %d",event.code);
    48.                                         buf = '0';                // LED off
    49.                                         write(fd1,&buf,1);
    50.                                         break;

    51.                                 case KEY_LEFT:
    52.                                         printf("LEFT  keybutton,the code is: %d",event.code);
    53.                                         break;

    54.                                 case KEY_RIGHT:
    55.                                         printf("RIGHT  keybutton,the code is: %d",event.code);
    56.                                         break;

    57.                                 case KEY_ENTER:
    58.                                         printf("ENTER  keybutton,the code is: %d",event.code);
    59.                                         break;

    60.                                 case KEY_ESC:
    61.                                         printf("ESC  keybutton,the code is: %d",event.code);
    62.                                         break;
    63.                         }

    64.                         // keybutton status
    65.                         if(event.value)
    66.                         {
    67.                                 printf("    press down\n");
    68.                         }
    69.                         else
    70.                         {
    71.                                 printf("    press up\n");
    72.                         }

    73.                         printf("\n");
    74.                 }

    75.         }
    76.         close(fd1);
    77.         return 0;
    78. }
    复制代码



    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-19 21:23 , Processed in 0.117292 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.