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 HW3 Controller Team!

main.cpp

Committer:
ahsueh
Date:
2015-09-20
Revision:
4:fea2289b42cd
Parent:
3:f3520156ca98
Child:
6:753418e86c95

File content as of revision 4:fea2289b42cd:

#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);
AnalogIn fsr(A5);
#define MMA8451_I2C_ADDRESS (0x1d<<1)

//serial connection to PC via USB
Serial pc(USBTX, USBRX);
LSM303DLHC lsm(SDA, SCL);
USBMouseKeyboard mouseKey(ABS_MOUSE);


float prevFsrVal;

// 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;

    float fsrVal;
    int mouseDistance = 0;
    int mouseMovement = 0;
    mouseKey.move(0,0);
    int x = 0;
    int y = 0;
    
    // Idea - use ABSOLUTE mouseposition and map fsrVal 0.00- 1.00 to a hardcoded 
    // mouse position/distance on the screen
    
        while (1) {
        fsrVal = fsr.read();
            mouseMovement = (int) ((fsrVal-prevFsrVal)*10);
            mouseKey.printf("fsrVal %1.2f movement %0d ", fsrVal, mouseMovement);   

        if (mouseDistance < 100 || (mouseMovement < 0)) {
            x += mouseMovement;
            mouseKey.move(x,y); 
            mouseDistance = mouseDistance + mouseMovement;
            mouseKey.printf("distance %0d \n", mouseDistance);
        } else {
            mouseKey.printf("\n");    
        }
        wait(0.2);
        prevFsrVal = fsrVal;

        if (fsrVal == 0){
             mouseKey.printf("clear %0d \n", mouseDistance);

             x = 0;
             y = 0;
             mouseKey.move(x,y);
             mouseDistance = 0;
             wait(0.2);
        }

    }
    
/*
    while (1) {
        fsrVal = fsr.read();

            if (fsrVal < prevFsrVal)
                mouseMovement = (int) ((fsrVal-prevFsrVal)*10);  
            else          
                mouseMovement = (int) (fsrVal*10);
            mouseKey.printf("fsrVal %1.2f movement %0d ", fsrVal, mouseMovement);   
            
        if (mouseDistance < 100 || (mouseMovement < 0)) {
            mouseKey.move(mouseMovement,0); 
            mouseDistance = mouseDistance + mouseMovement;
            mouseKey.printf("distance %0d \n", mouseDistance);
        } else {
            mouseKey.printf("\n");    
        }
        wait(0.2);
        prevFsrVal = fsrVal;

        if (fsrVal == 0){
             mouseKey.printf("clear %0d \n", mouseDistance);
             mouseKey.move(-mouseDistance/2, 0);
             mouseDistance = 0;
             wait(0.2);
        }

    }
*/
    while (0) {
        //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)
     }
}