Добавлен модуль ядра и приложение для работы с трехосевыми магнитометрами.
[lede-packages.git] / 3-axis / src / 3-axis.c
diff --git a/3-axis/src/3-axis.c b/3-axis/src/3-axis.c
new file mode 100644 (file)
index 0000000..cff0640
--- /dev/null
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <time.h>
+#include <sys/times.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <assert.h>
+#include <linux/input.h>
+
+int read_all(int fd, char *buf, int count)
+{
+    int n_read = 0;
+    while (n_read != count) {
+        int result = read(fd, buf + n_read, count - n_read);
+        if (result < 0)
+            return result;
+        else if (result == 0)
+            return n_read;
+        n_read += result;
+    }
+    return n_read;
+}
+
+int main(int argc, char *argv[])
+{
+    int fd;
+    struct input_event ev;
+    __s32 x_value,
+        y_value,
+        z_value;
+    __u8 x_got=0,
+        y_got=0,
+        z_got=0;    
+    int timeout=10;
+    int start;
+
+    assert(16 == sizeof(struct input_event));
+
+    if (argc != 2) {
+        fprintf(stderr, "missing /dev/input/XXX\n");
+        return 1;
+    }
+
+    start=time(0);
+
+    if ((fd = open(argv[1], O_RDONLY)) == -1) {
+        perror("open");
+        return 1;
+    }
+
+    while (! (x_got && y_got && z_got)) {
+
+        int ret = read(fd, (char *) &ev, sizeof(struct input_event));
+        if (ret != sizeof(struct input_event)) {
+            fprintf(stderr, "ret == %d\n", ret);
+            perror("read");
+            return 1;
+        }
+        if (ev.type==3 && ev.code==16) {
+         x_value=ev.value;
+         x_got=1;
+        }
+        if (ev.type==3 && ev.code==17) {
+          y_value=ev.value;
+          y_got=1;
+        }
+        if (ev.type==3 && ev.code==10) {
+          z_value=ev.value;
+          z_got=1;
+        }
+        if (time(0) > start+timeout) break;
+    }
+
+    close(fd);
+
+    if (x_got && y_got && z_got) {
+      printf("%d %d %d\n",x_value,y_value,z_value);
+    } else {
+      fprintf(stderr, "data not aquired until timeout");
+      perror("timeout");
+      return 1;
+    }
+
+    return 0;
+}