* adding accel-neutral-settings API proposal master
authorDariusz Wiechecki <stranger@hp-laptop.(none)>
Mon, 25 Jan 2010 20:04:28 +0000 (22:04 +0200)
committerDariusz Wiechecki <stranger@hp-laptop.(none)>
Mon, 25 Jan 2010 20:04:28 +0000 (22:04 +0200)
include/accel-neutral-settings.h [new file with mode: 0644]
include/accel.h
src/accel-neutral-settings_p.c [new file with mode: 0644]
src/accel_p.c
src/global_p.c
src/global_p.h
tests/api_test.c [new file with mode: 0644]
tests/reading_test.c [new file with mode: 0644]

diff --git a/include/accel-neutral-settings.h b/include/accel-neutral-settings.h
new file mode 100644 (file)
index 0000000..7921cd8
--- /dev/null
@@ -0,0 +1,43 @@
+/* ============================================================================
+    Copyright (C) 2010 nomrasco
+    Nom Rasco <nomrasco@gmail.com>
+
+    This file is part of libnomaccel.
+
+    libnomaccel is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Lesser General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    libnomaccel is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public License
+    along with libnomaccel.  If not, see <http://www.gnu.org/licenses/>.
+============================================================================ */
+#ifndef _NOM_NEUTRAL_SETTINGS_H_
+#define _NOM_NEUTRAL_SETTINGS_H_
+#include "accel.h"
+
+typedef enum {
+       NOM_ACCEL_ZERO_NEUTRAL = 0,
+       NOM_ACCEL_LOCAL_NEUTRAL,
+       NOM_ACCEL_SYSTEM_NEUTRAL,
+       NOM_ACCEL_SETTINGS_NUMBER
+} NomAccelNeutralSetting;
+
+NomAccelNeutralSetting nom_accel_nsettings_get_current ();
+void  nom_accel_nsettings_set_current (NomAccelNeutralSetting settings);
+
+void  nom_accel_nsettings_set_neutral_pitch ( NomAccelNeutralSetting settings,
+                                              angle new_pitch );
+void  nom_accel_nsettings_set_neutral_roll  ( NomAccelNeutralSetting settings,
+                                              angle new_roll );
+
+angle nom_accel_nsettings_get_neutral_pitch (NomAccelNeutralSetting settings);
+angle nom_accel_nsettings_get_neutral_roll  (NomAccelNeutralSetting settings);
+
+
+#endif
index 9863530..38bd385 100644 (file)
 typedef int   value;
 typedef float angle;
 
 typedef int   value;
 typedef float angle;
 
+typedef struct {
+       value x;
+       value y;
+       value z;
+       angle pitch;
+       angle roll;
+} NomAccelCache;
+
 /* simplest retrieving functions: x,y,z,pitch,roll */
 value nom_accel_get_x ();
 value nom_accel_get_y ();
 /* simplest retrieving functions: x,y,z,pitch,roll */
 value nom_accel_get_x ();
 value nom_accel_get_y ();
@@ -37,5 +45,11 @@ void  nom_accel_get_all ( value* x, value* y, value* z,
                           angle* pitch, angle* roll     );
 
 
                           angle* pitch, angle* roll     );
 
 
+void    nom_accel_cache_get  (NomAccelCache* put_to);
+#define nom_accel_cache_get_x(cache)     ((cache)->x)
+#define nom_accel_cache_get_y(cache)     ((cache)->y)
+#define nom_accel_cache_get_z(cache)     ((cache)->z)
+#define nom_accel_cache_get_pitch(cache) ((cache)->pitch)
+#define nom_accel_cache_get_roll(cache)  ((cache)->roll)
 
 #endif
 
 #endif
diff --git a/src/accel-neutral-settings_p.c b/src/accel-neutral-settings_p.c
new file mode 100644 (file)
index 0000000..98dc872
--- /dev/null
@@ -0,0 +1,85 @@
+/* ============================================================================
+    Copyright (C) 2010 nomrasco
+    Nom Rasco <nomrasco@gmail.com>
+
+    This file is part of libnomaccel.
+
+    libnomaccel is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Lesser General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    libnomaccel is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public License
+    along with libnomaccel.  If not, see <http://www.gnu.org/licenses/>.
+============================================================================ */
+#include "../accel-neutral-settings.h"
+#include "global_p.h"
+
+NomAccelNeutralSetting nom_accel_nsettings_get_current ()
+{
+       return _accel_settings_current;
+}
+
+void  nom_accel_nsettings_set_current (NomAccelNeutralSetting settings)
+{
+       if (settings >= NOM_ACCEL_SETTINGS_NUMBER) return;
+       _accel_settings_current = settings;
+}
+
+void  nom_accel_nsettings_set_neutral_pitch ( NomAccelNeutralSetting settings,
+                                              angle new_pitch )
+{
+       if (settings >= NOM_ACCEL_SETTINGS_NUMBER) return;
+       if (settings <= NOM_ACCEL_ZERO_NEUTRAL) return;
+       _accel_settings[settings].pitch_neutral = new_pitch;
+
+       if (settings == NOM_ACCEL_SYSTEM_NEUTRAL) _accel_nsettings_save_system ();
+}
+
+void  nom_accel_nsettings_set_neutral_roll  ( NomAccelNeutralSetting settings,
+                                              angle new_roll )
+{
+       if (settings >= NOM_ACCEL_SETTINGS_NUMBER) return;
+       if (settings <= NOM_ACCEL_ZERO_NEUTRAL) return;
+       _accel_settings[settings].roll_neutral = new_roll;
+
+       if (settings == NOM_ACCEL_SYSTEM_NEUTRAL) _accel_nsettings_save_system ();
+}
+
+angle nom_accel_nsettings_get_neutral_pitch (NomAccelNeutralSetting settings)
+{
+       if (settings >= NOM_ACCEL_SETTINGS_NUMBER) return;
+       return _accel_settings[settings].pitch_neutral;
+}
+
+angle nom_accel_nsettings_get_neutral_roll  (NomAccelNeutralSetting settings)
+{
+       if (settings >= NOM_ACCEL_SETTINGS_NUMBER) return;
+       return _accel_settings[settings].roll_neutral;
+}
+
+void
+_accel_nsettings_read_system ()
+{
+       // load system settings from conf file (if not exists, set to zero settings)
+       // TODO: pseudo-implement this
+
+       // set system settings as a current local settings
+       _accel_settings[NOM_ACCEL_LOCAL_NEUTRAL] = _accel_settings[NOM_ACCEL_SYSTEM_NEUTRAL];
+
+       // set zero settings
+       _accel_settings[NOM_ACCEL_ZERO_NEUTRAL].pitch_offset = 0.0;
+       _accel_settings[NOM_ACCEL_ZERO_NEUTRAL].roll_offset = 0.0;
+}
+
+void
+_accel_nsettings_save_system ()
+{
+       // set system settings as a current local settings
+       // TODO: pseudo-implement this
+}
index 467757e..3836c50 100644 (file)
@@ -79,4 +79,13 @@ nom_accel_get_all (value* x, value* y, value* z,
            *x = _accel_proxy.x;
            *y = _accel_proxy.y;
            *z = _accel_proxy.z;
            *x = _accel_proxy.x;
            *y = _accel_proxy.y;
            *z = _accel_proxy.z;
-}
\ No newline at end of file
+}
+
+void nom_accel_get_cache (NomAccelCache* target)
+{
+       nom_accel_get_all ( &(target->x),
+                           &(target->y),
+                           &(target->z),
+                           &(target->pitch),
+                           &(target->roll) );
+}
index 14d5289..eccaee2 100644 (file)
 #include "global_p.h"
 #include <stdlib.h>
 
 #include "global_p.h"
 #include <stdlib.h>
 
+NomAccelProxy     _accel_proxy = {0};
+NomAccelNeutral   _accel_settings[NOM_ACCEL_SETTINGS_NUMBER] = {{0.0,0.0}};
+NomAccelNeutralSetting _accel_settings_current = NOM_ACCEL_LOCAL_NEUTRAL;
+
 void
 _nom_accel_proxy_init ()
 {
 void
 _nom_accel_proxy_init ()
 {
index 4841845..03ddd51 100644 (file)
@@ -35,6 +35,12 @@ struct _NomAccelProxy {
        angle roll;
 };
 
        angle roll;
 };
 
+typedef struct _NomAccelNeutral NomAccelNeutral;
+struct _NomAccelNeutral {
+       angle pitch_neutral;
+       angle roll_neutral;
+};
+
 void _accel_proxy_init ();
 void _accel_proxy_finit ();
 void _accel_read_real_values ();
 void _accel_proxy_init ();
 void _accel_proxy_finit ();
 void _accel_read_real_values ();
@@ -42,7 +48,13 @@ void _accel_recalculate_pitch ();
 void _accel_recalculate_roll ();
 #define _accel_recalculate_pitch_roll() _accel_recalculate_pitch(); _accel_recalculate_roll()
 
 void _accel_recalculate_roll ();
 #define _accel_recalculate_pitch_roll() _accel_recalculate_pitch(); _accel_recalculate_roll()
 
+void _accel_nsettings_read_system ();
+void _accel_nsettings_save_system ();
+
 const char* const _accel_device_file_name = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
 const char* const _accel_device_file_name = "/sys/class/i2c-adapter/i2c-3/3-001d/coord";
-NomAccelProxy     _accel_proxy = {0};
+const char* const _accel_system_setting_file_name = "/etc/nomaccel/settings";
+extern NomAccelProxy     _accel_proxy;
+extern NomAccelNeutral   _accel_settings[NOM_ACCEL_SETTINGS_NUMBER];
+extern NomAccelNeutralSetting _accel_settings_current;
 
 #endif
 
 #endif
diff --git a/tests/api_test.c b/tests/api_test.c
new file mode 100644 (file)
index 0000000..16a56fc
--- /dev/null
@@ -0,0 +1,3 @@
+int nom_accel_is_stationary (&x, &y, &z);
+int nom_accel_get_face_up_down (&x, &y, &z);
+int nom_accel_get_top_edge (&x, &y, &z);
\ No newline at end of file
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