Skip to content

Loading…

fixes for arduino 1.x, add support for wheel mouse #2

Open
wants to merge 6 commits into from

2 participants

@saces

have fun

@kristopher
@saces

Should be better now.
Reject this one and go with the new requests.
Again, have fun. ;)
Mfg saces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 19, 2014
  1. @saces

    whitespace/ident

    saces committed
  2. @saces

    fix for Arduino 1.0.x

    saces committed
  3. @saces
  4. @saces

    whitespace

    saces committed
  5. @saces

    add wheel mouse support

    saces committed
Commits on Mar 25, 2014
  1. @saces

    whitespace

    saces committed
Showing with 118 additions and 13 deletions.
  1. +59 −7 PS2Mouse.cpp
  2. +15 −3 PS2Mouse.h
  3. +4 −3 examples/{mouse.pde → Simple/Simple.ino}
  4. +40 −0 examples/Wheel/Wheel.ino
View
66 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<<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;
+}
View
18 PS2Mouse.h
@@ -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();
@@ -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
View
7 examples/mouse.pde → examples/Simple/Simple.ino
@@ -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
View
40 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 <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);
+}
Something went wrong with that request. Please try again.