using the ps2 device library

I’m currently trying to compile the example to see if it will work however I get strange errors when i try to.
The library can be downloaded at http://www.arduino.cc/playground/ComponentLib/Ps2mouse

The example code was

#include "ps2dev.h"


PS2dev mouse(3,2); // 2 data 3clock


char buttons[3] = {0,0,0};


int delta_x = 0;
int delta_y = 0;
 //we start off not enabled

int enabled =0;

//ack a host command

void ack()
 {

  while(mouse.write(0xFA));

}



void write_packet()
 {
 
 char overflowx =0;

  char overflowy =0;

  char data[3];
  int x,y;
 
 
  if (delta_x > 255) {

    overflowx =1;

    x=255;

  }
 
 if (delta_x < -255)
 {

    overflowx = 1;

    x=-255;

  }
  
  if (delta_y > 255)
 {

    overflowy =1;

    y=255;

  }

  if
 (delta_y < -255)
 {

    overflowy = 1;

    y=-255;
  }

  
  data[0] = ((overflowy & 1) << 7) |
    ( (overflowx & 1) << 6) |
    ( (((delta_y &0x100)>>8) & 1) << 5) |
    ( ( ((delta_x &0x100)>>8)& 1) << 4) |
    ( ( 1) << 3) |
    ( ( buttons[1] & 1) << 2) |
    ( ( buttons[2] & 1) << 1) |
    ( ( buttons[0] & 1) << 0) ;

    
  data[1] = delta_x & 0xff;

  data[2] = delta_y & 0xff;

  
  mouse.write(data[0]);

  mouse.write(data[1]);


  mouse.write(data[2]);


  delta_x = 0;

  delta_y = 0;

}


int mousecommand(int command)
 {

  unsigned char val;

  //This implements enough mouse commands to get by, most of them are
  //just acked without really doing anything


  switch (command)
 {

  case 0xFF: //reset

    ack();
    //the while loop lets us wait for the host to be ready

    while(mouse.write(0xAA)!=0);
  
    while(mouse.write(0x00)!=0);

  
    break;
  case 0xFE: //resend

    ack();

    break;

  case 0xF6: //set defaults 
    //enter stream mode
   
    ack();

    break;

  case 0xF5:  //disable data reporting

    //FM

    ack();

    break;
  case 0xF4: //enable data reporting
    //FM
 
   enabled = 1;

    ack();
 
   break;

  case 0xF3: //set sample rate

    ack();

    mouse.read(&val); // for now drop the new rate on the floor
    //
      Serial.println(val,HEX);

    ack();

    break;
  case 0xF2: //get device id
    ack();
    mouse.write(00);
    break;
  case 0xF0: //set remote mode 
    ack();  
    break;
  case 0xEE: //set wrap mode
    ack();
    break;
  case 0xEC: //reset wrap mode
    ack();
    break;
  case 0xEB: //read data
    ack();
    write_packet();
    break;
  case 0xEA: //set stream mode
    ack();
    break;
  case 0xE9: //status request
    ack();
    //      send_status();
    break;
  case 0xE8: //set resolution
    ack();
    mouse.read(&val);
    //    Serial.println(val,HEX);
    ack();
    break;
  case 0xE7: //set scaling 2:1
    ack();
    break;
  case 0xE6: //set scaling 1:1
    ack();
    break;

  } 
  
}

int xcenter ;
int ycenter;

int xsum = 0;
int ysum = 0;

void setup() {
  unsigned char val;
 
  // send the mouse start up
  while(mouse.write(0xAA)!=0);  
  while(mouse.write(0x00)!=0);


}




void loop() {
  unsigned char  c;
  if( (digitalRead(3)==LOW) || (digitalRead(2) == LOW)) {
    while(mouse.read(&c)) ;
    mousecommand(c);
  } 

  if (enabled) {
    // move the mouse diagonally
    delta_x = 1;
    delta_y = 1;
    write_packet()  ;
  }
  delay(50);

}

And I get the following error when I compile it

c:/users/bucolo/desktop/arduino-0017/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'

c:/users/bucolo/desktop/arduino-0017/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'

c:/users/bucolo/desktop/arduino-0017/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'

Bad error line: -2

Help would be greately appreciated.

Getting the same error :(

I'm clueless.

Give this a try…

  • Open ps2dev.h in an editor like Notepad or Visual Studio
  • Comment out the line with #include “WConstants.h”
  • After that line add a new line with #include <WProgram.h>
  • Save ps2dev.h
  • Try again

This is a snippet around the two lines…

#define ps2dev_h

// #include "WConstants.h"
#include <WProgram.h>

class PS2dev
{
      public:

Thanks for the reply.

Actually i tried this earlier kindof. I commented out #include “WConstants.h” and it compiled without errors, i didnt add the other include file though, will try that. I didn’t get the library to work but I’ll continue to try tomorrow.

I assume clock and data goes to digital pings, ground to ground, and 5 volt should be unconnected since arduino gets its own poweR?

I commented out #include "WConstants.h" and it compiled without errors

Huh. I didn't think to try that. I assumed something in the header needed an Arduino definition.

i didnt add the other include file though, will try that

Don't bother. If the Sketch compiled, you're good to go.

I assume clock and data goes to digital pings, ground to ground, and 5 volt should be unconnected since arduino gets its own poweR?

I have no idea. Someone else will have to help with that question.

Hey!

I’m following this.
Changed WConstants to WProgram and it worked for the ps2mouse example which comes with the library zip.

Now, my point is to interface with a keyboard using this ps2dev library:

< keyboard --(4 wires)-- arduino > — RF — < arduino --(ps2 output)-- PC >

ALL the keyboard libraries I found are full of bugs (like this constants to program that I cant handle by my own).

ok, I found this tutorial that I thought could help me understanding the code:

it points to an arduino code:
*http://awgh.org/files/iPhonePS2.txt

*changed ps2 to ps2dev.

However now here is a new error with the ‘PS2dev::read()’
error: no matching function for call to ‘PS2dev::read()’/Applications/Arduino.app/Contents/Resources/Java/hardware/libraries/ps2dev/ps2dev.h:21: note: candidates are: int PS2dev::read(unsigned char*)

I think, in this case, I cant comment all statements wich says class::read()…for instance, the INIT void:

void kbd_init()
{
char ack;

kbd.write(0xff); // send reset code
ack = kbd.read(); // byte, kbd does self test
ack = kbd.read(); // another ack when self test is done
}

I can’t go further… =/

what is happening? Is there any library really working?

Thanks :wink:
Btp~