Ghost Mouse / Mbed 2 deprecated ghost_mouse

Dependencies:   mbed

main.cpp

Committer:
jennabarton
Date:
2017-04-01
Revision:
9:55473409c585
Parent:
8:d27efcac2dd7
Child:
10:d67a15ba5748

File content as of revision 9:55473409c585:

#include "mbed.h"

//******************************************************************
// All variables defined below
//******************************************************************

//communication
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
Serial keyOut(p13, p14);
I2C camera1(p9, p10);

//initial camera data
int IRsensorAddress = 0xB0;
int slaveAddress;
char data_buf[16];
char s;
int i;

//previous point values
int prevX = 1023;
int prevY = 1023;

//point variables
int point1x = 0;
int point1y = 0;
int point2x = 0;
int point2y = 0;
int point3x = 0;
int point3y = 0;
int point4x = 0;
int point4y = 0;

//sensitivity
//Level 5: p0 = 0x96, p1 = 0xFE, p2 = 0xFE, p3 = 0x05
//highest sensitivity to more accurately detect points
int sen0 = 0x96;
int sen1 = 0xFE;
int sen2 = 0xFE;
int sen3 = 0x00;

//matrices of x and y coordinates from the first camera
int onex[4];
int oney[4];

//******************************************************************
// All methods defined below
//******************************************************************


//writes two bytes to the camera
void write2bytes(char data1, char data2){
    char out[2];
    out[0] = data1;
    out[1] = data2;
    camera1.write(slaveAddress, out, 2);
    wait(0.01);   
}

//TODO: get rid of these b/c they don't work. instead use printc
//
//void mouseCommand(uint8_t buttons, uint8_t x, uint8_t y) {
//  keyOut.putc(0xFD);
//  keyOut.putc(0x00);
//  keyOut.putc(0x03);
//  keyOut.putc(buttons);
//  keyOut.putc(x);
//  keyOut.putc(y);
//  keyOut.putc(0x00);
//  keyOut.putc(0x00);
//  keyOut.putc(0x00);
//}
//
//void keyCommand(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2 = 0, uint8_t keycode3 = 0, 
//                uint8_t keycode4 = 0, uint8_t keycode5 = 0, uint8_t keycode6 = 0) {
//  keyOut.putc(0xFD);       // our command
//  keyOut.putc(modifiers);  // modifier!
//  keyOut.putc(0x00); // 0x00  
//  keyOut.putc(keycode1);   // key code #1
//  keyOut.putc(keycode2); // key code #2
//  keyOut.putc(keycode3); // key code #3
//  keyOut.putc(keycode4); // key code #4
//  keyOut.putc(keycode5); // key code #5
//  keyOut.putc(keycode6); // key code #6
//}

// Initialize WiiMote Camera
void initCamera(void){
    write2bytes(0x30, 0x01); 
    write2bytes(0x00, 0x02); 
    write2bytes(0x00, 0x00); 
    write2bytes(0x71, 0x01); 
    write2bytes(0x00, sen0); 
    write2bytes(0x07, 0x00); 
    write2bytes(sen1, 0x1A);
    write2bytes(sen2, sen3); 
    write2bytes(0x33, 0x03); 
    write2bytes(0x30, 0x08);
    //wait(0.1);

}

//get data from camera one 
//populates onex and oney with values depending on the measured points
//NOTE: 1023 means nothing was detected
void readCameraData(void){
        
    //request data from camera 
    char out[1];
    out[0] = 0x36;   
    camera1.write(slaveAddress, out, 1);
    //wait(0.2); //do we need this?
    
    //get data from camera
    camera1.read(slaveAddress, data_buf, 16);
        
    //POINT 1
    //get data
    point1x = data_buf[1];
    point1y = data_buf[2];
    s = data_buf[3];
    //load x,y    
    onex[0] = point1x + ((s & 0x30) << 4);
    oney[0] = point1y + ((s & 0xC0) << 2);
    
    
    //>>>>>>>>>>>>>>>>>Begin unfinished code for moving averages
    
    //look at delta btwn prev val and current
    //TODO: moving average
    if(prevX != 1023 || prevY != 1023){
        if(onex[0] - prevX > 5){
            keyOut.putc(0x41);
        } else if ((onex[0] - prevX) < -5){
            keyOut.putc(0x42);
            
        }
    }
    
    //update prev values
    prevX = onex[0];
    prevY = oney[0];
    
    
    //<<<<<<<<<<<<<<<<End unfinished code for moving averages
    
    //POINT 2
    //get data
    point2x = data_buf[4];
    point2y = data_buf[5];
    s = data_buf[6];
    //load x,y
    onex[1] = point2x + ((s & 0x30) << 4);
    oney[1] = point2y + ((s & 0xC0) << 2);
      
    //POINT 3
    //get data
    point3x = data_buf[7];
    point3y = data_buf[8];
    s = data_buf[9];
    //load x,y
    onex[2] = point3x + ((s & 0x30) << 4);
    oney[2] = point3y + ((s & 0xC0) << 2);
    
    //POINT 4
    //get data
    point4x = data_buf[10];
    point4y = data_buf[11];
    s = data_buf[12];
    //load x,y
    onex[3] = point4x + ((s & 0x30) << 4);
    oney[3] = point4y + ((s & 0xC0) << 2);
    
}

//print to serial monitor the coordinates of the points stored in
//the passed x and y arrays
void printCamData(int xcor[4], int ycor[4]){
    for(int i = 0; i<4; i++){
        int x = xcor[i];
        int y = ycor[i];
        //determine what to print
        //x coordinate
        pc.printf(" %d,", x);
        
        //y coordinate
        pc.printf(" %d\t", y);     
    }
    
    //new line and delay
    pc.printf("\n");      
    //wait(0.01);
}




//entrance to the code
int main() {
    myled = 0;
    //slaveAddress = IRsensorAddress >> 1;
    slaveAddress = IRsensorAddress;
    initCamera();
    
    //update baud rate
    pc.baud(115200);
    
    //loop to search for new info using the camera    
    while(1) {
        
        //wait 
        wait(0.04);
        
        //toggle test LED 
        myled = 1 - myled;
        
        //get the camera data
        readCameraData();
        
        //print points
        printCamData(onex, oney);  
        
        //uncomment below to test infinite print
        //keyOut.putc(0x41);
        
        
        //uncomment below to infinitely move mouse up
        keyOut.putc(0xFD);
        keyOut.putc(0x00);
        keyOut.putc(0x03);
        keyOut.putc(0x00); //button press
        keyOut.putc(0x00); //x direction
        keyOut.putc(0x50); //y direction
        keyOut.putc(0x00);
        keyOut.putc(0x00);
        keyOut.putc(0x00);
        
        
    }
}