Skip to content
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
66 changes: 59 additions & 7 deletions PS2Mouse.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WConstants.h"
#endif

#include "HardwareSerial.h"
#include "PS2Mouse.h"

PS2Mouse::PS2Mouse(int clock_pin, int data_pin, int mode) {
_clock_pin = clock_pin;
_data_pin = data_pin;
_mode = mode;
_initialized = false;
_initialized = false;
_disabled = true;
_enabled = false;
}
Expand Down Expand Up @@ -48,15 +54,15 @@ void PS2Mouse::set_mode(int data) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
if (_initialized) {
delayMicroseconds(100);
delayMicroseconds(100);
}
}

void PS2Mouse::set_remote_mode() {
set_mode(0xf0);
_mode = REMOTE;
}

void PS2Mouse::set_stream_mode() {
set_mode(0xea);
_mode = STREAM;
Expand Down Expand Up @@ -140,7 +146,7 @@ void PS2Mouse::write(int data) {
while (digitalRead(_clock_pin)) {;}
parity = parity ^ (data & 0x01);
data = data >> 1;
}
}
// parity
if (parity) {
pull_high(_data_pin);
Expand Down Expand Up @@ -188,7 +194,7 @@ int PS2Mouse::read_byte() {

int PS2Mouse::read_bit() {
while (digitalRead(_clock_pin)) {;}
int bit = digitalRead(_data_pin);
int bit = digitalRead(_data_pin);
while (!digitalRead(_clock_pin)) {;}
return bit;
}
Expand All @@ -213,12 +219,58 @@ 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);
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}

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

7 changes: 4 additions & 3 deletions examples/mouse.pde → examples/Simple/Simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
*/

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

PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM);
// PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM);
PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA);

/**
* Setup
Expand Down
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
* https://github.com/kristopher/PS2-Mouse-Arduino/
* Original by Kristopher Chambers <kristopher.chambers@gmail.com>
* Updated by Jonathan Oxer <jon@oxer.com.au>
*/

#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);
}