the E-Dice version of the wooden labyrinth. Try catching the blinking led by pitch and roll the dice. please forgive... it my first c++ program

Dependencies:   USBDevice mbed

Committer:
VBDaniel
Date:
Sat Dec 13 22:26:44 2014 +0000
Revision:
0:e2c7e03219ab
the E-Dice version of the wooden labyrinth. Try catching the blinking led by pitch and roll the dice. ; please forgive... it my first c++ program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
VBDaniel 0:e2c7e03219ab 1 /* Copyright (c) 2014 GHI Electronics, LLC
VBDaniel 0:e2c7e03219ab 2 *
VBDaniel 0:e2c7e03219ab 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
VBDaniel 0:e2c7e03219ab 4 * and associated documentation files (the "Software"), to deal in the Software without
VBDaniel 0:e2c7e03219ab 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
VBDaniel 0:e2c7e03219ab 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
VBDaniel 0:e2c7e03219ab 7 * Software is furnished to do so, subject to the following conditions:
VBDaniel 0:e2c7e03219ab 8 *
VBDaniel 0:e2c7e03219ab 9 * The above copyright notice and this permission notice shall be included in all copies or
VBDaniel 0:e2c7e03219ab 10 * substantial portions of the Software.
VBDaniel 0:e2c7e03219ab 11 *
VBDaniel 0:e2c7e03219ab 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
VBDaniel 0:e2c7e03219ab 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
VBDaniel 0:e2c7e03219ab 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
VBDaniel 0:e2c7e03219ab 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
VBDaniel 0:e2c7e03219ab 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
VBDaniel 0:e2c7e03219ab 17 */
VBDaniel 0:e2c7e03219ab 18 /*edit by VBDaniel ...to by continued*/
VBDaniel 0:e2c7e03219ab 19
VBDaniel 0:e2c7e03219ab 20 #include "MMA845x.h"
VBDaniel 0:e2c7e03219ab 21
VBDaniel 0:e2c7e03219ab 22 #define X_AXIS 0x01
VBDaniel 0:e2c7e03219ab 23 #define Y_AXIS 0x03
VBDaniel 0:e2c7e03219ab 24 #define Z_AXIS 0x05
VBDaniel 0:e2c7e03219ab 25 #define SYSMOD 0x0B // System Mode Register (Read Only)
VBDaniel 0:e2c7e03219ab 26 #define CRTL_REG1 0X2A // ODR = 800 Hz, STANDBY Mode.
VBDaniel 0:e2c7e03219ab 27 #define CTRL_REG2 0x2B // Sleep Enable, OS Modes, RST, ST
VBDaniel 0:e2c7e03219ab 28 #define CTRL_REG3 0x2C // Wake from Sleep, IPOL, PP_OD
VBDaniel 0:e2c7e03219ab 29 #define CTRL_REG4 0x2D // Interrupt enable register
VBDaniel 0:e2c7e03219ab 30 #define CTRL_REG5 0x2E // Interrupt pin (INT1/INT2) map
VBDaniel 0:e2c7e03219ab 31
VBDaniel 0:e2c7e03219ab 32 enum SYSMOD_BITS
VBDaniel 0:e2c7e03219ab 33 {
VBDaniel 0:e2c7e03219ab 34 SYSMOD0 ,
VBDaniel 0:e2c7e03219ab 35 SYSMOD1 ,
VBDaniel 0:e2c7e03219ab 36 /* SYSMOD[1:0]
VBDaniel 0:e2c7e03219ab 37 System Mode. Default value: 00.
VBDaniel 0:e2c7e03219ab 38 00: STANDBY mode
VBDaniel 0:e2c7e03219ab 39 01: WAKE mode
VBDaniel 0:e2c7e03219ab 40 10: SLEEP mode
VBDaniel 0:e2c7e03219ab 41 */
VBDaniel 0:e2c7e03219ab 42 FGT_1 ,
VBDaniel 0:e2c7e03219ab 43 FGT_2 ,
VBDaniel 0:e2c7e03219ab 44 FGT_3 ,
VBDaniel 0:e2c7e03219ab 45 FGT_4 ,
VBDaniel 0:e2c7e03219ab 46 FGERR ,
VBDaniel 0:e2c7e03219ab 47 };
VBDaniel 0:e2c7e03219ab 48
VBDaniel 0:e2c7e03219ab 49
VBDaniel 0:e2c7e03219ab 50 MMA845x::MMA845x(PinName sda, PinName scl, int i2cAddress, PinName intPin1, PinName intPin2) : i2cBus(sda, scl), i2cAddress(i2cAddress), int1(intPin1), int2(intPin2)
VBDaniel 0:e2c7e03219ab 51 {
VBDaniel 0:e2c7e03219ab 52 uint8_t initializeDataCommand[2] = {0x2A, 0x01};
VBDaniel 0:e2c7e03219ab 53 i2cBus.write(i2cAddress, (char*)initializeDataCommand, 2);
VBDaniel 0:e2c7e03219ab 54 }
VBDaniel 0:e2c7e03219ab 55
VBDaniel 0:e2c7e03219ab 56
VBDaniel 0:e2c7e03219ab 57 MMA845x::~MMA845x()
VBDaniel 0:e2c7e03219ab 58 {
VBDaniel 0:e2c7e03219ab 59 }
VBDaniel 0:e2c7e03219ab 60
VBDaniel 0:e2c7e03219ab 61 int16_t MMA845x::getX()
VBDaniel 0:e2c7e03219ab 62 {
VBDaniel 0:e2c7e03219ab 63 return readAxisRegister(X_AXIS);
VBDaniel 0:e2c7e03219ab 64 }
VBDaniel 0:e2c7e03219ab 65
VBDaniel 0:e2c7e03219ab 66 int16_t MMA845x::getY()
VBDaniel 0:e2c7e03219ab 67 {
VBDaniel 0:e2c7e03219ab 68 return readAxisRegister(Y_AXIS);
VBDaniel 0:e2c7e03219ab 69 }
VBDaniel 0:e2c7e03219ab 70
VBDaniel 0:e2c7e03219ab 71 int16_t MMA845x::getZ()
VBDaniel 0:e2c7e03219ab 72 {
VBDaniel 0:e2c7e03219ab 73 return readAxisRegister(Z_AXIS);
VBDaniel 0:e2c7e03219ab 74 }
VBDaniel 0:e2c7e03219ab 75
VBDaniel 0:e2c7e03219ab 76 void MMA845x::GetXYZ(int& x, int& y, int& z)
VBDaniel 0:e2c7e03219ab 77 {
VBDaniel 0:e2c7e03219ab 78 }
VBDaniel 0:e2c7e03219ab 79
VBDaniel 0:e2c7e03219ab 80 int16_t MMA845x::readAxisRegister(uint8_t axisRegisterAddress)
VBDaniel 0:e2c7e03219ab 81 {
VBDaniel 0:e2c7e03219ab 82 uint8_t axisData[2];
VBDaniel 0:e2c7e03219ab 83 readRegister(axisRegisterAddress, axisData, 2);
VBDaniel 0:e2c7e03219ab 84
VBDaniel 0:e2c7e03219ab 85 int16_t rawAxisData = ((axisData[0] << 2) | (axisData[1] >> 6));
VBDaniel 0:e2c7e03219ab 86
VBDaniel 0:e2c7e03219ab 87 if (rawAxisData > 511)
VBDaniel 0:e2c7e03219ab 88 rawAxisData = rawAxisData - 1024;
VBDaniel 0:e2c7e03219ab 89
VBDaniel 0:e2c7e03219ab 90 return rawAxisData;
VBDaniel 0:e2c7e03219ab 91 }
VBDaniel 0:e2c7e03219ab 92
VBDaniel 0:e2c7e03219ab 93 void MMA845x::readRegister(uint8_t registerAddress, uint8_t* data, int length)
VBDaniel 0:e2c7e03219ab 94 {
VBDaniel 0:e2c7e03219ab 95 char convertedRegisterAddress[] = {registerAddress};
VBDaniel 0:e2c7e03219ab 96 i2cBus.write(i2cAddress, convertedRegisterAddress, 1, true);
VBDaniel 0:e2c7e03219ab 97 i2cBus.read(i2cAddress, (char*)data, length);
VBDaniel 0:e2c7e03219ab 98 }
VBDaniel 0:e2c7e03219ab 99
VBDaniel 0:e2c7e03219ab 100
VBDaniel 0:e2c7e03219ab 101 void MMA845x::writeRegister(uint8_t* data, int length)
VBDaniel 0:e2c7e03219ab 102 {
VBDaniel 0:e2c7e03219ab 103 }