GHI Electronics / Mbed 2 deprecated Official_E-Dice

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA845x.cpp Source File

MMA845x.cpp

00001 /* Copyright (c) 2014 GHI Electronics, LLC
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "MMA845x.h"
00020 
00021 #define X_AXIS  0x01
00022 #define Y_AXIS  0x03
00023 #define Z_AXIS  0x05
00024 
00025 MMA845x::MMA845x(PinName sda, PinName scl, int i2cAddress) : i2cBus(sda, scl), i2cAddress(i2cAddress)
00026 {
00027     uint8_t initializeDataCommand[2] = {0x2A, 0x01};
00028     i2cBus.write(i2cAddress, (char*)initializeDataCommand, 2);
00029 }
00030 
00031 MMA845x::~MMA845x()
00032 {
00033 }
00034 
00035 int16_t MMA845x::getX()
00036 {
00037     return readAxisRegister(X_AXIS);
00038 }
00039 
00040 int16_t MMA845x::getY()
00041 {
00042     return readAxisRegister(Y_AXIS);
00043 }
00044 
00045 int16_t MMA845x::getZ()
00046 {
00047     return readAxisRegister(Z_AXIS);
00048 }
00049 
00050 void MMA845x::GetXYZ(int& x, int& y, int& z)
00051 {
00052 }
00053 
00054 int16_t MMA845x::readAxisRegister(uint8_t axisRegisterAddress)
00055 {
00056     uint8_t axisData[2];
00057     readRegister(axisRegisterAddress, axisData, 2);
00058     
00059     int16_t rawAxisData = ((axisData[0] << 2) | (axisData[1] >> 6));
00060     
00061     if (rawAxisData > 511)
00062         rawAxisData = rawAxisData - 1024;
00063         
00064     return rawAxisData;
00065 }
00066 
00067 void MMA845x::readRegister(uint8_t registerAddress, uint8_t* data, int length)
00068 {
00069     char convertedRegisterAddress[] = {registerAddress};
00070     i2cBus.write(i2cAddress, convertedRegisterAddress, 1, true);
00071     i2cBus.read(i2cAddress, (char*)data, length);
00072 }
00073 
00074