Interface for an LIS302 accelerometer, using the SPI interface

Dependents:   LIS302_HelloWorld mbed_line_camera

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LIS302.h Source File

LIS302.h

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