The preloaded firmware shipped on the E-Dice.
Dependencies: mbed
MMA845x.cpp@1:a30065092967, 2014-09-15 (annotated)
- Committer:
- Experiment626
- Date:
- Mon Sep 15 16:09:51 2014 +0000
- Revision:
- 1:a30065092967
- Parent:
- 0:7c263455b8b9
Added Apache 2 license text
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Experiment626 | 0:7c263455b8b9 | 1 | /* Copyright (c) 2014 GHI Electronics, LLC |
Experiment626 | 0:7c263455b8b9 | 2 | * |
Experiment626 | 0:7c263455b8b9 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Experiment626 | 0:7c263455b8b9 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Experiment626 | 0:7c263455b8b9 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Experiment626 | 0:7c263455b8b9 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Experiment626 | 0:7c263455b8b9 | 7 | * Software is furnished to do so, subject to the following conditions: |
Experiment626 | 0:7c263455b8b9 | 8 | * |
Experiment626 | 0:7c263455b8b9 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Experiment626 | 0:7c263455b8b9 | 10 | * substantial portions of the Software. |
Experiment626 | 0:7c263455b8b9 | 11 | * |
Experiment626 | 0:7c263455b8b9 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Experiment626 | 0:7c263455b8b9 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Experiment626 | 0:7c263455b8b9 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Experiment626 | 0:7c263455b8b9 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Experiment626 | 0:7c263455b8b9 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Experiment626 | 0:7c263455b8b9 | 17 | */ |
Experiment626 | 0:7c263455b8b9 | 18 | |
Experiment626 | 0:7c263455b8b9 | 19 | #include "MMA845x.h" |
Experiment626 | 0:7c263455b8b9 | 20 | |
Experiment626 | 0:7c263455b8b9 | 21 | #define X_AXIS 0x01 |
Experiment626 | 0:7c263455b8b9 | 22 | #define Y_AXIS 0x03 |
Experiment626 | 0:7c263455b8b9 | 23 | #define Z_AXIS 0x05 |
Experiment626 | 0:7c263455b8b9 | 24 | |
Experiment626 | 0:7c263455b8b9 | 25 | MMA845x::MMA845x(PinName sda, PinName scl, int i2cAddress) : i2cBus(sda, scl), i2cAddress(i2cAddress) |
Experiment626 | 0:7c263455b8b9 | 26 | { |
Experiment626 | 0:7c263455b8b9 | 27 | uint8_t initializeDataCommand[2] = {0x2A, 0x01}; |
Experiment626 | 0:7c263455b8b9 | 28 | i2cBus.write(i2cAddress, (char*)initializeDataCommand, 2); |
Experiment626 | 0:7c263455b8b9 | 29 | } |
Experiment626 | 0:7c263455b8b9 | 30 | |
Experiment626 | 0:7c263455b8b9 | 31 | MMA845x::~MMA845x() |
Experiment626 | 0:7c263455b8b9 | 32 | { |
Experiment626 | 0:7c263455b8b9 | 33 | } |
Experiment626 | 0:7c263455b8b9 | 34 | |
Experiment626 | 0:7c263455b8b9 | 35 | int16_t MMA845x::getX() |
Experiment626 | 0:7c263455b8b9 | 36 | { |
Experiment626 | 0:7c263455b8b9 | 37 | return readAxisRegister(X_AXIS); |
Experiment626 | 0:7c263455b8b9 | 38 | } |
Experiment626 | 0:7c263455b8b9 | 39 | |
Experiment626 | 0:7c263455b8b9 | 40 | int16_t MMA845x::getY() |
Experiment626 | 0:7c263455b8b9 | 41 | { |
Experiment626 | 0:7c263455b8b9 | 42 | return readAxisRegister(Y_AXIS); |
Experiment626 | 0:7c263455b8b9 | 43 | } |
Experiment626 | 0:7c263455b8b9 | 44 | |
Experiment626 | 0:7c263455b8b9 | 45 | int16_t MMA845x::getZ() |
Experiment626 | 0:7c263455b8b9 | 46 | { |
Experiment626 | 0:7c263455b8b9 | 47 | return readAxisRegister(Z_AXIS); |
Experiment626 | 0:7c263455b8b9 | 48 | } |
Experiment626 | 0:7c263455b8b9 | 49 | |
Experiment626 | 0:7c263455b8b9 | 50 | void MMA845x::GetXYZ(int& x, int& y, int& z) |
Experiment626 | 0:7c263455b8b9 | 51 | { |
Experiment626 | 0:7c263455b8b9 | 52 | } |
Experiment626 | 0:7c263455b8b9 | 53 | |
Experiment626 | 0:7c263455b8b9 | 54 | int16_t MMA845x::readAxisRegister(uint8_t axisRegisterAddress) |
Experiment626 | 0:7c263455b8b9 | 55 | { |
Experiment626 | 0:7c263455b8b9 | 56 | uint8_t axisData[2]; |
Experiment626 | 0:7c263455b8b9 | 57 | readRegister(axisRegisterAddress, axisData, 2); |
Experiment626 | 0:7c263455b8b9 | 58 | |
Experiment626 | 0:7c263455b8b9 | 59 | int16_t rawAxisData = ((axisData[0] << 2) | (axisData[1] >> 6)); |
Experiment626 | 0:7c263455b8b9 | 60 | |
Experiment626 | 0:7c263455b8b9 | 61 | if (rawAxisData > 511) |
Experiment626 | 0:7c263455b8b9 | 62 | rawAxisData = rawAxisData - 1024; |
Experiment626 | 0:7c263455b8b9 | 63 | |
Experiment626 | 0:7c263455b8b9 | 64 | return rawAxisData; |
Experiment626 | 0:7c263455b8b9 | 65 | } |
Experiment626 | 0:7c263455b8b9 | 66 | |
Experiment626 | 0:7c263455b8b9 | 67 | void MMA845x::readRegister(uint8_t registerAddress, uint8_t* data, int length) |
Experiment626 | 0:7c263455b8b9 | 68 | { |
Experiment626 | 0:7c263455b8b9 | 69 | char convertedRegisterAddress[] = {registerAddress}; |
Experiment626 | 0:7c263455b8b9 | 70 | i2cBus.write(i2cAddress, convertedRegisterAddress, 1, true); |
Experiment626 | 0:7c263455b8b9 | 71 | i2cBus.read(i2cAddress, (char*)data, length); |
Experiment626 | 0:7c263455b8b9 | 72 | } |
Experiment626 | 0:7c263455b8b9 | 73 | |
Experiment626 | 0:7c263455b8b9 | 74 |