Dimple Bhuta / Mbed 2 deprecated mbed_adns9800_xvelocity

Dependencies:   mbed

Fork of mbed_adns9800 by Dimple Bhuta

main.cpp

Committer:
DimpleB05
Date:
2017-01-11
Revision:
2:899aa692d83f
Parent:
1:b6f857ea3ce2
Child:
3:ca2c09c6fb48

File content as of revision 2:899aa692d83f:

#include "mbed.h"
#include "adns9800.h"

InterruptIn pulse(p8); // connects to FPGA and listens for start recording pulse
DigitalOut gndPin(p7);
DigitalOut ledPin(LED1);
ADNS9800 adns(p11,p12,p13,p10);
Serial pc(USBTX, USBRX);

int dx, dy;

int movementFlag, recFlag;
Ticker tmr;
uint32_t time_start, time_now, time_elapsed;

void displayRegisters()
{
  int oreg[4] = {0x00,0x3F,0x2A,0x02};
  char* oregname[] = {"Product_ID:","Inverse_Product_ID:","SROM_Version:","Motion:"};
  uint8_t regres;
  
  int rctr;
  
  for(rctr=0; rctr<4; rctr++){
    pc.printf("---\r\n");
    regres = adns.read_reg(oreg[rctr]);
    pc.printf(oregname[rctr]);
    pc.printf("%02x\r\n", regres);
    wait_ms(1);
  }
}

void read_motion()
{
    int motion, obs;
    adns.read_burst(&motion, &obs, &dx, &dy);
   // if(recFlag == 1 && (dx > 0 || dy > 0))
   if(recFlag == 1)
    {
        time_now = us_ticker_read();
        time_elapsed = time_now - time_start;
        time_start = time_now;
        
        movementFlag = 1;
    }
}



void start_rec()
{
    recFlag = 1;
    ledPin = 1;
    time_start = us_ticker_read();
}

void stop_rec()
{
    ledPin = 0;
    recFlag = 0;
}

void format_and_send()
 {  

/*/int outPkt_x, outPkt_y;
   
    if(dx & 0x8000)
     {
         dx = (dx^0xFFFF) + 1;
         outPkt_x= 0x40 | (dx & 0x3F); // negative
     }
     else
     {
         outPkt_x = dx & 0x3F;
     }
     
    if(dy & 0x8000)
     {
         dy = (dy^0xFFFF) + 1;
         outPkt_y = 0x40 | (dy & 0x3F);
     }
     else
     {
         outPkt_y = dy & 0x3F;
     }
   pc.printf("Valuex= %d, Valuey = %d\r\n", outPkt_x, outPkt_y);*/
   int outPkt_x;
    if(dx & 0x8000)
     {
         dx = (dx^0xFFFF) + 1; //2's complement
         outPkt_x= 0x40 | (dx & 0x3F); // negative
     }
     else
     {
         outPkt_x = dx & 0x3F;
     }
     /*if (dx &0x8000)
     {
         dx = -1 * ((dx ^ 0xff) + 1);
     }
     else
     {
         outPkt_x = dx;   
     }*/
   pc.printf("Valuex=%d\r\n", outPkt_x);
}


/*
void format_and_send()
{
    // follow LSA's packet format
    
     * Data format from microblaze to PC:
     * 100000TT_0TTTTTTT_0TTTTTTT_0PXXXXXX_0PYYYYYY
     * Where T = delta timestamp, P = polarity, X = delta x, Y = delta y
     * Only first byte has MSB set
     * timestamp in 100us per increment
     * dx = 0(P)XXXXXX where 2nd bit is polarity
     
     
     uint8_t outPkt[5];
     time_elapsed = time_elapsed / 100;
     
     outPkt[0] = 0x84 | ((time_elapsed >> 14) & 0x00000003);
     outPkt[1] = ((time_elapsed >> 7) & 0x0000007F);
     outPkt[2] = (time_elapsed & 0x0000007F);
     
     

    if(dy & 0x8000) {
        dy = (dy^0xFFFF) + 1;
        outPkt[4] = 0x40 | (dy & 0x3F);
    } else {
        outPkt[4] = dy & 0x3F;
    }

    // send packet
    for (int i=0; i<5; i++) {
        pc.putc(outPkt[i]);
    }
}
*/



int main() {
    gndPin = 0; // lazy to connect to true ground pin
    movementFlag = 0;
    //recFlag = 0;
     recFlag = 1;
    
    pc.baud(115200);
    pc.printf("ADNS9800 test\r\n");
    
    adns.initialize();
    
    // display registers
    displayRegisters();
    
    pc.printf("Sensor initialized\r\n");
    
    // for sync with FPGA
    //pulse.rise(&start_rec);
    //pulse.fall(&stop_rec);
    // current 1000
    //tmr.attach_us(&read_motion, 100000); // timer interrupt to read registers, 2nd value time between calls in micro-seconds
    tmr.attach (&read_motion, 0.01); // replace it with 0.01 to execute 100 tasks in one second
    
    while(1) {
        
        if(movementFlag == 1)
        {
            format_and_send();
            movementFlag = 0;
        }
    }
}