/* * the clock and data pins can be wired directly to the clk and data pins * of the PS2 connector. No external parts are needed. */ PS2::PS2(int clk, int data) { _ps2clk = clk; _ps2data = data; gohi(_ps2clk); gohi(_ps2data); }
unsigned char PS2::read(void){ unsigned char data=0, bit=1; gohi(_ps2clk); gohi(_ps2data); delayMicroseconds(50); while(digitalRead(_ps2clk)==HIGH); delayMicroseconds(5); while(digitalRead(_ps2clk)==LOW); for(int i=0; i<8; i++){ while(digitalRead(_ps2clk)==HIGH); if(digitalRead(_ps2data)==HIGH) data|=bit; while(digitalRead(_ps2clk)==LOW); bit=bit<<1; } while(digitalRead(_ps2clk)==HIGH); while(digitalRead(_ps2clk)==LOW); while(digitalRead(_ps2clk)==HIGH); while(digitalRead(_ps2clk)==LOW); golo(_ps2clk); return data; }
/* * read a byte of data from the ps2 device. Ignores parity. */ unsigned char PS2::read(void) { unsigned char data = 0x00; unsigned char i; unsigned char bit = 0x01; // start clock gohi(_ps2clk); gohi(_ps2data); delayMicroseconds(50); while (digitalRead(_ps2clk) == HIGH) ; delayMicroseconds(5); // not sure why. while (digitalRead(_ps2clk) == LOW) ; // eat start bit for (i=0; i < 8; i++) { while (digitalRead(_ps2clk) == HIGH) ; if (digitalRead(_ps2data) == HIGH) { data = data | bit; } while (digitalRead(_ps2clk) == LOW) ; bit = bit << 1; } // eat parity bit, ignore it. while (digitalRead(_ps2clk) == HIGH) ; while (digitalRead(_ps2clk) == LOW) ; // eat stop bit while (digitalRead(_ps2clk) == HIGH) ; while (digitalRead(_ps2clk) == LOW) ; golo(_ps2clk); // hold incoming data return data; }
uint8_t PS2dev::read() { uint8_t data = 0x00; uint8_t i; uint8_t bit = 0x01; uint8_t parity = 1; // wait for data line to go low while (digitalRead(_ps2data) == HIGH); // wait for clock line to go high while (digitalRead(_ps2clk) == LOW); delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); for (i = 0; i < 8; i++) { if (digitalRead(_ps2data) == HIGH) data |= bit; bit = bit << 1; delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); parity = parity ^ (data & 0x01); } // we do the delay at the end of the loop, so at this point we have // already done the delay for the parity bit // stop bit delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); delayMicroseconds(CLKHALF); golo(_ps2data); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); gohi(_ps2data); return data; }
int8_t PS2dev::write(uint8_t data) { uint8_t i; uint8_t parity = 1; DEBUG_PRN_PS2("PS2dev debug: sending "); DEBUG_PRNLF_PS2(data, HEX); if (digitalRead(_ps2clk) == LOW) return -1; if (digitalRead(_ps2data) == LOW) return -2; golo(_ps2data); delayMicroseconds(CLKHALF); // device sends on falling clock // start bit golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); // data bits for (i = 0; i < 8; i++) { if (data & 0x01) gohi(_ps2data); else golo(_ps2data); delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); parity = parity ^ (data & 0x01); data = data >> 1; } // parity bit if (parity) gohi(_ps2data); else golo(_ps2data); delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); // stop bit gohi(_ps2data); delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); delayMicroseconds(50); return 0; }
/* write a byte to the PS2 device */ void PS2::write(unsigned char data) { unsigned char i; unsigned char parity = 1; gohi(_ps2data); gohi(_ps2clk); delayMicroseconds(300); golo(_ps2clk); delayMicroseconds(300); golo(_ps2data); delayMicroseconds(10); gohi(_ps2clk); // start bit /* wait for device to take control of clock */ while (digitalRead(_ps2clk) == HIGH) ; // this loop intentionally left blank // clear to send data for (i=0; i < 8; i++) { if (data & 0x01) { gohi(_ps2data); } else { golo(_ps2data); } // wait for clock while (digitalRead(_ps2clk) == LOW) ; while (digitalRead(_ps2clk) == HIGH) ; parity = parity ^ (data & 0x01); data = data >> 1; } // parity bit if (parity) { gohi(_ps2data); } else { golo(_ps2data); } // clock cycle - like ack. while (digitalRead(_ps2clk) == LOW) ; while (digitalRead(_ps2clk) == HIGH) ; // stop bit gohi(_ps2data); delayMicroseconds(50); while (digitalRead(_ps2clk) == HIGH) ; // mode switch while ((digitalRead(_ps2clk) == LOW) || (digitalRead(_ps2data) == LOW)) ; // hold up incoming data golo(_ps2clk); }
void PS2::write(unsigned char data){ unsigned char parity=1; gohi(_ps2data); gohi(_ps2clk); delayMicroseconds(300); golo(_ps2clk); delayMicroseconds(300); golo(_ps2data); delayMicroseconds(10); gohi(_ps2clk); while(digitalRead(_ps2clk)==HIGH); for(int i=0; i<8; i++){ if(data&0x01) gohi(_ps2data); else golo(_ps2data); while(digitalRead(_ps2clk)==LOW); while(digitalRead(_ps2clk)==HIGH); parity^=(data&0x01); data=data>>1; } if(parity) gohi(_ps2data); else golo(_ps2data); while(digitalRead(_ps2clk)==LOW); while(digitalRead(_ps2clk)==HIGH); gohi(_ps2data); delayMicroseconds(50); while(digitalRead(_ps2clk)==HIGH); while((digitalRead(_ps2clk)==LOW)||(digitalRead(_ps2data)==LOW)); golo(_ps2clk); }
int PS2dev::write(unsigned char data) { unsigned char i; unsigned char parity = 1; // Serial.print("sending "); //Serial.println(data,HEX); if (digitalRead(_ps2clk) == LOW) { return -1; } if (digitalRead(_ps2data) == LOW) { return -2; } golo(_ps2data); delayMicroseconds(CLKHALF); // device sends on falling clock golo(_ps2clk); // start bit delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); for (i=0; i < 8; i++) { if (data & 0x01) { gohi(_ps2data); } else { golo(_ps2data); } delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); parity = parity ^ (data & 0x01); data = data >> 1; } // parity bit if (parity) { gohi(_ps2data); } else { golo(_ps2data); } delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); // stop bit gohi(_ps2data); delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); delayMicroseconds(50); return 0; }
int PS2dev::read(unsigned char * value) { unsigned char data = 0x00; unsigned char i; unsigned char bit = 0x01; unsigned char parity = 1; //wait for data line to go low while (digitalRead(_ps2data) == HIGH) { } //wait for clock line to go high while (digitalRead(_ps2clk) == LOW) { } delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); for (i=0; i < 8; i++) { if (digitalRead(_ps2data) == HIGH) { data = data | bit; } else { } bit = bit << 1; delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); parity = parity ^ (data & 0x01); } // we do the delay at the end of the loop, so at this point we have // already done the delay for the parity bit // stop bit delayMicroseconds(CLKHALF); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); delayMicroseconds(CLKHALF); golo(_ps2data); golo(_ps2clk); delayMicroseconds(CLKFULL); gohi(_ps2clk); delayMicroseconds(CLKHALF); gohi(_ps2data); *value = data; return 0; }
/* * the clock and data pins can be wired directly to the clk and data pins * of the PS2 connector. No external parts are needed. */ PS2dev::PS2dev(uint8_t clockPin, uint8_t dataPin) { _ps2clk = clockPin; _ps2data = dataPin; gohi(_ps2clk); gohi(_ps2data); }