OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /* | |
6 * TSL2563 light sensor functional test | |
7 * | |
8 * When I spoof an I2C device on i2c adapter 0, I see these files show | |
9 * up in /sys/devices/pci0000:00/0000:00:02.0/i2c-0/0-0029/iio/device0: | |
10 * -r--r--r-- 1 root root 4096 Mar 25 17:21 adc0 | |
11 * -r--r--r-- 1 root root 4096 Mar 25 17:21 adc1 | |
12 * -rw-r--r-- 1 root root 4096 Mar 25 17:21 calib0 | |
13 * -rw-r--r-- 1 root root 4096 Mar 25 17:21 calib1 | |
14 * lrwxrwxrwx 1 root root 0 Mar 25 17:21 device -> ../../../0-0029 | |
15 * -r--r--r-- 1 root root 4096 Mar 25 17:21 lux | |
16 * drwxr-xr-x 2 root root 0 Mar 25 17:21 power | |
17 * lrwxrwxrwx 1 root root 0 Mar 25 17:21 subsystem -> ../../../../../../../c
lass/iio | |
18 * -rw-r--r-- 1 root root 4096 Mar 25 17:21 uevent | |
19 * This is done with: | |
20 * echo tsl2563 0x29 > /sys/class/i2c-adapter/i2c-0/new_device | |
21 * | |
22 * Raw and calibrated ADC data come from adc{0|1} and calib{0|1}, but | |
23 * it is probably best to use the lux file because it gives the calibrated | |
24 * ADC translated to luminescence. | |
25 */ | |
26 | |
27 #include <stdio.h> | |
28 #include <stdlib.h> | |
29 | |
30 #define _GNU_SOURCE | |
31 #include <getopt.h> | |
32 | |
33 | |
34 /* | |
35 * The "lux" sysfs file gives luminescence data with a "%d" printf. | |
36 * I do not yet know the range of expected values. | |
37 */ | |
38 int lux_read(FILE *lux_fp, int *luxp) | |
39 { | |
40 int rval; | |
41 | |
42 rval = fscanf(lux_fp, "%d", luxp); | |
43 | |
44 if (rval != 1) { | |
45 printf("Failed to read an integer from the lux file.\n"); | |
46 return -1; | |
47 } | |
48 return 0; | |
49 } | |
50 | |
51 void repeated_lux_read(FILE *lux_fp, int us_period) | |
52 { | |
53 int lux; | |
54 | |
55 while (lux_read(lux_fp, &lux) == 0) { | |
56 printf("lux: %d\n", lux); | |
57 usleep(us_period); | |
58 } | |
59 } | |
60 | |
61 struct option long_options[] = { | |
62 {"file", 1, 0, 'f'}, | |
63 {"repeat", 0, 0, 'r'}, | |
64 {"period", 1, 0, 'p'}, | |
65 {"help", 0, 0, 'h'}, | |
66 {0, 0, 0, 0} | |
67 }; | |
68 | |
69 int opt_repeat = 0; | |
70 int opt_period_usecs = 500000; | |
71 char *opt_file = "/sys/devices/pci0000:00/0000:00:02.0/i2c-0/0-0029/iio/device0/
lux"; | |
72 | |
73 void print_help(void) | |
74 { | |
75 printf("Usage: tsl2563tst [options]\n"); | |
76 printf(" will read the tsl2563 light sensor sysfs file.\n"); | |
77 printf(" options:\n"); | |
78 printf(" --file <file>: explicitly specify the sysfs light sensor file\n"); | |
79 printf(" --repeat: repeatedly read light sensor data\n"); | |
80 printf(" --period <usecs>: set the period between repeated reads\n"); | |
81 printf(" --help: print this help message\n"); | |
82 } | |
83 | |
84 | |
85 int main(int argc, char *argv[]) | |
86 { | |
87 int c; | |
88 | |
89 while ((c = getopt_long(argc, argv, "hf:p:r", long_options, NULL)) != -1) { | |
90 switch (c) { | |
91 case 'h': | |
92 print_help(); | |
93 exit(0); | |
94 case 'f': | |
95 opt_file = optarg; | |
96 break; | |
97 case 'p': | |
98 opt_period_usecs = atoi(optarg); | |
99 break; | |
100 case 'r': | |
101 opt_repeat = 1; | |
102 break; | |
103 case '?': | |
104 print_help(); | |
105 exit(1); | |
106 default: | |
107 printf("Unknown parameter 0x%x.\n", c); | |
108 exit(1); | |
109 } | |
110 } | |
111 | |
112 FILE *lux_fp; | |
113 lux_fp = fopen(opt_file, "r"); | |
114 if (lux_fp == NULL) { | |
115 perror("open device file"); | |
116 fprintf(stderr, "Cannot open %s.\n", opt_file); | |
117 fprintf(stderr, "Perhaps the tsl2563 module is not loaded.\n" | |
118 "Or perhaps the kernel needs to be told where to find the device.\n" | |
119 "(eg, 'echo tsl2563 0x29 > /sys/class/i2c-adapter/i2c-0/new_device'\n"); | |
120 exit(1); | |
121 } | |
122 | |
123 if (opt_repeat) { | |
124 repeated_lux_read(lux_fp, opt_period_usecs); | |
125 } else { | |
126 int lux; | |
127 if (lux_read(lux_fp, &lux) != 0) | |
128 exit(1); | |
129 printf("lux: %d\n", lux); | |
130 } | |
131 return 0; | |
132 } | |
OLD | NEW |