|
楼主 |
发表于 2013-4-27 11:23:06
|
显示全部楼层
简化了下前面红外测试代码,并提交到github.com/cubieboard/buildroot-sunxi.git
commit 9d0aaa64c5bd034f1f53dd5889f7a77af0da40fd
Author: matson <matson@cubietech.com>
Date: Sat Apr 27 11:20:55 2013 +0800
cubieboard: add cb_tools package
Add ir-daemon example program. This allow you to trigger some other program via IR module. The instructions to do this are:
1. After system startup, insmod sunxi-ir.ko module
2. run 'ir-daemon'
3. Add a hook script or program, and the full name should be '/tools/ir-hook'
when you push the IR key, ir-daemon will start '/tools/ir-hook', pass the keycode and value to '/tools/ir-hook'
diff --git a/configs/cubieboard_defconfig b/configs/cubieboard_defconfig
index 82deac0..68bcb41 100644
--- a/configs/cubieboard_defconfig
+++ b/configs/cubieboard_defconfig
@@ -1,6 +1,6 @@
#
# Automatically generated make config: don't edit
-# Buildroot 2013.02-dirty Configuration
+# Buildroot 2013.02-00001-g49408af-dirty Configuration
#
BR2_HAVE_DOT_CONFIG=y
BR2_HOSTARCH_NEEDS_IA32_LIBS=y
@@ -988,6 +988,7 @@ BR2_PACKAGE_UTIL_LINUX_LIBBLKID=y
# BR2_PACKAGE_UTIL_LINUX_LOGIN_UTILS is not set
# BR2_PACKAGE_UTIL_LINUX_WRITE is not set
# BR2_PACKAGE_DSP_TOOLS is not set
+BR2_PACKAGE_CB_TOOLS=y
#
# Text editors and viewers
diff --git a/package/Config.in b/package/Config.in
index faee5c3..6aba157 100644
--- a/package/Config.in
+++ b/package/Config.in
@@ -800,6 +800,7 @@ source "package/supervisor/Config.in"
source "package/systemd/Config.in"
source "package/util-linux/Config.in"
source "package/dsp-tools/Config.in"
+source "package/cb_tools/Config.in"
endmenu
menu "Text editors and viewers"
diff --git a/package/cb_tools/Config.in b/package/cb_tools/Config.in
new file mode 100644
index 0000000..67943e9
--- /dev/null
+++ b/package/cb_tools/Config.in
@@ -0,0 +1,5 @@
+config BR2_PACKAGE_CB_TOOLS
+ bool "cb_tools"
+ help
+ tools for cubieboard
+
diff --git a/package/cb_tools/cb_tools.mk b/package/cb_tools/cb_tools.mk
new file mode 100644
index 0000000..20a7ffe
--- /dev/null
+++ b/package/cb_tools/cb_tools.mk
@@ -0,0 +1,28 @@
+CB_TOOLS_DIR := $(BUILD_DIR)/cb_tools
+
+$(CB_TOOLS_DIR)/.source :
+ mkdir -pv $(CB_TOOLS_DIR)
+ cp -rf package/cb_tools/src/* $(CB_TOOLS_DIR)
+ touch $@
+
+$(CB_TOOLS_DIR)/.configured : $(CB_TOOLS_DIR)/.source
+ touch $@
+
+
+cb_tools-binary: $(CB_TOOLS_DIR)/.configured
+ $(MAKE) CC="$(TARGET_CC)" -C $(CB_TOOLS_DIR)
+
+
+cb_tools: cb_tools-binary
+ $(MAKE) DESTDIR="$(TARGET_DIR)" -C $(CB_TOOLS_DIR) install
+ rm -rf $(CB_TOOLS_DIR)/.source $(CB_TOOLS_DIR)/.configured
+
+
+##############################################################
+#
+# Add our target
+#
+#############################################################
+ifeq ($(BR2_PACKAGE_CB_TOOLS),y)
+TARGETS += cb_tools
+endif
diff --git a/package/cb_tools/src/Makefile b/package/cb_tools/src/Makefile
new file mode 100644
index 0000000..f70f181
--- /dev/null
+++ b/package/cb_tools/src/Makefile
@@ -0,0 +1,26 @@
+
+#CROSS_COMPILE?=arm-linux-gnueabihf-
+#CC=$(CROSS_COMPILE)gcc
+#LD=$(CROSS_COMPILE)ld
+
+#ifneq "CROSS_SYSROOT" ""
+#CROSS_SYSROOT=$(shell cd ../../../../out/br/staging; pwd)
+#endif
+
+#CFLAGS+=--sysroot=$(CROSS_SYSROOT)
+
+ir-daemon:ir-daemon.c
+ $(CC) $(CFLAGS) -o ir-daemon -lsysfs ir-daemon.c
+
+
+all: ir-daemon
+
+install:
+ install ir-daemon $(DESTDIR)/bin
+
+clean:
+ rm -rf *.o ir-daemon
+
+.PHONY: all clean
+
+
diff --git a/package/cb_tools/src/ir-daemon.c b/package/cb_tools/src/ir-daemon.c
new file mode 100644
index 0000000..76a58c9
--- /dev/null
+++ b/package/cb_tools/src/ir-daemon.c
@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <linux/input.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/inotify.h>
+#include <sys/stat.h>
+#include <limits.h>
+#include <sys/errno.h>
+#include <sysfs/dlist.h>
+#include <sysfs/libsysfs.h>
+
+#define SUNXI_IR_NAME "sun4i-ir"
+#define HOOK_PROGRAM "/tools/ir-hook"
+
+int get_sunxi_ir_device(char *buf_name, size_t buf_len)
+{
+ struct sysfs_class *input_class = NULL;
+ struct dlist *input_devices = NULL;
+ struct sysfs_class_device *cls_dev = NULL;
+ struct sysfs_device *tdev = NULL;
+ struct sysfs_attribute *tattr = NULL;
+ int ret = -1;
+
+ input_class = sysfs_open_class("input");
+ input_devices = sysfs_get_class_devices(input_class);
+
+ dlist_for_each_data(input_devices, cls_dev, struct sysfs_class_device) {
+ tdev = sysfs_get_classdev_device(cls_dev);
+ if (tdev != NULL) {
+ tattr = sysfs_get_device_attr(tdev, "name");
+ if (tattr == NULL)
+ continue;
+ if (tattr->value == NULL)
+ continue;
+
+ if (strncmp(tattr->value, SUNXI_IR_NAME, 8)) {
+ continue;
+ }
+ memset(buf_name, buf_len, 0);
+ strncpy(buf_name, cls_dev->name, buf_len - 1);
+ ret = 0;
+
+ }
+ }
+
+ sysfs_close_class(input_class);
+ return ret;
+}
+
+
+int main (int argc, char **argv)
+{
+ int fd, rd, i, ret;
+ struct input_event ev[64];
+ char name_buf1[128];
+ char name_buf2[128];
+
+ ret = get_sunxi_ir_device(name_buf1, sizeof(name_buf1));
+ if (ret) {
+ printf("Please insmod sunxi-ir.ko\n");
+ return -1;
+ }
+
+ memset(name_buf2, sizeof(name_buf2), 0);
+ snprintf(name_buf2, sizeof(name_buf2), "/dev/input/%s", name_buf1);
+
+ printf("ir: %s\n", name_buf2);
+
+ if ((fd = open(name_buf2, O_RDONLY)) < 0) {
+ perror("evtest");
+ return 1;
+ }
+
+ daemon(0, 1);
+
+ while (1) {
+ rd = read(fd, ev, sizeof(struct input_event) * 64);
+
+ if (rd < (int) sizeof(struct input_event)) {
+ perror("read");
+ return 1;
+ }
+
+ for (i = 0; i < rd / sizeof(struct input_event); i++) {
+ if (ev.type == 1) {
+ if (ev.value == 1) {
+ printf("IR: %d DOWN\n", ev.code);
+ } else {
+ printf("IR: %d UP\n", ev.code);
+ }
+ if (!access(HOOK_PROGRAM, X_OK)) {
+ memset(name_buf1, sizeof(name_buf1), 0);
+ snprintf(name_buf1, sizeof(name_buf1), "%s %d %d",
+ HOOK_PROGRAM, ev.code, ev.value);
+ system(name_buf1);
+ }
+ }
+ }
+ }
+
+ close(fd);
+ return 0;
+}
编出固件后,在/tools/下面添加ir-hook程序或者脚本就可以了。ir-daemon在收到信号后,会启动/tools/ir-hook,并把code, value作为$1 $2参数传递给它。这样就可以通过红外控制各种东西了。 |
|