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