1 /* Industrialio test ring buffer with a lis3l02dq acceleromter
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Assumes suitable udev rules are used to create the dev nodes as named here.
17 #include <sys/types.h>
21 #include <linux/types.h>
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;
33 * Could get this from ring bps, but only after starting the ring
34 * which is a bit late for it to be useful
36 int size_from_scanmode(int numVals, int timestamp)
38 if (numVals && timestamp)
46 int main(int argc, char **argv)
53 struct iio_event_data dat;
55 char *BaseDirectoryName,
56 *TriggerDirectoryName,
57 *RingBufferDirectoryName;
59 BaseDirectoryName = find_type_by_name(device_name, "device");
60 if (BaseDirectoryName == NULL) {
61 printf("Failed to find the %s \n", device_name);
64 TriggerDirectoryName = find_type_by_name(trigger_name, "trigger");
65 if (TriggerDirectoryName == NULL) {
66 printf("Failed to find the %s\n", trigger_name);
69 RingBufferDirectoryName = find_ring_subelement(BaseDirectoryName,
71 if (RingBufferDirectoryName == NULL) {
72 printf("Failed to find ring buffer\n");
76 if (write_sysfs_string_and_verify("trigger/current_trigger",
78 (char *)trigger_name) < 0) {
79 printf("Failed to write current_trigger file \n");
83 /* Setup ring buffer parameters */
84 if (write_sysfs_int("length", RingBufferDirectoryName,
86 printf("Failed to open the ring buffer length file \n");
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");
96 data = malloc(size_from_scanmode(NumVals, scan_ts)*RingLength);
98 printf("Could not allocate space for usespace data store\n");
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);
108 /* Attempt to open the event access dev (blocking this time) */
109 fp_ev = fopen(ring_event, "rb");
111 printf("Failed to open %s\n", ring_event);
115 /* Wait for events 10 times */
116 for (j = 0; j < 10; j++) {
117 read_size = fread(&dat, 1, sizeof(struct iio_event_data),
120 case IIO_EVENT_CODE_RING_100_FULL:
123 case IIO_EVENT_CODE_RING_75_FULL:
124 toread = RingLength*3/4;
126 case IIO_EVENT_CODE_RING_50_FULL:
127 toread = RingLength/2;
130 printf("Unexpecteded event code\n");
135 toread*size_from_scanmode(NumVals, scan_ts));
136 if (read_size == -EAGAIN) {
137 printf("nothing available \n");
142 i < read_size/size_from_scanmode(NumVals, scan_ts);
144 for (k = 0; k < NumVals; k++) {
145 __s16 val = *(__s16 *)(&data[i*size_from_scanmode(NumVals, scan_ts)
147 printf("%05d ", val);
150 *(__s64 *)(&data[(i+1)*size_from_scanmode(NumVals, scan_ts)
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");
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);