Trackball based on the NXP LPC11U24 and the ADNS-9500

Dependencies:   ADNS9500 USBDevice mbed 25LCxxx_SPI

main.cpp

Committer:
xxann5
Date:
2012-12-16
Revision:
5:c7056267daa7
Parent:
2:72a8d2b11320
Child:
6:4cb2c9a3abcd

File content as of revision 5:c7056267daa7:

#include "main.h"

/*
 * type, VID, PID, release
 */
 
USBMouse mouse( REL_MOUSE, 0x192f, 0x0000, 0x0001 ) ;

//We declare a USBHID device. By default input and output reports are 64 bytes long.
USBHID hid(8, 8);


/* 
 * mosi miso sclk ncs FREQ, motion
 */
adns9500::ADNS9500 sensor(p5, p6, p7, p8, adns9500::MAX_SPI_FREQUENCY, p21);

void motionCallback()
{
    motion_triggered = true;
}

void btn_hr_press(){
    high_rez_active = true;
    sensor.setResolution( default_hirez_cpi ); 
}
void btn_hr_release(){
    high_rez_active = false;
    sensor.setResolution( default_motion_cpi );
}

void btn_z_press(){
    z_axis_active = true;
    sensor.setResolution( default_z_cpi );
}
void btn_z_release(){
    z_axis_active = false;
    sensor.setResolution( default_motion_cpi );
}

void btn_l_press(){
    mouse.press(MOUSE_LEFT);
}
void btn_l_release(){
    //mouse.release(MOUSE_LEFT);
}

void btn_m_press(){
    //mouse.press(MOUSE_MIDDLE);
}
void btn_m_release(){
    //mouse.release(MOUSE_MIDDLE);
}

void btn_r_press(){
    //mouse.press(MOUSE_RIGHT);
}
void btn_r_release(){
    //mouse.release(MOUSE_RIGHT);
}

void set( uint8_t attrib, uint8_t *val ){
    switch (attrib){
        case CHAT_MOTION_DEFAULT_CPI:
            default_motion_cpi = *val;
            break;
        case CHAT_Z_DEFAULT_CPI:
            default_z_cpi = *val;
            break;
        default:
            // FIXME: error handling.
            printf("crap\r\n");
    }
}

void get( uint8_t attrib ){
}

int main(void)
{

    send_rep.length = 8;
    
    btn_hr.rise(&btn_hr_press);
    btn_hr.fall(&btn_hr_release);

    btn_z.rise(&btn_z_press);
    btn_z.fall(&btn_z_release);

    btn_l.rise(&btn_l_press);
    btn_l.fall(&btn_l_release);

    btn_m.rise(&btn_m_press);
    btn_m.fall(&btn_m_release);

    btn_r.rise(&btn_r_press);
    btn_r.fall(&btn_r_release);
    
    int dx, dy;

    sensor.attach(&motionCallback);
    
    sensor.reset();
    
    uint16_t crc = sensor.sromDownload(adns9500FWArray, (uint16_t)ADNS9500_FIRMWARE_LEN );
      
    if( (uint16_t)ADNS6010_FIRMWARE_CRC != crc )
    {
        error( "CRC does not match: [%x] [%x], Exiting.\r\n", (uint16_t)ADNS6010_FIRMWARE_CRC, crc );
    }

    sensor.enableLaser();
    
    sensor.setResolution( default_motion_cpi );
    
    while (true)
    {
        //try to read a msg
        if(hid.readNB(&recv_rep)) {
            led4 = !led4;
            switch (recv_rep.data[0]){
                case CHAT_SET:
                    set(recv_rep.data[1],&recv_rep.data[2]);
                    break;
                case CHAT_GET:
                    get(recv_rep.data[1]);
                    break;
                default:
                    // FIXME: error handling.
                    printf("crap\r\n");
            }
        }
            
        if (motion_triggered) {
            led1 = !led1;
            motion_triggered = false;
            
            sensor.getMotionDelta(dx, dy);

            /*
             * The sensor does not know its upside down and backwords
             * so we are helping it out with the y axis.
             */
            if( z_axis_active ){
                //////mouse.scroll( - dy );
            }
            else{
                //////mouse.move( dx, - dy ); 
            }
        }
    }
}