Kazushi Mukaiyama / LS302i2c
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LIS302i2c.h Source File

LIS302i2c.h

00001 /* mbed LIS302 Accelerometer Library
00002  * Copyright (c) 2008-2010, sford, cstyles, wreynolds
00003  * modified for i2c version by Kazushi Mukaiyama 2010
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #ifndef MBED_LIS302I2C_H
00025 #define MBED_LIS302I2C_H
00026  
00027 #include "mbed.h"
00028 
00029 /** An interface for the LIS302 triple axis I2C accelerometer
00030  *
00031  * @code
00032  * // Print out the Z axis acceleration
00033  * #include "mbed.h"
00034  * #include "LIS302i2c.h"
00035  * 
00036  * LIS302i2c acc(p28, p27); // sda, scl
00037  * 
00038  * int main() {
00039  *     while(1) {
00040  *         printf("Z axis acceleration = %.2f\n", acc.getZ());
00041  *         wait(0.1);              
00042  *     }
00043  * }
00044  * @endcode
00045  */
00046 class LIS302i2c  {
00047 public:
00048 
00049     /** Create an LIS302 interface, connected to the specified pins
00050      *
00051      * @param sda I2C data line pin
00052      * @param scl I2C clock line pin
00053      */
00054     LIS302i2c(int addr, PinName sda, PinName scl);
00055     
00056     float x, y, z;
00057 
00058     void update();
00059     
00060     /** Read the X axis acceleration
00061      *
00062      * @return A floating-point value representing acceleration in g
00063      */    
00064     float getX();
00065 
00066     /** Read the Y axis acceleration
00067      *
00068      * @return A floating-point value representing acceleration in g
00069      */    
00070     float getY();
00071 
00072     /** Read the Z axis acceleration
00073      *
00074      * @return - A floating-point value representing acceleration in g
00075      */    
00076     float getZ();
00077 
00078     /** Select the range of the accelerometer
00079      *
00080      * @param range 0 = 2g, 1 = 8g
00081      */        
00082     void range(int g);
00083 
00084     /** Configure the minima and maxima for the axes to linearise the readings
00085      *
00086      * @param maxx float defining the maximum X value
00087      * @param minx float defining the minimum X value
00088      * @param maxy float defining the maximum Y value
00089      * @param miny float defining the minimum Y value
00090      * @param maxz float defining the maximum Z value
00091      * @param minz float defining the minimum Z value
00092      */        
00093     void calibrate(float maxx = 1, float minx = -1, float maxy = 1, float miny = -1, float maxz = 1, float minz = -1);
00094     
00095 private:
00096     I2C _i2c;
00097     int _addr;
00098     char _cmd[6];
00099     
00100     char whoami();
00101     char status();
00102     
00103     float _factor;
00104     float _maxx, _minx;
00105     float _maxy, _miny;
00106     float _maxz, _minz;        
00107 };
00108 
00109 #endif