]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/iio/Documentation/lis3l02dqbuffersimple.c
Staging: iio: Documentation/lis3l02dqbuffersimple.c: duplicated include
[net-next-2.6.git] / drivers / staging / iio / Documentation / lis3l02dqbuffersimple.c
1 /* Industrialio test ring buffer with a lis3l02dq acceleromter
2  *
3  * Copyright (c) 2008 Jonathan Cameron
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * Assumes suitable udev rules are used to create the dev nodes as named here.
10  */
11
12 #include <dirent.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <errno.h>
16 #include <stdint.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/dir.h>
20
21 #include <linux/types.h>
22 #include "iio_util.h"
23
24 static const char *ring_access = "/dev/iio/lis3l02dq_ring_access";
25 static const char *ring_event = "/dev/iio/lis3l02dq_ring_event";
26 static const char *device_name = "lis3l02dq";
27 static const char *trigger_name = "lis3l02dq-dev0";
28 static int NumVals = 3;
29 static int scan_ts = 1;
30 static int RingLength = 128;
31
32 /*
33  * Could get this from ring bps, but only after starting the ring
34  * which is a bit late for it to be useful
35  */
36 int size_from_scanmode(int numVals, int timestamp)
37 {
38         if (numVals && timestamp)
39                 return 16;
40         else if (timestamp)
41                 return 8;
42         else
43                 return numVals*2;
44 }
45
46 int main(int argc, char **argv)
47 {
48         int i, j, k, toread;
49         FILE *fp_ev;
50         int fp;
51         char *data;
52         size_t read_size;
53         struct iio_event_data dat;
54
55         char    *BaseDirectoryName,
56                 *TriggerDirectoryName,
57                 *RingBufferDirectoryName;
58
59         BaseDirectoryName = find_type_by_name(device_name, "device");
60         if (BaseDirectoryName == NULL) {
61                 printf("Failed to find the %s \n", device_name);
62                 return -1;
63         }
64         TriggerDirectoryName = find_type_by_name(trigger_name, "trigger");
65         if (TriggerDirectoryName == NULL) {
66                 printf("Failed to find the %s\n", trigger_name);
67                 return -1;
68         }
69         RingBufferDirectoryName = find_ring_subelement(BaseDirectoryName,
70                                                        "ring_buffer");
71         if (RingBufferDirectoryName == NULL) {
72                 printf("Failed to find ring buffer\n");
73                 return -1;
74         }
75
76         if (write_sysfs_string_and_verify("trigger/current_trigger",
77                                           BaseDirectoryName,
78                                           (char *)trigger_name) < 0) {
79                 printf("Failed to write current_trigger file \n");
80                 return -1;
81         }
82
83         /* Setup ring buffer parameters */
84         if (write_sysfs_int("length", RingBufferDirectoryName,
85                             RingLength) < 0) {
86                 printf("Failed to open the ring buffer length file \n");
87                 return -1;
88         }
89
90         /* Enable the ring buffer */
91         if (write_sysfs_int("ring_enable", RingBufferDirectoryName, 1) < 0) {
92                 printf("Failed to open the ring buffer control file \n");
93                 return -1;
94         };
95
96         data = malloc(size_from_scanmode(NumVals, scan_ts)*RingLength);
97         if (!data) {
98                 printf("Could not allocate space for usespace data store\n");
99                 return -1;
100         }
101
102         /* Attempt to open non blocking the access dev */
103         fp = open(ring_access, O_RDONLY | O_NONBLOCK);
104         if (fp == -1) { /*If it isn't there make the node */
105                 printf("Failed to open %s\n", ring_access);
106                 return -1;
107         }
108         /* Attempt to open the event access dev (blocking this time) */
109         fp_ev = fopen(ring_event, "rb");
110         if (fp_ev == NULL) {
111                 printf("Failed to open %s\n", ring_event);
112                 return -1;
113         }
114
115         /* Wait for events 10 times */
116         for (j = 0; j < 10; j++) {
117                 read_size = fread(&dat, 1, sizeof(struct iio_event_data),
118                                   fp_ev);
119                 switch (dat.id) {
120                 case IIO_EVENT_CODE_RING_100_FULL:
121                         toread = RingLength;
122                         break;
123                 case IIO_EVENT_CODE_RING_75_FULL:
124                         toread = RingLength*3/4;
125                         break;
126                 case IIO_EVENT_CODE_RING_50_FULL:
127                         toread = RingLength/2;
128                         break;
129                 default:
130                         printf("Unexpecteded event code\n");
131                         continue;
132                 }
133                 read_size = read(fp,
134                                  data,
135                                  toread*size_from_scanmode(NumVals, scan_ts));
136                 if (read_size == -EAGAIN) {
137                         printf("nothing available \n");
138                         continue;
139                 }
140
141                 for (i = 0;
142                      i < read_size/size_from_scanmode(NumVals, scan_ts);
143                      i++) {
144                         for (k = 0; k < NumVals; k++) {
145                                 __s16 val = *(__s16 *)(&data[i*size_from_scanmode(NumVals, scan_ts)
146                                                              + (k)*2]);
147                                 printf("%05d ", val);
148                         }
149                         printf(" %lld\n",
150                                *(__s64 *)(&data[(i+1)*size_from_scanmode(NumVals, scan_ts)
151                                                 - sizeof(__s64)]));
152                 }
153         }
154
155         /* Stop the ring buffer */
156         if (write_sysfs_int("ring_enable", RingBufferDirectoryName, 0) < 0) {
157                 printf("Failed to open the ring buffer control file \n");
158                 return -1;
159         };
160
161         /* Disconnect from the trigger - writing something that doesn't exist.*/
162         write_sysfs_string_and_verify("trigger/current_trigger",
163                                       BaseDirectoryName, "NULL");
164         free(BaseDirectoryName);
165         free(TriggerDirectoryName);
166         free(RingBufferDirectoryName);
167         free(data);
168
169         return 0;
170 }