How to calibrate readings from a Touchpad?

Hey everyone!.

I always wanted an XY MIDI touch controller to use with Ableton, but they are way to expensive for what they are, I figured It would be better to use an old Laptop Touchpad that I had laying around.

I managed to get it working the way I wanted using the PS2mouse library and the code from another user as guidance, but the problem that I’m having is that in the X axis it goes from 0 to 127 (MIDI min and max values) before I reach all the way in the touchpad, something like 60%, the Y axis has the same problem but it’s not really noticeable, it could be because the Y axis is something like half the size of the X axis.

How can I fix this?

Thanks in advance, here’s my code.

#define MDATA 10         // Mouse Data
#define MCLK 9          // Mouse Clock

int midivalueX;
int midivalueY;

int XCC = 21;          // Midi Continous Controler assigned by default to X
int YCC = 24;          // Midi Continous Controler assigned by default to Y

/**********************************************************************
*
* Mouse
* Source : Arduino Playground
* URL : http://www.arduino.cc/playground/ComponentLib/Ps2mouse
* Author : Unknown
**********************************************************************/


void gohi(int pin)
{
 pinMode(pin, INPUT);
 digitalWrite(pin, HIGH);
}

void golo(int pin)
{
 pinMode(pin, OUTPUT);
 digitalWrite(pin, LOW);
}

void mouse_write(char data)
{
 char i;
 char parity = 1;

 /* put pins in output mode */
 gohi(MDATA);
 gohi(MCLK);
 delayMicroseconds(300);
 golo(MCLK);
 delayMicroseconds(300);
 golo(MDATA);
 delayMicroseconds(10);
 /* start bit */
 gohi(MCLK);
 /* wait for mouse to take control of clock); */
 while (digitalRead(MCLK) == HIGH);
 /* clock is low, and we are clear to send data */
 for (i=0; i < 8; i++) {
   if (data & 0x01) {
     gohi(MDATA);
   } 
   else {
     golo(MDATA);
   }
   /* wait for clock cycle */
   while (digitalRead(MCLK) == LOW)
     ;
   while (digitalRead(MCLK) == HIGH)
     ;
   parity = parity ^ (data & 0x01);
   data = data >> 1;
 }  
 /* parity */
 if (parity) {
   gohi(MDATA);
 } 
 else {
   golo(MDATA);
 }
 while (digitalRead(MCLK) == LOW)
   ;
 while (digitalRead(MCLK) == HIGH)
   ;
 /* stop bit */
 gohi(MDATA);
 delayMicroseconds(50);
 while (digitalRead(MCLK) == HIGH)
   ;
 /* wait for mouse to switch modes */
 while ((digitalRead(MCLK) == LOW) || (digitalRead(MDATA) == LOW))
   ;
 /* put a hold on the incoming data. */
 golo(MCLK);
 //  Serial.print("done.\n");
}

/*
* Get a byte of data from the mouse
*/
char mouse_read(void)
{
 char data = 0x00;
 int i;
 char bit = 0x01;

 //  Serial.print("reading byte from mouse\n");
 /* start the clock */
 gohi(MCLK);
 gohi(MDATA);
 delayMicroseconds(50);
 while (digitalRead(MCLK) == HIGH)
   ;
 delayMicroseconds(5);  /* not sure why */
 while (digitalRead(MCLK) == LOW) /* eat start bit */
   ;
 for (i=0; i < 8; i++) {
   while (digitalRead(MCLK) == HIGH)
     ;
   if (digitalRead(MDATA) == HIGH) {
     data = data | bit;
   }
   while (digitalRead(MCLK) == LOW)
     ;
   bit = bit << 1;
 }
 /* eat parity bit, which we ignore */
 while (digitalRead(MCLK) == HIGH)
   ;
 while (digitalRead(MCLK) == LOW)
   ;
 /* eat stop bit */
 while (digitalRead(MCLK) == HIGH)
   ;
 while (digitalRead(MCLK) == LOW)
   ;

 /* put a hold on the incoming data. */
 golo(MCLK);
 //  Serial.print("Recvd data ");
 //  Serial.print(data, HEX);
 //  Serial.print(" from mouse\n");
 return data;
}

void mouse_init()
{
 gohi(MCLK);
 gohi(MDATA);
 //  Serial.print("Sending reset to mouse\n");
 mouse_write(0xff);
 mouse_read();  /* ack byte */
 //  Serial.print("Read ack byte1\n");
 mouse_read();  /* blank */
 mouse_read();  /* blank */
 //  Serial.print("Sending remote mode code\n");
 mouse_write(0xf0);  /* remote mode */
 mouse_read();  /* ack */
 //  Serial.print("Read ack byte2\n");
 delayMicroseconds(100);
}

/**********************************************************************
*
* MIDI
*
**********************************************************************/

// Send a MIDI note-on message.  Like pressing a piano key
void noteOn(byte channel, byte note, byte velocity) {
 midiMsg( (0x80 | (channel<<4)), note, velocity);
}

// Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
 midiMsg( (0x80 | (channel<<4)), note, velocity);
}

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
 Serial.write(cmd);
 Serial.write(data1);
 Serial.write(data2);

}      

/**********************************************************************
*
* Main
*
**********************************************************************/

void setup() {

 Serial.begin(115200);// midi
 //Serial.begin(9600); //debug

 
 mouse_init();
 
}

void loop() {
 char mstat;
 char mx;
 char my;

 /* get a reading from the mouse */
 mouse_write(0xeb);  /* give me data! */
 mouse_read();      /* ignore ack */

 mstat = mouse_read();
 mx = mouse_read();
 my = mouse_read();
 
 if (digitalRead(4)==HIGH)
   {
     XCC += mx;
     XCC = constrain (XCC, 0 , 127);

   }
   else if (digitalRead(5)==HIGH)
   {
     YCC += my;
     YCC = constrain (YCC, 0 , 127);

   }
 else if (mx!=0 || my!=0 )
 {
    
     midivalueX += mx;
     midivalueY += my;
   
     midivalueX = constrain (midivalueX, 0 , 127);
     midivalueY = constrain (midivalueY, 0 , 127);

     midiMsg(0xb0, XCC, midivalueX);
     midiMsg(0xb0, YCC, midivalueY);

   }
 
 
}