Skip to content

add wheel mouse support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions PS2Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ int PS2Mouse::read_movement_y(int status) {
return y;
}

int PS2IMouse::read_movement_z() {
int z = read();
if (bitRead(z, 5)) {
for(int i = 4; i < 16; ++i) {
z |= (1<<i);
}
}
return z;
}

void PS2Mouse::pull_low(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
Expand All @@ -222,3 +232,39 @@ void PS2Mouse::pull_high(int pin) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}

PS2IMouse::PS2IMouse(int clock_pin, int data_pin, int mode): PS2Mouse(clock_pin, data_pin, mode)
{
}

void PS2IMouse::initialize() {
PS2Mouse::initialize();
msMode();
}

void PS2IMouse::msMode() {
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(200); // Send Set Sample Rate
read_byte(); // Read ack byte
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(100); // Send Set Sample Rate
read_byte(); // Read ack byte
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(80); // Send Set Sample Rate
read_byte(); // Read ack byte
write(0xf2); // Get device ID.
read_byte(); // Read should be 03
}

int * PS2IMouse::report(int data[]) {
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
data[0] = read(); // Status bit
data[1] = read_movement_x(data[0]); // X Movement Packet
data[2] = read_movement_y(data[0]); // Y Movement Packet
data[3] = read_movement_z(); // Z Movement Packet
return data;
}
18 changes: 15 additions & 3 deletions PS2Mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class PS2Mouse
int _initialized;
int _enabled;
int _disabled;
int read_byte();
int read_bit();
int read_movement_x(int);
int read_movement_y(int);
void pull_high(int);
void pull_low(int);
void set_mode(int);
protected:
int read_byte();
int read_movement_x(int);
int read_movement_y(int);
public:
PS2Mouse(int, int, int mode = REMOTE);
void initialize();
Expand All @@ -38,5 +39,16 @@ class PS2Mouse
void set_sample_rate(int);
};

class PS2IMouse : public PS2Mouse
{
private:
void msMode();
int read_movement_z();
public:
PS2IMouse(int, int, int mode = REMOTE);
void initialize();
int* report(int data[]);
};

#endif

40 changes: 40 additions & 0 deletions examples/Wheel/Wheel.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Reads X/Y values from a PS/2 mouse connected to an Arduino
* using the PS2Mouse library available from
* http://github.com/kristopher/PS2-Mouse-Arduino/
* Original by Kristopher Chambers <[email protected]>
* Updated by Jonathan Oxer <[email protected]>
*/

#include <PS2Mouse.h>
#define MOUSE_DATA 4
#define MOUSE_CLOCK 2

PS2IMouse mouse(MOUSE_CLOCK, MOUSE_DATA, REMOTE);

/**
* Setup
*/
void setup()
{
Serial.begin(38400);
mouse.initialize();
}

/**
* Main program loop
*/
void loop()
{
int data[4];
mouse.report(data);
Serial.print(data[0]); // Status Byte
Serial.print(":");
Serial.print(data[1]); // X Movement Data
Serial.print(",");
Serial.print(data[2]); // Y Movement Data
Serial.print(",");
Serial.print(data[3]); // Z Movement Data (wheel)
Serial.println();
delay(20);
}