Library to access multiple giro - with ability to setup them when starting

Committer:
Pythia
Date:
Wed Jul 22 13:23:44 2015 +0000
Revision:
0:8aa01dbab64e
Child:
1:816ba6bfbb20
Initial version (STUB!)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pythia 0:8aa01dbab64e 1 /* FXAS21002CQ sensor driver
Pythia 0:8aa01dbab64e 2 * Copyright (c) 2015 WD
Pythia 0:8aa01dbab64e 3 *
Pythia 0:8aa01dbab64e 4 *
Pythia 0:8aa01dbab64e 5 * Unless required by applicable law or agreed to in writing, software
Pythia 0:8aa01dbab64e 6 * distributed under the License is distributed on an "AS IS" BASIS,
Pythia 0:8aa01dbab64e 7 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pythia 0:8aa01dbab64e 8 * See the License for the specific language governing permissions and
Pythia 0:8aa01dbab64e 9 * limitations under the License.
Pythia 0:8aa01dbab64e 10 */
Pythia 0:8aa01dbab64e 11
Pythia 0:8aa01dbab64e 12 #include "FXAS21002CQ.h"
Pythia 0:8aa01dbab64e 13
Pythia 0:8aa01dbab64e 14 static int16_t dummy_int16_t = 0;
Pythia 0:8aa01dbab64e 15 static float dummy_float = 0.0f;
Pythia 0:8aa01dbab64e 16
Pythia 0:8aa01dbab64e 17
Pythia 0:8aa01dbab64e 18 FXAS21002CQ::FXAS21002CQ(I2C &i2c, uint8_t addr)
Pythia 0:8aa01dbab64e 19 {
Pythia 0:8aa01dbab64e 20 _i2c = &i2c;
Pythia 0:8aa01dbab64e 21 _addr = addr;
Pythia 0:8aa01dbab64e 22 /*
Pythia 0:8aa01dbab64e 23 // activate the peripheral
Pythia 0:8aa01dbab64e 24 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
Pythia 0:8aa01dbab64e 25 _i2c->frequency(400000);
Pythia 0:8aa01dbab64e 26 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 27 data[0] = FXOS8700Q_M_CTRL_REG1;
Pythia 0:8aa01dbab64e 28 data[1] = 0x1F;
Pythia 0:8aa01dbab64e 29 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 30 data[0] = FXOS8700Q_M_CTRL_REG2;
Pythia 0:8aa01dbab64e 31 data[1] = 0x20;
Pythia 0:8aa01dbab64e 32 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 33 data[0] = FXOS8700Q_XYZ_DATA_CFG;
Pythia 0:8aa01dbab64e 34 data[1] = 0x00;
Pythia 0:8aa01dbab64e 35 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 36 data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 37 data[1] = 0x1C;
Pythia 0:8aa01dbab64e 38 writeRegs(data, 2);
Pythia 0:8aa01dbab64e 39 */
Pythia 0:8aa01dbab64e 40 }
Pythia 0:8aa01dbab64e 41
Pythia 0:8aa01dbab64e 42 FXAS21002CQ::~FXAS21002CQ()
Pythia 0:8aa01dbab64e 43 {
Pythia 0:8aa01dbab64e 44 _i2c = 0;
Pythia 0:8aa01dbab64e 45 _addr = 0;
Pythia 0:8aa01dbab64e 46 }
Pythia 0:8aa01dbab64e 47
Pythia 0:8aa01dbab64e 48 void FXAS21002CQ::readRegs(uint8_t addr, uint8_t *data, uint32_t len)
Pythia 0:8aa01dbab64e 49 {
Pythia 0:8aa01dbab64e 50 uint8_t t[1] = {addr};
Pythia 0:8aa01dbab64e 51 _i2c->write(_addr, (char *)t, sizeof(t), true);
Pythia 0:8aa01dbab64e 52 _i2c->read(_addr, (char *)data, len);
Pythia 0:8aa01dbab64e 53 }
Pythia 0:8aa01dbab64e 54
Pythia 0:8aa01dbab64e 55 uint8_t FXAS21002CQ::whoAmI()
Pythia 0:8aa01dbab64e 56 {
Pythia 0:8aa01dbab64e 57 uint8_t who_am_i = 0;
Pythia 0:8aa01dbab64e 58 readRegs(FXAS21002CQ_WHO_AM_I, &who_am_i, sizeof(who_am_i));
Pythia 0:8aa01dbab64e 59 return who_am_i;
Pythia 0:8aa01dbab64e 60 }
Pythia 0:8aa01dbab64e 61
Pythia 0:8aa01dbab64e 62 void FXAS21002CQ::writeRegs(uint8_t * data, uint32_t len) const
Pythia 0:8aa01dbab64e 63 {
Pythia 0:8aa01dbab64e 64 _i2c->write(_addr, (char *)data, len);
Pythia 0:8aa01dbab64e 65 }
Pythia 0:8aa01dbab64e 66
Pythia 0:8aa01dbab64e 67 int16_t FXAS21002CQ::getSensorAxis(uint8_t addr) const
Pythia 0:8aa01dbab64e 68 {
Pythia 0:8aa01dbab64e 69 uint8_t res[2];
Pythia 0:8aa01dbab64e 70 // readRegs(addr, res, sizeof(res));
Pythia 0:8aa01dbab64e 71 return static_cast<int16_t>((res[0] << 8) | res[1]);
Pythia 0:8aa01dbab64e 72 }
Pythia 0:8aa01dbab64e 73
Pythia 0:8aa01dbab64e 74 void FXAS21002CQ::enable(void) const
Pythia 0:8aa01dbab64e 75 {
Pythia 0:8aa01dbab64e 76 uint8_t data[2];
Pythia 0:8aa01dbab64e 77 // readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
Pythia 0:8aa01dbab64e 78 // data[1] |= 0x01;
Pythia 0:8aa01dbab64e 79 // data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 80 // writeRegs(data, sizeof(data));
Pythia 0:8aa01dbab64e 81 }
Pythia 0:8aa01dbab64e 82
Pythia 0:8aa01dbab64e 83 void FXAS21002CQ::disable(void) const
Pythia 0:8aa01dbab64e 84 {
Pythia 0:8aa01dbab64e 85 uint8_t data[2];
Pythia 0:8aa01dbab64e 86 // readRegs(FXOS8700Q_CTRL_REG1, &data[1], 1);
Pythia 0:8aa01dbab64e 87 // data[1] &= 0xFE;
Pythia 0:8aa01dbab64e 88 // data[0] = FXOS8700Q_CTRL_REG1;
Pythia 0:8aa01dbab64e 89 // writeRegs(data, sizeof(data));
Pythia 0:8aa01dbab64e 90 }
Pythia 0:8aa01dbab64e 91
Pythia 0:8aa01dbab64e 92 uint32_t FXAS21002CQ::dataReady(void) const
Pythia 0:8aa01dbab64e 93 {
Pythia 0:8aa01dbab64e 94 // uint8_t stat = 0;
Pythia 0:8aa01dbab64e 95 // readRegs(FXOS8700Q_STATUS, &stat, 1);
Pythia 0:8aa01dbab64e 96 return (uint32_t)1;
Pythia 0:8aa01dbab64e 97 }
Pythia 0:8aa01dbab64e 98
Pythia 0:8aa01dbab64e 99 uint32_t FXAS21002CQ::sampleRate(uint32_t frequency) const
Pythia 0:8aa01dbab64e 100 {
Pythia 0:8aa01dbab64e 101 return(50); // for now sample rate is fixed at 50Hz
Pythia 0:8aa01dbab64e 102 }
Pythia 0:8aa01dbab64e 103
Pythia 0:8aa01dbab64e 104 int16_t FXAS21002CQ::getX(int16_t &x = dummy_int16_t) const
Pythia 0:8aa01dbab64e 105 {
Pythia 0:8aa01dbab64e 106 // x = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
Pythia 0:8aa01dbab64e 107 // normalize_14bits(x);
Pythia 0:8aa01dbab64e 108 return x;
Pythia 0:8aa01dbab64e 109 }
Pythia 0:8aa01dbab64e 110
Pythia 0:8aa01dbab64e 111 int16_t FXAS21002CQ::getY(int16_t &y = dummy_int16_t) const
Pythia 0:8aa01dbab64e 112 {
Pythia 0:8aa01dbab64e 113 // y = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
Pythia 0:8aa01dbab64e 114 // normalize_14bits(y);
Pythia 0:8aa01dbab64e 115 return y;
Pythia 0:8aa01dbab64e 116 }
Pythia 0:8aa01dbab64e 117
Pythia 0:8aa01dbab64e 118 int16_t FXAS21002CQ::getZ(int16_t &z = dummy_int16_t) const
Pythia 0:8aa01dbab64e 119 {
Pythia 0:8aa01dbab64e 120 // z = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
Pythia 0:8aa01dbab64e 121 // normalize_14bits(z);
Pythia 0:8aa01dbab64e 122 return z;
Pythia 0:8aa01dbab64e 123 }
Pythia 0:8aa01dbab64e 124
Pythia 0:8aa01dbab64e 125 float FXAS21002CQ::getX(float &x = dummy_float) const
Pythia 0:8aa01dbab64e 126 {
Pythia 0:8aa01dbab64e 127 // int16_t val = getSensorAxis(FXOS8700Q_OUT_X_MSB) >> 2;
Pythia 0:8aa01dbab64e 128 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 129 // x = val / 4096.0f;
Pythia 0:8aa01dbab64e 130 return x;
Pythia 0:8aa01dbab64e 131 }
Pythia 0:8aa01dbab64e 132
Pythia 0:8aa01dbab64e 133 float FXAS21002CQ::getY(float &y = dummy_float) const
Pythia 0:8aa01dbab64e 134 {
Pythia 0:8aa01dbab64e 135 // int16_t val = getSensorAxis(FXOS8700Q_OUT_Y_MSB) >> 2;
Pythia 0:8aa01dbab64e 136 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 137 // y = val / 4096.0f;
Pythia 0:8aa01dbab64e 138 return y;
Pythia 0:8aa01dbab64e 139 }
Pythia 0:8aa01dbab64e 140
Pythia 0:8aa01dbab64e 141 float FXAS21002CQ::getZ(float &z = dummy_float) const
Pythia 0:8aa01dbab64e 142 {
Pythia 0:8aa01dbab64e 143 // int16_t val = getSensorAxis(FXOS8700Q_OUT_Z_MSB) >> 2;
Pythia 0:8aa01dbab64e 144 // normalize_14bits(val);
Pythia 0:8aa01dbab64e 145 // z = val / 4096.0f;
Pythia 0:8aa01dbab64e 146 return z;
Pythia 0:8aa01dbab64e 147 }
Pythia 0:8aa01dbab64e 148
Pythia 0:8aa01dbab64e 149 void FXAS21002CQ::getAxis(motion_data_counts_t &xyz) const
Pythia 0:8aa01dbab64e 150 {
Pythia 0:8aa01dbab64e 151 uint8_t res[6];
Pythia 0:8aa01dbab64e 152 // readRegs(FXOS8700Q_OUT_X_MSB, res, sizeof(res));
Pythia 0:8aa01dbab64e 153 // xyz.x = static_cast<int16_t>((res[0] << 8) | res[1]) >> 2;
Pythia 0:8aa01dbab64e 154 // xyz.y = static_cast<int16_t>((res[2] << 8) | res[3]) >> 2;
Pythia 0:8aa01dbab64e 155 // xyz.z = static_cast<int16_t>((res[4] << 8) | res[5]) >> 2;
Pythia 0:8aa01dbab64e 156 // normalize_14bits(xyz.x);
Pythia 0:8aa01dbab64e 157 // normalize_14bits(xyz.y);
Pythia 0:8aa01dbab64e 158 // normalize_14bits(xyz.z);
Pythia 0:8aa01dbab64e 159 }
Pythia 0:8aa01dbab64e 160
Pythia 0:8aa01dbab64e 161 void FXAS21002CQ::getAxis(motion_data_units_t &xyz) const
Pythia 0:8aa01dbab64e 162 {
Pythia 0:8aa01dbab64e 163 // motion_data_counts_t _xyz;
Pythia 0:8aa01dbab64e 164 // FXOS8700QAccelerometer::getAxis(_xyz);
Pythia 0:8aa01dbab64e 165 // xyz.x = _xyz.x / 4096.0f;
Pythia 0:8aa01dbab64e 166 // xyz.y = _xyz.y / 4096.0f;
Pythia 0:8aa01dbab64e 167 // xyz.z = _xyz.z / 4096.0f;
Pythia 0:8aa01dbab64e 168 }
Pythia 0:8aa01dbab64e 169
Pythia 0:8aa01dbab64e 170 int8_t FXAS21002CQ::getTemperature(void)
Pythia 0:8aa01dbab64e 171 {
Pythia 0:8aa01dbab64e 172 uint8_t res;
Pythia 0:8aa01dbab64e 173 readRegs(FXAS21002CQ_TEMP, &res, sizeof(res));
Pythia 0:8aa01dbab64e 174 return (int8_t)res;
Pythia 0:8aa01dbab64e 175 }