Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Committer:
arnoz
Date:
Fri Oct 01 08:19:46 2021 +0000
Revision:
116:7a67265d7c19
Parent:
77:0b96f6867312
- Correct information regarding your last merge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 1:d913e0afb2ac 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
mjr 1:d913e0afb2ac 2 *
mjr 1:d913e0afb2ac 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mjr 1:d913e0afb2ac 4 * and associated documentation files (the "Software"), to deal in the Software without
mjr 1:d913e0afb2ac 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mjr 1:d913e0afb2ac 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mjr 1:d913e0afb2ac 7 * Software is furnished to do so, subject to the following conditions:
mjr 1:d913e0afb2ac 8 *
mjr 1:d913e0afb2ac 9 * The above copyright notice and this permission notice shall be included in all copies or
mjr 1:d913e0afb2ac 10 * substantial portions of the Software.
mjr 1:d913e0afb2ac 11 *
mjr 1:d913e0afb2ac 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mjr 1:d913e0afb2ac 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mjr 1:d913e0afb2ac 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mjr 1:d913e0afb2ac 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mjr 1:d913e0afb2ac 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mjr 1:d913e0afb2ac 17 */
mjr 1:d913e0afb2ac 18
mjr 1:d913e0afb2ac 19 #ifndef MMA8451Q_H
mjr 1:d913e0afb2ac 20 #define MMA8451Q_H
mjr 1:d913e0afb2ac 21
mjr 1:d913e0afb2ac 22 #include "mbed.h"
mjr 1:d913e0afb2ac 23
mjr 1:d913e0afb2ac 24 /**
mjr 1:d913e0afb2ac 25 * MMA8451Q accelerometer example
mjr 1:d913e0afb2ac 26 *
mjr 1:d913e0afb2ac 27 * @code
mjr 1:d913e0afb2ac 28 * #include "mbed.h"
mjr 1:d913e0afb2ac 29 * #include "MMA8451Q.h"
mjr 1:d913e0afb2ac 30 *
mjr 1:d913e0afb2ac 31 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
mjr 1:d913e0afb2ac 32 *
mjr 1:d913e0afb2ac 33 * int main(void) {
mjr 1:d913e0afb2ac 34 *
mjr 1:d913e0afb2ac 35 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
mjr 1:d913e0afb2ac 36 * PwmOut rled(LED_RED);
mjr 1:d913e0afb2ac 37 * PwmOut gled(LED_GREEN);
mjr 1:d913e0afb2ac 38 * PwmOut bled(LED_BLUE);
mjr 1:d913e0afb2ac 39 *
mjr 1:d913e0afb2ac 40 * while (true) {
mjr 1:d913e0afb2ac 41 * rled = 1.0 - abs(acc.getAccX());
mjr 1:d913e0afb2ac 42 * gled = 1.0 - abs(acc.getAccY());
mjr 1:d913e0afb2ac 43 * bled = 1.0 - abs(acc.getAccZ());
mjr 1:d913e0afb2ac 44 * wait(0.1);
mjr 1:d913e0afb2ac 45 * }
mjr 1:d913e0afb2ac 46 * }
mjr 1:d913e0afb2ac 47 * @endcode
mjr 1:d913e0afb2ac 48 */
mjr 1:d913e0afb2ac 49 class MMA8451Q
mjr 1:d913e0afb2ac 50 {
mjr 1:d913e0afb2ac 51 public:
mjr 1:d913e0afb2ac 52 /**
mjr 1:d913e0afb2ac 53 * MMA8451Q constructor
mjr 1:d913e0afb2ac 54 *
mjr 1:d913e0afb2ac 55 * @param sda SDA pin
mjr 1:d913e0afb2ac 56 * @param sdl SCL pin
mjr 1:d913e0afb2ac 57 * @param addr addr of the I2C peripheral
mjr 1:d913e0afb2ac 58 */
mjr 1:d913e0afb2ac 59 MMA8451Q(PinName sda, PinName scl, int addr);
mjr 1:d913e0afb2ac 60
mjr 1:d913e0afb2ac 61 /**
mjr 1:d913e0afb2ac 62 * MMA8451Q destructor
mjr 1:d913e0afb2ac 63 */
mjr 1:d913e0afb2ac 64 ~MMA8451Q();
mjr 5:a70c0bce770d 65
mjr 5:a70c0bce770d 66 /**
mjr 5:a70c0bce770d 67 * Reset the accelerometer hardware and set our initial parameters
mjr 5:a70c0bce770d 68 */
mjr 5:a70c0bce770d 69 void init();
mjr 1:d913e0afb2ac 70
mjr 1:d913e0afb2ac 71 /**
mjr 1:d913e0afb2ac 72 * Enter standby mode
mjr 1:d913e0afb2ac 73 */
mjr 1:d913e0afb2ac 74 void standby();
mjr 1:d913e0afb2ac 75
mjr 1:d913e0afb2ac 76 /**
mjr 1:d913e0afb2ac 77 * Enter active mode
mjr 1:d913e0afb2ac 78 */
mjr 1:d913e0afb2ac 79 void active();
mjr 1:d913e0afb2ac 80
mjr 1:d913e0afb2ac 81 /**
mjr 1:d913e0afb2ac 82 * Get the value of the WHO_AM_I register
mjr 1:d913e0afb2ac 83 *
mjr 1:d913e0afb2ac 84 * @returns WHO_AM_I value
mjr 1:d913e0afb2ac 85 */
mjr 1:d913e0afb2ac 86 uint8_t getWhoAmI();
mjr 1:d913e0afb2ac 87
mjr 1:d913e0afb2ac 88 /**
mjr 1:d913e0afb2ac 89 * Get X axis acceleration
mjr 1:d913e0afb2ac 90 *
mjr 1:d913e0afb2ac 91 * @returns X axis acceleration
mjr 1:d913e0afb2ac 92 */
mjr 1:d913e0afb2ac 93 float getAccX();
mjr 1:d913e0afb2ac 94
mjr 1:d913e0afb2ac 95 /**
mjr 1:d913e0afb2ac 96 * Get Y axis acceleration
mjr 1:d913e0afb2ac 97 *
mjr 1:d913e0afb2ac 98 * @returns Y axis acceleration
mjr 1:d913e0afb2ac 99 */
mjr 1:d913e0afb2ac 100 float getAccY();
mjr 1:d913e0afb2ac 101
mjr 1:d913e0afb2ac 102 /**
mjr 1:d913e0afb2ac 103 * Read an X,Y pair
mjr 1:d913e0afb2ac 104 */
mjr 1:d913e0afb2ac 105 void getAccXY(float &x, float &y);
mjr 3:3514575d4f86 106
mjr 3:3514575d4f86 107 /**
mjr 77:0b96f6867312 108 * Read X,Y,Z as floats. This is the second most efficient way
mjr 77:0b96f6867312 109 * to fetch all three axes (after the integer version), since it
mjr 77:0b96f6867312 110 * fetches all axes in a single I2C transaction.
mjr 3:3514575d4f86 111 */
mjr 3:3514575d4f86 112 void getAccXYZ(float &x, float &y, float &z);
mjr 77:0b96f6867312 113
mjr 77:0b96f6867312 114 /**
mjr 77:0b96f6867312 115 * Read X,Y,Z as integers. This reads the three axes in a single
mjr 77:0b96f6867312 116 * I2C transaction and returns them in the native integer scale,
mjr 77:0b96f6867312 117 * so it's the most efficient way to read the current 3D status.
mjr 77:0b96f6867312 118 * Each axis value is represented an an integer using the device's
mjr 77:0b96f6867312 119 * native 14-bit scale, so each is in the range -8192..+8191.
mjr 77:0b96f6867312 120 */
mjr 77:0b96f6867312 121 void getAccXYZ(int &x, int &y, int &z);
mjr 1:d913e0afb2ac 122
mjr 1:d913e0afb2ac 123 /**
mjr 1:d913e0afb2ac 124 * Get Z axis acceleration
mjr 1:d913e0afb2ac 125 *
mjr 1:d913e0afb2ac 126 * @returns Z axis acceleration
mjr 1:d913e0afb2ac 127 */
mjr 1:d913e0afb2ac 128 float getAccZ();
mjr 1:d913e0afb2ac 129
mjr 1:d913e0afb2ac 130 /**
mjr 1:d913e0afb2ac 131 * Get XYZ axis acceleration
mjr 1:d913e0afb2ac 132 *
mjr 1:d913e0afb2ac 133 * @param res array where acceleration data will be stored
mjr 1:d913e0afb2ac 134 */
mjr 1:d913e0afb2ac 135 void getAccAllAxis(float * res);
mjr 3:3514575d4f86 136
mjr 3:3514575d4f86 137 /**
mjr 3:3514575d4f86 138 * Set interrupt mode. 'pin' is 1 for INT1_ACCEL (PTA14) and 2 for INT2_ACCEL (PTA15).
mjr 3:3514575d4f86 139 * The caller is responsible for setting up an interrupt handler on the corresponding
mjr 3:3514575d4f86 140 * PTAxx pin.
mjr 3:3514575d4f86 141 */
mjr 3:3514575d4f86 142 void setInterruptMode(int pin);
mjr 76:7f5912b6340e 143
mjr 76:7f5912b6340e 144 /**
mjr 77:0b96f6867312 145 * Set the hardware dynamic range, in G. Valid ranges are 2, 4, and 8.
mjr 77:0b96f6867312 146 */
mjr 77:0b96f6867312 147 void setRange(int g);
mjr 77:0b96f6867312 148
mjr 77:0b96f6867312 149 /**
mjr 76:7f5912b6340e 150 * Disable interrupts.
mjr 76:7f5912b6340e 151 */
mjr 76:7f5912b6340e 152 void clearInterruptMode();
mjr 77:0b96f6867312 153
mjr 77:0b96f6867312 154 /**
mjr 77:0b96f6867312 155 * Is a sample ready?
mjr 77:0b96f6867312 156 */
mjr 77:0b96f6867312 157 bool sampleReady();
mjr 77:0b96f6867312 158
mjr 77:0b96f6867312 159 /**
mjr 77:0b96f6867312 160 * Get the number of FIFO samples available
mjr 77:0b96f6867312 161 */
mjr 77:0b96f6867312 162 int getFIFOCount();
mjr 1:d913e0afb2ac 163
mjr 1:d913e0afb2ac 164 private:
mjr 1:d913e0afb2ac 165 I2C m_i2c;
mjr 1:d913e0afb2ac 166 int m_addr;
mjr 1:d913e0afb2ac 167 void readRegs(int addr, uint8_t * data, int len);
mjr 1:d913e0afb2ac 168 void writeRegs(uint8_t * data, int len);
mjr 1:d913e0afb2ac 169 int16_t getAccAxis(uint8_t addr);
mjr 77:0b96f6867312 170
mjr 77:0b96f6867312 171 // Translate a 14-bit register value to a signed integer. The
mjr 77:0b96f6867312 172 // most significant 8 bits are in the first byte, and the least
mjr 77:0b96f6867312 173 // significant 6 bits are in the second byte. To adjust to a
mjr 77:0b96f6867312 174 // regular integer, left-justify the 14 bits in an int16_t, then
mjr 77:0b96f6867312 175 // divide by 4 to shift out the unused low two bits. Note that
mjr 77:0b96f6867312 176 // we have to divide rather than right-shift (>>) to ensure proper
mjr 77:0b96f6867312 177 // filling of the sign bits. The compiler should convert the
mjr 77:0b96f6867312 178 // divide-by-4 to an arithmetic shift right by 2, so this should
mjr 77:0b96f6867312 179 // still be efficient.
mjr 77:0b96f6867312 180 inline int xlat14(const uint8_t *buf)
mjr 77:0b96f6867312 181 {
mjr 77:0b96f6867312 182 // Store the 16 bits left-justified in an int16_t, then cast
mjr 77:0b96f6867312 183 // to a regular int to sign-extend to the full int width.
mjr 77:0b96f6867312 184 // Divide the result by 4 to shift out the unused 2 bits
mjr 77:0b96f6867312 185 // at the right end.
mjr 77:0b96f6867312 186 return int(int16_t((buf[0] << 8) | buf[1])) / 4;
mjr 77:0b96f6867312 187 }
mjr 1:d913e0afb2ac 188
mjr 1:d913e0afb2ac 189 };
mjr 1:d913e0afb2ac 190
mjr 1:d913e0afb2ac 191 #endif