summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Shyti <andi@etezian.org>2013-05-18 23:32:12 +0200
committerAndi Shyti <andi@etezian.org>2013-05-18 23:32:12 +0200
commitc0f84b0437615ba26307d93018a0d0e3f8bbb9e4 (patch)
tree46506472e7f6964f1056244ee3b838948423ced6
parenta12ebfe966b625d714547f57689847c787ef96b1 (diff)
misc: apds990x_als: added apds990x proximity test
apds990x is a combined proximity/als sensor. The test provides a basic test for the als part. The following are the testing steps: - power on the sensor - read 10 times the lux value on 1 second interval Signed-off-by: Andi Shyti <andi@etezian.org>
-rw-r--r--drivers/misc/apds990x_als.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/drivers/misc/apds990x_als.c b/drivers/misc/apds990x_als.c
new file mode 100644
index 0000000..abad5e8
--- /dev/null
+++ b/drivers/misc/apds990x_als.c
@@ -0,0 +1,153 @@
+/*
+ * apds990x ambient light sensor testing module
+ * Copyright (C) 2013 Andi Shyti <andi@etezian.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#define MAX_N 100
+#define ENABLE "1"
+#define DISABLE "0"
+
+struct device {
+ unsigned int n;
+
+ char *power;
+ char *als_raw;
+};
+
+struct device apds990x_dev = {
+ .n = 10,
+ .power = "/sys/class/i2c-adapter/i2c-0012/12-0039/power_state",
+ .als_raw = "/sys/class/i2c-adapter/i2c-0012/12-0039/lux0_raw",
+};
+
+int write_file(char *fname, const char *val)
+{
+ int fd;
+ ssize_t ret;
+
+ fd = open(fname, O_WRONLY);
+ if (fd < 0) {
+ fprintf(stderr, "%s: %s\n", fname, strerror(errno));
+ return errno;
+ }
+
+ ret = write(fd, val, 1);
+ if (ret != 1) {
+ fprintf(stderr, "%s: %s\n", fname, strerror(errno));
+ close(fd);
+ return errno;
+ }
+
+ fd = close(fd);
+ if (fd < 0)
+ fprintf(stderr, "%s: %s\n", fname, strerror(errno));
+
+ return errno;
+}
+
+int read_als(void)
+{
+ int i, fd;
+ ssize_t ret;
+ char val[6];
+
+ fd = open(apds990x_dev.als_raw, O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "%s: %s\n", apds990x_dev.als_raw,
+ strerror(errno));
+ return errno;
+ }
+
+ for (i = 0; i < apds990x_dev.n; i++) {
+ ret = read(fd, val, 5);
+ if (!ret) {
+ fprintf(stderr, "%s: %s\n",
+ apds990x_dev.als_raw, strerror(errno));
+ break;
+ }
+
+ val[ret] = '\0';
+ printf("%s\n", val);
+ sleep(1);
+ }
+
+ return errno;
+}
+
+void print_usage(char *name)
+{
+ printf("Usage: %s [-n <number>]\n", name);
+ printf("\n");
+ printf(" -n <number> - number of readings on the device\n");
+ printf(" -h, --help - print help menu\n");
+}
+
+int parse_cmdline(int argc, char *argv[])
+{
+ int i;
+ unsigned long int n;
+
+ for (i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "-n")) {
+ n = strtoul(argv[++i], NULL, 10);
+ /* check for overflow */
+ if (errno)
+ return errno;
+
+ apds990x_dev.n = ((n > MAX_N) || (n <= 0)) ?
+ MAX_N : (unsigned int) n;
+ } else if (!strcmp(argv[i], "-h") ||
+ !strcmp(argv[i], "--help")) {
+ print_usage(argv[0]);
+ exit(EXIT_SUCCESS);
+ } else {
+ fprintf(stderr, "%s: %s\n\n",
+ argv[i], strerror(EINVAL));
+ print_usage(argv[0]);
+ return EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ int err;
+
+ if (argc > 1) {
+ err = parse_cmdline(argc, argv);
+ if (err)
+ return err;
+ }
+
+ err = write_file(apds990x_dev.power, ENABLE);
+ if (err)
+ return err;
+
+ read_als();
+ err = write_file(apds990x_dev.power, DISABLE);
+
+ return err;
+}