Modification to FXOS8700Q for use in Data Logger project
Dependencies: FXOS8700Q MotionSensor
Dependents: GPS_6Axis_DataLogger_SD_UDP
Fork of FXOS8700Q by
FXOS8700Q.cpp@4:be6abf9f2d59, 2014-04-19 (annotated)
- Committer:
- JimCarver
- Date:
- Sat Apr 19 00:16:49 2014 +0000
- Revision:
- 4:be6abf9f2d59
- Parent:
- 3:eb1271ef90bc
- Child:
- 5:c4176a12f7d5
Added unit converted magnetometer support
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 | 4:be6abf9f2d59 | 26 | m_i2c.frequency(400000); |
JimCarver | 1:8b53edef272f | 27 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 28 | data[0] = FXOS8700Q_M_CTRL_REG1; |
JimCarver | 1:8b53edef272f | 29 | data[1] = 0x1F; |
JimCarver | 1:8b53edef272f | 30 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 31 | data[0] = FXOS8700Q_M_CTRL_REG2; |
JimCarver | 1:8b53edef272f | 32 | data[1] = 0x20; |
JimCarver | 1:8b53edef272f | 33 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 34 | data[0] = FXOS8700Q_XYZ_DATA_CFG; |
JimCarver | 1:8b53edef272f | 35 | data[1] = 0x00; |
JimCarver | 1:8b53edef272f | 36 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 37 | data[0] = FXOS8700Q_CTRL_REG1; |
JimCarver | 1:8b53edef272f | 38 | data[1] = 0x19;//0x1D; |
JimCarver | 1:8b53edef272f | 39 | writeRegs(data, 2); |
JimCarver | 1:8b53edef272f | 40 | } |
JimCarver | 0:2562215f5bc0 | 41 | |
JimCarver | 0:2562215f5bc0 | 42 | |
JimCarver | 1:8b53edef272f | 43 | FXOS8700Q::~FXOS8700Q() { } |
JimCarver | 1:8b53edef272f | 44 | |
JimCarver | 1:8b53edef272f | 45 | uint8_t FXOS8700Q::getWhoAmI() { |
JimCarver | 1:8b53edef272f | 46 | uint8_t who_am_i = 0; |
JimCarver | 1:8b53edef272f | 47 | readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1); |
JimCarver | 1:8b53edef272f | 48 | return who_am_i; |
JimCarver | 1:8b53edef272f | 49 | } |
JimCarver | 1:8b53edef272f | 50 | |
JimCarver | 1:8b53edef272f | 51 | float FXOS8700Q::getAccX() { |
JimCarver | 1:8b53edef272f | 52 | return (float(getAccAxis(FXOS8700Q_OUT_X_MSB))/4096.0); |
JimCarver | 0:2562215f5bc0 | 53 | } |
JimCarver | 1:8b53edef272f | 54 | |
JimCarver | 1:8b53edef272f | 55 | float FXOS8700Q::getAccY() { |
JimCarver | 1:8b53edef272f | 56 | return (float(getAccAxis(FXOS8700Q_OUT_Y_MSB))/4096.0); |
JimCarver | 1:8b53edef272f | 57 | } |
JimCarver | 1:8b53edef272f | 58 | |
JimCarver | 1:8b53edef272f | 59 | float FXOS8700Q::getAccZ() { |
JimCarver | 1:8b53edef272f | 60 | return (float(getAccAxis(FXOS8700Q_OUT_Z_MSB))/4096.0); |
JimCarver | 1:8b53edef272f | 61 | } |
JimCarver | 1:8b53edef272f | 62 | |
JimCarver | 1:8b53edef272f | 63 | |
JimCarver | 1:8b53edef272f | 64 | void FXOS8700Q::getAccAllAxis(float * res) { |
JimCarver | 1:8b53edef272f | 65 | res[0] = getAccX(); |
JimCarver | 1:8b53edef272f | 66 | res[1] = getAccY(); |
JimCarver | 1:8b53edef272f | 67 | res[2] = getAccZ(); |
JimCarver | 0:2562215f5bc0 | 68 | } |
JimCarver | 0:2562215f5bc0 | 69 | |
JimCarver | 1:8b53edef272f | 70 | void FXOS8700Q::AccXYZraw(int16_t * d) { |
JimCarver | 1:8b53edef272f | 71 | int16_t acc; |
JimCarver | 1:8b53edef272f | 72 | uint8_t res[6]; |
JimCarver | 1:8b53edef272f | 73 | readRegs(FXOS8700Q_OUT_X_MSB, res, 6); |
JimCarver | 0:2562215f5bc0 | 74 | |
JimCarver | 1:8b53edef272f | 75 | acc = (res[0] << 6) | (res[1] >> 2); |
JimCarver | 1:8b53edef272f | 76 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 77 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 78 | d[0] = acc; |
JimCarver | 1:8b53edef272f | 79 | acc = (res[2] << 6) | (res[3] >> 2); |
JimCarver | 1:8b53edef272f | 80 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 81 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 82 | d[1] = acc; |
JimCarver | 1:8b53edef272f | 83 | acc = (res[4] << 6) | (res[5] >> 2); |
JimCarver | 1:8b53edef272f | 84 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 85 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 86 | d[2] = acc; |
JimCarver | 0:2562215f5bc0 | 87 | } |
JimCarver | 0:2562215f5bc0 | 88 | |
JimCarver | 1:8b53edef272f | 89 | void FXOS8700Q::MagXYZraw(int16_t * d) { |
JimCarver | 1:8b53edef272f | 90 | int16_t acc; |
JimCarver | 1:8b53edef272f | 91 | uint8_t res[6]; |
JimCarver | 1:8b53edef272f | 92 | readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6); |
JimCarver | 1:8b53edef272f | 93 | |
JimCarver | 1:8b53edef272f | 94 | d[0] = (res[0] << 8) | res[1]; |
JimCarver | 1:8b53edef272f | 95 | d[1] = (res[2] << 8) | res[3]; |
JimCarver | 1:8b53edef272f | 96 | d[2] = (res[4] << 8) | res[5]; |
JimCarver | 1:8b53edef272f | 97 | } |
JimCarver | 1:8b53edef272f | 98 | |
JimCarver | 4:be6abf9f2d59 | 99 | void FXOS8700Q::getMagAllAxis(float * res) { |
JimCarver | 4:be6abf9f2d59 | 100 | int16_t raw[3]; |
JimCarver | 4:be6abf9f2d59 | 101 | MagXYZraw( raw); |
JimCarver | 4:be6abf9f2d59 | 102 | res[0] = (float) raw[0] * 0.1f; |
JimCarver | 4:be6abf9f2d59 | 103 | res[1] = (float) raw[1] * 0.1f; |
JimCarver | 4:be6abf9f2d59 | 104 | res[2] = (float) raw[2] * 0.1f; |
JimCarver | 4:be6abf9f2d59 | 105 | } |
JimCarver | 4:be6abf9f2d59 | 106 | |
JimCarver | 1:8b53edef272f | 107 | int16_t FXOS8700Q::getAccAxis(uint8_t addr) { |
JimCarver | 1:8b53edef272f | 108 | int16_t acc; |
JimCarver | 1:8b53edef272f | 109 | uint8_t res[2]; |
JimCarver | 1:8b53edef272f | 110 | readRegs(addr, res, 2); |
JimCarver | 0:2562215f5bc0 | 111 | |
JimCarver | 1:8b53edef272f | 112 | acc = (res[0] << 6) | (res[1] >> 2); |
JimCarver | 1:8b53edef272f | 113 | if (acc > UINT14_MAX/2) |
JimCarver | 1:8b53edef272f | 114 | acc -= UINT14_MAX; |
JimCarver | 1:8b53edef272f | 115 | |
JimCarver | 1:8b53edef272f | 116 | return acc; |
JimCarver | 1:8b53edef272f | 117 | } |
JimCarver | 0:2562215f5bc0 | 118 | |
JimCarver | 1:8b53edef272f | 119 | void FXOS8700Q::readRegs(int addr, uint8_t * data, int len) { |
JimCarver | 1:8b53edef272f | 120 | char t[1] = {addr}; |
JimCarver | 1:8b53edef272f | 121 | m_i2c.write(m_addr, t, 1, true); |
JimCarver | 1:8b53edef272f | 122 | m_i2c.read(m_addr, (char *)data, len); |
JimCarver | 0:2562215f5bc0 | 123 | } |
JimCarver | 1:8b53edef272f | 124 | |
JimCarver | 1:8b53edef272f | 125 | void FXOS8700Q::writeRegs(uint8_t * data, int len) { |
JimCarver | 1:8b53edef272f | 126 | m_i2c.write(m_addr, (char *)data, len); |
JimCarver | 1:8b53edef272f | 127 | } |
JimCarver | 1:8b53edef272f | 128 | |
JimCarver | 0:2562215f5bc0 | 129 |