Eric Wu / Mbed 2 deprecated WifiRobot

Dependencies:   mbed

Committer:
wueric
Date:
Tue Dec 09 05:06:37 2014 +0000
Revision:
0:2deb247ab4bd
sharing stuff;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wueric 0:2deb247ab4bd 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
wueric 0:2deb247ab4bd 2 *
wueric 0:2deb247ab4bd 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wueric 0:2deb247ab4bd 4 * and associated documentation files (the "Software"), to deal in the Software without
wueric 0:2deb247ab4bd 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
wueric 0:2deb247ab4bd 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
wueric 0:2deb247ab4bd 7 * Software is furnished to do so, subject to the following conditions:
wueric 0:2deb247ab4bd 8 *
wueric 0:2deb247ab4bd 9 * The above copyright notice and this permission notice shall be included in all copies or
wueric 0:2deb247ab4bd 10 * substantial portions of the Software.
wueric 0:2deb247ab4bd 11 *
wueric 0:2deb247ab4bd 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wueric 0:2deb247ab4bd 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wueric 0:2deb247ab4bd 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wueric 0:2deb247ab4bd 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wueric 0:2deb247ab4bd 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wueric 0:2deb247ab4bd 17 */
wueric 0:2deb247ab4bd 18
wueric 0:2deb247ab4bd 19 #include "MMA8451Q.h"
wueric 0:2deb247ab4bd 20
wueric 0:2deb247ab4bd 21 #define REG_WHO_AM_I 0x0D
wueric 0:2deb247ab4bd 22 #define REG_CTRL_REG_1 0x2A
wueric 0:2deb247ab4bd 23 #define REG_OUT_X_MSB 0x01
wueric 0:2deb247ab4bd 24 #define REG_OUT_Y_MSB 0x03
wueric 0:2deb247ab4bd 25 #define REG_OUT_Z_MSB 0x05
wueric 0:2deb247ab4bd 26
wueric 0:2deb247ab4bd 27 #define UINT14_MAX 16383
wueric 0:2deb247ab4bd 28
wueric 0:2deb247ab4bd 29 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
wueric 0:2deb247ab4bd 30 // activate the peripheral
wueric 0:2deb247ab4bd 31 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
wueric 0:2deb247ab4bd 32 writeRegs(data, 2);
wueric 0:2deb247ab4bd 33 }
wueric 0:2deb247ab4bd 34
wueric 0:2deb247ab4bd 35 MMA8451Q::~MMA8451Q() { }
wueric 0:2deb247ab4bd 36
wueric 0:2deb247ab4bd 37 uint8_t MMA8451Q::getWhoAmI() {
wueric 0:2deb247ab4bd 38 uint8_t who_am_i = 0;
wueric 0:2deb247ab4bd 39 readRegs(REG_WHO_AM_I, &who_am_i, 1);
wueric 0:2deb247ab4bd 40 return who_am_i;
wueric 0:2deb247ab4bd 41 }
wueric 0:2deb247ab4bd 42
wueric 0:2deb247ab4bd 43 float MMA8451Q::getAccX() {
wueric 0:2deb247ab4bd 44 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
wueric 0:2deb247ab4bd 45 }
wueric 0:2deb247ab4bd 46
wueric 0:2deb247ab4bd 47 float MMA8451Q::getAccY() {
wueric 0:2deb247ab4bd 48 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
wueric 0:2deb247ab4bd 49 }
wueric 0:2deb247ab4bd 50
wueric 0:2deb247ab4bd 51 float MMA8451Q::getAccZ() {
wueric 0:2deb247ab4bd 52 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
wueric 0:2deb247ab4bd 53 }
wueric 0:2deb247ab4bd 54
wueric 0:2deb247ab4bd 55 void MMA8451Q::getAccAllAxis(float * res) {
wueric 0:2deb247ab4bd 56 res[0] = getAccX();
wueric 0:2deb247ab4bd 57 res[1] = getAccY();
wueric 0:2deb247ab4bd 58 res[2] = getAccZ();
wueric 0:2deb247ab4bd 59 }
wueric 0:2deb247ab4bd 60
wueric 0:2deb247ab4bd 61 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
wueric 0:2deb247ab4bd 62 int16_t acc;
wueric 0:2deb247ab4bd 63 uint8_t res[2];
wueric 0:2deb247ab4bd 64 readRegs(addr, res, 2);
wueric 0:2deb247ab4bd 65
wueric 0:2deb247ab4bd 66 acc = (res[0] << 6) | (res[1] >> 2);
wueric 0:2deb247ab4bd 67 if (acc > UINT14_MAX/2)
wueric 0:2deb247ab4bd 68 acc -= UINT14_MAX;
wueric 0:2deb247ab4bd 69
wueric 0:2deb247ab4bd 70 return acc;
wueric 0:2deb247ab4bd 71 }
wueric 0:2deb247ab4bd 72
wueric 0:2deb247ab4bd 73 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
wueric 0:2deb247ab4bd 74 char t[1] = {addr};
wueric 0:2deb247ab4bd 75 m_i2c.write(m_addr, t, 1, true);
wueric 0:2deb247ab4bd 76 m_i2c.read(m_addr, (char *)data, len);
wueric 0:2deb247ab4bd 77 }
wueric 0:2deb247ab4bd 78
wueric 0:2deb247ab4bd 79 void MMA8451Q::writeRegs(uint8_t * data, int len) {
wueric 0:2deb247ab4bd 80 m_i2c.write(m_addr, (char *)data, len);
wueric 0:2deb247ab4bd 81 }