IIO: Add 4-byte unsigned reads into generic-buffer example
[deliverable/linux.git] / drivers / staging / iio / Documentation / generic_buffer.c
index bf553356fdada818bf537165753f77e57210e260..40d0ecac047f5a4511ae99a4fb250d2a8ebb3196 100644 (file)
@@ -18,6 +18,8 @@
  *
  */
 
+#define _GNU_SOURCE
+
 #include <unistd.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <string.h>
 #include <poll.h>
 #include <endian.h>
+#include <getopt.h>
+#include <inttypes.h>
 #include "iio_utils.h"
 
 /**
  * size_from_channelarray() - calculate the storage size of a scan
- * @channels: the channel info array
- * @num_channels: size of the channel info array
+ * @channels:          the channel info array
+ * @num_channels:      number of channels
  *
  * Has the side effect of filling the channels[i].location values used
  * in processing the buffer output.
@@ -58,22 +62,22 @@ int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
 void print2byte(int input, struct iio_channel_info *info)
 {
        /* First swap if incorrect endian */
-
        if (info->be)
                input = be16toh((uint16_t)input);
        else
                input = le16toh((uint16_t)input);
 
-       /* shift before conversion to avoid sign extension
-          of left aligned data */
+       /*
+        * Shift before conversion to avoid sign extension
+        * of left aligned data
+        */
        input = input >> info->shift;
        if (info->is_signed) {
                int16_t val = input;
                val &= (1 << info->bits_used) - 1;
                val = (int16_t)(val << (16 - info->bits_used)) >>
                        (16 - info->bits_used);
-               printf("%05f  ", val,
-                      (float)(val + info->offset)*info->scale);
+               printf("%05f ", ((float)val + info->offset)*info->scale);
        } else {
                uint16_t val = input;
                val &= (1 << info->bits_used) - 1;
@@ -83,39 +87,49 @@ void print2byte(int input, struct iio_channel_info *info)
 /**
  * process_scan() - print out the values in SI units
  * @data:              pointer to the start of the scan
- * @infoarray:         information about the channels. Note
+ * @channels:          information about the channels. Note
  *  size_from_channelarray must have been called first to fill the
  *  location offsets.
- * @num_channels:      the number of active channels
+ * @num_channels:      number of channels
  **/
 void process_scan(char *data,
-                 struct iio_channel_info *infoarray,
+                 struct iio_channel_info *channels,
                  int num_channels)
 {
        int k;
        for (k = 0; k < num_channels; k++)
-               switch (infoarray[k].bytes) {
+               switch (channels[k].bytes) {
                        /* only a few cases implemented so far */
                case 2:
-                       print2byte(*(uint16_t *)(data + infoarray[k].location),
-                                  &infoarray[k]);
+                       print2byte(*(uint16_t *)(data + channels[k].location),
+                                  &channels[k]);
+                       break;
+               case 4:
+                       if (!channels[k].is_signed) {
+                               uint32_t val = *(uint32_t *)
+                                       (data + channels[k].location);
+                               printf("%05f ", ((float)val +
+                                                channels[k].offset)*
+                                      channels[k].scale);
+
+                       }
                        break;
                case 8:
-                       if (infoarray[k].is_signed) {
+                       if (channels[k].is_signed) {
                                int64_t val = *(int64_t *)
                                        (data +
-                                        infoarray[k].location);
-                               if ((val >> infoarray[k].bits_used) & 1)
-                                       val = (val & infoarray[k].mask) |
-                                               ~infoarray[k].mask;
+                                        channels[k].location);
+                               if ((val >> channels[k].bits_used) & 1)
+                                       val = (val & channels[k].mask) |
+                                               ~channels[k].mask;
                                /* special case for timestamp */
-                               if (infoarray[k].scale == 1.0f &&
-                                   infoarray[k].offset == 0.0f)
-                                       printf(" %lld", val);
+                               if (channels[k].scale == 1.0f &&
+                                   channels[k].offset == 0.0f)
+                                       printf("%" PRId64 " ", val);
                                else
                                        printf("%05f ", ((float)val +
-                                                        infoarray[k].offset)*
-                                              infoarray[k].scale);
+                                                        channels[k].offset)*
+                                              channels[k].scale);
                        }
                        break;
                default:
@@ -130,10 +144,7 @@ int main(int argc, char **argv)
        unsigned long timedelay = 1000000;
        unsigned long buf_len = 128;
 
-
        int ret, c, i, j, toread;
-
-       FILE *fp_ev;
        int fp;
 
        int num_channels;
@@ -149,7 +160,7 @@ int main(int argc, char **argv)
        int noevents = 0;
        char *dummy;
 
-       struct iio_channel_info *infoarray;
+       struct iio_channel_info *channels;
 
        while ((c = getopt(argc, argv, "l:w:c:et:n:")) != -1) {
                switch (c) {
@@ -192,7 +203,7 @@ int main(int argc, char **argv)
        asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
        if (trigger_name == NULL) {
                /*
-                * Build the trigger name. If it is device associated it's
+                * Build the trigger name. If it is device associated its
                 * name is <device_name>_dev[n] where n matches the device
                 * number found above
                 */
@@ -217,7 +228,7 @@ int main(int argc, char **argv)
         * Parse the files in scan_elements to identify what channels are
         * present
         */
-       ret = build_channel_array(dev_dir_name, &infoarray, &num_channels);
+       ret = build_channel_array(dev_dir_name, &channels, &num_channels);
        if (ret) {
                printf("Problem reading scan element information\n");
                printf("diag %s\n", dev_dir_name);
@@ -236,7 +247,7 @@ int main(int argc, char **argv)
                goto error_free_triggername;
        }
        printf("%s %s\n", dev_dir_name, trigger_name);
-       /* Set the device trigger to be the data rdy trigger found above */
+       /* Set the device trigger to be the data ready trigger found above */
        ret = write_sysfs_string_and_verify("trigger/current_trigger",
                                        dev_dir_name,
                                        trigger_name);
@@ -254,7 +265,7 @@ int main(int argc, char **argv)
        ret = write_sysfs_int("enable", buf_dir_name, 1);
        if (ret < 0)
                goto error_free_buf_dir_name;
-       scan_size = size_from_channelarray(infoarray, num_channels);
+       scan_size = size_from_channelarray(channels, num_channels);
        data = malloc(scan_size*buf_len);
        if (!data) {
                ret = -ENOMEM;
@@ -269,7 +280,7 @@ int main(int argc, char **argv)
 
        /* Attempt to open non blocking the access dev */
        fp = open(buffer_access, O_RDONLY | O_NONBLOCK);
-       if (fp == -1) { /*If it isn't there make the node */
+       if (fp == -1) { /* If it isn't there make the node */
                printf("Failed to open %s\n", buffer_access);
                ret = -errno;
                goto error_free_buffer_access;
@@ -300,16 +311,16 @@ int main(int argc, char **argv)
                }
                for (i = 0; i < read_size/scan_size; i++)
                        process_scan(data + scan_size*i,
-                                    infoarray,
+                                    channels,
                                     num_channels);
        }
 
-       /* Stop the ring buffer */
+       /* Stop the buffer */
        ret = write_sysfs_int("enable", buf_dir_name, 0);
        if (ret < 0)
                goto error_close_buffer_access;
 
-       /* Disconnect from the trigger - just write a dummy name.*/
+       /* Disconnect the trigger - just write a dummy name. */
        write_sysfs_string("trigger/current_trigger",
                        dev_dir_name, "NULL");
 
This page took 0.029061 seconds and 5 git commands to generate.