Submitted by Angela Hsueh, Maya Mardini, Yi Tong Slingshot controller using a force sensor and accelerometer for an Angry Birds clone game.
Dependencies: LSM303DLHC MMA8451Q PinDetect USBDevice mbed
Fork of hw3_controller by
main.cpp
- Committer:
- ahsueh
- Date:
- 2015-09-19
- Revision:
- 2:31cf09b1ad9c
- Parent:
- 1:01b4684bc210
- Child:
- 3:f3520156ca98
File content as of revision 2:31cf09b1ad9c:
#include "mbed.h" //#include "MMA8451Q.h" //#include "USBMouse.h" //#include "USBKeyboard.h" #include "USBMouseKeyboard.h" #include "LSM303DLHC.h" #include "PinDetect.h" #include "math.h" // define I2C Pins and address for KL25Z. Taken from default sample code. PinName const SDA = D14; PinName const SCL = D15; PinDetect button(D2); #define MMA8451_I2C_ADDRESS (0x1d<<1) //serial connection to PC via USB Serial pc(USBTX, USBRX); LSM303DLHC lsm(SDA, SCL); USBMouseKeyboard mouseKey; // acc and mag values float ax, ay, az; float mx, my, mz; float mx0, my0, mz0; // Original Position float mx1, my1, mz1; float mx2, my2, mz2; int trackMouse = 0; // Subtract original point from input point float calcDistance(float mxin, float myin, float mzin) { float deltax, deltay, deltaz; float final; deltax = mxin - mx0; deltay = myin - my0; deltaz = mzin - mz0; final = sqrt(pow(deltax,2) + pow(deltay,2) + pow(deltaz,2)); mouseKey.printf("CalcDistance o(%1.2f %1.2f %1.2f) d(%1.2f %1.2f %1.2f) Final: %1.2f\n", mx0, my0, mz0, mxin, myin, mzin, final); return final; } // Magically convert magnetic coordinates to mouse coordinates int convMagToMouse() { return 0; } void buttonPress(void) { //Grab position lsm.read(&ax, &ay, &az, &mx0, &my0, &mz0); //get acceleration mouseKey.printf("PRESS HELD mag1 %1.2f %1.2f %1.2f\n", mx0, my0, mz0); //print ascii-encoded float to serial port trackMouse = 1; } void buttonPressHeld(void) { } void buttonDeassert(void) { // Grab position lsm.read(&ax, &ay, &az, &mx2, &my2, &mz2); //get acceleration mouseKey.printf("PRESS RELEASE %1.2f %1.2f %1.2f\n", mx2, my2, mz2); //print ascii-encoded float to serial port trackMouse = 0; float distance = calcDistance(mx2, my2, mz2); // TODO figure out how to scale mouse movement //if(distance > 0){ // mouseKey.move(distance,0); //} } int main(void) { pc.printf("Hello\n"); //configure on-board I2C accelerometer on KL25Z //MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); button.mode(PullUp); button.setSampleFrequency(); button.attach_asserted(&buttonPress); button.attach_asserted_held(&buttonPressHeld); button.attach_deasserted(&buttonDeassert); button.setAssertValue(0); mx1 = 0; mx2 = 0; my1 = 0; my2 = 0; mz1 = 0; mz2 = 0; while (true) { //lsm.read(&ax, &ay, &az, &mx, &my, &mz); //get acceleration // While button is pressed, keep polling position for mouse pointer. if (trackMouse == 1) { lsm.read(&ax, &ay, &az, &mx1, &my1, &mz1); //get acceleration } //pc.printf("acc %1.2f %1.2f %1.2f\n", ax, ay, az); //print ascii-encoded float to serial port //keyboard.printf("mag %1.2f %1.2f %1.2f\n", mx, my, mz); //print ascii-encoded float to serial port wait(0.05f); // wait 50ms (20Hz update rate) } }