|
本帖最后由 @allen 于 2014-9-1 20:41 编辑
更多的基于cubieboard 的学习贴可搜:嵌入式学习
原文 :http://blog.csdn.net/lanmanck/article/details/8423669
因为我只用到了power 键输入,所以我删了其他部分
做成了按住 power 键 2秒就调用"shuttdown"命令 。 当然也可以改成一按就调用其他命令,这样有需要按键的人就不用去外接个电路,有个power键凑合着用
不用等长按6秒断电,长按2秒关机如下
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <time.h>
- #include <linux/input.h>
- struct input_event event;
- int main(int argc, char **argv)
- {
- char name[64]; /* RATS: Use ok, but could be better */
- char buf[256] = { 0, }; /* RATS: Use ok */
- unsigned char mask[EV_MAX/8 + 1]; /* RATS: Use ok */
- int version;
- int fd = 0;
- int rc;
- int i, j;
- char *tmp;
- long unsigned int p_time;
- long unsigned int r_time;
- char *type;
- #define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8)))
- if (argc > 1) {
- sprintf(name, "/dev/input/event%d", atoi(argv[1]));
- if ((fd = open(name, O_RDWR, 0)) >= 0) {
- printf("%s: open, fd = %d\n", name, fd);
- for (i = 0; i < LED_MAX; i++) {
- event.time.tv_sec = time(0);
- event.time.tv_usec = 0;
- event.type = EV_LED;
- event.code = i;
- event.value = 0;
- write(fd, &event, sizeof(event));
- }
-
- while ((rc = read(fd, &event, sizeof(event))) > 0) {
- /* printf("%lu.%06lu type 0x%04x; code 0x%04x;"
- " value 0x%08x; ",
- event.time.tv_sec,
- event.time.tv_usec,
- event.type, event.code, event.value);*/
- switch (event.type) {
- case EV_KEY:
- if (event.code > BTN_MISC) {
- printf("Button %d %s",
- event.code & 0xff,
- event.value ? "press" : "release");
- } else {
- printf("Key %d (0x%x) %s",
- event.code & 0xff,
- event.code & 0xff,
- event.value ? "press\n" : "release\n");
- if(event.value){
-
- type= "press";
- }
- else {
-
- type= "release";
- }
- if(type=="press"){
-
- p_time = time(0);
- printf("p_time = %lu\n",p_time);
- }
- else{
-
- r_time = time(0);
- printf("r_time = %lu\n",r_time);
- printf("r_time-p_time = %lu\n",r_time - p_time);
- if((r_time - p_time)>=2){
- printf("\n");
- system ("echo \n");
- system ("echo turn off leds!\n");
- system ("echo 0 > /sys/class/leds/blue\:ph21\:led1/brightness");
- system("echo 0 > /sys/class/leds/orange\:ph20\:led2/brightness");
- system("echo 0 > /sys/class/leds/white\:ph11\:led3/brightness");
- system("echo 0 > /sys/class/leds/green\:ph07\:led4/brightness ");
- system ("/sbin/shutdown -h now");
- }
- }
- // printf(" value 0x%08x; ",event.value);
-
- }
- break;
- case EV_FF: printf("FF"); break;
- break;
- }
-
- }
- printf("rc = %d, (%s)\n", rc, strerror(errno));
- close(fd);
- }
- }
- return 0;
- }
复制代码 $arm-linux-gnueabihf-gcc guanji.c -o guanji
拷贝到板子上,
#./guanji 1 &
1 是因为power 对应 /dev/input/event1 , 红外 对应/dev/input/event0 ,我板子的lubuntu server 是这样的,其他应该有变化吧,
cat /dev/input/even1 按下按键,看有没有反映就是不是知道对应
|
|