Refactoring Ironcup 2020

Dependencies:   mbed mbed-rtos MotionSensor EthernetInterface

Committer:
starling
Date:
Mon Sep 21 21:42:07 2020 +0000
Revision:
0:8f5db5085df7
12 mar 2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
starling 0:8f5db5085df7 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
starling 0:8f5db5085df7 2 *
starling 0:8f5db5085df7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
starling 0:8f5db5085df7 4 * and associated documentation files (the "Software"), to deal in the Software without
starling 0:8f5db5085df7 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
starling 0:8f5db5085df7 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
starling 0:8f5db5085df7 7 * Software is furnished to do so, subject to the following conditions:
starling 0:8f5db5085df7 8 *
starling 0:8f5db5085df7 9 * The above copyright notice and this permission notice shall be included in all copies or
starling 0:8f5db5085df7 10 * substantial portions of the Software.
starling 0:8f5db5085df7 11 *
starling 0:8f5db5085df7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
starling 0:8f5db5085df7 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
starling 0:8f5db5085df7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
starling 0:8f5db5085df7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
starling 0:8f5db5085df7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
starling 0:8f5db5085df7 17 */
starling 0:8f5db5085df7 18
starling 0:8f5db5085df7 19 #include "FXOS8700Q.h"
starling 0:8f5db5085df7 20 #define UINT14_MAX 16383
starling 0:8f5db5085df7 21
starling 0:8f5db5085df7 22
starling 0:8f5db5085df7 23 FXOS8700Q_acc::FXOS8700Q_acc(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
starling 0:8f5db5085df7 24 // activate the peripheral
starling 0:8f5db5085df7 25 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
starling 0:8f5db5085df7 26 m_i2c.frequency(400000);
starling 0:8f5db5085df7 27 writeRegs(data, 2);
starling 0:8f5db5085df7 28 data[0] = FXOS8700Q_M_CTRL_REG1;
starling 0:8f5db5085df7 29 data[1] = 0x1F;
starling 0:8f5db5085df7 30 writeRegs(data, 2);
starling 0:8f5db5085df7 31 data[0] = FXOS8700Q_M_CTRL_REG2;
starling 0:8f5db5085df7 32 data[1] = 0x20;
starling 0:8f5db5085df7 33 writeRegs(data, 2);
starling 0:8f5db5085df7 34 data[0] = FXOS8700Q_XYZ_DATA_CFG;
starling 0:8f5db5085df7 35 data[1] = 0x00;
starling 0:8f5db5085df7 36 writeRegs(data, 2);
starling 0:8f5db5085df7 37 data[0] = FXOS8700Q_CTRL_REG1;
starling 0:8f5db5085df7 38 data[1] = 0x18;//0x1D;
starling 0:8f5db5085df7 39 writeRegs(data, 2);
starling 0:8f5db5085df7 40 }
starling 0:8f5db5085df7 41
starling 0:8f5db5085df7 42 FXOS8700Q_acc::~FXOS8700Q_acc() { }
starling 0:8f5db5085df7 43
starling 0:8f5db5085df7 44 void FXOS8700Q_acc::enable(void) {
starling 0:8f5db5085df7 45 uint8_t data[2];
starling 0:8f5db5085df7 46 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
starling 0:8f5db5085df7 47 data[1] |= 0x01;
starling 0:8f5db5085df7 48 data[0] = FXOS8700Q_CTRL_REG1;
starling 0:8f5db5085df7 49 writeRegs(data, 2);
starling 0:8f5db5085df7 50 }
starling 0:8f5db5085df7 51
starling 0:8f5db5085df7 52 void FXOS8700Q_acc::disable(void) {
starling 0:8f5db5085df7 53 uint8_t data[2];
starling 0:8f5db5085df7 54 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
starling 0:8f5db5085df7 55 data[1] &= 0xFE;
starling 0:8f5db5085df7 56 data[0] = FXOS8700Q_CTRL_REG1;
starling 0:8f5db5085df7 57 writeRegs(data, 2);
starling 0:8f5db5085df7 58 }
starling 0:8f5db5085df7 59
starling 0:8f5db5085df7 60
starling 0:8f5db5085df7 61
starling 0:8f5db5085df7 62 uint32_t FXOS8700Q_acc::whoAmI() {
starling 0:8f5db5085df7 63 uint8_t who_am_i = 0;
starling 0:8f5db5085df7 64 readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1);
starling 0:8f5db5085df7 65 return (uint32_t) who_am_i;
starling 0:8f5db5085df7 66 }
starling 0:8f5db5085df7 67
starling 0:8f5db5085df7 68 uint32_t FXOS8700Q_acc::dataReady(void) {
starling 0:8f5db5085df7 69 uint8_t stat = 0;
starling 0:8f5db5085df7 70 readRegs(FXOS8700Q_STATUS, &stat, 1);
starling 0:8f5db5085df7 71 return (uint32_t) stat;
starling 0:8f5db5085df7 72 }
starling 0:8f5db5085df7 73
starling 0:8f5db5085df7 74 uint32_t FXOS8700Q_acc::sampleRate(uint32_t f) {
starling 0:8f5db5085df7 75 return(50); // for now sample rate is fixed at 50Hz
starling 0:8f5db5085df7 76 }
starling 0:8f5db5085df7 77
starling 0:8f5db5085df7 78 void FXOS8700Q_acc::getX(float * x) {
starling 0:8f5db5085df7 79 *x = (float(getAccAxis(FXOS8700Q_OUT_X_MSB))/4096.0f);
starling 0:8f5db5085df7 80 }
starling 0:8f5db5085df7 81
starling 0:8f5db5085df7 82 void FXOS8700Q_acc::getY(float * y) {
starling 0:8f5db5085df7 83 *y = (float(getAccAxis(FXOS8700Q_OUT_Y_MSB))/4096.0f);
starling 0:8f5db5085df7 84 }
starling 0:8f5db5085df7 85
starling 0:8f5db5085df7 86 void FXOS8700Q_acc::getZ(float * z) {
starling 0:8f5db5085df7 87 *z = (float(getAccAxis(FXOS8700Q_OUT_Z_MSB))/4096.0f);
starling 0:8f5db5085df7 88 }
starling 0:8f5db5085df7 89
starling 0:8f5db5085df7 90 void FXOS8700Q_acc::getX(int16_t * d) {
starling 0:8f5db5085df7 91 *d = getAccAxis(FXOS8700Q_OUT_X_MSB);
starling 0:8f5db5085df7 92 }
starling 0:8f5db5085df7 93
starling 0:8f5db5085df7 94 void FXOS8700Q_acc::getY(int16_t * d) {
starling 0:8f5db5085df7 95 *d = getAccAxis(FXOS8700Q_OUT_Y_MSB);
starling 0:8f5db5085df7 96 }
starling 0:8f5db5085df7 97
starling 0:8f5db5085df7 98 void FXOS8700Q_acc::getZ(int16_t * d) {
starling 0:8f5db5085df7 99 *d = getAccAxis(FXOS8700Q_OUT_Z_MSB);
starling 0:8f5db5085df7 100 }
starling 0:8f5db5085df7 101
starling 0:8f5db5085df7 102
starling 0:8f5db5085df7 103 void FXOS8700Q_acc::getAxis(MotionSensorDataUnits &data) {
starling 0:8f5db5085df7 104 int16_t acc, t[3];
starling 0:8f5db5085df7 105 uint8_t res[6];
starling 0:8f5db5085df7 106 readRegs(FXOS8700Q_OUT_X_MSB, res, 6);
starling 0:8f5db5085df7 107
starling 0:8f5db5085df7 108 acc = (res[0] << 6) | (res[1] >> 2);
starling 0:8f5db5085df7 109 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 110 acc -= UINT14_MAX;
starling 0:8f5db5085df7 111 t[0] = acc;
starling 0:8f5db5085df7 112 acc = (res[2] << 6) | (res[3] >> 2);
starling 0:8f5db5085df7 113 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 114 acc -= UINT14_MAX;
starling 0:8f5db5085df7 115 t[1] = acc;
starling 0:8f5db5085df7 116 acc = (res[4] << 6) | (res[5] >> 2);
starling 0:8f5db5085df7 117 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 118 acc -= UINT14_MAX;
starling 0:8f5db5085df7 119 t[2] = acc;
starling 0:8f5db5085df7 120 data.x = ((float) t[0]) / 4096.0f;
starling 0:8f5db5085df7 121 data.y = ((float) t[1]) / 4096.0f;
starling 0:8f5db5085df7 122 data.z = ((float) t[2]) / 4096.0f;
starling 0:8f5db5085df7 123 }
starling 0:8f5db5085df7 124
starling 0:8f5db5085df7 125
starling 0:8f5db5085df7 126 void FXOS8700Q_acc::getAxis(MotionSensorDataCounts &data) {
starling 0:8f5db5085df7 127 int16_t acc;
starling 0:8f5db5085df7 128 uint8_t res[6];
starling 0:8f5db5085df7 129 readRegs(FXOS8700Q_OUT_X_MSB, res, 6);
starling 0:8f5db5085df7 130
starling 0:8f5db5085df7 131 acc = (res[0] << 6) | (res[1] >> 2);
starling 0:8f5db5085df7 132 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 133 acc -= UINT14_MAX;
starling 0:8f5db5085df7 134 data.x = acc;
starling 0:8f5db5085df7 135 acc = (res[2] << 6) | (res[3] >> 2);
starling 0:8f5db5085df7 136 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 137 acc -= UINT14_MAX;
starling 0:8f5db5085df7 138 data.y = acc;
starling 0:8f5db5085df7 139 acc = (res[4] << 6) | (res[5] >> 2);
starling 0:8f5db5085df7 140 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 141 acc -= UINT14_MAX;
starling 0:8f5db5085df7 142 data.z = acc;
starling 0:8f5db5085df7 143 }
starling 0:8f5db5085df7 144
starling 0:8f5db5085df7 145 void FXOS8700Q_acc::readRegs(int addr, uint8_t * data, int len) {
starling 0:8f5db5085df7 146 char t[1] = {addr};
starling 0:8f5db5085df7 147 m_i2c.write(m_addr, t, 1, true);
starling 0:8f5db5085df7 148 m_i2c.read(m_addr, (char *)data, len);
starling 0:8f5db5085df7 149 }
starling 0:8f5db5085df7 150
starling 0:8f5db5085df7 151 void FXOS8700Q_acc::writeRegs(uint8_t * data, int len) {
starling 0:8f5db5085df7 152 m_i2c.write(m_addr, (char *)data, len);
starling 0:8f5db5085df7 153 }
starling 0:8f5db5085df7 154
starling 0:8f5db5085df7 155
starling 0:8f5db5085df7 156 int16_t FXOS8700Q_acc::getAccAxis(uint8_t addr) {
starling 0:8f5db5085df7 157 int16_t acc;
starling 0:8f5db5085df7 158 uint8_t res[2];
starling 0:8f5db5085df7 159 readRegs(addr, res, 2);
starling 0:8f5db5085df7 160
starling 0:8f5db5085df7 161 acc = (res[0] << 6) | (res[1] >> 2);
starling 0:8f5db5085df7 162 if (acc > UINT14_MAX/2)
starling 0:8f5db5085df7 163 acc -= UINT14_MAX;
starling 0:8f5db5085df7 164
starling 0:8f5db5085df7 165 return acc;
starling 0:8f5db5085df7 166 }
starling 0:8f5db5085df7 167
starling 0:8f5db5085df7 168
starling 0:8f5db5085df7 169
starling 0:8f5db5085df7 170 FXOS8700Q_mag::FXOS8700Q_mag(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
starling 0:8f5db5085df7 171 // activate the peripheral
starling 0:8f5db5085df7 172 uint8_t data[2] = {FXOS8700Q_CTRL_REG1, 0x00};
starling 0:8f5db5085df7 173 m_i2c.frequency(400000);
starling 0:8f5db5085df7 174 writeRegs(data, 2);
starling 0:8f5db5085df7 175 data[0] = FXOS8700Q_M_CTRL_REG1;
starling 0:8f5db5085df7 176 data[1] = 0x1F;
starling 0:8f5db5085df7 177 writeRegs(data, 2);
starling 0:8f5db5085df7 178 data[0] = FXOS8700Q_M_CTRL_REG2;
starling 0:8f5db5085df7 179 data[1] = 0x20;
starling 0:8f5db5085df7 180 writeRegs(data, 2);
starling 0:8f5db5085df7 181 data[0] = FXOS8700Q_XYZ_DATA_CFG;
starling 0:8f5db5085df7 182 data[1] = 0x00;
starling 0:8f5db5085df7 183 writeRegs(data, 2);
starling 0:8f5db5085df7 184 data[0] = FXOS8700Q_CTRL_REG1;
starling 0:8f5db5085df7 185 data[1] = 0x18;//0x1D;
starling 0:8f5db5085df7 186 writeRegs(data, 2);
starling 0:8f5db5085df7 187 }
starling 0:8f5db5085df7 188
starling 0:8f5db5085df7 189 FXOS8700Q_mag::~FXOS8700Q_mag() { }
starling 0:8f5db5085df7 190
starling 0:8f5db5085df7 191 void FXOS8700Q_mag::enable(void) {
starling 0:8f5db5085df7 192 uint8_t data[2];
starling 0:8f5db5085df7 193 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
starling 0:8f5db5085df7 194 data[1] |= 0x01;
starling 0:8f5db5085df7 195 data[0] = FXOS8700Q_CTRL_REG1;
starling 0:8f5db5085df7 196 writeRegs(data, 2);
starling 0:8f5db5085df7 197 }
starling 0:8f5db5085df7 198
starling 0:8f5db5085df7 199 void FXOS8700Q_mag::disable(void) {
starling 0:8f5db5085df7 200 uint8_t data[2];
starling 0:8f5db5085df7 201 readRegs( FXOS8700Q_CTRL_REG1, &data[1], 1);
starling 0:8f5db5085df7 202 data[1] &= 0xFE;
starling 0:8f5db5085df7 203 data[0] = FXOS8700Q_CTRL_REG1;
starling 0:8f5db5085df7 204 writeRegs(data, 2);
starling 0:8f5db5085df7 205 }
starling 0:8f5db5085df7 206
starling 0:8f5db5085df7 207
starling 0:8f5db5085df7 208
starling 0:8f5db5085df7 209 uint32_t FXOS8700Q_mag::whoAmI() {
starling 0:8f5db5085df7 210 uint8_t who_am_i = 0;
starling 0:8f5db5085df7 211 readRegs(FXOS8700Q_WHOAMI, &who_am_i, 1);
starling 0:8f5db5085df7 212 return (uint32_t) who_am_i;
starling 0:8f5db5085df7 213 }
starling 0:8f5db5085df7 214
starling 0:8f5db5085df7 215 uint32_t FXOS8700Q_mag::dataReady(void) {
starling 0:8f5db5085df7 216 uint8_t stat = 0;
starling 0:8f5db5085df7 217 readRegs(FXOS8700Q_STATUS, &stat, 1);
starling 0:8f5db5085df7 218 return (uint32_t) stat;
starling 0:8f5db5085df7 219 }
starling 0:8f5db5085df7 220
starling 0:8f5db5085df7 221 uint32_t FXOS8700Q_mag::sampleRate(uint32_t f) {
starling 0:8f5db5085df7 222 return(50); // for now sample rate is fixed at 50Hz
starling 0:8f5db5085df7 223 }
starling 0:8f5db5085df7 224
starling 0:8f5db5085df7 225 void FXOS8700Q_mag::getX(float * x) {
starling 0:8f5db5085df7 226 *x = (float(getAccAxis(FXOS8700Q_M_OUT_X_MSB)) * 0.1f);
starling 0:8f5db5085df7 227 }
starling 0:8f5db5085df7 228
starling 0:8f5db5085df7 229 void FXOS8700Q_mag::getY(float * y) {
starling 0:8f5db5085df7 230 *y = (float(getAccAxis(FXOS8700Q_M_OUT_Y_MSB)) * 0.1f);
starling 0:8f5db5085df7 231 }
starling 0:8f5db5085df7 232
starling 0:8f5db5085df7 233 void FXOS8700Q_mag::getZ(float * z) {
starling 0:8f5db5085df7 234 *z = (float(getAccAxis(FXOS8700Q_M_OUT_Z_MSB)) * 0.1f);
starling 0:8f5db5085df7 235 }
starling 0:8f5db5085df7 236
starling 0:8f5db5085df7 237 void FXOS8700Q_mag::getX(int16_t * d) {
starling 0:8f5db5085df7 238 *d = getAccAxis(FXOS8700Q_M_OUT_X_MSB);
starling 0:8f5db5085df7 239 }
starling 0:8f5db5085df7 240
starling 0:8f5db5085df7 241 void FXOS8700Q_mag::getY(int16_t * d) {
starling 0:8f5db5085df7 242 *d = getAccAxis(FXOS8700Q_M_OUT_Y_MSB);
starling 0:8f5db5085df7 243 }
starling 0:8f5db5085df7 244
starling 0:8f5db5085df7 245 void FXOS8700Q_mag::getZ(int16_t * d) {
starling 0:8f5db5085df7 246 *d = getAccAxis(FXOS8700Q_M_OUT_Z_MSB);
starling 0:8f5db5085df7 247 }
starling 0:8f5db5085df7 248
starling 0:8f5db5085df7 249
starling 0:8f5db5085df7 250 void FXOS8700Q_mag::getAxis(MotionSensorDataUnits &data) {
starling 0:8f5db5085df7 251 int16_t t[3];
starling 0:8f5db5085df7 252 uint8_t res[6];
starling 0:8f5db5085df7 253 readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6);
starling 0:8f5db5085df7 254
starling 0:8f5db5085df7 255 t[0] = (res[0] << 8) | res[1];
starling 0:8f5db5085df7 256 t[1] = (res[2] << 8) | res[3];
starling 0:8f5db5085df7 257 t[2] = (res[4] << 8) | res[5];
starling 0:8f5db5085df7 258
starling 0:8f5db5085df7 259 data.x = ((float) t[0]) * 0.1f;
starling 0:8f5db5085df7 260 data.y = ((float) t[1]) * 0.1f;
starling 0:8f5db5085df7 261 data.z = ((float) t[2]) * 0.1f;
starling 0:8f5db5085df7 262 }
starling 0:8f5db5085df7 263
starling 0:8f5db5085df7 264
starling 0:8f5db5085df7 265 void FXOS8700Q_mag::getAxis(MotionSensorDataCounts &data) {
starling 0:8f5db5085df7 266 int16_t acc;
starling 0:8f5db5085df7 267 uint8_t res[6];
starling 0:8f5db5085df7 268 readRegs(FXOS8700Q_M_OUT_X_MSB, res, 6);
starling 0:8f5db5085df7 269
starling 0:8f5db5085df7 270 data.x = (res[0] << 8) | res[1];
starling 0:8f5db5085df7 271 data.y = (res[2] << 8) | res[3];
starling 0:8f5db5085df7 272 data.z = (res[4] << 8) | res[5];
starling 0:8f5db5085df7 273 }
starling 0:8f5db5085df7 274
starling 0:8f5db5085df7 275 void FXOS8700Q_mag::readRegs(int addr, uint8_t * data, int len) {
starling 0:8f5db5085df7 276 char t[1] = {addr};
starling 0:8f5db5085df7 277 m_i2c.write(m_addr, t, 1, true);
starling 0:8f5db5085df7 278 m_i2c.read(m_addr, (char *)data, len);
starling 0:8f5db5085df7 279 }
starling 0:8f5db5085df7 280
starling 0:8f5db5085df7 281 void FXOS8700Q_mag::writeRegs(uint8_t * data, int len) {
starling 0:8f5db5085df7 282 m_i2c.write(m_addr, (char *)data, len);
starling 0:8f5db5085df7 283 }
starling 0:8f5db5085df7 284
starling 0:8f5db5085df7 285
starling 0:8f5db5085df7 286 int16_t FXOS8700Q_mag::getAccAxis(uint8_t addr) {
starling 0:8f5db5085df7 287 int16_t acc;
starling 0:8f5db5085df7 288 uint8_t res[2];
starling 0:8f5db5085df7 289 readRegs(addr, res, 2);
starling 0:8f5db5085df7 290
starling 0:8f5db5085df7 291 acc = (res[0] << 8) | res[1];
starling 0:8f5db5085df7 292
starling 0:8f5db5085df7 293 return acc;
starling 0:8f5db5085df7 294 }