Skip to content

Instantly share code, notes, and snippets.

@buzztiaan
Last active May 15, 2020 00:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save buzztiaan/7274417 to your computer and use it in GitHub Desktop.
Save buzztiaan/7274417 to your computer and use it in GitHub Desktop.
Implementation of synaptics touchpad driver example NON-interrupt driven, just polling!
#define PS2_clockpin PE_2
#define PS2_datapin PE_3
byte read_CLOCK() {
return digitalRead(PS2_clockpin);
}
byte read_DATA() {
return digitalRead(PS2_datapin);
}
void set_CLOCK(byte state) {
if (!state) {
pinMode(PS2_clockpin, OUTPUT);
digitalWrite(PS2_clockpin, LOW);
} else {
pinMode(PS2_clockpin, INPUT);
}
}
void set_DATA(byte state) {
if (!state) {
pinMode(PS2_datapin, OUTPUT);
digitalWrite(PS2_datapin, LOW);
} else {
pinMode(PS2_datapin, INPUT);
}
}
void PS2_error() {
// pff
Serial.println("hark");
}
void wait_CLOCK(byte state) {
// add timeout?
while (read_CLOCK() != state) {};
}
byte PS2_get() {
noInterrupts();
byte i, bit, value = 0, p = 0;
set_CLOCK(1); /* Release inhibit, if necessary. */
wait_CLOCK(0);
wait_CLOCK(1);
for (i=0; i<8; i++) {
wait_CLOCK(0);
bit = read_DATA();
value = value + (bit << i);
p = p + bit;
wait_CLOCK(1);
}
wait_CLOCK(0);
p = p + read_DATA();;
if ( (p & 0x01) == 0 ) PS2_error();
wait_CLOCK(1);
wait_CLOCK(0);
if (read_DATA() == 0) PS2_error();
set_CLOCK(0); /* Pull low during stop bit to inhibit. */
delay(5);
interrupts();
return value;
}
void PS2_send(byte value) {
noInterrupts();
byte i, ack, p = 1;
set_CLOCK(0); /* Begin inhibit, if necessary */
delay(10);
set_DATA(0);
set_CLOCK(1);
for (i = 0; i < 8; i++) {
wait_CLOCK(0);
set_DATA(value & 0x01);
p = p + value;
wait_CLOCK(1);
value = value >> 1;
}
wait_CLOCK(0);
set_DATA(p & 0x01);
wait_CLOCK(1);
wait_CLOCK(0);
set_DATA(1);
wait_CLOCK(1);
wait_CLOCK(0);
if (read_DATA() == 1) PS2_error();
wait_CLOCK(1);
ack = PS2_get();
if (ack != 0xFA) PS2_error();
interrupts();
}
void send_tp_arg( byte arg) {
byte i,ack;
for (i = 0; i < 4; i++) {
PS2_send(0xE8);
PS2_send((arg >> (6-2*i)) & 3);
}
}
void setup()
{
byte minor,middle,major;
Serial.begin(9600);
PS2_send(0xFF);
Serial.println(PS2_get(), HEX);
// if (PS2_get() != 0xAA) PS2_error();
Serial.println(PS2_get(), HEX);
// if (PS2_get() != 0x00) PS2_error();
send_tp_arg(0x00);
PS2_send(0xE9);
minor = PS2_get();
middle = PS2_get();
major = PS2_get() & 0x0F;
Serial.print("minor ");
Serial.println(minor, HEX);
Serial.print("middle ");
Serial.println(middle, HEX);
Serial.print("major ");
Serial.println(major, HEX);
send_tp_arg(0x80);
PS2_send(0xF3);
PS2_send(0x14);
PS2_send(0xF0);
set_CLOCK(1);
}
signed long next_poll;
void loop()
{
byte mstat1, mstat2, mx;
byte mxy, my, mz;
unsigned int cx,cy;
PS2_send(0xEB);
mstat1 = PS2_get();
mxy = PS2_get();
mz = PS2_get();
mstat2 = PS2_get();
mx = PS2_get();
my = PS2_get();
cx = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx);
cy = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my);
Serial.print("x ");
Serial.print(cx);
Serial.print(" y ");
Serial.print(cy);
Serial.print(" z ");
Serial.println(mz);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment