CMAccelerometer Class Reference

Managing accelerometers. More...

Public Member Functions

 CMAccelerometer (const char *file)
 ~CMAccelerometer ()
int getValue (int dimension)
int getValue (char dimension)
void check ()

Private Member Functions

int readAll (int fd, struct input_event *buf)

Private Attributes

char * file
int x
int y
int z

Detailed Description

Header for managing the accelerometers.

Author:
Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>

Code derived from openmoko.org ( accelerometer data retrieval ).

See also:
http://wiki.openmoko.org/wiki/Accelerometer_data_retrieval

Codigo is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Codigo 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Examples:

TestAccelerometer.cpp.


Constructor & Destructor Documentation

CMAccelerometer::CMAccelerometer ( const char *  file  ) 

Constructor.

Parameters:
[in] file The file of the device.

00035 {
00036   this->file = (char*)malloc((strlen(file)+1)*sizeof(char));
00037   strcpy(this->file,file);
00038   this->x=this->y=this->z=0;
00039 }

CMAccelerometer::~CMAccelerometer (  ) 

Destructor.

00042 {
00043   free(this->file);
00044 }


Member Function Documentation

void CMAccelerometer::check (  ) 

Updates the acelerometer values.

Examples:
TestAccelerometer.cpp.

00062 {
00063   int fd,ret;
00064   struct input_event ev;
00065 
00066   if ((fd = open(this->file, O_RDONLY)) == -1)
00067   {
00068     exit(EXIT_FAILURE);
00069   }
00070   
00071   // Retrieve data
00072   while(ev.type != 3)
00073   {
00074     ret = this->readAll(fd,&ev);
00075     if (ret != sizeof(struct input_event))
00076     {
00077       exit(EXIT_FAILURE);
00078     }
00079     
00080     switch(ev.code)
00081     {
00082       case 0:
00083         this->x=ev.value;
00084         break;
00085       case 1:
00086         this->y=ev.value;
00087         break;
00088       case 2:
00089         this->z=ev.value;
00090         break;
00091     }
00092     
00093   }
00094   
00095   close(fd);
00096 }

int CMAccelerometer::getValue ( char  dimension  ) 

Get the accelerometer value.

Parameters:
[in] dimension Dimension value.
Returns:
The accelerometer value.

00116 {
00117   switch(dimension)
00118   {
00119     case 'x':
00120       return this->x;
00121       break;
00122     case 'y':
00123       return this->y;
00124       break;
00125     case 'z':
00126       return this->z;
00127       break;
00128   }  
00129   return -1;
00130 }

int CMAccelerometer::getValue ( int  dimension  ) 

Get the accelerometer value.
Y(0),X(1) => MacBook
X(0),Y(1),Z(2) => OpenMoko

Parameters:
[in] dimension Dimension value.
Returns:
The accelerometer value.
Examples:
TestAccelerometer.cpp.

00099 {
00100   switch(dimension)
00101   {
00102     case 0:
00103       return this->x;
00104       break;
00105     case 1:
00106       return this->y;
00107       break;
00108     case 2:
00109       return this->z;
00110       break;
00111   }  
00112   return -1;
00113 }

int CMAccelerometer::readAll ( int  fd,
struct input_event *  buf 
) [private]

Returns all the values of accelerometers.

Parameters:
[in] fd The descriptor of accelerometer device.
[out] buf The result in format of input_event(struct).
Returns:
The number of bytes readed, -1 if error.

00047 {
00048   int count = sizeof(struct input_event);
00049   int n_read = 0;
00050     while (n_read != count) {
00051         int result = read(fd, buf + n_read, count - n_read);
00052         if (result < 0)
00053             return result;
00054         else if (result == 0)
00055             return n_read;
00056         n_read += result;
00057     }
00058     return n_read;
00059 }


Field Documentation

char* CMAccelerometer::file [private]

The sys file for the accelerometers device.

int CMAccelerometer::x [private]

The X value.

int CMAccelerometer::y [private]

The Y value.

int CMAccelerometer::z [private]

The Z value.

 All Data Structures Functions Variables
Generated on Mon Sep 12 07:59:09 2011 for API MokArdu by  doxygen 1.6.3