diff --git a/PS2Mouse.cpp b/PS2Mouse.cpp index 9e1e693..1ad7d64 100644 --- a/PS2Mouse.cpp +++ b/PS2Mouse.cpp @@ -1,4 +1,10 @@ + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else #include "WConstants.h" +#endif + #include "HardwareSerial.h" #include "PS2Mouse.h" @@ -6,7 +12,7 @@ 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; } @@ -48,7 +54,7 @@ void PS2Mouse::set_mode(int data) { enable_data_reporting(); // Tell the mouse to start sending data again } if (_initialized) { - delayMicroseconds(100); + delayMicroseconds(100); } } @@ -56,7 +62,7 @@ void PS2Mouse::set_remote_mode() { set_mode(0xf0); _mode = REMOTE; } - + void PS2Mouse::set_stream_mode() { set_mode(0xea); _mode = STREAM; @@ -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); @@ -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; } @@ -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< -#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 diff --git a/examples/Wheel/Wheel.ino b/examples/Wheel/Wheel.ino new file mode 100644 index 0000000..fa1bc4b --- /dev/null +++ b/examples/Wheel/Wheel.ino @@ -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 + * Updated by Jonathan Oxer + */ + +#include +#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); +}