Modification to FXOS8700Q for use in Data Logger project
Dependencies: FXOS8700Q MotionSensor
Dependents: GPS_6Axis_DataLogger_SD_UDP
Fork of FXOS8700Q by
FXOS8700Q.cpp@3:eb1271ef90bc, 2014-04-17 (annotated)
- Committer:
- JimCarver
- Date:
- Thu Apr 17 17:54:13 2014 +0000
- Revision:
- 3:eb1271ef90bc
- Parent:
- 1:8b53edef272f
- Child:
- 4:be6abf9f2d59
Added necessary Comments and API documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 3:eb1271ef90bc | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
JimCarver | 3:eb1271ef90bc | 2 | * |
JimCarver | 3:eb1271ef90bc | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
JimCarver | 3:eb1271ef90bc | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
JimCarver | 3:eb1271ef90bc | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
JimCarver | 3:eb1271ef90bc | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
JimCarver | 3:eb1271ef90bc | 7 | * Software is furnished to do so, subject to the following conditions: |
JimCarver | 3:eb1271ef90bc | 8 | * |
JimCarver | 3:eb1271ef90bc | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
JimCarver | 3:eb1271ef90bc | 10 | * substantial portions of the Software. |
JimCarver | 3:eb1271ef90bc | 11 | * |
JimCarver | 3:eb1271ef90bc | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
JimCarver | 3:eb1271ef90bc | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
JimCarver | 3:eb1271ef90bc | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
JimCarver | 3:eb1271ef90bc | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
JimCarver | 3:eb1271ef90bc | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
JimCarver | 3:eb1271ef90bc | 17 | */ |
JimCarver | 3:eb1271ef90bc | 18 | |
JimCarver | 0:2562215f5bc0 | 19 | #include "FXOS8700Q.h" |
JimCarver | 1:8b53edef272f | 20 | #define UINT14_MAX 16383 |
JimCarver | 1:8b53edef272f | 21 | |
JimCarver | 1:8b53edef272f | 22 | |
JimCarver | 1:8b53edef272f | 23 | FXOS8700Q::FXOS8700Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
JimCarver | 1:8b53edef272f | 24 | // activate the peripheral |
JimCarver | 1:8b53edef272f | 25 | uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00}; |
JimCarver | 1:8b53edef272f | 26 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 27 | data[0] = FXOS8700Q_M_CTRL_REG1; |
JimCarver | 1:8b53edef272f | 28 | data[1] = 0x1F; |
JimCarver | 1:8b53edef272f | 29 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 30 | data[0] = FXOS8700Q_M_CTRL_REG2; |
JimCarver | 1:8b53edef272f | 31 | data[1] = 0x20; |
JimCarver | 1:8b53edef272f | 32 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 33 | data[0] = FXOS8700Q_XYZ_DATA_CFG; |
JimCarver | 1:8b53edef272f | 34 | data[1] = 0x00; |
JimCarver | 1:8b53edef272f | 35 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 36 | data[0] = FXOS8700Q_CTRL_REG1; |
JimCarver | 1:8b53edef272f | 37 | data[1] = 0x19;//0x1D; |
JimCarver | 1:8b53edef272f | 38 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 39 | } |
JimCarver | 0:2562215f5bc0 | 40 | |
JimCarver | 0:2562215f5bc0 | 41 | |
JimCarver | 1:8b53edef272f | 42 | FXOS8700Q::~FXOS8700Q() { } |
JimCarver | 1:8b53edef272f | 43 | |
JimCarver | 1:8b53edef272f | 44 | uint8_t FXOS8700Q::getWhoAmI() { |
JimCarver | 1:8b53edef272f | 45 | uint8_t who_am_i = 0; |
JimCarver | 1:8b53edef272f | 46 | readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1); |
JimCarver | 1:8b53edef272f | 47 | return who_am_i; |
JimCarver | 1:8b53edef272f | 48 | } |
JimCarver | 1:8b53edef272f | 49 | |
JimCarver | 1:8b53edef272f | 50 | float FXOS8700Q::getAccX() { |
JimCarver | 1:8b53edef272f | 51 | return (float(getAccAxis(FXOS8700Q_OUT_X_MSB))/4096.0); |
JimCarver | 0:2562215f5bc0 | 52 | } |
JimCarver | 1:8b53edef272f | 53 | |
JimCarver | 1:8b53edef272f | 54 | float FXOS8700Q::getAccY() { |
JimCarver | 1:8b53edef272f | 55 | return (float(getAccAxis(FXOS8700Q_OUT_Y_MSB))/4096.0); |
JimCarver | 1:8b53edef272f | 56 | } |
JimCarver | 1:8b53edef272f | 57 | |
JimCarver | 1:8b53edef272f | 58 | float FXOS8700Q::getAccZ() { |
JimCarver | 1:8b53edef272f | 59 | return (float(getAccAxis(FXOS8700Q_OUT_Z_MSB))/4096.0); |
JimCarver | 1:8b53edef272f | 60 | } |
JimCarver | 1:8b53edef272f | 61 | |
JimCarver | 1:8b53edef272f | 62 | |
JimCarver | 1:8b53edef272f | 63 | void FXOS8700Q::getAccAllAxis(float * res) { |
JimCarver | 1:8b53edef272f | 64 | res[0] = getAccX(); |
JimCarver | 1:8b53edef272f | 65 | res[1] = getAccY(); |
JimCarver | 1:8b53edef272f | 66 | res[2] = getAccZ(); |
JimCarver | 0:2562215f5bc0 | 67 | } |
JimCarver | 0:2562215f5bc0 | 68 | |
JimCarver | 1:8b53edef272f | 69 | void FXOS8700Q::AccXYZraw(int16_t * d) { |
JimCarver | 1:8b53edef272f | 70 | int16_t acc; |
JimCarver | 1:8b53edef272f | 71 | uint8_t res[6]; |
JimCarver | 1:8b53edef272f | 72 | readRegs(FXOS8700Q_OUT_X_MSB, res, 6); |
JimCarver | 0:2562215f5bc0 | 73 | |
JimCarver | 1:8b53edef272f | 74 | acc = (res[0] << 6) | (res[1] >> 2); |
JimCarver | 1:8b53edef272f | 75 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 76 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 77 | d[0] = acc; |
JimCarver | 1:8b53edef272f | 78 | acc = (res[2] << 6) | (res[3] >> 2); |
JimCarver | 1:8b53edef272f | 79 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 80 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 81 | d[1] = acc; |
JimCarver | 1:8b53edef272f | 82 | acc = (res[4] << 6) | (res[5] >> 2); |
JimCarver | 1:8b53edef272f | 83 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 84 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 85 | d[2] = acc; |
JimCarver | 0:2562215f5bc0 | 86 | } |
JimCarver | 0:2562215f5bc0 | 87 | |
JimCarver | 1:8b53edef272f | 88 | void FXOS8700Q::MagXYZraw(int16_t * d) { |
JimCarver | 1:8b53edef272f | 89 | int16_t acc; |
JimCarver | 1:8b53edef272f | 90 | uint8_t res[6]; |
JimCarver | 1:8b53edef272f | 91 | readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6); |
JimCarver | 1:8b53edef272f | 92 | |
JimCarver | 1:8b53edef272f | 93 | d[0] = (res[0] << 8) | res[1]; |
JimCarver | 1:8b53edef272f | 94 | d[1] = (res[2] << 8) | res[3]; |
JimCarver | 1:8b53edef272f | 95 | d[2] = (res[4] << 8) | res[5]; |
JimCarver | 1:8b53edef272f | 96 | } |
JimCarver | 1:8b53edef272f | 97 | |
JimCarver | 1:8b53edef272f | 98 | int16_t FXOS8700Q::getAccAxis(uint8_t addr) { |
JimCarver | 1:8b53edef272f | 99 | int16_t acc; |
JimCarver | 1:8b53edef272f | 100 | uint8_t res[2]; |
JimCarver | 1:8b53edef272f | 101 | readRegs(addr, res, 2); |
JimCarver | 0:2562215f5bc0 | 102 | |
JimCarver | 1:8b53edef272f | 103 | acc = (res[0] << 6) | (res[1] >> 2); |
JimCarver | 1:8b53edef272f | 104 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 105 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 106 | |
JimCarver | 1:8b53edef272f | 107 | return acc; |
JimCarver | 1:8b53edef272f | 108 | } |
JimCarver | 0:2562215f5bc0 | 109 | |
JimCarver | 1:8b53edef272f | 110 | void FXOS8700Q::readRegs(int addr, uint8_t * data, int len) { |
JimCarver | 1:8b53edef272f | 111 | char t[1] = {addr}; |
JimCarver | 1:8b53edef272f | 112 | m_i2c.write(m_addr, t, 1, true); |
JimCarver | 1:8b53edef272f | 113 | m_i2c.read(m_addr, (char *)data, len); |
JimCarver | 0:2562215f5bc0 | 114 | } |
JimCarver | 1:8b53edef272f | 115 | |
JimCarver | 1:8b53edef272f | 116 | void FXOS8700Q::writeRegs(uint8_t * data, int len) { |
JimCarver | 1:8b53edef272f | 117 | m_i2c.write(m_addr, (char *)data, len); |
JimCarver | 1:8b53edef272f | 118 | } |
JimCarver | 1:8b53edef272f | 119 | |
JimCarver | 0:2562215f5bc0 | 120 |