masaaki makabe / Mbed 2 deprecated BLE_API

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA845x.h Source File

MMA845x.h

Go to the documentation of this file.
00001 /**
00002  * @file    MMA845x.h
00003  * @brief   Device driver - MMA845x 3-axis accelerometer IC
00004  * @author  sam grove
00005  * @version 1.0
00006  * @see     http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf
00007  * @see     http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf
00008  * @see     http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8453Q.pdf
00009  *
00010  * Copyright (c) 2013
00011  *
00012  * Licensed under the Apache License, Version 2.0 (the "License");
00013  * you may not use this file except in compliance with the License.
00014  * You may obtain a copy of the License at
00015  *
00016  *     http://www.apache.org/licenses/LICENSE-2.0
00017  *
00018  * Unless required by applicable law or agreed to in writing, software
00019  * distributed under the License is distributed on an "AS IS" BASIS,
00020  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  * See the License for the specific language governing permissions and
00022  * limitations under the License.
00023  */
00024  
00025 #ifndef MMA845X_H
00026 #define MMA845X_H
00027 
00028 #include "mbed.h"
00029 
00030 /** Using the Sparkfun SEN-10955
00031  *
00032  * Example:
00033  * @code
00034  *  #include "mbed.h"
00035  *  #include "MMA845x.h"
00036  *
00037 
00038  * 
00039  *  int main() 
00040  *  {
00041 
00042  *  }
00043  * @endcode
00044  */
00045 
00046 
00047 /**
00048  *  @class MMA845x_DATA
00049  *  @brief API abstraction for the MMA845x 3-axis accelerometer IC data
00050  */  
00051 class MMA845x_DATA
00052 {
00053 public:
00054     
00055     volatile uint16_t _x ;   /*!< volatile data variable */
00056     volatile uint16_t _y ;   /*!< volatile data variable */
00057     volatile uint16_t _z ;   /*!< volatile data variable */
00058    
00059     /** Create the MMA845x_DATA object initialized to the parameter (or 0 if none)
00060      *  @param x - the init value of _x
00061      *  @param y - the init value of _y
00062      *  @param x - the init value of _z
00063      */
00064     MMA845x_DATA(uint16_t x = 0, uint16_t y = 0, uint16_t z = 0) : _x (x), _y (y), _z (z) {}
00065     
00066     /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another
00067      *  @param rhs - an object of the same type to assign ourself the same values of
00068      *  @return this
00069      */
00070     MMA845x_DATA &operator= (MMA845x_DATA const &rhs)
00071     {
00072         _x  = rhs._x ;
00073         _y  = rhs._y ;
00074         _z  = rhs._z ;
00075         
00076         return *this;
00077     }
00078     
00079     /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another
00080      *  @param val - Assign each data member (_x, _y, _z) this value
00081      *  @return this
00082      */
00083     MMA845x_DATA &operator= (uint16_t const val)
00084     {
00085         _x  = _y  = _z  = val;
00086         
00087         return *this;
00088     }
00089     
00090     /** Overloaded '==' operator to allow shorthand coding, test objects to one another
00091      *  @param rhs - the object to compare against
00092      *  @return 1 if the data members are the same and 0 otherwise
00093      */
00094     bool operator== (MMA845x_DATA const &rhs) const
00095     {
00096         return ((_x  == rhs._x )&&(_y  == rhs._y )&&(_z  == rhs._z )) ? 1 : 0;
00097     }
00098     
00099 };
00100 
00101 /**
00102  *  @class MMA845x
00103  *  @brief API abstraction for the MMA845x 3-axis accelerometer IC
00104  */ 
00105 class MMA845x
00106 {  
00107 public:
00108     
00109     /**
00110      *  @enum MMA845x_SA0
00111      *  @brief Possible terminations for the ADDR pin
00112      */ 
00113     enum MMA845x_SA0
00114     { 
00115         SA0_VSS  = 0, /*!< SA0 connected to VSS */
00116         SA0_VDD       /*!< SA0 connected to VDD */
00117     };
00118     
00119     /**
00120      *  @enum MMA845x_WHO_AM_I
00121      *  @brief Device ID's that this class is compatible with
00122      */ 
00123     enum MMA845x_WHO_AM_I
00124     { 
00125         MMA8451  = 0x1a, /*!< MMA8451 WHO_AM_I register content */
00126         MMA8452  = 0x2a, /*!< MMA8452 WHO_AM_I register content */
00127         MMA8453  = 0x3a, /*!< MMA8453 WHO_AM_I register content */
00128         MMA8652  = 0x4a, /*!< MMA8652 WHO_AM_I register content */
00129     };
00130     
00131     /**
00132      *  @enum MMA845x_REGISTER
00133      *  @brief The device register map
00134      */
00135     enum MMA845x_REGISTER
00136     {
00137         STATUS = 0x0,
00138         OUT_X_MSB, OUT_X_LSB, OUT_Y_MSB, OUT_Y_LSB, OUT_Z_MSB, OUT_Z_LSB,
00139         
00140         F_SETUP = 0x9, TRIG_CFG, // only available on the MMA8451 variant
00141         
00142         SYSMOD = 0xb,
00143         INT_SOURCE, WHO_AM_I, XYZ_DATA_CFG, HP_FILTER_CUTOFF, PL_STATUS,
00144         PL_CFG, PL_COUNT, PL_BF_ZCOMP, P_L_THS_REG, FF_MT_CFG, FF_MT_SRC,
00145         FF_MT_THS, FF_MT_COUNT,
00146         
00147         TRANSIENT_CFG = 0x1d,
00148         TRANSIENT_SRC, TRANSIENT_THS, TRANSIENT_COUNT, PULSE_CFG, PULSE_SRC,
00149         PULSE_THSX, PULSE_THSY, PULSE_THSZ, PULSE_TMLT, PULSE_LTCY, PULSE_WIND,
00150         ASLP_COUNT, CTRL_REG1, CTRL_REG2, CTRL_REG3, CTRL_REG4, CTRL_REG5,
00151         OFF_X, OFF_Y, OFF_Z
00152     };
00153         
00154     /** Create the MMA845x object
00155      *  @param i2c - A defined I2C object
00156      *  @param int1 - A defined InterruptIn object
00157      *  @param int2 - A defined InterruptIn object
00158      *  @param i2c_addr - Connection of the address line
00159      */ 
00160     MMA845x(I2C &i2c, PinName p_int1, PinName p_int2, MMA845x_SA0 const i2c_addr) : _int1(p_int1), _int2(p_int2)
00161     //MMA845x(I2C &i2c, InterruptIn int1, InterruptIn int2, MMA845x_SA0 const i2c_addr)
00162     {
00163         _i2c = &i2c;
00164     
00165         _i2c_addr = (0x1d << 1) | i2c_addr;
00166         
00167         _int1_waiting = false;
00168         _intMode = 0;
00169         
00170         _int1.fall(this, &MMA845x::interrupt_1);
00171         _int1.mode(PullUp);
00172     };
00173     // 割込み使わない場合のコンストラクタ
00174     MMA845x(I2C &i2c, MMA845x_SA0 const i2c_addr) : _int1(P0_29/*dummy*/), _int2(P0_29/*dummy*/)
00175     {
00176         _i2c = &i2c;
00177         _i2c_addr = (0x1d << 1) | i2c_addr;
00178         _int1_waiting = false;
00179         _intMode = 0;
00180         //_ticker.attach(this, &MMA845x::interrupt_1, 0.5); // ポーリングで見る
00181     }
00182     
00183     float getXX(void){
00184         interrupt_1();
00185         return g_values[0];
00186     }
00187     float getYY(void){
00188         interrupt_1();
00189         return g_values[1];
00190     }
00191     float getZZ(void){
00192         interrupt_1();
00193         return g_values[2];
00194     }
00195    
00196     
00197     /** Get the X data
00198      *  @return The last valid reading from the accelerometer
00199      */
00200     uint16_t getX(void) const;
00201     
00202     /** Get the Y data
00203      *  @return The last valid reading from the accelerometer
00204      */
00205     uint16_t getY(void) const;
00206     
00207     /** Get the Z data
00208      *  @return The last valid reading from the accelerometer
00209      */
00210     uint16_t getZ(void) const;
00211     
00212     /** Get the XYZ data structure
00213      *  @return The last valid reading from the accelerometer
00214      */
00215     MMA845x_DATA getXYZ(void) const;
00216     
00217     void enableDataReadyMode(void) const;
00218     void enableMotionMode(void) const;
00219     void enablePulseMode(void) const;
00220     void enableOrientationMode(void) const;
00221     void enableTransitMode(void) const;
00222     void enableAutoSleepMode(void) const;
00223     void enableFIFOMode(void) const;
00224     /** Put the MMA845x in the lowest possible power mode and suspend operation
00225      */
00226     void disable(void);
00227     
00228     /** Write to a register (exposed for debugging reasons)
00229      *  Note: most writes are only valid in stop mode
00230      *  @param reg - The register to be written
00231      *  @param data - The data to be written
00232      */
00233     void writeRegister(uint8_t const reg, uint8_t const data) const;
00234     
00235     /** Read from a register (exposed for debugging reasons)
00236      *  @param reg - The register to read from
00237      *  @return The register contents
00238      */
00239     uint8_t readRegister(uint8_t const reg) const;
00240     
00241     /** print the register map and values to the console
00242      */
00243     void registerDump(void) const;
00244     
00245     /** Set display mode of interrupt
00246      *  @return The last valid reading from the accelerometer
00247      */
00248     void setIntMode(int);
00249     void attachZAxisPulse(void (*function)(void));
00250     void interrupt_1(void);
00251     void interrupt_handler(void);
00252     
00253 private:
00254     
00255     I2C             *_i2c;
00256     InterruptIn     _int1;
00257     InterruptIn     _int2;
00258     uint8_t         _i2c_addr;
00259     MMA845x_DATA    _data;
00260     bool            _int1_waiting;
00261     int             _intMode;
00262     FunctionPointer _callbackZAxisPulse;
00263     
00264     void init(void) const;
00265     //Ticker _ticker;
00266     float g_values[3];
00267 
00268 };
00269 
00270 #endif