An I/O controller for virtual pinball machines: accelerometer nudge sensing, analog plunger input, button input encoding, LedWiz compatible output controls, and more.
Dependencies: mbed FastIO FastPWM USBDevice
Fork of Pinscape_Controller by
MMA8451Q.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef MMA8451Q_H 00020 #define MMA8451Q_H 00021 00022 #include "mbed.h" 00023 00024 /** 00025 * MMA8451Q accelerometer example 00026 * 00027 * @code 00028 * #include "mbed.h" 00029 * #include "MMA8451Q.h" 00030 * 00031 * #define MMA8451_I2C_ADDRESS (0x1d<<1) 00032 * 00033 * int main(void) { 00034 * 00035 * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); 00036 * PwmOut rled(LED_RED); 00037 * PwmOut gled(LED_GREEN); 00038 * PwmOut bled(LED_BLUE); 00039 * 00040 * while (true) { 00041 * rled = 1.0 - abs(acc.getAccX()); 00042 * gled = 1.0 - abs(acc.getAccY()); 00043 * bled = 1.0 - abs(acc.getAccZ()); 00044 * wait(0.1); 00045 * } 00046 * } 00047 * @endcode 00048 */ 00049 class MMA8451Q 00050 { 00051 public: 00052 /** 00053 * MMA8451Q constructor 00054 * 00055 * @param sda SDA pin 00056 * @param sdl SCL pin 00057 * @param addr addr of the I2C peripheral 00058 */ 00059 MMA8451Q(PinName sda, PinName scl, int addr); 00060 00061 /** 00062 * MMA8451Q destructor 00063 */ 00064 ~MMA8451Q(); 00065 00066 /** 00067 * Reset the accelerometer hardware and set our initial parameters 00068 */ 00069 void init(); 00070 00071 /** 00072 * Enter standby mode 00073 */ 00074 void standby(); 00075 00076 /** 00077 * Enter active mode 00078 */ 00079 void active(); 00080 00081 /** 00082 * Get the value of the WHO_AM_I register 00083 * 00084 * @returns WHO_AM_I value 00085 */ 00086 uint8_t getWhoAmI(); 00087 00088 /** 00089 * Get X axis acceleration 00090 * 00091 * @returns X axis acceleration 00092 */ 00093 float getAccX(); 00094 00095 /** 00096 * Get Y axis acceleration 00097 * 00098 * @returns Y axis acceleration 00099 */ 00100 float getAccY(); 00101 00102 /** 00103 * Read an X,Y pair 00104 */ 00105 void getAccXY(float &x, float &y); 00106 00107 /** 00108 * Read X,Y,Z as floats. This is the second most efficient way 00109 * to fetch all three axes (after the integer version), since it 00110 * fetches all axes in a single I2C transaction. 00111 */ 00112 void getAccXYZ(float &x, float &y, float &z); 00113 00114 /** 00115 * Read X,Y,Z as integers. This reads the three axes in a single 00116 * I2C transaction and returns them in the native integer scale, 00117 * so it's the most efficient way to read the current 3D status. 00118 * Each axis value is represented an an integer using the device's 00119 * native 14-bit scale, so each is in the range -8192..+8191. 00120 */ 00121 void getAccXYZ(int &x, int &y, int &z); 00122 00123 /** 00124 * Get Z axis acceleration 00125 * 00126 * @returns Z axis acceleration 00127 */ 00128 float getAccZ(); 00129 00130 /** 00131 * Get XYZ axis acceleration 00132 * 00133 * @param res array where acceleration data will be stored 00134 */ 00135 void getAccAllAxis(float * res); 00136 00137 /** 00138 * Set interrupt mode. 'pin' is 1 for INT1_ACCEL (PTA14) and 2 for INT2_ACCEL (PTA15). 00139 * The caller is responsible for setting up an interrupt handler on the corresponding 00140 * PTAxx pin. 00141 */ 00142 void setInterruptMode(int pin); 00143 00144 /** 00145 * Set the hardware dynamic range, in G. Valid ranges are 2, 4, and 8. 00146 */ 00147 void setRange(int g); 00148 00149 /** 00150 * Disable interrupts. 00151 */ 00152 void clearInterruptMode(); 00153 00154 /** 00155 * Is a sample ready? 00156 */ 00157 bool sampleReady(); 00158 00159 /** 00160 * Get the number of FIFO samples available 00161 */ 00162 int getFIFOCount(); 00163 00164 private: 00165 I2C m_i2c; 00166 int m_addr; 00167 void readRegs(int addr, uint8_t * data, int len); 00168 void writeRegs(uint8_t * data, int len); 00169 int16_t getAccAxis(uint8_t addr); 00170 00171 // Translate a 14-bit register value to a signed integer. The 00172 // most significant 8 bits are in the first byte, and the least 00173 // significant 6 bits are in the second byte. To adjust to a 00174 // regular integer, left-justify the 14 bits in an int16_t, then 00175 // divide by 4 to shift out the unused low two bits. Note that 00176 // we have to divide rather than right-shift (>>) to ensure proper 00177 // filling of the sign bits. The compiler should convert the 00178 // divide-by-4 to an arithmetic shift right by 2, so this should 00179 // still be efficient. 00180 inline int xlat14(const uint8_t *buf) 00181 { 00182 // Store the 16 bits left-justified in an int16_t, then cast 00183 // to a regular int to sign-extend to the full int width. 00184 // Divide the result by 4 to shift out the unused 2 bits 00185 // at the right end. 00186 return int(int16_t((buf[0] << 8) | buf[1])) / 4; 00187 } 00188 00189 }; 00190 00191 #endif
Generated on Wed Jul 13 2022 03:30:11 by 1.7.2