Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wue 0:7b58cdacf811 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
wue 0:7b58cdacf811 2 *
wue 0:7b58cdacf811 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wue 0:7b58cdacf811 4 * and associated documentation files (the "Software"), to deal in the Software without
wue 0:7b58cdacf811 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
wue 0:7b58cdacf811 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
wue 0:7b58cdacf811 7 * Software is furnished to do so, subject to the following conditions:
wue 0:7b58cdacf811 8 *
wue 0:7b58cdacf811 9 * The above copyright notice and this permission notice shall be included in all copies or
wue 0:7b58cdacf811 10 * substantial portions of the Software.
wue 0:7b58cdacf811 11 *
wue 0:7b58cdacf811 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wue 0:7b58cdacf811 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wue 0:7b58cdacf811 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wue 0:7b58cdacf811 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wue 0:7b58cdacf811 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wue 0:7b58cdacf811 17 */
wue 0:7b58cdacf811 18
wue 0:7b58cdacf811 19 #ifndef MMA8451Q_H
wue 0:7b58cdacf811 20 #define MMA8451Q_H
wue 0:7b58cdacf811 21
wue 0:7b58cdacf811 22 #include "mbed.h"
wue 0:7b58cdacf811 23
wue 0:7b58cdacf811 24 /**
wue 0:7b58cdacf811 25 * MMA8451Q accelerometer example
wue 0:7b58cdacf811 26 *
wue 0:7b58cdacf811 27 * @code
wue 0:7b58cdacf811 28 * #include "mbed.h"
wue 0:7b58cdacf811 29 * #include "MMA8451Q.h"
wue 0:7b58cdacf811 30 *
wue 0:7b58cdacf811 31 * #define MMA8451_I2C_ADDRESS (0x1d<<1)
wue 0:7b58cdacf811 32 *
wue 0:7b58cdacf811 33 * int main(void) {
wue 0:7b58cdacf811 34 *
wue 0:7b58cdacf811 35 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS);
wue 0:7b58cdacf811 36 * PwmOut rled(LED_RED);
wue 0:7b58cdacf811 37 * PwmOut gled(LED_GREEN);
wue 0:7b58cdacf811 38 * PwmOut bled(LED_BLUE);
wue 0:7b58cdacf811 39 *
wue 0:7b58cdacf811 40 * while (true) {
wue 0:7b58cdacf811 41 * rled = 1.0 - abs(acc.getAccX());
wue 0:7b58cdacf811 42 * gled = 1.0 - abs(acc.getAccY());
wue 0:7b58cdacf811 43 * bled = 1.0 - abs(acc.getAccZ());
wue 0:7b58cdacf811 44 * wait(0.1);
wue 0:7b58cdacf811 45 * }
wue 0:7b58cdacf811 46 * }
wue 0:7b58cdacf811 47 * @endcode
wue 0:7b58cdacf811 48 */
wue 0:7b58cdacf811 49 class MMA8451Q
wue 0:7b58cdacf811 50 {
wue 0:7b58cdacf811 51 public:
wue 0:7b58cdacf811 52 /**
wue 0:7b58cdacf811 53 * MMA8451Q constructor
wue 0:7b58cdacf811 54 *
wue 0:7b58cdacf811 55 * @param sda SDA pin
wue 0:7b58cdacf811 56 * @param sdl SCL pin
wue 0:7b58cdacf811 57 * @param addr addr of the I2C peripheral
wue 0:7b58cdacf811 58 */
wue 0:7b58cdacf811 59 MMA8451Q(PinName sda, PinName scl, int addr);
wue 0:7b58cdacf811 60
wue 0:7b58cdacf811 61 /**
wue 0:7b58cdacf811 62 * MMA8451Q destructor
wue 0:7b58cdacf811 63 */
wue 0:7b58cdacf811 64 ~MMA8451Q();
wue 0:7b58cdacf811 65
wue 0:7b58cdacf811 66 /**
wue 0:7b58cdacf811 67 * Get the value of the WHO_AM_I register
wue 0:7b58cdacf811 68 *
wue 0:7b58cdacf811 69 * @returns WHO_AM_I value
wue 0:7b58cdacf811 70 */
wue 0:7b58cdacf811 71 uint8_t getWhoAmI();
wue 0:7b58cdacf811 72
wue 0:7b58cdacf811 73 /**
wue 0:7b58cdacf811 74 * Get X axis acceleration
wue 0:7b58cdacf811 75 *
wue 0:7b58cdacf811 76 * @returns X axis acceleration
wue 0:7b58cdacf811 77 */
wue 0:7b58cdacf811 78 float getAccX();
wue 0:7b58cdacf811 79
wue 0:7b58cdacf811 80 /**
wue 0:7b58cdacf811 81 * Get Y axis acceleration
wue 0:7b58cdacf811 82 *
wue 0:7b58cdacf811 83 * @returns Y axis acceleration
wue 0:7b58cdacf811 84 */
wue 0:7b58cdacf811 85 float getAccY();
wue 0:7b58cdacf811 86
wue 0:7b58cdacf811 87 /**
wue 0:7b58cdacf811 88 * Get Z axis acceleration
wue 0:7b58cdacf811 89 *
wue 0:7b58cdacf811 90 * @returns Z axis acceleration
wue 0:7b58cdacf811 91 */
wue 0:7b58cdacf811 92 float getAccZ();
wue 0:7b58cdacf811 93
wue 0:7b58cdacf811 94 /**
wue 0:7b58cdacf811 95 * Get XYZ axis acceleration
wue 0:7b58cdacf811 96 *
wue 0:7b58cdacf811 97 * @param res array where acceleration data will be stored
wue 0:7b58cdacf811 98 */
wue 0:7b58cdacf811 99 void getAccAllAxis(float * res);
wue 0:7b58cdacf811 100
wue 0:7b58cdacf811 101 /** JK
wue 0:7b58cdacf811 102 * Setup Double Tap detection
wue 0:7b58cdacf811 103
wue 0:7b58cdacf811 104
wue 0:7b58cdacf811 105 Example:
wue 0:7b58cdacf811 106
wue 0:7b58cdacf811 107 #include "mbed.h"
wue 0:7b58cdacf811 108 #include "MMA8451Q.h"
wue 0:7b58cdacf811 109
wue 0:7b58cdacf811 110 #define MMA8451_I2C_ADDRESS (0x1d<<1)
wue 0:7b58cdacf811 111 #define ON 0
wue 0:7b58cdacf811 112 #define OFF !ON
wue 0:7b58cdacf811 113
wue 0:7b58cdacf811 114 //Setup the interrupts for the MMA8451Q
wue 0:7b58cdacf811 115 InterruptIn accInt1(PTA14);
wue 0:7b58cdacf811 116 InterruptIn accInt2(PTA15);//not used in this prog but this is the other int from the accelorometer
wue 0:7b58cdacf811 117
wue 0:7b58cdacf811 118 uint8_t togstat=0;//Led status
wue 0:7b58cdacf811 119 DigitalOut bled(LED_BLUE);
wue 0:7b58cdacf811 120
wue 0:7b58cdacf811 121
wue 0:7b58cdacf811 122 void tapTrue(void){//ISR
wue 0:7b58cdacf811 123 if(togstat == 0){
wue 0:7b58cdacf811 124 togstat = 1;
wue 0:7b58cdacf811 125 bled=ON;
wue 0:7b58cdacf811 126 } else {
wue 0:7b58cdacf811 127 togstat = 0;
wue 0:7b58cdacf811 128 bled=OFF;
wue 0:7b58cdacf811 129 }
wue 0:7b58cdacf811 130
wue 0:7b58cdacf811 131 }
wue 0:7b58cdacf811 132
wue 0:7b58cdacf811 133
wue 0:7b58cdacf811 134 int main(void) {
wue 0:7b58cdacf811 135
wue 0:7b58cdacf811 136 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);//accelorometer instance
wue 0:7b58cdacf811 137
wue 0:7b58cdacf811 138 acc.setDoubleTap();//Setup the MMA8451Q to look for a double Tap
wue 0:7b58cdacf811 139 accInt1.rise(&tapTrue);//call tapTrue when an interrupt is generated on PTA14
wue 0:7b58cdacf811 140
wue 0:7b58cdacf811 141 while (true) {
wue 0:7b58cdacf811 142 //Interrupt driven so nothing in main loop
wue 0:7b58cdacf811 143 }
wue 0:7b58cdacf811 144 }
wue 0:7b58cdacf811 145
wue 0:7b58cdacf811 146
wue 0:7b58cdacf811 147 */
wue 0:7b58cdacf811 148 void setDoubleTap(void);
wue 0:7b58cdacf811 149
wue 0:7b58cdacf811 150 private:
wue 0:7b58cdacf811 151 I2C m_i2c;
wue 0:7b58cdacf811 152 int m_addr;
wue 0:7b58cdacf811 153 void readRegs(int addr, uint8_t * data, int len);
wue 0:7b58cdacf811 154 void writeRegs(uint8_t * data, int len);
wue 0:7b58cdacf811 155 int16_t getAccAxis(uint8_t addr);
wue 0:7b58cdacf811 156
wue 0:7b58cdacf811 157 };
wue 0:7b58cdacf811 158
wue 0:7b58cdacf811 159 #endif