Forked MMA7660 , extend implementation by using i2c asynch API, to sleep while waiting for transfer -> blocking asynch :-D
Fork of MMA7660 by
MMA7660.h@5:556829f081f6, 2015-05-05 (annotated)
- Committer:
- Kojto
- Date:
- Tue May 05 07:23:40 2015 +0000
- Revision:
- 5:556829f081f6
- Parent:
- 2:a8e20db7901e
Support I2C Asynch API
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 2:a8e20db7901e | 1 | /* Copyright (c) <year> <copyright holders>, MIT License |
Sissors | 2:a8e20db7901e | 2 | * |
Sissors | 2:a8e20db7901e | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Sissors | 2:a8e20db7901e | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Sissors | 2:a8e20db7901e | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Sissors | 2:a8e20db7901e | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Sissors | 2:a8e20db7901e | 7 | * furnished to do so, subject to the following conditions: |
Sissors | 2:a8e20db7901e | 8 | * |
Sissors | 2:a8e20db7901e | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Sissors | 2:a8e20db7901e | 10 | * substantial portions of the Software. |
Sissors | 2:a8e20db7901e | 11 | * |
Sissors | 2:a8e20db7901e | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Sissors | 2:a8e20db7901e | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Sissors | 2:a8e20db7901e | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Sissors | 2:a8e20db7901e | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Sissors | 2:a8e20db7901e | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sissors | 2:a8e20db7901e | 17 | */ |
Sissors | 2:a8e20db7901e | 18 | |
Sissors | 0:7bc29a9ea016 | 19 | #include "mbed.h" |
Sissors | 0:7bc29a9ea016 | 20 | |
Sissors | 0:7bc29a9ea016 | 21 | |
Sissors | 0:7bc29a9ea016 | 22 | #ifndef MMA7660_H |
Sissors | 0:7bc29a9ea016 | 23 | #define MMA7660_H |
Sissors | 0:7bc29a9ea016 | 24 | |
Sissors | 0:7bc29a9ea016 | 25 | #define MMA7660_ADDRESS 0x98 |
Sissors | 0:7bc29a9ea016 | 26 | #define MMA7660_SENSITIVITY 21.33 |
Sissors | 0:7bc29a9ea016 | 27 | |
Sissors | 0:7bc29a9ea016 | 28 | #define MMA7660_XOUT_R 0x00 |
Sissors | 0:7bc29a9ea016 | 29 | #define MMA7660_YOUT_R 0x01 |
Sissors | 0:7bc29a9ea016 | 30 | #define MMA7660_ZOUT_R 0x02 |
Sissors | 1:8997a1b348dd | 31 | #define MMA7660_TILT_R 0x03 |
Sissors | 1:8997a1b348dd | 32 | #define MMA7660_INT_R 0x06 |
Sissors | 0:7bc29a9ea016 | 33 | #define MMA7660_MODE_R 0x07 |
Sissors | 1:8997a1b348dd | 34 | #define MMA7660_SR_R 0x08 |
Sissors | 1:8997a1b348dd | 35 | |
Sissors | 0:7bc29a9ea016 | 36 | |
Sissors | 2:a8e20db7901e | 37 | /** An interface for the MMA7660 triple axis accelerometer |
Sissors | 2:a8e20db7901e | 38 | * |
Sissors | 2:a8e20db7901e | 39 | * @code |
Sissors | 2:a8e20db7901e | 40 | * //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed |
Sissors | 2:a8e20db7901e | 41 | * |
Sissors | 2:a8e20db7901e | 42 | * #include "mbed.h" |
Sissors | 2:a8e20db7901e | 43 | * #include "MMA7660.h" |
Sissors | 2:a8e20db7901e | 44 | * |
Sissors | 2:a8e20db7901e | 45 | * MMA7660 MMA(p28, p27); |
Sissors | 2:a8e20db7901e | 46 | * |
Sissors | 2:a8e20db7901e | 47 | * DigitalOut connectionLed(LED1); |
Sissors | 2:a8e20db7901e | 48 | * PwmOut Zaxis_p(LED2); |
Sissors | 2:a8e20db7901e | 49 | * PwmOut Zaxis_n(LED3); |
Sissors | 2:a8e20db7901e | 50 | * |
Sissors | 2:a8e20db7901e | 51 | * int main() { |
Sissors | 2:a8e20db7901e | 52 | * if (MMA.testConnection()) |
Sissors | 2:a8e20db7901e | 53 | * connectionLed = 1; |
Sissors | 2:a8e20db7901e | 54 | * |
Sissors | 2:a8e20db7901e | 55 | * while(1) { |
Sissors | 2:a8e20db7901e | 56 | * Zaxis_p = MMA.z(); |
Sissors | 2:a8e20db7901e | 57 | * Zaxis_n = -MMA.z(); |
Sissors | 2:a8e20db7901e | 58 | * } |
Sissors | 2:a8e20db7901e | 59 | * |
Sissors | 2:a8e20db7901e | 60 | * } |
Sissors | 2:a8e20db7901e | 61 | * @endcode |
Sissors | 2:a8e20db7901e | 62 | */ |
Sissors | 0:7bc29a9ea016 | 63 | class MMA7660 |
Sissors | 0:7bc29a9ea016 | 64 | { |
Sissors | 0:7bc29a9ea016 | 65 | public: |
Sissors | 2:a8e20db7901e | 66 | /** |
Sissors | 1:8997a1b348dd | 67 | * The 6 different orientations and unknown |
Sissors | 1:8997a1b348dd | 68 | * |
Sissors | 1:8997a1b348dd | 69 | * Up & Down = X-axis |
Sissors | 1:8997a1b348dd | 70 | * Right & Left = Y-axis |
Sissors | 1:8997a1b348dd | 71 | * Back & Front = Z-axis |
Sissors | 2:a8e20db7901e | 72 | * |
Sissors | 1:8997a1b348dd | 73 | */ |
Sissors | 2:a8e20db7901e | 74 | enum Orientation {Up, Down, |
Sissors | 2:a8e20db7901e | 75 | Right, Left, |
Sissors | 2:a8e20db7901e | 76 | Back, Front, |
Sissors | 2:a8e20db7901e | 77 | Unknown |
Sissors | 2:a8e20db7901e | 78 | }; |
Sissors | 2:a8e20db7901e | 79 | |
Sissors | 0:7bc29a9ea016 | 80 | /** |
Sissors | 0:7bc29a9ea016 | 81 | * Creates a new MMA7660 object |
Sissors | 0:7bc29a9ea016 | 82 | * |
Sissors | 0:7bc29a9ea016 | 83 | * @param sda - I2C data pin |
Sissors | 0:7bc29a9ea016 | 84 | * @param scl - I2C clock pin |
Sissors | 2:a8e20db7901e | 85 | * @param active - true (default) to enable the device, false to keep it standby |
Kojto | 5:556829f081f6 | 86 | * @param asynch - use asynch i2c - the API is blocking which does not break the current app |
Sissors | 0:7bc29a9ea016 | 87 | */ |
Kojto | 5:556829f081f6 | 88 | MMA7660(PinName sda, PinName scl, bool active = true, bool asynch = false); |
Sissors | 0:7bc29a9ea016 | 89 | |
Sissors | 0:7bc29a9ea016 | 90 | /** |
Sissors | 0:7bc29a9ea016 | 91 | * Tests if communication is possible with the MMA7660 |
Sissors | 0:7bc29a9ea016 | 92 | * |
Sissors | 0:7bc29a9ea016 | 93 | * Because the MMA7660 lacks a WHO_AM_I register, this function can only check |
Sissors | 0:7bc29a9ea016 | 94 | * if there is an I2C device that responds to the MMA7660 address |
Sissors | 0:7bc29a9ea016 | 95 | * |
Sissors | 0:7bc29a9ea016 | 96 | * @param return - true for successfull connection, false for no connection |
Sissors | 0:7bc29a9ea016 | 97 | */ |
Sissors | 0:7bc29a9ea016 | 98 | bool testConnection( void ); |
Sissors | 2:a8e20db7901e | 99 | |
Sissors | 0:7bc29a9ea016 | 100 | /** |
Sissors | 0:7bc29a9ea016 | 101 | * Sets the active state of the MMA7660 |
Sissors | 0:7bc29a9ea016 | 102 | * |
Sissors | 0:7bc29a9ea016 | 103 | * Note: This is unrelated to awake/sleep mode |
Sissors | 0:7bc29a9ea016 | 104 | * |
Sissors | 0:7bc29a9ea016 | 105 | * @param state - true for active, false for standby |
Sissors | 0:7bc29a9ea016 | 106 | */ |
Sissors | 0:7bc29a9ea016 | 107 | void setActive( bool state); |
Sissors | 2:a8e20db7901e | 108 | |
Sissors | 0:7bc29a9ea016 | 109 | /** |
Sissors | 0:7bc29a9ea016 | 110 | * Reads acceleration data from the sensor |
Sissors | 0:7bc29a9ea016 | 111 | * |
Sissors | 2:a8e20db7901e | 112 | * When the parameter is a pointer to an integer array it will be the raw data. |
Sissors | 0:7bc29a9ea016 | 113 | * When it is a pointer to a float array it will be the acceleration in g's |
Sissors | 2:a8e20db7901e | 114 | * |
Sissors | 2:a8e20db7901e | 115 | * @param data - pointer to array with length 3 where the acceleration data will be stored, X-Y-Z |
Sissors | 0:7bc29a9ea016 | 116 | */ |
Sissors | 0:7bc29a9ea016 | 117 | void readData( int *data); |
Sissors | 0:7bc29a9ea016 | 118 | void readData( float *data); |
Sissors | 2:a8e20db7901e | 119 | |
Sissors | 2:a8e20db7901e | 120 | /** |
Sissors | 1:8997a1b348dd | 121 | * Get X-data |
Sissors | 0:7bc29a9ea016 | 122 | * |
Sissors | 0:7bc29a9ea016 | 123 | * @param return - X-acceleration in g's |
Sissors | 0:7bc29a9ea016 | 124 | */ |
Sissors | 2:a8e20db7901e | 125 | float x( void ); |
Sissors | 2:a8e20db7901e | 126 | |
Sissors | 2:a8e20db7901e | 127 | /** |
Sissors | 1:8997a1b348dd | 128 | * Get Y-data |
Sissors | 0:7bc29a9ea016 | 129 | * |
Sissors | 0:7bc29a9ea016 | 130 | * @param return - Y-acceleration in g's |
Sissors | 0:7bc29a9ea016 | 131 | */ |
Sissors | 2:a8e20db7901e | 132 | float y( void ); |
Sissors | 2:a8e20db7901e | 133 | |
Sissors | 2:a8e20db7901e | 134 | /** |
Sissors | 1:8997a1b348dd | 135 | * Get Z-data |
Sissors | 0:7bc29a9ea016 | 136 | * |
Sissors | 0:7bc29a9ea016 | 137 | * @param return - Z-acceleration in g's |
Sissors | 0:7bc29a9ea016 | 138 | */ |
Sissors | 2:a8e20db7901e | 139 | float z( void ); |
Sissors | 2:a8e20db7901e | 140 | |
Sissors | 2:a8e20db7901e | 141 | /** |
Sissors | 1:8997a1b348dd | 142 | * Sets the active samplerate |
Sissors | 1:8997a1b348dd | 143 | * |
Sissors | 1:8997a1b348dd | 144 | * The entered samplerate will be rounded to nearest supported samplerate. |
Sissors | 1:8997a1b348dd | 145 | * Supported samplerates are: 120 - 64 - 32 - 16 - 8 - 4 - 2 - 1 samples/second. |
Sissors | 1:8997a1b348dd | 146 | * |
Sissors | 1:8997a1b348dd | 147 | * @param samplerate - the samplerate that will be set |
Sissors | 1:8997a1b348dd | 148 | */ |
Sissors | 2:a8e20db7901e | 149 | void setSampleRate(int samplerate); |
Sissors | 1:8997a1b348dd | 150 | |
Sissors | 1:8997a1b348dd | 151 | /** |
Sissors | 1:8997a1b348dd | 152 | * Returns if it is on its front, back, or unknown side |
Sissors | 1:8997a1b348dd | 153 | * |
Sissors | 1:8997a1b348dd | 154 | * This is read from MMA7760s registers, page 12 of datasheet |
Sissors | 1:8997a1b348dd | 155 | * |
Sissors | 1:8997a1b348dd | 156 | * @param return - Front, Back or Unknown orientation |
Sissors | 1:8997a1b348dd | 157 | */ |
Sissors | 1:8997a1b348dd | 158 | Orientation getSide( void ); |
Sissors | 2:a8e20db7901e | 159 | |
Sissors | 1:8997a1b348dd | 160 | /** |
Sissors | 1:8997a1b348dd | 161 | * Returns if it is on it left, right, down or up side |
Sissors | 1:8997a1b348dd | 162 | * |
Sissors | 1:8997a1b348dd | 163 | * This is read from MMA7760s registers, page 12 of datasheet |
Sissors | 1:8997a1b348dd | 164 | * |
Sissors | 1:8997a1b348dd | 165 | * @param return - Left, Right, Down, Up or Unknown orientation |
Sissors | 1:8997a1b348dd | 166 | */ |
Sissors | 1:8997a1b348dd | 167 | Orientation getOrientation ( void ); |
Sissors | 0:7bc29a9ea016 | 168 | |
Sissors | 0:7bc29a9ea016 | 169 | |
Sissors | 0:7bc29a9ea016 | 170 | private: |
Sissors | 0:7bc29a9ea016 | 171 | |
Kojto | 5:556829f081f6 | 172 | void callback(int event); |
Kojto | 5:556829f081f6 | 173 | |
Sissors | 0:7bc29a9ea016 | 174 | /** |
Sissors | 1:8997a1b348dd | 175 | * Writes data to the device |
Sissors | 0:7bc29a9ea016 | 176 | * |
Sissors | 0:7bc29a9ea016 | 177 | * @param adress - register address to write to |
Sissors | 0:7bc29a9ea016 | 178 | * @param data - data to write |
Sissors | 0:7bc29a9ea016 | 179 | */ |
Sissors | 0:7bc29a9ea016 | 180 | void write( char address, char data); |
Sissors | 0:7bc29a9ea016 | 181 | |
Sissors | 0:7bc29a9ea016 | 182 | /** |
Sissors | 1:8997a1b348dd | 183 | * Read data from the device |
Sissors | 0:7bc29a9ea016 | 184 | * |
Sissors | 0:7bc29a9ea016 | 185 | * @param adress - register address to write to |
Sissors | 0:7bc29a9ea016 | 186 | * @return - data from the register specified by RA |
Sissors | 0:7bc29a9ea016 | 187 | */ |
Sissors | 0:7bc29a9ea016 | 188 | char read( char adress); |
Sissors | 2:a8e20db7901e | 189 | |
Sissors | 0:7bc29a9ea016 | 190 | /** |
Sissors | 2:a8e20db7901e | 191 | * Read multiple regigsters from the device, more efficient than using multiple normal reads. |
Sissors | 0:7bc29a9ea016 | 192 | * |
Sissors | 0:7bc29a9ea016 | 193 | * @param adress - register address to write to |
Sissors | 0:7bc29a9ea016 | 194 | * @param length - number of bytes to read |
Sissors | 2:a8e20db7901e | 195 | * @param data - pointer where the data needs to be written to |
Sissors | 0:7bc29a9ea016 | 196 | */ |
Sissors | 2:a8e20db7901e | 197 | void read( char adress, char *data, int length); |
Sissors | 2:a8e20db7901e | 198 | |
Sissors | 2:a8e20db7901e | 199 | /** |
Sissors | 2:a8e20db7901e | 200 | * Reads single axis |
Sissors | 2:a8e20db7901e | 201 | */ |
Sissors | 2:a8e20db7901e | 202 | float getSingle(int number); |
Sissors | 0:7bc29a9ea016 | 203 | |
Sissors | 0:7bc29a9ea016 | 204 | I2C _i2c; |
Sissors | 0:7bc29a9ea016 | 205 | bool active; |
Sissors | 0:7bc29a9ea016 | 206 | float samplerate; |
Kojto | 5:556829f081f6 | 207 | volatile bool callback_done; |
Kojto | 5:556829f081f6 | 208 | event_callback_t event; |
Kojto | 5:556829f081f6 | 209 | const bool asynch; |
Sissors | 0:7bc29a9ea016 | 210 | }; |
Sissors | 0:7bc29a9ea016 | 211 | |
Sissors | 0:7bc29a9ea016 | 212 | |
Sissors | 0:7bc29a9ea016 | 213 | #endif |