* adding accel-neutral-settings API proposal
[libnomaccel] / tests / reading_test.c
diff --git a/tests/reading_test.c b/tests/reading_test.c
new file mode 100644 (file)
index 0000000..f6877b5
--- /dev/null
@@ -0,0 +1,152 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <time.h>
+
+
+
+static const char* const dev = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
+static int x, y, z;
+static int i = 0;
+static int number = 4000;
+
+static fpos_t dev_start;
+
+char*
+next_space(char* ptr) {
+       while(*ptr != ' ') ++ptr;
+       return ptr + 1;
+}
+
+static unsigned char offset = 0x000f;
+static unsigned char buffer[256];
+static const unsigned char space = ' ';
+static const unsigned char minus = '-';
+static int values[3];
+#define asci_2_digit(asci) (asci & offset)
+#define add_next_value(val,_ptr) *val=(*val<<3)+(*val<<1)+asci_2_digit(*_ptr)
+#define is_space(_ptr) !(*_ptr ^ space)
+#define is_minus(_ptr) !(*_ptr ^ minus)
+
+static void
+read_values() {
+
+       if(minus == *ptr) { ++ptr; x = -1 * ((*ptr)-offset); ++ptr; }
+       while(space != *ptr) {
+               x = (x << 3) + (x << 1);
+               x += *ptr - offset;
+               ++ptr;
+       }
+
+       ++ptr;
+       if(minus == *ptr) { ++ptr; y = -1 * ((*ptr)-offset); ++ptr; }
+       while(space != *ptr) {
+               y = (y << 3) + (y << 1);;
+               y += *ptr - offset;
+               ++ptr;
+       }
+
+       ++ptr;
+       if(minus == *ptr) { ++ptr; z = -1 * ((*ptr)-offset); ++ptr; }
+       while(space != *ptr) {
+               z = (z << 3) + (z << 1);
+               z += *ptr - offset;
+               ++ptr;
+       }
+}
+
+static void inline driver_read() {
+
+}
+
+int
+main (int argc, char* argv[])
+{
+       clock_t t0, t1;
+
+       printf ("Starting application...\n");
+
+       printf ("Standard way of reading...\n");
+       t0 = clock();
+       for (i = 0; i < number; ++i)
+       {
+               FILE *fd;
+               fd = fopen(dev, "r");
+               if(0 == fd)
+                       break;
+               fscanf(fd,"%i %i %i", &x, &y, &z);
+               fclose(fd);
+
+               //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
+       }
+       t1 = clock();
+       printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
+
+       printf ("No closing file...\n");
+
+       FILE *fd;
+       fd = fopen(dev, "r");
+       t0 = clock();
+       for (i = 0; i < number; ++i)
+       {
+               fflush(fd);
+               fscanf(fd,"%i %i %i", &x, &y, &z);
+               fseek (fd ,0 ,SEEK_SET);
+               //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
+       }
+       t1 = clock();
+       fclose(fd);
+       printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
+
+       printf ("No closing file (setpos)...\n");
+       fd = fopen(dev, "r");
+       fgetpos (fd, &dev_start);
+       t0 = clock();
+       for (i = 0; i < number; ++i)
+       {
+               fflush(fd);
+               fscanf(fd,"%i %i %i", &x, &y, &z);
+               fsetpos (fd, &dev_start);
+               //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
+       }
+       t1 = clock();
+       fclose(fd);
+       printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
+
+       printf ("Linux kernel...\n");
+       int de = open(dev, O_RDONLY);
+       t0 = clock();
+       for (i = 0; i < number; ++i)
+       {
+               lseek(de, 0, SEEK_SET);
+               read(de, buffer, 255);
+               sscanf((char*)buffer,"%i %i %i", &x, &y, &z);
+               //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
+       }
+       t1 = clock();
+       close(de);
+       printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
+
+       printf ("Linux kernel (own conversion)...\n");
+       de = open(dev, O_RDONLY);
+       t0 = clock();
+       for (i = 0; i < number; ++i)
+       {
+               lseek(de, 0, SEEK_SET);
+               read(de, buffer, 255);
+               read_values ();
+               //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
+       }
+       t1 = clock();
+       close(de);
+       printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
+
+
+       printf ("Application stoped.\n");
+       return 0;
+}
\ No newline at end of file