* adding accel-neutral-settings API proposal
[libnomaccel] / tests / reading_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/mman.h>
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 #include <time.h>
10
11
12
13 static const char* const dev = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
14 static int x, y, z;
15 static int i = 0;
16 static int number = 4000;
17
18 static fpos_t dev_start;
19
20 char*
21 next_space(char* ptr) {
22         while(*ptr != ' ') ++ptr;
23         return ptr + 1;
24 }
25
26 static unsigned char offset = 0x000f;
27 static unsigned char buffer[256];
28 static const unsigned char space = ' ';
29 static const unsigned char minus = '-';
30 static int values[3];
31 #define asci_2_digit(asci) (asci & offset)
32 #define add_next_value(val,_ptr) *val=(*val<<3)+(*val<<1)+asci_2_digit(*_ptr)
33 #define is_space(_ptr) !(*_ptr ^ space)
34 #define is_minus(_ptr) !(*_ptr ^ minus)
35
36 static void
37 read_values() {
38
39         if(minus == *ptr) { ++ptr; x = -1 * ((*ptr)-offset); ++ptr; }
40         while(space != *ptr) {
41                 x = (x << 3) + (x << 1);
42                 x += *ptr - offset;
43                 ++ptr;
44         }
45
46         ++ptr;
47         if(minus == *ptr) { ++ptr; y = -1 * ((*ptr)-offset); ++ptr; }
48         while(space != *ptr) {
49                 y = (y << 3) + (y << 1);;
50                 y += *ptr - offset;
51                 ++ptr;
52         }
53
54         ++ptr;
55         if(minus == *ptr) { ++ptr; z = -1 * ((*ptr)-offset); ++ptr; }
56         while(space != *ptr) {
57                 z = (z << 3) + (z << 1);
58                 z += *ptr - offset;
59                 ++ptr;
60         }
61 }
62
63 static void inline driver_read() {
64
65 }
66
67 int
68 main (int argc, char* argv[])
69 {
70         clock_t t0, t1;
71
72         printf ("Starting application...\n");
73
74         printf ("Standard way of reading...\n");
75         t0 = clock();
76         for (i = 0; i < number; ++i)
77         {
78                 FILE *fd;
79                 fd = fopen(dev, "r");
80                 if(0 == fd)
81                         break;
82                 fscanf(fd,"%i %i %i", &x, &y, &z);
83                 fclose(fd);
84
85                 //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
86         }
87         t1 = clock();
88         printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
89
90         printf ("No closing file...\n");
91
92         FILE *fd;
93         fd = fopen(dev, "r");
94         t0 = clock();
95         for (i = 0; i < number; ++i)
96         {
97                 fflush(fd);
98                 fscanf(fd,"%i %i %i", &x, &y, &z);
99                 fseek (fd ,0 ,SEEK_SET);
100                 //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
101         }
102         t1 = clock();
103         fclose(fd);
104         printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
105
106         printf ("No closing file (setpos)...\n");
107         fd = fopen(dev, "r");
108         fgetpos (fd, &dev_start);
109         t0 = clock();
110         for (i = 0; i < number; ++i)
111         {
112                 fflush(fd);
113                 fscanf(fd,"%i %i %i", &x, &y, &z);
114                 fsetpos (fd, &dev_start);
115                 //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
116         }
117         t1 = clock();
118         fclose(fd);
119         printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
120
121         printf ("Linux kernel...\n");
122         int de = open(dev, O_RDONLY);
123         t0 = clock();
124         for (i = 0; i < number; ++i)
125         {
126                 lseek(de, 0, SEEK_SET);
127                 read(de, buffer, 255);
128                 sscanf((char*)buffer,"%i %i %i", &x, &y, &z);
129                 //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
130         }
131         t1 = clock();
132         close(de);
133         printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
134
135         printf ("Linux kernel (own conversion)...\n");
136         de = open(dev, O_RDONLY);
137         t0 = clock();
138         for (i = 0; i < number; ++i)
139         {
140                 lseek(de, 0, SEEK_SET);
141                 read(de, buffer, 255);
142                 read_values ();
143                 //printf("%.3i. Coords: (x,y,z)=(%i, %i, %i)\n", i+1, x, y, z);
144         }
145         t1 = clock();
146         close(de);
147         printf("clock ticks pased: %i\n", (int)(t1-t0) / 1000);
148
149
150         printf ("Application stoped.\n");
151         return 0;
152 }