Update version of EALib.

Dependencies:   FATFileSystem

Fork of EALib by IONX

Committer:
embeddedartists
Date:
Fri Oct 18 12:48:58 2013 +0200
Revision:
4:b32cf4ef45c5
Child:
12:15597e45eea0
Added AR1021 touch controller
Added MMA7455 accelerometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 4:b32cf4ef45c5 1
embeddedartists 4:b32cf4ef45c5 2 /******************************************************************************
embeddedartists 4:b32cf4ef45c5 3 * Includes
embeddedartists 4:b32cf4ef45c5 4 *****************************************************************************/
embeddedartists 4:b32cf4ef45c5 5
embeddedartists 4:b32cf4ef45c5 6 #include "mbed.h"
embeddedartists 4:b32cf4ef45c5 7 #include "mbed_debug.h"
embeddedartists 4:b32cf4ef45c5 8
embeddedartists 4:b32cf4ef45c5 9 #include "MMA7455.h"
embeddedartists 4:b32cf4ef45c5 10
embeddedartists 4:b32cf4ef45c5 11 /******************************************************************************
embeddedartists 4:b32cf4ef45c5 12 * Defines and typedefs
embeddedartists 4:b32cf4ef45c5 13 *****************************************************************************/
embeddedartists 4:b32cf4ef45c5 14
embeddedartists 4:b32cf4ef45c5 15 #define MMA7455_I2C_ADDR (0x1D << 1)
embeddedartists 4:b32cf4ef45c5 16
embeddedartists 4:b32cf4ef45c5 17 #define MMA7455_ADDR_XOUTL 0x00
embeddedartists 4:b32cf4ef45c5 18 #define MMA7455_ADDR_XOUTH 0x01
embeddedartists 4:b32cf4ef45c5 19 #define MMA7455_ADDR_YOUTL 0x02
embeddedartists 4:b32cf4ef45c5 20 #define MMA7455_ADDR_YOUTH 0x03
embeddedartists 4:b32cf4ef45c5 21 #define MMA7455_ADDR_ZOUTL 0x04
embeddedartists 4:b32cf4ef45c5 22 #define MMA7455_ADDR_ZOUTH 0x05
embeddedartists 4:b32cf4ef45c5 23 #define MMA7455_ADDR_XOUT8 0x06
embeddedartists 4:b32cf4ef45c5 24 #define MMA7455_ADDR_YOUT8 0x07
embeddedartists 4:b32cf4ef45c5 25 #define MMA7455_ADDR_ZOUT8 0x08
embeddedartists 4:b32cf4ef45c5 26 #define MMA7455_ADDR_STATUS 0x09
embeddedartists 4:b32cf4ef45c5 27 #define MMA7455_ADDR_DETSRC 0x0A
embeddedartists 4:b32cf4ef45c5 28 #define MMA7455_ADDR_TOUT 0x0B
embeddedartists 4:b32cf4ef45c5 29 #define MMA7455_ADDR_I2CAD 0x0D
embeddedartists 4:b32cf4ef45c5 30 #define MMA7455_ADDR_USRINF 0x0E
embeddedartists 4:b32cf4ef45c5 31 #define MMA7455_ADDR_WHOAMI 0x0F
embeddedartists 4:b32cf4ef45c5 32 #define MMA7455_ADDR_XOFFL 0x10
embeddedartists 4:b32cf4ef45c5 33 #define MMA7455_ADDR_XOFFH 0x11
embeddedartists 4:b32cf4ef45c5 34 #define MMA7455_ADDR_YOFFL 0x12
embeddedartists 4:b32cf4ef45c5 35 #define MMA7455_ADDR_YOFFH 0x13
embeddedartists 4:b32cf4ef45c5 36 #define MMA7455_ADDR_ZOFFL 0x14
embeddedartists 4:b32cf4ef45c5 37 #define MMA7455_ADDR_ZOFFH 0x15
embeddedartists 4:b32cf4ef45c5 38 #define MMA7455_ADDR_MCTL 0x16
embeddedartists 4:b32cf4ef45c5 39 #define MMA7455_ADDR_INTRST 0x17
embeddedartists 4:b32cf4ef45c5 40 #define MMA7455_ADDR_CTL1 0x18
embeddedartists 4:b32cf4ef45c5 41 #define MMA7455_ADDR_CTL2 0x19
embeddedartists 4:b32cf4ef45c5 42 #define MMA7455_ADDR_LDTH 0x1A
embeddedartists 4:b32cf4ef45c5 43 #define MMA7455_ADDR_PDTH 0x1B
embeddedartists 4:b32cf4ef45c5 44 #define MMA7455_ADDR_PW 0x1C
embeddedartists 4:b32cf4ef45c5 45 #define MMA7455_ADDR_LT 0x1D
embeddedartists 4:b32cf4ef45c5 46 #define MMA7455_ADDR_TW 0x1E
embeddedartists 4:b32cf4ef45c5 47
embeddedartists 4:b32cf4ef45c5 48 #define MMA7455_MCTL_MODE(m) ((m) << 0)
embeddedartists 4:b32cf4ef45c5 49 #define MMA7455_MCTL_GLVL(g) ((g) << 2)
embeddedartists 4:b32cf4ef45c5 50
embeddedartists 4:b32cf4ef45c5 51 #define MMA7455_STATUS_DRDY (1 << 0)
embeddedartists 4:b32cf4ef45c5 52 #define MMA7455_STATUS_DOVR (1 << 1)
embeddedartists 4:b32cf4ef45c5 53 #define MMA7455_STATUS_PERR (1 << 2)
embeddedartists 4:b32cf4ef45c5 54
embeddedartists 4:b32cf4ef45c5 55
embeddedartists 4:b32cf4ef45c5 56 MMA7455::MMA7455(PinName sda, PinName scl) : _i2c(sda, scl)
embeddedartists 4:b32cf4ef45c5 57 {
embeddedartists 4:b32cf4ef45c5 58 _mode = ModeStandby;
embeddedartists 4:b32cf4ef45c5 59 _range = Range_8g;
embeddedartists 4:b32cf4ef45c5 60
embeddedartists 4:b32cf4ef45c5 61 _xOff = 0;
embeddedartists 4:b32cf4ef45c5 62 _yOff = 0;
embeddedartists 4:b32cf4ef45c5 63 _zOff = 0;
embeddedartists 4:b32cf4ef45c5 64 }
embeddedartists 4:b32cf4ef45c5 65
embeddedartists 4:b32cf4ef45c5 66 bool MMA7455::setMode(Mode mode) {
embeddedartists 4:b32cf4ef45c5 67 bool result = false;
embeddedartists 4:b32cf4ef45c5 68 int mCtrl = 0;
embeddedartists 4:b32cf4ef45c5 69
embeddedartists 4:b32cf4ef45c5 70 do {
embeddedartists 4:b32cf4ef45c5 71 mCtrl = getModeControl();
embeddedartists 4:b32cf4ef45c5 72 if (mCtrl < 0) break;
embeddedartists 4:b32cf4ef45c5 73
embeddedartists 4:b32cf4ef45c5 74 mCtrl &= ~(0x03 << 0);
embeddedartists 4:b32cf4ef45c5 75 mCtrl |= MMA7455_MCTL_MODE(mode);
embeddedartists 4:b32cf4ef45c5 76
embeddedartists 4:b32cf4ef45c5 77 if (setModeControl((uint8_t)mCtrl) < 0) {
embeddedartists 4:b32cf4ef45c5 78 break;
embeddedartists 4:b32cf4ef45c5 79 }
embeddedartists 4:b32cf4ef45c5 80
embeddedartists 4:b32cf4ef45c5 81 _mode = mode;
embeddedartists 4:b32cf4ef45c5 82 result = true;
embeddedartists 4:b32cf4ef45c5 83 } while(0);
embeddedartists 4:b32cf4ef45c5 84
embeddedartists 4:b32cf4ef45c5 85
embeddedartists 4:b32cf4ef45c5 86
embeddedartists 4:b32cf4ef45c5 87 return result;
embeddedartists 4:b32cf4ef45c5 88 }
embeddedartists 4:b32cf4ef45c5 89
embeddedartists 4:b32cf4ef45c5 90 bool MMA7455::setRange(Range range) {
embeddedartists 4:b32cf4ef45c5 91 bool result = false;
embeddedartists 4:b32cf4ef45c5 92 int mCtrl = 0;
embeddedartists 4:b32cf4ef45c5 93
embeddedartists 4:b32cf4ef45c5 94 do {
embeddedartists 4:b32cf4ef45c5 95 mCtrl = getModeControl();
embeddedartists 4:b32cf4ef45c5 96 if (mCtrl < 0) break;
embeddedartists 4:b32cf4ef45c5 97
embeddedartists 4:b32cf4ef45c5 98 mCtrl &= ~(0x03 << 2);
embeddedartists 4:b32cf4ef45c5 99 mCtrl |= MMA7455_MCTL_GLVL(range);
embeddedartists 4:b32cf4ef45c5 100
embeddedartists 4:b32cf4ef45c5 101 if (setModeControl((uint8_t)mCtrl) < 0) {
embeddedartists 4:b32cf4ef45c5 102 break;
embeddedartists 4:b32cf4ef45c5 103 }
embeddedartists 4:b32cf4ef45c5 104
embeddedartists 4:b32cf4ef45c5 105 _range = range;
embeddedartists 4:b32cf4ef45c5 106 result = true;
embeddedartists 4:b32cf4ef45c5 107 } while(0);
embeddedartists 4:b32cf4ef45c5 108
embeddedartists 4:b32cf4ef45c5 109
embeddedartists 4:b32cf4ef45c5 110
embeddedartists 4:b32cf4ef45c5 111 return result;
embeddedartists 4:b32cf4ef45c5 112
embeddedartists 4:b32cf4ef45c5 113 }
embeddedartists 4:b32cf4ef45c5 114
embeddedartists 4:b32cf4ef45c5 115 bool MMA7455::read(int32_t& x, int32_t& y, int32_t& z) {
embeddedartists 4:b32cf4ef45c5 116 bool result = false;
embeddedartists 4:b32cf4ef45c5 117
embeddedartists 4:b32cf4ef45c5 118
embeddedartists 4:b32cf4ef45c5 119 // nothing to read in standby mode
embeddedartists 4:b32cf4ef45c5 120 if (_mode == ModeStandby) return false;
embeddedartists 4:b32cf4ef45c5 121
embeddedartists 4:b32cf4ef45c5 122 // wait for ready flag
embeddedartists 4:b32cf4ef45c5 123 int status = 0;
embeddedartists 4:b32cf4ef45c5 124 do {
embeddedartists 4:b32cf4ef45c5 125 status = getStatus();
embeddedartists 4:b32cf4ef45c5 126 } while (status >= 0 && (status & MMA7455_STATUS_DRDY) == 0);
embeddedartists 4:b32cf4ef45c5 127
embeddedartists 4:b32cf4ef45c5 128
embeddedartists 4:b32cf4ef45c5 129 do {
embeddedartists 4:b32cf4ef45c5 130 if (status < 0) break;
embeddedartists 4:b32cf4ef45c5 131
embeddedartists 4:b32cf4ef45c5 132
embeddedartists 4:b32cf4ef45c5 133 char buf[6];
embeddedartists 4:b32cf4ef45c5 134 buf[0] = MMA7455_ADDR_XOUTL;
embeddedartists 4:b32cf4ef45c5 135 if (_i2c.write(MMA7455_I2C_ADDR, buf, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 136 if (_i2c.read(MMA7455_I2C_ADDR, buf, 6) != 0) break;
embeddedartists 4:b32cf4ef45c5 137
embeddedartists 4:b32cf4ef45c5 138 // check if second bit is set in high byte -> negative value
embeddedartists 4:b32cf4ef45c5 139 // expand negative value to full byte
embeddedartists 4:b32cf4ef45c5 140 if (buf[1] & 0x02) buf[1] |= 0xFC;
embeddedartists 4:b32cf4ef45c5 141 if (buf[3] & 0x02) buf[3] |= 0xFC;
embeddedartists 4:b32cf4ef45c5 142 if (buf[5] & 0x02) buf[5] |= 0xFC;
embeddedartists 4:b32cf4ef45c5 143
embeddedartists 4:b32cf4ef45c5 144 x = (int16_t)((buf[1] << 8) | buf[0]) + _xOff;
embeddedartists 4:b32cf4ef45c5 145 y = (int16_t)((buf[3] << 8) | buf[2]) + _yOff;
embeddedartists 4:b32cf4ef45c5 146 z = (int16_t)((buf[5] << 8) | buf[4]) + _zOff;
embeddedartists 4:b32cf4ef45c5 147
embeddedartists 4:b32cf4ef45c5 148
embeddedartists 4:b32cf4ef45c5 149 result = true;
embeddedartists 4:b32cf4ef45c5 150
embeddedartists 4:b32cf4ef45c5 151 } while(0);
embeddedartists 4:b32cf4ef45c5 152
embeddedartists 4:b32cf4ef45c5 153
embeddedartists 4:b32cf4ef45c5 154 return result;
embeddedartists 4:b32cf4ef45c5 155 }
embeddedartists 4:b32cf4ef45c5 156
embeddedartists 4:b32cf4ef45c5 157 bool MMA7455::calibrate() {
embeddedartists 4:b32cf4ef45c5 158 bool result = false;
embeddedartists 4:b32cf4ef45c5 159 bool failed = false;
embeddedartists 4:b32cf4ef45c5 160
embeddedartists 4:b32cf4ef45c5 161 int32_t x = 0;
embeddedartists 4:b32cf4ef45c5 162 int32_t y = 0;
embeddedartists 4:b32cf4ef45c5 163 int32_t z = 0;
embeddedartists 4:b32cf4ef45c5 164
embeddedartists 4:b32cf4ef45c5 165 int32_t xr = 0;
embeddedartists 4:b32cf4ef45c5 166 int32_t yr = 0;
embeddedartists 4:b32cf4ef45c5 167 int32_t zr = 0;
embeddedartists 4:b32cf4ef45c5 168
embeddedartists 4:b32cf4ef45c5 169 int xOff = 0;
embeddedartists 4:b32cf4ef45c5 170 int yOff = 0;
embeddedartists 4:b32cf4ef45c5 171 int zOff = 16;
embeddedartists 4:b32cf4ef45c5 172 if (_range == Range_2g) {
embeddedartists 4:b32cf4ef45c5 173 zOff = 64;
embeddedartists 4:b32cf4ef45c5 174 }
embeddedartists 4:b32cf4ef45c5 175 if (_range == Range_4g) {
embeddedartists 4:b32cf4ef45c5 176 zOff = 32;
embeddedartists 4:b32cf4ef45c5 177 }
embeddedartists 4:b32cf4ef45c5 178
embeddedartists 4:b32cf4ef45c5 179 do {
embeddedartists 4:b32cf4ef45c5 180
embeddedartists 4:b32cf4ef45c5 181 // get an average of 6 values
embeddedartists 4:b32cf4ef45c5 182 for (int i = 0; i < 6; i++) {
embeddedartists 4:b32cf4ef45c5 183 if (!read(xr, yr, zr)) {
embeddedartists 4:b32cf4ef45c5 184 failed = true;
embeddedartists 4:b32cf4ef45c5 185 break;
embeddedartists 4:b32cf4ef45c5 186 }
embeddedartists 4:b32cf4ef45c5 187 x += xr;
embeddedartists 4:b32cf4ef45c5 188 y += yr;
embeddedartists 4:b32cf4ef45c5 189 z += zr;
embeddedartists 4:b32cf4ef45c5 190
embeddedartists 4:b32cf4ef45c5 191 wait_ms(100);
embeddedartists 4:b32cf4ef45c5 192 }
embeddedartists 4:b32cf4ef45c5 193
embeddedartists 4:b32cf4ef45c5 194 if (failed) break;
embeddedartists 4:b32cf4ef45c5 195 x /= 6;
embeddedartists 4:b32cf4ef45c5 196 y /= 6;
embeddedartists 4:b32cf4ef45c5 197 z /= 6;
embeddedartists 4:b32cf4ef45c5 198
embeddedartists 4:b32cf4ef45c5 199 xOff -= x;
embeddedartists 4:b32cf4ef45c5 200 yOff -= y;
embeddedartists 4:b32cf4ef45c5 201 zOff -= z;
embeddedartists 4:b32cf4ef45c5 202
embeddedartists 4:b32cf4ef45c5 203 /*
embeddedartists 4:b32cf4ef45c5 204 * For some reason we have not got correct/reliable calibration
embeddedartists 4:b32cf4ef45c5 205 * by using the offset drift registers. Instead we are
embeddedartists 4:b32cf4ef45c5 206 * calculating the offsets and store them in member variables.
embeddedartists 4:b32cf4ef45c5 207 *
embeddedartists 4:b32cf4ef45c5 208 * These member variables are then used in the read() method
embeddedartists 4:b32cf4ef45c5 209 */
embeddedartists 4:b32cf4ef45c5 210
embeddedartists 4:b32cf4ef45c5 211 _xOff = xOff;
embeddedartists 4:b32cf4ef45c5 212 _yOff = yOff;
embeddedartists 4:b32cf4ef45c5 213 _zOff = zOff;
embeddedartists 4:b32cf4ef45c5 214
embeddedartists 4:b32cf4ef45c5 215
embeddedartists 4:b32cf4ef45c5 216 result = true;
embeddedartists 4:b32cf4ef45c5 217
embeddedartists 4:b32cf4ef45c5 218 } while (0);
embeddedartists 4:b32cf4ef45c5 219
embeddedartists 4:b32cf4ef45c5 220
embeddedartists 4:b32cf4ef45c5 221
embeddedartists 4:b32cf4ef45c5 222 return result;
embeddedartists 4:b32cf4ef45c5 223 }
embeddedartists 4:b32cf4ef45c5 224
embeddedartists 4:b32cf4ef45c5 225 bool MMA7455::setCalibrationOffsets(int32_t xOff, int32_t yOff, int32_t zOff) {
embeddedartists 4:b32cf4ef45c5 226 _xOff = xOff;
embeddedartists 4:b32cf4ef45c5 227 _yOff = yOff;
embeddedartists 4:b32cf4ef45c5 228 _zOff = zOff;
embeddedartists 4:b32cf4ef45c5 229
embeddedartists 4:b32cf4ef45c5 230 return true;
embeddedartists 4:b32cf4ef45c5 231 }
embeddedartists 4:b32cf4ef45c5 232
embeddedartists 4:b32cf4ef45c5 233 bool MMA7455::getCalibrationOffsets(int32_t& xOff, int32_t& yOff, int32_t& zOff) {
embeddedartists 4:b32cf4ef45c5 234 xOff = _xOff;
embeddedartists 4:b32cf4ef45c5 235 yOff = _yOff;
embeddedartists 4:b32cf4ef45c5 236 zOff = _zOff;
embeddedartists 4:b32cf4ef45c5 237
embeddedartists 4:b32cf4ef45c5 238 return true;
embeddedartists 4:b32cf4ef45c5 239 }
embeddedartists 4:b32cf4ef45c5 240
embeddedartists 4:b32cf4ef45c5 241 int MMA7455::getStatus() {
embeddedartists 4:b32cf4ef45c5 242 int result = -1;
embeddedartists 4:b32cf4ef45c5 243 char data[1];
embeddedartists 4:b32cf4ef45c5 244
embeddedartists 4:b32cf4ef45c5 245 do {
embeddedartists 4:b32cf4ef45c5 246 data[0] = MMA7455_ADDR_STATUS;
embeddedartists 4:b32cf4ef45c5 247 if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 248
embeddedartists 4:b32cf4ef45c5 249 if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 250
embeddedartists 4:b32cf4ef45c5 251 result = data[0];
embeddedartists 4:b32cf4ef45c5 252
embeddedartists 4:b32cf4ef45c5 253 } while (0);
embeddedartists 4:b32cf4ef45c5 254
embeddedartists 4:b32cf4ef45c5 255
embeddedartists 4:b32cf4ef45c5 256
embeddedartists 4:b32cf4ef45c5 257 return result;
embeddedartists 4:b32cf4ef45c5 258 }
embeddedartists 4:b32cf4ef45c5 259
embeddedartists 4:b32cf4ef45c5 260 int MMA7455::getModeControl() {
embeddedartists 4:b32cf4ef45c5 261
embeddedartists 4:b32cf4ef45c5 262 int result = -1;
embeddedartists 4:b32cf4ef45c5 263 char data[1];
embeddedartists 4:b32cf4ef45c5 264
embeddedartists 4:b32cf4ef45c5 265 do {
embeddedartists 4:b32cf4ef45c5 266 data[0] = MMA7455_ADDR_MCTL;
embeddedartists 4:b32cf4ef45c5 267 if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 268
embeddedartists 4:b32cf4ef45c5 269 if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break;
embeddedartists 4:b32cf4ef45c5 270
embeddedartists 4:b32cf4ef45c5 271 result = data[0];
embeddedartists 4:b32cf4ef45c5 272
embeddedartists 4:b32cf4ef45c5 273 } while (0);
embeddedartists 4:b32cf4ef45c5 274
embeddedartists 4:b32cf4ef45c5 275
embeddedartists 4:b32cf4ef45c5 276
embeddedartists 4:b32cf4ef45c5 277 return result;
embeddedartists 4:b32cf4ef45c5 278 }
embeddedartists 4:b32cf4ef45c5 279
embeddedartists 4:b32cf4ef45c5 280 int MMA7455::setModeControl(uint8_t mctl) {
embeddedartists 4:b32cf4ef45c5 281 int result = -1;
embeddedartists 4:b32cf4ef45c5 282 char data[2];
embeddedartists 4:b32cf4ef45c5 283
embeddedartists 4:b32cf4ef45c5 284 do {
embeddedartists 4:b32cf4ef45c5 285 data[0] = MMA7455_ADDR_MCTL;
embeddedartists 4:b32cf4ef45c5 286 data[1] = (char)mctl;
embeddedartists 4:b32cf4ef45c5 287 if (_i2c.write(MMA7455_I2C_ADDR, data, 2) != 0) break;
embeddedartists 4:b32cf4ef45c5 288
embeddedartists 4:b32cf4ef45c5 289 result = 0;
embeddedartists 4:b32cf4ef45c5 290
embeddedartists 4:b32cf4ef45c5 291 } while (0);
embeddedartists 4:b32cf4ef45c5 292
embeddedartists 4:b32cf4ef45c5 293
embeddedartists 4:b32cf4ef45c5 294
embeddedartists 4:b32cf4ef45c5 295 return result;
embeddedartists 4:b32cf4ef45c5 296 }
embeddedartists 4:b32cf4ef45c5 297
embeddedartists 4:b32cf4ef45c5 298